Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Fix all test cases related to device code token handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vamsii777 committed Aug 24, 2023
1 parent f74ba60 commit 5a45fde
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 49 deletions.
68 changes: 34 additions & 34 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,44 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/async-http-client.git",
"state" : {
"revision" : "fc510a39cff61b849bf5cdff17eb2bd6d0777b49",
"version" : "1.11.5"
"revision" : "16f7e62c08c6969899ce6cc277041e868364e5cf",
"version" : "1.19.0"
}
},
{
"identity" : "async-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/async-kit.git",
"state" : {
"revision" : "c3329e444bafbb12d1d312af9191be95348a8175",
"version" : "1.13.0"
"revision" : "eab9edff78e8ace20bd7cb6e792ab46d54f59ab9",
"version" : "1.18.0"
}
},
{
"identity" : "console-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/console-kit.git",
"state" : {
"revision" : "a7e67a1719933318b5ab7eaaed355cde020465b1",
"version" : "4.5.0"
"revision" : "9a12000f4064a2bdc49068d7258292ec1bdc88fc",
"version" : "4.7.0"
}
},
{
"identity" : "multipart-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/multipart-kit.git",
"state" : {
"revision" : "0d55c35e788451ee27222783c7d363cb88092fab",
"version" : "4.5.2"
"revision" : "1adfd69df2da08f7931d4281b257475e32c96734",
"version" : "4.5.4"
}
},
{
"identity" : "routing-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/routing-kit.git",
"state" : {
"revision" : "ffac7b3a127ce1e85fb232f1a6271164628809ad",
"version" : "4.6.0"
"revision" : "e0539da5b60a60d7381f44cdcf04036f456cee2f",
"version" : "4.8.0"
}
},
{
Expand All @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "919eb1d83e02121cdb434c7bfc1f0c66ef17febe",
"version" : "1.0.2"
"revision" : "6c89474e62719ddcc1e9614989fff2f68208fe10",
"version" : "1.1.0"
}
},
{
Expand All @@ -77,80 +77,80 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "f504716c27d2e5d4144fa4794b12129301d17729",
"version" : "1.0.3"
"revision" : "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version" : "1.0.4"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "d9825fa541df64b1a7b182178d61b9a82730d01f",
"version" : "2.1.0"
"revision" : "60f13f60c4d093691934dc6cfdf5f508ada1f894",
"version" : "2.6.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "6fe203dc33195667ce1759bf0182975e4653ba1c",
"version" : "1.4.4"
"revision" : "532d8b529501fb73a2455b179e0bbb6d49b652ed",
"version" : "1.5.3"
}
},
{
"identity" : "swift-metrics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-metrics.git",
"state" : {
"revision" : "53be78637ecd165d1ddedc4e20de69b8f43ec3b7",
"version" : "2.3.2"
"revision" : "971ba26378ab69c43737ee7ba967a896cb74c0d1",
"version" : "2.4.1"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "b4e0a274f7f34210e97e2f2c50ab02a10b549250",
"version" : "2.41.1"
"revision" : "cf281631ff10ec6111f2761052aa81896a83a007",
"version" : "2.58.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "6c84d247754ad77487a6f0694273b89b83efd056",
"version" : "1.14.0"
"revision" : "0e0d0aab665ff1a0659ce75ac003081f2b1c8997",
"version" : "1.19.0"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "f9ab1c94c80d568efd762d2a638f25162691d766",
"version" : "1.22.1"
"revision" : "a8ccf13fa62775277a5d56844878c828bbb3be1a",
"version" : "1.27.0"
}
},
{
"identity" : "swift-nio-ssl",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-ssl.git",
"state" : {
"revision" : "ba7c0d7f82affc518147ea61d240330bf7f7ea9b",
"version" : "2.22.1"
"revision" : "320bd978cceb8e88c125dcbb774943a92f6286e9",
"version" : "2.25.0"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "4e02d9cf35cabfb538c96613272fb027dd0c8692",
"version" : "1.13.1"
"revision" : "e7403c35ca6bb539a7ca353b91cc2d8ec0362d58",
"version" : "1.19.0"
}
},
{
Expand All @@ -167,17 +167,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/vapor.git",
"state" : {
"revision" : "dda0de537e7906414dccd551e77095be1e34e3da",
"version" : "4.65.2"
"revision" : "1bb4a2ed94bec7a92f92e82896408c785d068f5c",
"version" : "4.79.0"
}
},
{
"identity" : "websocket-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/websocket-kit.git",
"state" : {
"revision" : "2d9d2188a08eef4a869d368daab21b3c08510991",
"version" : "2.6.1"
"revision" : "53fe0639a98903858d0196b699720decb42aee7b",
"version" : "2.14.0"
}
}
],
Expand Down
16 changes: 16 additions & 0 deletions Tests/VaporOAuthTests/Fakes/FakeCodeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,36 @@ class FakeCodeManager: CodeManager {

private(set) var usedCodes: [String] = []
var codes: [String: OAuthCode] = [:]
var deviceCodes: [String: OAuthDeviceCode] = [:]
var generatedCode = UUID().uuidString

func getCode(_ code: String) -> OAuthCode? {
return codes[code]
}

func getDeviceCode(_ deviceCode: String) -> OAuthDeviceCode? {
return deviceCodes[deviceCode]
}

func generateCode(userID: String, clientID: String, redirectURI: String, scopes: [String]?) throws -> String {
let code = OAuthCode(codeID: generatedCode, clientID: clientID, redirectURI: redirectURI, userID: userID, expiryDate: Date().addingTimeInterval(60), scopes: scopes)
codes[generatedCode] = code
return generatedCode
}

func generateDeviceCode(userID: String, clientID: String, scopes: [String]?) throws -> String { // Added to generate a device code
let deviceCode = OAuthDeviceCode(deviceCodeID: generatedCode, userCode: "USER_CODE", clientID: clientID, userID: userID, expiryDate: Date().addingTimeInterval(60), scopes: scopes)
deviceCodes[generatedCode] = deviceCode
return generatedCode
}

func codeUsed(_ code: OAuthCode) {
usedCodes.append(code.codeID)
codes.removeValue(forKey: code.codeID)
}

func deviceCodeUsed(_ deviceCode: OAuthDeviceCode) {
usedCodes.append(deviceCode.deviceCodeID)
deviceCodes.removeValue(forKey: deviceCode.deviceCodeID)
}
}
16 changes: 9 additions & 7 deletions Tests/VaporOAuthTests/Fakes/FakeTokenManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,41 @@ import VaporOAuth
import Foundation

