Added support for an API key to the AuthMiddleware middleware in the library target. (#4)
This PR contains the work done to extend the implementation of the `AuthMiddleware` middleware to handle an API key as well as private and public keys. Reviewed-on: #4 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #4.
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
//===----------------------------------------------------------------------===
|
||||||
|
//
|
||||||
|
// This source file is part of the MarvelService open source project
|
||||||
|
//
|
||||||
|
// Copyright (c) -2025 Röck+Cöde VoF. and the MarvelService project authors
|
||||||
|
// Licensed under the EUPL 1.2 or later.
|
||||||
|
//
|
||||||
|
// See LICENSE for license information
|
||||||
|
// See CONTRIBUTORS for the list of MarvelService project authors
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===
|
||||||
|
|
||||||
|
extension String {
|
||||||
|
/// A namespace assigned for parameter keys of URI paths.
|
||||||
|
enum Parameter {
|
||||||
|
/// A Marvel API key parameter.
|
||||||
|
static let apiKey = "apikey"
|
||||||
|
/// A MD5 hash parameter.
|
||||||
|
static let hash = "hash"
|
||||||
|
/// A timestamp parameter.
|
||||||
|
static let timestamp = "ts"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,13 +26,24 @@ public struct AuthMiddleware {
|
|||||||
|
|
||||||
// MARK: Properties
|
// MARK: Properties
|
||||||
|
|
||||||
|
/// A Marvel API key.
|
||||||
|
private let apiKey: String
|
||||||
|
|
||||||
/// A use case that generates a MD5 hash value to use as an authentication parameter.
|
/// A use case that generates a MD5 hash value to use as an authentication parameter.
|
||||||
private let hash: GenerateHashUseCase
|
private let hash: GenerateHashUseCase?
|
||||||
|
|
||||||
/// A Marvel API public key.
|
|
||||||
private let publicKey: String
|
|
||||||
|
|
||||||
// MARK: Initializers
|
// MARK: Initializers
|
||||||
|
|
||||||
|
/// Initializes this middleware with an api key.
|
||||||
|
///
|
||||||
|
/// The middleware attaches the required `apikey` parameter to the URI path of the intercepted request.
|
||||||
|
/// This initializer should be used for client-side applications, as indicated in the [Marvel API documentation](https://developer.marvel.com/documentation/authorization)
|
||||||
|
///
|
||||||
|
/// - Parameter apiKey: A Marvel API key.
|
||||||
|
public init(apiKey: String) {
|
||||||
|
self.apiKey = apiKey
|
||||||
|
self.hash = nil
|
||||||
|
}
|
||||||
|
|
||||||
/// Initializes this middleware with private and public keys.
|
/// Initializes this middleware with private and public keys.
|
||||||
///
|
///
|
||||||
@@ -46,11 +57,11 @@ public struct AuthMiddleware {
|
|||||||
privateKey: String,
|
privateKey: String,
|
||||||
publicKey: String
|
publicKey: String
|
||||||
) {
|
) {
|
||||||
|
self.apiKey = publicKey
|
||||||
self.hash = .init(
|
self.hash = .init(
|
||||||
privateKey: privateKey,
|
privateKey: privateKey,
|
||||||
publicKey: publicKey
|
publicKey: publicKey
|
||||||
)
|
)
|
||||||
self.publicKey = publicKey
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -68,37 +79,68 @@ extension AuthMiddleware: ClientMiddleware {
|
|||||||
operationID: String,
|
operationID: String,
|
||||||
next: @Sendable (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)
|
next: @Sendable (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)
|
||||||
) async throws -> (HTTPResponse, HTTPBody?) {
|
) async throws -> (HTTPResponse, HTTPBody?) {
|
||||||
guard
|
guard let path = request.path else {
|
||||||
let uriPath = request.path,
|
|
||||||
var urlComponents = URLComponents(string: uriPath)
|
|
||||||
else {
|
|
||||||
return try await next(request, body, baseURL)
|
return try await next(request, body, baseURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
let queryItems = urlComponents.queryItems ?? []
|
return try await next(
|
||||||
let timestamp = Date().timeIntervalSince1970
|
.init(
|
||||||
|
method: request.method,
|
||||||
|
scheme: request.scheme,
|
||||||
|
authority: request.authority,
|
||||||
|
path: authenticatedPath(path),
|
||||||
|
headerFields: request.headerFields
|
||||||
|
),
|
||||||
|
body,
|
||||||
|
baseURL
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
urlComponents.queryItems = queryItems + [
|
// MARK: - Helpers
|
||||||
.init(name: "ts", value: timestamp.asString),
|
|
||||||
.init(name: "apikey", value: publicKey),
|
private extension AuthMiddleware {
|
||||||
.init(name: "hash", value: hash(timestamp: timestamp))
|
|
||||||
]
|
// MARK: Functions
|
||||||
|
|
||||||
|
/// Adds the necessary authentication parameters to a given request path.
|
||||||
|
/// - Parameter path: A request path to authenticate.
|
||||||
|
/// - Returns: A request path with the necessary authentication parameters added.
|
||||||
|
func authenticatedPath(_ path: String) -> String {
|
||||||
|
guard var urlComponents = URLComponents(string: path) else {
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
let newPath = if let urlQuery = urlComponents.query {
|
var queryItems = urlComponents.queryItems ?? []
|
||||||
|
|
||||||
|
queryItems.append(.init(
|
||||||
|
name: .Parameter.apiKey,
|
||||||
|
value: apiKey
|
||||||
|
))
|
||||||
|
|
||||||
|
if let hash {
|
||||||
|
let timestamp = Date().timeIntervalSince1970
|
||||||
|
|
||||||
|
queryItems.append(contentsOf: [
|
||||||
|
.init(
|
||||||
|
name: .Parameter.hash,
|
||||||
|
value: hash(timestamp: timestamp)
|
||||||
|
),
|
||||||
|
.init(
|
||||||
|
name: .Parameter.timestamp,
|
||||||
|
value: timestamp.asString
|
||||||
|
),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
urlComponents.queryItems = queryItems
|
||||||
|
|
||||||
|
return if let urlQuery = urlComponents.query {
|
||||||
urlComponents.path + "?" + urlQuery
|
urlComponents.path + "?" + urlQuery
|
||||||
} else {
|
} else {
|
||||||
urlComponents.path
|
urlComponents.path
|
||||||
}
|
}
|
||||||
|
|
||||||
let newRequest = HTTPRequest(
|
|
||||||
method: request.method,
|
|
||||||
scheme: request.scheme,
|
|
||||||
authority: request.authority,
|
|
||||||
path: newPath,
|
|
||||||
headerFields: request.headerFields
|
|
||||||
)
|
|
||||||
|
|
||||||
return try await next(newRequest, body, baseURL)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ info:
|
|||||||
termsOfService: https://developer.marvel.com/terms
|
termsOfService: https://developer.marvel.com/terms
|
||||||
version: Cable
|
version: Cable
|
||||||
servers:
|
servers:
|
||||||
- url: https://gateway.marvel.com/
|
- url: https://gateway.marvel.com
|
||||||
description: Live service
|
description: Live service
|
||||||
tags:
|
tags:
|
||||||
- name: characters
|
- name: characters
|
||||||
@@ -5763,13 +5763,10 @@ components:
|
|||||||
$ref: '#/components/schemas/ComicSummary'
|
$ref: '#/components/schemas/ComicSummary'
|
||||||
ErrorResponse:
|
ErrorResponse:
|
||||||
type: object
|
type: object
|
||||||
|
description: This is the standard error response type.
|
||||||
properties:
|
properties:
|
||||||
code:
|
code:
|
||||||
type: integer
|
|
||||||
description: The HTTP status code of the returned result.
|
|
||||||
reason:
|
|
||||||
type: string
|
type: string
|
||||||
description: A reason describing the error.
|
description: The HTTP status code of the returned result.
|
||||||
required:
|
required:
|
||||||
- code
|
- code
|
||||||
- reason
|
|
||||||
@@ -16,7 +16,8 @@ import struct Foundation.URL
|
|||||||
import struct Foundation.URLComponents
|
import struct Foundation.URLComponents
|
||||||
import struct HTTPTypes.HTTPRequest
|
import struct HTTPTypes.HTTPRequest
|
||||||
import struct HTTPTypes.HTTPResponse
|
import struct HTTPTypes.HTTPResponse
|
||||||
import struct MarvelService.AuthMiddleware
|
|
||||||
|
@testable import MarvelService
|
||||||
|
|
||||||
@Suite("Auth Middleware", .tags(.middleware))
|
@Suite("Auth Middleware", .tags(.middleware))
|
||||||
struct AuthMiddlewareTest {
|
struct AuthMiddlewareTest {
|
||||||
@@ -25,13 +26,45 @@ struct AuthMiddlewareTest {
|
|||||||
|
|
||||||
#if swift(>=6.2)
|
#if swift(>=6.2)
|
||||||
@Test(arguments: Input.pathRequests)
|
@Test(arguments: Input.pathRequests)
|
||||||
func `intercept`(path: String?) async throws {
|
func `intercept with API key`(
|
||||||
try await assertIntercept(path: path)
|
path: String?
|
||||||
|
) async throws {
|
||||||
|
try await assertIntercept(
|
||||||
|
path: path,
|
||||||
|
with: .Key.api
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(arguments: Input.pathRequests)
|
||||||
|
func `intercept with private and public keys`(
|
||||||
|
path: String?
|
||||||
|
) async throws {
|
||||||
|
try await assertIntercept(
|
||||||
|
path: path,
|
||||||
|
with: .Key.public,
|
||||||
|
and: .Key.private
|
||||||
|
)
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@Test("intercept", arguments: Input.pathRequests)
|
@Test("intercept with API key", arguments: Input.pathRequests)
|
||||||
func intercept(path: String?) async throws {
|
func interceptWithAPIKey(
|
||||||
try await assertIntercept(path: path)
|
path: String?
|
||||||
|
) async throws {
|
||||||
|
try await assertIntercept(
|
||||||
|
path: path,
|
||||||
|
with: .Key.api
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test("intercept with private and public keys", arguments: Input.pathRequests)
|
||||||
|
func interceptWithKeys(
|
||||||
|
path: String?
|
||||||
|
) async throws {
|
||||||
|
try await assertIntercept(
|
||||||
|
path: path,
|
||||||
|
with: .Key.public,
|
||||||
|
and: .Key.private
|
||||||
|
)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -45,16 +78,26 @@ private extension AuthMiddlewareTest {
|
|||||||
|
|
||||||
/// Asserts the interception of a request to add authentication parameters in it.
|
/// Asserts the interception of a request to add authentication parameters in it.
|
||||||
/// - Parameter path: A URI path for a request.
|
/// - Parameter path: A URI path for a request.
|
||||||
|
/// - Parameter apiKey: A Marvel (public) API key.
|
||||||
|
/// - Parameter privateKey: <#publicKey description#>
|
||||||
/// - Throws: An error in case
|
/// - Throws: An error in case
|
||||||
func assertIntercept(path: String?) async throws {
|
func assertIntercept(
|
||||||
|
path: String?,
|
||||||
|
with apiKey: String,
|
||||||
|
and privateKey: String? = nil
|
||||||
|
) async throws {
|
||||||
// GIVEN
|
// GIVEN
|
||||||
let baseURL: URL = .baseURL
|
let baseURL: URL = .baseURL
|
||||||
let request: HTTPRequest = .init(path: path)
|
let request: HTTPRequest = .init(path: path)
|
||||||
|
|
||||||
let middleware: AuthMiddleware = .init(
|
let middleware: AuthMiddleware = if let privateKey {
|
||||||
privateKey: .Key.private,
|
.init(
|
||||||
publicKey: .Key.public
|
privateKey: privateKey,
|
||||||
)
|
publicKey: apiKey
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
.init(apiKey: apiKey)
|
||||||
|
}
|
||||||
|
|
||||||
// WHEN
|
// WHEN
|
||||||
_ = try await confirmation { confirmation in
|
_ = try await confirmation { confirmation in
|
||||||
@@ -70,10 +113,15 @@ private extension AuthMiddlewareTest {
|
|||||||
let urlComponents = try #require(URLComponents(string: pathRequest))
|
let urlComponents = try #require(URLComponents(string: pathRequest))
|
||||||
let queryItems = try #require(urlComponents.queryItems)
|
let queryItems = try #require(urlComponents.queryItems)
|
||||||
|
|
||||||
#expect(queryItems.count >= 3)
|
#expect(queryItems.contains(where: { $0.name == .Parameter.apiKey }))
|
||||||
#expect(queryItems.contains(where: { $0.name == "ts" }))
|
|
||||||
#expect(queryItems.contains(where: { $0.name == "apikey" }))
|
if privateKey == nil {
|
||||||
#expect(queryItems.contains(where: { $0.name == "hash" }))
|
#expect(queryItems.count >= 1)
|
||||||
|
} else {
|
||||||
|
#expect(queryItems.contains(where: { $0.name == .Parameter.hash }))
|
||||||
|
#expect(queryItems.contains(where: { $0.name == .Parameter.timestamp }))
|
||||||
|
#expect(queryItems.count >= 3)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
#expect(request.path == nil)
|
#expect(request.path == nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,8 @@
|
|||||||
extension String {
|
extension String {
|
||||||
/// A namespace assigned for Marvel API key samples.
|
/// A namespace assigned for Marvel API key samples.
|
||||||
enum Key {
|
enum Key {
|
||||||
|
/// A Marvel API key sample.
|
||||||
|
static let api = "SomeAPIKey"
|
||||||
/// A Marvel API private key sample.
|
/// A Marvel API private key sample.
|
||||||
static let `private` = "SomePrivateKey"
|
static let `private` = "SomePrivateKey"
|
||||||
/// A Marvel API public key sample.
|
/// A Marvel API public key sample.
|
||||||
|
|||||||
Reference in New Issue
Block a user