|
| 1 | +// |
| 2 | +// AuthenticationMiddleware.swift |
| 3 | +// SyntaxKit |
| 4 | +// |
| 5 | +// Created by Leo Dion. |
| 6 | +// Copyright © 2026 BrightDigit. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person |
| 9 | +// obtaining a copy of this software and associated documentation |
| 10 | +// files (the “Software”), to deal in the Software without |
| 11 | +// restriction, including without limitation the rights to use, |
| 12 | +// copy, modify, merge, publish, distribute, sublicense, and/or |
| 13 | +// sell copies of the Software, and to permit persons to whom the |
| 14 | +// Software is furnished to do so, subject to the following |
| 15 | +// conditions: |
| 16 | +// |
| 17 | +// The above copyright notice and this permission notice shall be |
| 18 | +// included in all copies or substantial portions of the Software. |
| 19 | +// |
| 20 | +// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, |
| 21 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 28 | +// |
| 29 | + |
| 30 | +public import HTTPTypes |
| 31 | +public import OpenAPIRuntime |
| 32 | + |
| 33 | +#if os(Linux) |
| 34 | + @preconcurrency public import struct Foundation.URL |
| 35 | +#else |
| 36 | + public import struct Foundation.URL |
| 37 | +#endif |
| 38 | + |
| 39 | +/// OpenAPI client middleware that adds Anthropic API authentication headers. |
| 40 | +/// |
| 41 | +/// Injects the `x-api-key` and `anthropic-version` headers into every |
| 42 | +/// outgoing request before forwarding it to the next handler in the chain. |
| 43 | +public struct AuthenticationMiddleware: ClientMiddleware { |
| 44 | + /// The Anthropic API version sent with every request. |
| 45 | + private static let anthropicVersion = "2023-06-01" |
| 46 | + |
| 47 | + /// The `x-api-key` header name, constructed once. `"x-api-key"` is a valid |
| 48 | + /// RFC 9110 token, so this is always non-`nil`. |
| 49 | + private static let apiKeyFieldName = HTTPField.Name("x-api-key") |
| 50 | + |
| 51 | + /// The `anthropic-version` header name, constructed once. `"anthropic-version"` |
| 52 | + /// is a valid RFC 9110 token, so this is always non-`nil`. |
| 53 | + private static let anthropicVersionFieldName = HTTPField.Name("anthropic-version") |
| 54 | + |
| 55 | + /// The Anthropic API key used to authenticate requests. |
| 56 | + private let apiKey: String |
| 57 | + |
| 58 | + /// Creates a middleware that authenticates requests with the given API key. |
| 59 | + /// - Parameter apiKey: The Anthropic API key to send in the `x-api-key` header. |
| 60 | + public init(apiKey: String) { |
| 61 | + self.apiKey = apiKey |
| 62 | + } |
| 63 | + |
| 64 | + /// Adds the Anthropic authentication headers, then forwards the request. |
| 65 | + /// |
| 66 | + /// Any `x-api-key` or `anthropic-version` header already present on the |
| 67 | + /// request is silently overwritten with this middleware's values. |
| 68 | + /// - Parameters: |
| 69 | + /// - request: The outgoing HTTP request. |
| 70 | + /// - body: The outgoing HTTP request body, if any. |
| 71 | + /// - baseURL: The base URL of the server. |
| 72 | + /// - operationID: The OpenAPI operation identifier for the request. |
| 73 | + /// - next: The next handler in the middleware chain. |
| 74 | + /// - Returns: The HTTP response and body from the next handler. |
| 75 | + /// - Throws: Any error thrown by the `next` handler. |
| 76 | + public func intercept( |
| 77 | + _ request: HTTPRequest, |
| 78 | + body: HTTPBody?, |
| 79 | + baseURL: URL, |
| 80 | + operationID: String, |
| 81 | + next: @Sendable (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?) |
| 82 | + ) async throws -> (HTTPResponse, HTTPBody?) { |
| 83 | + var request = request |
| 84 | + if let apiKeyFieldName = Self.apiKeyFieldName { |
| 85 | + request.headerFields[apiKeyFieldName] = apiKey |
| 86 | + } else { |
| 87 | + assertionFailure("\"x-api-key\" is a valid header name and must never be nil.") |
| 88 | + } |
| 89 | + if let anthropicVersionFieldName = Self.anthropicVersionFieldName { |
| 90 | + request.headerFields[anthropicVersionFieldName] = Self.anthropicVersion |
| 91 | + } else { |
| 92 | + assertionFailure("\"anthropic-version\" is a valid header name and must never be nil.") |
| 93 | + } |
| 94 | + return try await next(request, body, baseURL) |
| 95 | + } |
| 96 | +} |
0 commit comments