|
1 | 1 | import Foundation |
2 | 2 |
|
3 | 3 | public struct OAuth2: Decodable { |
| 4 | + public struct Request: Equatable { |
| 5 | + public let authURI: String |
| 6 | + public let tokenURI: String |
| 7 | + public let redirectURI: String |
| 8 | + public let responseType: String |
| 9 | + public let scope: [String] |
| 10 | + public let hosts: [String] |
| 11 | + public let clientID: String |
| 12 | + |
| 13 | + public func authURL(hint: String? = nil) -> URL { |
| 14 | + var components: URLComponents = URLComponents(string: authURI)! // Validated during init |
| 15 | + components.queryItems = [ |
| 16 | + URLQueryItem(name: "client_id", value: clientID), |
| 17 | + URLQueryItem(name: "redirect_uri", value: redirectURI), |
| 18 | + URLQueryItem(name: "response_type", value: responseType), |
| 19 | + URLQueryItem(name: "scope", value: scope.joined(separator: " ")) |
| 20 | + ] |
| 21 | + if let hint, !hint.isEmpty { // Prepopulate email address for specific user |
| 22 | + components.queryItems?.append(URLQueryItem(name: "login_hint", value: hint)) |
| 23 | + } |
| 24 | + return components.url! |
| 25 | + } |
| 26 | + |
| 27 | + public func tokenURL(_ code: String) -> URL { |
| 28 | + var components: URLComponents = URLComponents(string: tokenURI)! // Validated during init |
| 29 | + components.queryItems = [ |
| 30 | + URLQueryItem(name: "client_id", value: clientID), |
| 31 | + URLQueryItem(name: "client_secret", value: ""), |
| 32 | + URLQueryItem(name: "redirect_uri", value: redirectURI), |
| 33 | + URLQueryItem(name: "grant_type", value: "authorization_code"), |
| 34 | + URLQueryItem(name: "code", value: code) |
| 35 | + ] |
| 36 | + return components.url! |
| 37 | + } |
| 38 | + |
| 39 | + public func matches(_ host: String) -> Bool { |
| 40 | + for _host in hosts { |
| 41 | + guard host.hasSuffix(_host) else { continue } |
| 42 | + return true |
| 43 | + } |
| 44 | + return false |
| 45 | + } |
| 46 | + |
| 47 | + public init(authURI: String, tokenURI: String, redirectURI: String, responseType: String, scope: [String], clientID: String, hosts: [String] = []) throws { |
| 48 | + guard URL(string: authURI) != nil, |
| 49 | + URL(string: tokenURI) != nil, // Validate URI strings pass failable URL init |
| 50 | + !redirectURI.isEmpty, |
| 51 | + !scope.isEmpty, !(scope.first ?? "").isEmpty, |
| 52 | + !clientID.isEmpty |
| 53 | + else { |
| 54 | + throw URLError(.badURL) |
| 55 | + } |
| 56 | + self.authURI = authURI |
| 57 | + self.tokenURI = tokenURI |
| 58 | + self.redirectURI = redirectURI |
| 59 | + self.responseType = responseType |
| 60 | + self.scope = scope |
| 61 | + self.hosts = hosts |
| 62 | + self.clientID = clientID |
| 63 | + } |
| 64 | + |
| 65 | + public init(_ oauth2: OAuth2, redirectURI: String, responseType: String, clientID: String) throws { |
| 66 | + try self.init( |
| 67 | + authURI: oauth2.authURL.absoluteString, |
| 68 | + tokenURI: oauth2.tokenURL.absoluteString, |
| 69 | + redirectURI: redirectURI, |
| 70 | + responseType: responseType, |
| 71 | + scope: oauth2.scope, |
| 72 | + clientID: clientID |
| 73 | + ) |
| 74 | + } |
| 75 | + } |
| 76 | + |
4 | 77 | public let authURL: URL |
5 | | - public let issuer: String |
6 | | - public let scope: String |
7 | 78 | public let tokenURL: URL |
| 79 | + public let scope: [String] |
| 80 | + public let issuer: String |
| 81 | + |
| 82 | + // MARK: Decodable |
| 83 | + public init(from decoder: any Decoder) throws { |
| 84 | + let container: KeyedDecodingContainer = try decoder.container(keyedBy: Key.self) |
| 85 | + self.tokenURL = try container.decode(URL.self, forKey: .tokenURL) |
| 86 | + self.authURL = try container.decode(URL.self, forKey: .authURL) |
| 87 | + self.issuer = try container.decode(String.self, forKey: .issuer) |
| 88 | + self.scope = try container.decode(String.self, forKey: .scope).components(separatedBy: " ") |
| 89 | + } |
| 90 | + |
| 91 | + private enum Key: CodingKey { |
| 92 | + case authURL, issuer, scope, tokenURL |
| 93 | + } |
8 | 94 | } |
0 commit comments