|
| 1 | +// |
| 2 | +// OAuthFlows.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Mathew Polzin on 1/23/20. |
| 6 | +// |
| 7 | + |
| 8 | +import OpenAPIKitCore |
| 9 | +import Foundation |
| 10 | + |
| 11 | +extension OpenAPI { |
| 12 | + /// OpenAPI Spec "Oauth Flows Object" |
| 13 | + /// |
| 14 | + /// See [OpenAPI Oauth Flows Object](https://spec.openapis.org/oas/v3.0.4.html#oauth-flows-object). |
| 15 | + public struct OAuthFlows: HasConditionalWarnings, Sendable { |
| 16 | + public let implicit: Implicit? |
| 17 | + public let password: Password? |
| 18 | + public let clientCredentials: ClientCredentials? |
| 19 | + public let authorizationCode: AuthorizationCode? |
| 20 | + public let deviceAuthorization: DeviceAuthorization? |
| 21 | + |
| 22 | + public let conditionalWarnings: [(any Condition, OpenAPI.Warning)] |
| 23 | + |
| 24 | + public init( |
| 25 | + implicit: Implicit? = nil, |
| 26 | + password: Password? = nil, |
| 27 | + clientCredentials: ClientCredentials? = nil, |
| 28 | + authorizationCode: AuthorizationCode? = nil, |
| 29 | + deviceAuthorization: DeviceAuthorization? = nil |
| 30 | + ) { |
| 31 | + self.implicit = implicit |
| 32 | + self.password = password |
| 33 | + self.clientCredentials = clientCredentials |
| 34 | + self.authorizationCode = authorizationCode |
| 35 | + self.deviceAuthorization = deviceAuthorization |
| 36 | + |
| 37 | + self.conditionalWarnings = [ |
| 38 | + nonNilVersionWarning(fieldName: "deviceAuthorization", value: deviceAuthorization, minimumVersion: .v3_2_0) |
| 39 | + ].compactMap { $0 } |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +extension OpenAPI.OAuthFlows: Equatable { |
| 45 | + public static func == (lhs: Self, rhs: Self) -> Bool { |
| 46 | + lhs.implicit == rhs.implicit |
| 47 | + && lhs.password == rhs.password |
| 48 | + && lhs.clientCredentials == rhs.clientCredentials |
| 49 | + && lhs.authorizationCode == rhs.authorizationCode |
| 50 | + && lhs.deviceAuthorization == rhs.deviceAuthorization |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +fileprivate func nonNilVersionWarning<Subject>(fieldName: String, value: Subject?, minimumVersion: OpenAPI.Document.Version) -> (any Condition, OpenAPI.Warning)? { |
| 55 | + value.map { _ in |
| 56 | + OpenAPI.Document.ConditionalWarnings.version( |
| 57 | + lessThan: minimumVersion, |
| 58 | + doesNotSupport: "The OAuthFlows \(fieldName) field" |
| 59 | + ) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +extension OpenAPI.OAuthFlows { |
| 64 | + @dynamicMemberLookup |
| 65 | + public struct DeviceAuthorization: Equatable, Sendable { |
| 66 | + private let common: CommonFields |
| 67 | + public let deviceAuthorizationUrl: URL |
| 68 | + public let tokenUrl: URL |
| 69 | + |
| 70 | + public init(deviceAuthorizationUrl: URL, tokenUrl: URL, refreshUrl: URL? = nil, scopes: OrderedDictionary<Scope, ScopeDescription>) { |
| 71 | + self.deviceAuthorizationUrl = deviceAuthorizationUrl |
| 72 | + self.tokenUrl = tokenUrl |
| 73 | + common = .init(refreshUrl: refreshUrl, scopes: scopes) |
| 74 | + } |
| 75 | + |
| 76 | + public subscript<T>(dynamicMember path: KeyPath<CommonFields, T>) -> T { |
| 77 | + return common[keyPath: path] |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// MARK: - Codable |
| 83 | +extension OpenAPI.OAuthFlows { |
| 84 | + private enum CodingKeys: String, CodingKey { |
| 85 | + case implicit |
| 86 | + case password |
| 87 | + case clientCredentials |
| 88 | + case authorizationCode |
| 89 | + case deviceAuthorization |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +extension OpenAPI.OAuthFlows.DeviceAuthorization { |
| 94 | + private enum CodingKeys: String, CodingKey { |
| 95 | + case deviceAuthorizationUrl |
| 96 | + case tokenUrl |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +extension OpenAPI.OAuthFlows: Encodable { |
| 101 | + public func encode(to encoder: Encoder) throws { |
| 102 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 103 | + |
| 104 | + try container.encodeIfPresent(implicit, forKey: .implicit) |
| 105 | + try container.encodeIfPresent(password, forKey: .password) |
| 106 | + try container.encodeIfPresent(clientCredentials, forKey: .clientCredentials) |
| 107 | + try container.encodeIfPresent(authorizationCode, forKey: .authorizationCode) |
| 108 | + try container.encodeIfPresent(deviceAuthorization, forKey: .deviceAuthorization) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +extension OpenAPI.OAuthFlows: Decodable { |
| 113 | + public init(from decoder: Decoder) throws { |
| 114 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 115 | + |
| 116 | + implicit = try container.decodeIfPresent(OpenAPI.OAuthFlows.Implicit.self, forKey: .implicit) |
| 117 | + password = try container.decodeIfPresent(OpenAPI.OAuthFlows.Password.self, forKey: .password) |
| 118 | + clientCredentials = try container.decodeIfPresent(OpenAPI.OAuthFlows.ClientCredentials.self, forKey: .clientCredentials) |
| 119 | + authorizationCode = try container.decodeIfPresent(OpenAPI.OAuthFlows.AuthorizationCode.self, forKey: .authorizationCode) |
| 120 | + deviceAuthorization = try container.decodeIfPresent(OpenAPI.OAuthFlows.DeviceAuthorization.self, forKey: .deviceAuthorization) |
| 121 | + |
| 122 | + self.conditionalWarnings = [ |
| 123 | + nonNilVersionWarning(fieldName: "deviceAuthorization", value: deviceAuthorization, minimumVersion: .v3_2_0) |
| 124 | + ].compactMap { $0 } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +extension OpenAPI.OAuthFlows.DeviceAuthorization: Encodable { |
| 129 | + public func encode(to encoder: Encoder) throws { |
| 130 | + try common.encode(to: encoder) |
| 131 | + |
| 132 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 133 | + |
| 134 | + try container.encode(tokenUrl.absoluteString, forKey: .tokenUrl) |
| 135 | + try container.encode(deviceAuthorizationUrl.absoluteString, forKey: .deviceAuthorizationUrl) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +extension OpenAPI.OAuthFlows.DeviceAuthorization: Decodable { |
| 140 | + public init(from decoder: Decoder) throws { |
| 141 | + common = try OpenAPI.OAuthFlows.CommonFields(from: decoder) |
| 142 | + |
| 143 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 144 | + |
| 145 | + tokenUrl = try container.decodeURLAsString(forKey: .tokenUrl) |
| 146 | + deviceAuthorizationUrl = try container.decodeURLAsString(forKey: .deviceAuthorizationUrl) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +extension OpenAPI.OAuthFlows: Validatable {} |
| 151 | +extension OpenAPI.OAuthFlows.DeviceAuthorization: Validatable {} |
| 152 | +// The following conformances are found in Core |
| 153 | +// extension Shared.OAuthFlows.Implicit: Validatable {} |
| 154 | +// extension Shared.OAuthFlows.Password: Validatable {} |
| 155 | +// extension Shared.OAuthFlows.ClientCredentials: Validatable {} |
| 156 | +// extension Shared.OAuthFlows.AuthorizationCode: Validatable {} |
0 commit comments