This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
forked from brokenhandsio/vapor-oauth
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #10 from vamsii777/patch
Comprehensive Refactoring of OAuth Components for Concurrency and PKCE Support
- Loading branch information
Showing
16 changed files
with
156 additions
and
89 deletions.
There are no files selected for viewing
16 changes: 8 additions & 8 deletions
16
Sources/VaporOAuth/DefaultImplementations/EmptyCodeManager.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 |
---|---|---|
@@ -1,31 +1,31 @@ | ||
public struct EmptyCodeManager: CodeManager { | ||
public init() {} | ||
|
||
public func getCode(_ code: String) -> OAuthCode? { | ||
return nil | ||
} | ||
|
||
// Updated to include PKCE parameters | ||
|
||
public func generateCode( | ||
userID: String, | ||
clientID: String, | ||
redirectURI: String, | ||
scopes: [String]?, | ||
codeChallenge: String?, | ||
codeChallengeMethod: String? | ||
codeChallengeMethod: String?, | ||
nonce: String? | ||
) async throws -> String { | ||
return "" | ||
} | ||
|
||
public func codeUsed(_ code: OAuthCode) {} | ||
|
||
public func getDeviceCode(_ deviceCode: String) -> OAuthDeviceCode? { | ||
return nil | ||
} | ||
|
||
public func generateDeviceCode(userID: String, clientID: String, scopes: [String]?) async throws -> String { | ||
return "" | ||
} | ||
|
||
public func deviceCodeUsed(_ deviceCode: OAuthDeviceCode) {} | ||
} |
10 changes: 4 additions & 6 deletions
10
Sources/VaporOAuth/DefaultImplementations/StaticClientRetriever.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 |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import Vapor | ||
|
||
public actor StaticClientRetriever: ClientRetriever { | ||
public struct StaticClientRetriever: ClientRetriever { | ||
private let clients: [String: OAuthClient] | ||
|
||
public init(clients: [OAuthClient]) { | ||
self.clients = clients.reduce(into: [String: OAuthClient]()) { (dict, client) in | ||
dict[client.clientID] = client | ||
} | ||
} | ||
|
||
public func getClient(clientID: String) async throws -> OAuthClient? { | ||
public func getClient(clientID: String) throws -> OAuthClient? { | ||
return clients[clientID] | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,13 +1,46 @@ | ||
/// Responsible for generating and managing OAuth Codes | ||
public protocol CodeManager: Sendable { | ||
// Updated to include PKCE parameters | ||
func generateCode(userID: String, clientID: String, redirectURI: String, scopes: [String]?, codeChallenge: String?, codeChallengeMethod: String?) async throws -> String | ||
/// Generates an OAuth code for the specified user, client, redirect URI, scopes, code challenge, and code challenge method. | ||
/// - Parameters: | ||
/// - userID: The ID of the user. | ||
/// - clientID: The ID of the client. | ||
/// - redirectURI: The redirect URI. | ||
/// - scopes: The requested scopes. | ||
/// - codeChallenge: The code challenge. | ||
/// - codeChallengeMethod: The code challenge method. | ||
/// - nonce: The nonce. | ||
/// - Returns: The generated OAuth code. | ||
/// - Throws: An error if the code generation fails. | ||
func generateCode(userID: String, clientID: String, redirectURI: String, scopes: [String]?, codeChallenge: String?, codeChallengeMethod: String?, nonce: String?) async throws -> String | ||
|
||
/// Retrieves the OAuth code associated with the specified code. | ||
/// - Parameter code: The OAuth code. | ||
/// - Returns: The associated OAuth code, or `nil` if not found. | ||
/// - Throws: An error if the retrieval fails. | ||
func getCode(_ code: String) async throws -> OAuthCode? | ||
|
||
// This is explicit to ensure that the code is marked as used or deleted (it could be implied that this is done when you call | ||
// `getCode` but it is called explicitly to remind developers to ensure that codes can't be reused) | ||
|
||
/// Marks the specified OAuth code as used or deleted. | ||
/// - Parameter code: The OAuth code to mark as used or deleted. | ||
/// - Throws: An error if the operation fails. | ||
func codeUsed(_ code: OAuthCode) async throws | ||
|
||
/// Generates a device code for the specified user, client, and scopes. | ||
/// - Parameters: | ||
/// - userID: The ID of the user. | ||
/// - clientID: The ID of the client. | ||
/// - scopes: The requested scopes. | ||
/// - Returns: The generated device code. | ||
/// - Throws: An error if the code generation fails. | ||
func generateDeviceCode(userID: String, clientID: String, scopes: [String]?) async throws -> String | ||
|
||
/// Retrieves the device code associated with the specified device code. | ||
/// - Parameter deviceCode: The device code. | ||
/// - Returns: The associated device code, or `nil` if not found. | ||
/// - Throws: An error if the retrieval fails. | ||
func getDeviceCode(_ deviceCode: String) async throws -> OAuthDeviceCode? | ||
|
||
/// Marks the specified device code as used or deleted. | ||
/// - Parameter deviceCode: The device code to mark as used or deleted. | ||
/// - Throws: An error if the operation fails. | ||
func deviceCodeUsed(_ deviceCode: OAuthDeviceCode) async throws | ||
} |
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
7 changes: 0 additions & 7 deletions
7
Sources/VaporOAuth/RouteHandlers/TokenHandlers/DeviceCodeTokenHandler.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
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
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,34 @@ | ||
import Foundation | ||
import Crypto | ||
|
||
struct PKCEValidator { | ||
|
||
static func validate(codeChallenge: String, verifier: String?, method: String?) -> Bool { | ||
guard let verifier = verifier else { | ||
// Fail validation if codeVerifier is not provided | ||
return false | ||
} | ||
|
||
guard let method = method else { | ||
// Default to plain if no method is provided | ||
return codeChallenge == verifier | ||
} | ||
|
||
switch method { | ||
case "S256": | ||
return validateS256(codeChallenge: codeChallenge, verifier: verifier) | ||
case "plain": | ||
return codeChallenge == verifier | ||
default: | ||
// Unsupported code challenge method | ||
return false | ||
} | ||
} | ||
|
||
private static func validateS256(codeChallenge: String, verifier: String) -> Bool { | ||
guard let verifierData = verifier.data(using: .utf8) else { return false } | ||
let hashedVerifier = SHA256.hash(data: verifierData) | ||
let base64UrlEncodedHash = Data(hashedVerifier).base64URLEncodedString() | ||
return codeChallenge == base64UrlEncodedHash | ||
} | ||
} |
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
Oops, something went wrong.