|  | 
|  | 1 | +// | 
|  | 2 | +//  OAuth+DeviceCode.swift | 
|  | 3 | +//  OAuthKit | 
|  | 4 | +// | 
|  | 5 | +//  Created by Kevin McKee | 
|  | 6 | +// | 
|  | 7 | + | 
|  | 8 | +import Foundation | 
|  | 9 | + | 
|  | 10 | +extension OAuth { | 
|  | 11 | + | 
|  | 12 | +    /// A codable type that holds device code information. | 
|  | 13 | +    /// See: https://auth0.com/docs/get-started/authentication-and-authorization-flow/device-authorization-flow | 
|  | 14 | +    /// See: https://www.oauth.com/playground/device-code.html | 
|  | 15 | +    public struct DeviceCode: Codable, Equatable, Sendable { | 
|  | 16 | + | 
|  | 17 | +        /// A constant for the oauth grant type. | 
|  | 18 | +        static let grantType = "urn:ietf:params:oauth:grant-type:device_code" | 
|  | 19 | + | 
|  | 20 | +        /// The server assigned device code. | 
|  | 21 | +        public let deviceCode: String | 
|  | 22 | +        /// The code the user should enter when visiting the `verificationUri` | 
|  | 23 | +        public let userCode: String | 
|  | 24 | +        /// The uri the user should visit to enter the `userCode` | 
|  | 25 | +        public let verificationUri: String | 
|  | 26 | +        /// Either a QR Code or shortened URL with embedded user code | 
|  | 27 | +        public let verificationUriComplete: String? | 
|  | 28 | +        /// The lifetime in seconds for `deviceCode` and `userCode` | 
|  | 29 | +        public let expiresIn: Int? | 
|  | 30 | +        /// The polling interval | 
|  | 31 | +        public let interval: Int | 
|  | 32 | +        /// The issue date. | 
|  | 33 | +        public let issued: Date = .now | 
|  | 34 | + | 
|  | 35 | +        /// Returns true if the device code is expired. | 
|  | 36 | +        public var isExpired: Bool { | 
|  | 37 | +            guard let expiresIn = expiresIn else { return false } | 
|  | 38 | +            return issued.addingTimeInterval(Double(expiresIn)) < Date.now | 
|  | 39 | +        } | 
|  | 40 | + | 
|  | 41 | +        /// Returns the expiration date of the device token or nil if none exists. | 
|  | 42 | +        public var expiration: Date? { | 
|  | 43 | +            guard let expiresIn = expiresIn else { return nil } | 
|  | 44 | +            return issued.addingTimeInterval(TimeInterval(expiresIn)) | 
|  | 45 | +        } | 
|  | 46 | + | 
|  | 47 | +        enum CodingKeys: String, CodingKey { | 
|  | 48 | +            case deviceCode = "device_code" | 
|  | 49 | +            case userCode = "user_code" | 
|  | 50 | +            case verificationUri = "verification_uri" | 
|  | 51 | +            /// Google sends `verification_url` instead of `verification_uri` so we need to account for both. | 
|  | 52 | +            /// See: https://developers.google.com/identity/protocols/oauth2/limited-input-device | 
|  | 53 | +            case verificationUrl = "verification_url" | 
|  | 54 | +            case verificationUriComplete = "verification_uri_complete" | 
|  | 55 | +            case expiresIn = "expires_in" | 
|  | 56 | +            case interval | 
|  | 57 | +        } | 
|  | 58 | + | 
|  | 59 | +        /// Public initializer | 
|  | 60 | +        /// - Parameters: | 
|  | 61 | +        ///   - deviceCode: the device code | 
|  | 62 | +        ///   - userCode: the user code | 
|  | 63 | +        ///   - verificationUri: the verification uri | 
|  | 64 | +        ///   - verificationUriComplete: the qr code or shortened url with embedded user code | 
|  | 65 | +        ///   - expiresIn: lifetime in seconds | 
|  | 66 | +        ///   - interval: the polling interval | 
|  | 67 | +        public init(deviceCode: String, userCode: String, | 
|  | 68 | +                    verificationUri: String, verificationUriComplete: String? = nil, | 
|  | 69 | +                    expiresIn: Int?, interval: Int) { | 
|  | 70 | +            self.deviceCode = deviceCode | 
|  | 71 | +            self.userCode = userCode | 
|  | 72 | +            self.verificationUri = verificationUri | 
|  | 73 | +            self.verificationUriComplete = verificationUriComplete | 
|  | 74 | +            self.expiresIn = expiresIn | 
|  | 75 | +            self.interval = interval | 
|  | 76 | +        } | 
|  | 77 | + | 
|  | 78 | +        /// Custom initializer for handling different keys sent by different providers (Google) | 
|  | 79 | +        /// - Parameters: | 
|  | 80 | +        ///   - decoder: the decoder to use | 
|  | 81 | +        public init(from decoder: any Decoder) throws { | 
|  | 82 | + | 
|  | 83 | +            let container = try decoder.container(keyedBy: CodingKeys.self) | 
|  | 84 | +            deviceCode = try container.decode(String.self, forKey: .deviceCode) | 
|  | 85 | +            userCode = try container.decode(String.self, forKey: .userCode) | 
|  | 86 | +            expiresIn = try container.decodeIfPresent(Int.self, forKey: .expiresIn) | 
|  | 87 | +            interval = try container.decode(Int.self, forKey: .interval) | 
|  | 88 | +            verificationUriComplete = try container.decodeIfPresent(String.self, forKey: .verificationUriComplete) | 
|  | 89 | + | 
|  | 90 | +            let verification = try container.decodeIfPresent(String.self, forKey: .verificationUri) | 
|  | 91 | +            if let verification { | 
|  | 92 | +                verificationUri = verification | 
|  | 93 | +            } else { | 
|  | 94 | +                verificationUri = try container.decode(String.self, forKey: .verificationUrl) | 
|  | 95 | +            } | 
|  | 96 | +        } | 
|  | 97 | + | 
|  | 98 | +        /// Encodes the device code. | 
|  | 99 | +        /// - Parameters: | 
|  | 100 | +        ///   - encoder: the encoder to use | 
|  | 101 | +        public func encode(to encoder: any Encoder) throws { | 
|  | 102 | +            var container = encoder.container(keyedBy: CodingKeys.self) | 
|  | 103 | +            try container.encode(deviceCode, forKey: .deviceCode) | 
|  | 104 | +            try container.encode(userCode, forKey: .userCode) | 
|  | 105 | +            try container.encode(verificationUri, forKey: .verificationUri) | 
|  | 106 | +            try container.encodeIfPresent(verificationUriComplete, forKey: .verificationUri) | 
|  | 107 | +            try container.encode(interval, forKey: .interval) | 
|  | 108 | +            try container.encodeIfPresent(expiresIn, forKey: .expiresIn) | 
|  | 109 | +        } | 
|  | 110 | +    } | 
|  | 111 | +} | 
0 commit comments