-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from dewonderstruck/feat/device-code-flow
OAuth 2.0 Device Authorization Grant
- Loading branch information
Showing
14 changed files
with
1,197 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Sources/VaporOAuth/DefaultImplementations/EmptyDeviceCodeManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Foundation | ||
|
||
public struct EmptyDeviceCodeManager: DeviceCodeManager { | ||
public func updateLastPolled(_ deviceCode: String) async throws { | ||
return | ||
} | ||
|
||
public func increaseInterval(_ deviceCode: String, by seconds: Int) async throws { | ||
return | ||
} | ||
|
||
|
||
public func authorizeDeviceCode(_ deviceCode: OAuthDeviceCode, userID: String) async throws { | ||
return | ||
} | ||
|
||
public func removeDeviceCode(_ deviceCode: OAuthDeviceCode) async throws { | ||
return | ||
} | ||
|
||
public init() {} | ||
|
||
public func generateDeviceCode( | ||
clientID: String, | ||
scopes: [String]?, | ||
verificationURI: String, | ||
verificationURIComplete: String? | ||
) async throws -> OAuthDeviceCode? { | ||
return nil | ||
} | ||
|
||
public func getDeviceCode(_ code: String) async throws -> OAuthDeviceCode? { | ||
return nil | ||
} | ||
|
||
public func getUserCode(_ code: String) async throws -> OAuthDeviceCode? { | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Foundation | ||
|
||
public final class OAuthDeviceCode: @unchecked Sendable { | ||
public let deviceCode: String | ||
public let userCode: String | ||
public let clientID: String | ||
public let verificationURI: String | ||
public let verificationURIComplete: String? | ||
public let expiryDate: Date | ||
public let interval: Int | ||
public let scopes: [String]? | ||
public var status: DeviceCodeStatus | ||
public var userID: String? | ||
public let lastPolled: Date? | ||
|
||
public var shouldIncreasePollInterval: Bool { | ||
guard let lastPolled = lastPolled else { return false } | ||
return Date().timeIntervalSince(lastPolled) < Double(interval) | ||
} | ||
|
||
public init( | ||
deviceCode: String, | ||
userCode: String, | ||
clientID: String, | ||
verificationURI: String, | ||
verificationURIComplete: String?, | ||
expiryDate: Date, | ||
interval: Int, | ||
scopes: [String]?, | ||
status: DeviceCodeStatus = .pending, | ||
userID: String? = nil, | ||
lastPolled: Date? = nil | ||
) { | ||
self.deviceCode = deviceCode | ||
self.userCode = userCode | ||
self.clientID = clientID | ||
self.verificationURI = verificationURI | ||
self.verificationURIComplete = verificationURIComplete | ||
self.expiryDate = expiryDate | ||
self.interval = interval | ||
self.scopes = scopes | ||
self.status = status | ||
self.userID = userID | ||
self.lastPolled = lastPolled | ||
} | ||
} | ||
|
||
public enum DeviceCodeStatus { | ||
case pending | ||
case authorized | ||
case unauthorized | ||
case declined | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Vapor | ||
|
||
public struct OAuthConfiguration: Sendable { | ||
public let deviceVerificationURI: String | ||
|
||
public init(deviceVerificationURI: String) { | ||
self.deviceVerificationURI = deviceVerificationURI | ||
} | ||
} | ||
|
||
extension Application { | ||
private struct OAuthConfigurationKey: StorageKey { | ||
typealias Value = OAuthConfiguration | ||
} | ||
|
||
public var oauth: OAuthConfiguration { | ||
get { | ||
guard let config = storage[OAuthConfigurationKey.self] else { | ||
fatalError("OAuth configuration not set. Use app.oauth = OAuthConfiguration(...)") | ||
} | ||
return config | ||
} | ||
set { | ||
storage[OAuthConfigurationKey.self] = newValue | ||
} | ||
} | ||
} |
Oops, something went wrong.