class FakeTokenManager: TokenManager {

var accessTokenToReturn = "ACCESS-TOKEN-STRING"
var refreshTokenToReturn = "REFRESH-TOKEN-STRING"
var refreshTokens: [String: RefreshToken] = [:]
var accessTokens: [String: AccessToken] = [:]
var deviceCodes: [String: OAuthDeviceCode] = [:]
var currentTime = Date()

func getRefreshToken(_ refreshToken: String) -> RefreshToken? {
return refreshTokens[refreshToken]
}

func getAccessToken(_ accessToken: String) -> AccessToken? {
return accessTokens[accessToken]
}

func generateAccessRefreshTokens(clientID: String, userID: String?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken) {
let accessToken = FakeAccessToken(tokenString: accessTokenToReturn, clientID: clientID, userID: userID, scopes: scopes, expiryTime: currentTime.addingTimeInterval(TimeInterval(accessTokenExpiryTime)))
let refreshToken = FakeRefreshToken(tokenString: refreshTokenToReturn, clientID: clientID, userID: userID, scopes: scopes)

accessTokens[accessTokenToReturn] = accessToken
refreshTokens[refreshTokenToReturn] = refreshToken
return (accessToken, refreshToken)
}

func generateAccessToken(clientID: String, userID: String?, scopes: [String]?, expiryTime: Int) throws -> AccessToken {
let accessToken = FakeAccessToken(tokenString: accessTokenToReturn, clientID: clientID, userID: userID, scopes: scopes, expiryTime: currentTime.addingTimeInterval(TimeInterval(expiryTime)))
accessTokens[accessTokenToReturn] = accessToken
return accessToken
}

func updateRefreshToken(_ refreshToken: RefreshToken, scopes: [String]) {
var tempRefreshToken = refreshToken
tempRefreshToken.scopes = scopes
refreshTokens[refreshToken.tokenString] = tempRefreshToken
}

}
16 changes: 16 additions & 0 deletions Tests/VaporOAuthTests/Fakes/StubCodeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,20 @@ class StubCodeManager: CodeManager {
func codeUsed(_ code: OAuthCode) {

}

func getDeviceCode(_ deviceCode: String) -> OAuthDeviceCode? {

return nil
}

func generateDeviceCode(userID: String, clientID: String, scopes: [String]?) throws -> String {

return "DEVICE_CODE"
}

func deviceCodeUsed(_ deviceCode: OAuthDeviceCode) {

}


}
5 changes: 3 additions & 2 deletions Tests/VaporOAuthTests/Fakes/StubTokenManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import VaporOAuth
import Foundation

class StubTokenManager: TokenManager {

var accessToken = "ABCDEF"
var refreshToken = "GHIJKL"

var deviceCodes: [String: OAuthDeviceCode] = [:]

func generateAccessRefreshTokens(clientID: String, userID: String?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken) {
let access = FakeAccessToken(tokenString: accessToken, clientID: clientID, userID: userID, scopes: scopes, expiryTime: Date())
let refresh = FakeRefreshToken(tokenString: refreshToken, clientID: clientID, userID: nil, scopes: scopes)
Expand Down
Loading

0 comments on commit 5a45fde

Please sign in to comment.