diff --git a/Package.resolved b/Package.resolved index d1fa5fa..30d2888 100644 --- a/Package.resolved +++ b/Package.resolved @@ -14,17 +14,17 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/tkey/curvelib.swift", "state" : { - "revision" : "9f88bd5e56d1df443a908f7a7e81ae4f4d9170ea", - "version" : "1.0.1" + "revision" : "432bf1abe7ff505fc2ac9fcf697341ff5b2dc6d0", + "version" : "2.0.0" } }, { "identity" : "fetch-node-details-swift", "kind" : "remoteSourceControl", - "location" : "https://github.com/torusresearch/fetch-node-details-swift.git", + "location" : "https://github.com/torusresearch/fetch-node-details-swift", "state" : { - "revision" : "4bd96c33ba8d02d9e27190c5c7cedf09cfdfd656", - "version" : "6.0.3" + "revision" : "52fb5efaa94e0fe3775913ab00964bcb51601c2a", + "version" : "8.0.0" } }, { @@ -36,6 +36,15 @@ "version" : "4.13.2" } }, + { + "identity" : "jwtdecode.swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/auth0/JWTDecode.swift", + "state" : { + "revision" : "1e153ef009969543191970c66b7c60163c0b4a65", + "version" : "3.2.0" + } + }, { "identity" : "keychain-swift", "kind" : "remoteSourceControl", @@ -50,8 +59,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/Web3Auth/session-manager-swift.git", "state" : { - "revision" : "67d5f7db655d02778861057fb280ecf47c923b09", - "version" : "5.0.0" + "revision" : "90afccc0e5e2ea8d671cdacb26899f60740b4e0b", + "version" : "6.0.0" } }, { @@ -68,8 +77,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/torusresearch/torus-utils-swift.git", "state" : { - "revision" : "eda55b8537a600e657d19d4c452c0a36f217883c", - "version" : "9.0.1" + "revision" : "baa822fc67bdb208ed1a2dc5b3c10485cfca15df", + "version" : "10.0.0" } } ], diff --git a/Package.swift b/Package.swift index 8a73111..daa26ca 100644 --- a/Package.swift +++ b/Package.swift @@ -15,18 +15,18 @@ let package = Package( targets: ["SingleFactorAuth"]) ], dependencies: [ - .package(url: "https://github.com/torusresearch/fetch-node-details-swift.git", from: "6.0.3"), - .package(url: "https://github.com/torusresearch/torus-utils-swift.git", from: "9.0.1"), + .package(url: "https://github.com/torusresearch/torus-utils-swift.git", from: "10.0.0"), .package(url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"), - .package(url: "https://github.com/Web3Auth/session-manager-swift.git", from: "5.0.0"), + .package(url: "https://github.com/Web3Auth/session-manager-swift.git", from: "6.0.0"), + .package(url: "https://github.com/auth0/JWTDecode.swift.git", from: "3.2.0") ], targets: [ .target( name: "SingleFactorAuth", dependencies: [ - .product(name: "FetchNodeDetails", package: "fetch-node-details-swift"), .product(name: "TorusUtils", package: "torus-utils-swift"), - .product(name: "SessionManager", package: "session-manager-swift") + .product(name: "SessionManager", package: "session-manager-swift"), + .product(name: "JWTDecode", package: "JWTDecode.swift") ]), .testTarget( name: "SingleFactorAuthTests", diff --git a/README.md b/README.md index c0fc0d9..8e85782 100644 --- a/README.md +++ b/README.md @@ -21,21 +21,21 @@ You can install the SingleFactorAuth Swift using Swift Package Manager. ... dependencies: [ ... - .package(url: "https://github.com/Web3Auth/single-factor-auth-swift/", from: "8.0.0") + .package(url: "https://github.com/Web3Auth/single-factor-auth-swift/", from: "9.0.0") ], ... ``` ## Getting Started -Initialize the `SingleFactAuth` class by passing `SFAParams` +Initialize the `SingleFactAuth` class by passing `Web3AuthOptions` ```swift let singleFactorAuthArgs = SingleFactorAuthArgs( - web3AuthClientId: "", + clientId: "", network: Web3AuthNetwork.SAPPHIRE_MAINNET ) -let singleFactoreAuth = SingleFactorAuth(params: SFAParams) +let singleFactoreAuth = SingleFactorAuth(params: Web3AuthOptions) ``` Use the `getKey` function to login the user and get the privateKey and public address for the given user. @@ -51,14 +51,6 @@ let loginParams = LoginParams( let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) ``` -We also have included Session Management in this SDK so call initialize function to get TorusKey value without relogging in the user if a user has an active session it will return the TorusKey struct otherwise it will return nil. - -```swift -if let savedKey = try await singleFactoreAuth.initialize() { - print(savedKey.getPrivateKey()) - print(savedKey.getPublicAddress()) -} -``` ## Requirements - iOS 14 or above is required diff --git a/SingleFactorAuth.podspec b/SingleFactorAuth.podspec index 4ef3984..50b1e17 100644 --- a/SingleFactorAuth.podspec +++ b/SingleFactorAuth.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "SingleFactorAuth" - spec.version = "8.0.0" + spec.version = "9.0.0" spec.ios.deployment_target = "14.0" spec.summary = "Enable one key flow for Web3Auth" spec.homepage = "https://github.com/Web3Auth/single-factor-auth-swift" @@ -10,8 +10,7 @@ Pod::Spec.new do |spec| spec.module_name = "SingleFactorAuth" spec.source = { :git => "https://github.com/web3Auth/single-factor-auth-swift", :tag => spec.version } spec.source_files = "Sources/SingleFactorAuth/*.{swift,json}","Sources/SingleFactorAuth/**/*.{swift,json}" - spec.dependency 'Torus-fetchNodeDetails', '~> 6.0.3' - spec.dependency 'curvelib.swift', '~> 1.0.1' - spec.dependency 'Torus-utils', '~> 9.0.1' - spec.dependency 'TorusSessionManager', '~> 5.0.0' + spec.dependency 'Torus-utils', '~> 10.0.0' + spec.dependency 'TorusSessionManager', '~> 6.0.0' + spec.dependency 'JWTDecode', '~> 3.2' end diff --git a/Sources/SingleFactorAuth/Error.swift b/Sources/SingleFactorAuth/Error.swift new file mode 100644 index 0000000..06e20f1 --- /dev/null +++ b/Sources/SingleFactorAuth/Error.swift @@ -0,0 +1,14 @@ +import Foundation + +public enum SFAError: Error, Equatable { + case MFAAlreadyEnabled +} + +extension SFAError: LocalizedError { + public var errorDescription: String? { + switch self { + case .MFAAlreadyEnabled: + return "User has already enabled MFA" + } + } +} diff --git a/Sources/SingleFactorAuth/LoginParams.swift b/Sources/SingleFactorAuth/LoginParams.swift index dd1d4ef..bcc7331 100644 --- a/Sources/SingleFactorAuth/LoginParams.swift +++ b/Sources/SingleFactorAuth/LoginParams.swift @@ -3,19 +3,16 @@ public class LoginParams { public let verifierId: String public let idToken: String public let subVerifierInfoArray: [TorusSubVerifierInfo]? + public let serverTimeOffset: Int? + public let fallbackUserInfo: UserInfo? - public init(verifier: String, verifierId: String, idToken: String) { - self.verifier = verifier - self.verifierId = verifierId - self.idToken = idToken - subVerifierInfoArray = nil - } - - public init(verifier: String, verifierId: String, idToken: String, subVerifierInfoArray: [TorusSubVerifierInfo]) { + public init(verifier: String, verifierId: String, idToken: String, subVerifierInfoArray: [TorusSubVerifierInfo]? = nil, serverTimeOffset: Int? = nil, fallbackUserInfo: UserInfo? = nil) { self.verifier = verifier self.verifierId = verifierId self.idToken = idToken self.subVerifierInfoArray = subVerifierInfoArray + self.serverTimeOffset = serverTimeOffset + self.fallbackUserInfo = fallbackUserInfo } } diff --git a/Sources/SingleFactorAuth/SFAKey.swift b/Sources/SingleFactorAuth/SFAKey.swift deleted file mode 100644 index 2beb0c7..0000000 --- a/Sources/SingleFactorAuth/SFAKey.swift +++ /dev/null @@ -1,24 +0,0 @@ -import BigInt -import Foundation - -public class SFAKey: Codable { - let privateKey: String - let publicAddress: String - - init(privateKey: String, publicAddress: String) { - self.privateKey = privateKey - self.publicAddress = publicAddress - } - - public func getPrivateKey() -> String { - return privateKey - } - - public func getPublicAddress() -> String { - return publicAddress - } - - func toString() -> String { - return "TorusKey{ privateKey='\(privateKey.description)', publicAddress='\(publicAddress)' }" - } -} diff --git a/Sources/SingleFactorAuth/SFAParams.swift b/Sources/SingleFactorAuth/SFAParams.swift deleted file mode 100644 index a198290..0000000 --- a/Sources/SingleFactorAuth/SFAParams.swift +++ /dev/null @@ -1,37 +0,0 @@ -import FetchNodeDetails - -public typealias Web3AuthNetwork = TorusNetwork - -public class SFAParams{ - private var network: TorusNetwork - private var networkUrl: String - private var web3AuthClientId: String - private var sessionTime: Int - - public init(web3AuthClientId: String, network: Web3AuthNetwork, sessionTime: Int = 86400, networkUrl: String = "") { - self.network = network - self.networkUrl = networkUrl - self.web3AuthClientId = web3AuthClientId - self.sessionTime = sessionTime - } - - public func getWeb3AuthClientId() -> String { - return web3AuthClientId - } - - public func getNetwork() -> Web3AuthNetwork { - return network - } - - public func getSignerUrl() -> String? { - return network.signerMap - } - - public func setNetwork(network: Web3AuthNetwork) { - self.network = network - } - - public func getSessionTime() -> Int { - return sessionTime - } -} diff --git a/Sources/SingleFactorAuth/SessionData.swift b/Sources/SingleFactorAuth/SessionData.swift new file mode 100644 index 0000000..c40a90a --- /dev/null +++ b/Sources/SingleFactorAuth/SessionData.swift @@ -0,0 +1,33 @@ +import BigInt +import Foundation +import TorusUtils + +public class SessionData: Codable { + let privateKey: String + let publicAddress: String + let signatures: TorusKey.SessionData? + let userInfo: UserInfo? + + init(privateKey: String, publicAddress: String, signatures: TorusKey.SessionData? = nil, userInfo: UserInfo? = nil) { + self.privateKey = privateKey + self.publicAddress = publicAddress + self.signatures = signatures + self.userInfo = userInfo + } + + public func getPrivateKey() -> String { + return privateKey + } + + public func getPublicAddress() -> String { + return publicAddress + } + + public func getUserInfo() -> UserInfo? { + return userInfo + } + + public func getSignatures() -> TorusKey.SessionData? { + return self.signatures + } +} diff --git a/Sources/SingleFactorAuth/SingleFactorAuth.swift b/Sources/SingleFactorAuth/SingleFactorAuth.swift index 37ebe79..ae951f5 100644 --- a/Sources/SingleFactorAuth/SingleFactorAuth.swift +++ b/Sources/SingleFactorAuth/SingleFactorAuth.swift @@ -4,6 +4,7 @@ import FetchNodeDetails import Foundation import SessionManager import TorusUtils +import JWTDecode #if canImport(curveSecp256k1) import curveSecp256k1 #endif @@ -12,39 +13,49 @@ public class SingleFactorAuth { let nodeDetailManager: NodeDetailManager let torusUtils: TorusUtils private var sessionManager: SessionManager + private var state: SessionData? - public init(params: SFAParams) throws { - sessionManager = SessionManager(sessionTime: params.getSessionTime(), allowedOrigin: Bundle.main.bundleIdentifier ?? "single-factor-auth-swift") + public init(params: Web3AuthOptions) throws { + sessionManager = SessionManager(sessionServerBaseUrl: params.getStorageServerUrl(), sessionTime: params.getSessionTime(), allowedOrigin: Bundle.main.bundleIdentifier ?? "single-factor-auth-swift") nodeDetailManager = NodeDetailManager(network: params.getNetwork()) - let torusOptions = TorusOptions(clientId: params.getWeb3AuthClientId(), network: params.getNetwork(), enableOneKey: true) + let torusOptions = TorusOptions(clientId: params.getClientId(), network: params.getNetwork(), serverTimeOffset: params.getServerTimeOffset(), enableOneKey: true) try torusUtils = TorusUtils(params: torusOptions) } - public func initialize() async throws -> SFAKey { - let data = try await sessionManager.authorizeSession(origin: Bundle.main.bundleIdentifier ?? "single-factor-auth-swift") - guard let privKey = data["privateKey"] as? String, - let publicAddress = data["publicAddress"] as? String else { throw SessionManagerError.decodingError } - return .init(privateKey: privKey, publicAddress: publicAddress) + public func initialize() async throws { + let savedSessionId = SessionManager.getSessionIdFromStorage() + + if savedSessionId != nil && !savedSessionId!.isEmpty { + sessionManager.setSessionId(sessionId: savedSessionId!) + + let data = try await sessionManager.authorizeSession(origin: Bundle.main.bundleIdentifier ?? "single-factor-auth-swift") + guard let privKey = data["privateKey"] as? String, + let publicAddress = data["publicAddress"] as? String, + let userInfo = data["userInfo"], + let signatures = data["signatures"] else { throw SessionManagerError.decodingError } + state = SessionData(privateKey: privKey, publicAddress: publicAddress, signatures: signatures as? TorusKey.SessionData, userInfo: userInfo as? UserInfo) + } } - public func isSessionIdExists() -> Bool { - if (sessionManager.getSessionID() != nil) && !(sessionManager.getSessionID()!.isEmpty) { - return true - } - return false + public func getSessionData() -> SessionData? { + return self.state + } + + public func connected() -> Bool { + return self.state != nil } - public func getTorusKey(loginParams: LoginParams) async throws -> TorusKey { + private func getTorusKey(loginParams: LoginParams) async throws -> TorusKey { var retrieveSharesResponse: TorusKey let details = try await nodeDetailManager.getNodeDetails(verifier: loginParams.verifier, verifierID: loginParams.verifierId) - let userDetails = try await torusUtils.getUserTypeAndAddress(endpoints: details.getTorusNodeEndpoints(), verifier: loginParams.verifier, verifierId: loginParams.verifierId) - - if userDetails.metadata?.upgraded == true { - throw "User already has enabled MFA" + /* TODO: Fix me + if let serverTimeOffset = loginParams.serverTimeOffset { + torusUtils.setServerTimeOffset(serverTimeOffset: serverTimeOffset) } - + */ + if let subVerifierInfoArray = loginParams.subVerifierInfoArray, !subVerifierInfoArray.isEmpty { var aggregateIdTokenSeeds = [String]() var subVerifierIds = [String]() @@ -62,7 +73,7 @@ public class SingleFactorAuth { let verifierParams = VerifierParams(verifier_id: loginParams.verifierId, sub_verifier_ids: subVerifierIds, verify_params: verifyParams) let aggregateIdToken = try curveSecp256k1.keccak256(data: Data(aggregateIdTokenSeeds.joined(separator: "\u{001d}").utf8)).toHexString() - + retrieveSharesResponse = try await torusUtils.retrieveShares( endpoints: details.getTorusNodeEndpoints(), verifier: loginParams.verifier, @@ -79,18 +90,48 @@ public class SingleFactorAuth { idToken: loginParams.idToken ) } + + if retrieveSharesResponse.metadata.upgraded == true { + throw SFAError.MFAAlreadyEnabled + } return retrieveSharesResponse } - public func connect(loginParams: LoginParams) async throws -> SFAKey { + public func connect(loginParams: LoginParams) async throws -> SessionData { let torusKey = try await getTorusKey(loginParams: loginParams) let publicAddress = torusKey.finalKeyData.evmAddress - let privateKey = torusKey.finalKeyData.privKey + let privateKey = if (torusKey.finalKeyData.privKey.isEmpty) { + torusKey.oAuthKeyData.privKey + } else { + torusKey.finalKeyData.privKey + } - let sfaKey = SFAKey(privateKey: privateKey, publicAddress: publicAddress) + var decodedUserInfo: UserInfo? = nil + + do { + let jwt = try decode(jwt: loginParams.idToken) + decodedUserInfo = UserInfo.init(email: jwt.body["email"] as? String ?? "", name: jwt.body["name"] as? String ?? "", profileImage: jwt.body["picture"] as? String ?? "", verifier: loginParams.verifier, verifierId: loginParams.verifierId, typeOfLogin: LoginType.jwt, state: .init(params: [:])) + } catch { + decodedUserInfo = loginParams.fallbackUserInfo + } + + let sessionId = try SessionManager.generateRandomSessionID()! + sessionManager.setSessionId(sessionId: sessionId) + + let sfaKey = SessionData(privateKey: privateKey, publicAddress: publicAddress, signatures: torusKey.sessionData, userInfo: decodedUserInfo) _ = try await sessionManager.createSession(data: sfaKey) + + SessionManager.saveSessionIdToStorage(sessionId) + + self.state = sfaKey return sfaKey } + + public func logout() async throws { + try await sessionManager.invalidateSession() + SessionManager.deleteSessionIdFromStorage() + self.state = nil + } } diff --git a/Sources/SingleFactorAuth/Web3AuthOptions.swift b/Sources/SingleFactorAuth/Web3AuthOptions.swift new file mode 100644 index 0000000..1d3fc8e --- /dev/null +++ b/Sources/SingleFactorAuth/Web3AuthOptions.swift @@ -0,0 +1,47 @@ +import FetchNodeDetails + +public class Web3AuthOptions { + private var web3AuthNetwork: Web3AuthNetwork + private var clientId: String + private var sessionTime: Int + private var storageServerUrl: String? = nil + private var serverTimeOffset: Int = 0 + + public init(clientId: String, web3AuthNetwork: Web3AuthNetwork, sessionTime: Int = 86400, serverTimeOffset: Int? = nil, storageServerUrl: String? = nil) { + self.web3AuthNetwork = web3AuthNetwork + self.clientId = clientId + self.sessionTime = sessionTime + self.storageServerUrl = storageServerUrl + if serverTimeOffset != nil { + self.serverTimeOffset = serverTimeOffset! + } + } + + public func getClientId() -> String { + return clientId + } + + public func getNetwork() -> Web3AuthNetwork { + return web3AuthNetwork + } + + public func getServerTimeOffset() -> Int { + return self.serverTimeOffset + } + + public func getStorageServerUrl() -> String? { + return self.storageServerUrl + } + + public func getSignerUrl() -> String? { + return web3AuthNetwork.signerMap + } + + public func setNetwork(network: Web3AuthNetwork) { + self.web3AuthNetwork = network + } + + public func getSessionTime() -> Int { + return sessionTime + } +} diff --git a/Sources/SingleFactorAuth/common/LoginType.swift b/Sources/SingleFactorAuth/common/LoginType.swift new file mode 100644 index 0000000..115b375 --- /dev/null +++ b/Sources/SingleFactorAuth/common/LoginType.swift @@ -0,0 +1,19 @@ +import Foundation + +public enum LoginType: String, Equatable, Hashable, Codable { + case google + case facebook + case discord + case reddit + case twitch + case apple + case github + case linkedin + case twitter + case weibo + case line + case email_password + case email_passwordless + case sms_passwordless + case jwt +} diff --git a/Sources/SingleFactorAuth/common/TorusgenericContainer.swift b/Sources/SingleFactorAuth/common/TorusgenericContainer.swift new file mode 100644 index 0000000..ca1abf0 --- /dev/null +++ b/Sources/SingleFactorAuth/common/TorusgenericContainer.swift @@ -0,0 +1,9 @@ +import Foundation + +public class TorusGenericContainer: Codable { + public let params: [String: String] + + init(params: [String: String]) { + self.params = params + } +} diff --git a/Sources/SingleFactorAuth/common/UserInfo.swift b/Sources/SingleFactorAuth/common/UserInfo.swift new file mode 100644 index 0000000..b2d1e66 --- /dev/null +++ b/Sources/SingleFactorAuth/common/UserInfo.swift @@ -0,0 +1,37 @@ +//TODO: Contents of common folder must move to a common dependency +import Foundation + +public class UserInfo: Codable { + public let email: String + public let name: String + public let profileImage: String + public let aggregateVerifier: String? + public let verifier: String + public let verifierId: String + public let typeOfLogin: LoginType + public let ref: String? + // public let extraVerifierParams: PassKeyExtraParams? + public let accessToken: String? + public let idToken: String? + public let extraParams: String? + public let extraParamsPassed: String? + public let state: TorusGenericContainer + + public init(email: String, name: String, profileImage: String, aggregateVerifier: String? = nil, verifier: String, verifierId: String, typeOfLogin: LoginType, ref: String? = nil, // extraVerifierParams: PassKeyExtraParams? = nil, + accessToken: String? = nil, idToken: String? = nil, extraParams: String? = nil, extraParamsPassed: String? = nil, state: TorusGenericContainer) { + self.email = email + self.name = name + self.profileImage = profileImage + self.aggregateVerifier = aggregateVerifier + self.verifier = verifier + self.verifierId = verifierId + self.typeOfLogin = typeOfLogin + self.ref = ref + // extraVerifierParams = extraVerifierParams + self.accessToken = accessToken + self.idToken = idToken + self.extraParams = extraParams + self.extraParamsPassed = extraParamsPassed + self.state = state + } +} diff --git a/Tests/SingleFactorAuthTests/AquaTest.swift b/Tests/SingleFactorAuthTests/AquaTest.swift index 2bc5f4d..68e6c62 100644 --- a/Tests/SingleFactorAuthTests/AquaTest.swift +++ b/Tests/SingleFactorAuthTests/AquaTest.swift @@ -6,14 +6,14 @@ import SingleFactorAuth final class AquaTest: XCTestCase { var singleFactoreAuth: SingleFactorAuth! - var singleFactorAuthArgs: SFAParams! + var singleFactorAuthArgs: Web3AuthOptions! let TORUS_TEST_EMAIL = "hello@tor.us" let TEST_VERIFIER = "torus-test-health" let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate" override func setUp() { - singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.AQUA)) + singleFactorAuthArgs = Web3AuthOptions(clientId: "CLIENT ID", web3AuthNetwork: .AQUA) singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) } @@ -31,10 +31,10 @@ final class AquaTest: XCTestCase { let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) - let savedKey = try await singleFactoreAuth.initialize() + try await singleFactoreAuth.initialize() let requiredPrivateKey = "d8204e9f8c270647294c54acd8d49ee208789f981a7503158e122527d38626d8" - XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) - XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress()) + XCTAssertEqual(requiredPrivateKey, singleFactoreAuth.getSessionData()!.getPrivateKey()) + XCTAssertEqual(torusKey.getPublicAddress(), singleFactoreAuth.getSessionData()!.getPublicAddress()) } func testAggregrateGetTorusKey() async throws { diff --git a/Tests/SingleFactorAuthTests/CyanTest.swift b/Tests/SingleFactorAuthTests/CyanTest.swift index ecf1b81..92972af 100644 --- a/Tests/SingleFactorAuthTests/CyanTest.swift +++ b/Tests/SingleFactorAuthTests/CyanTest.swift @@ -5,14 +5,14 @@ import SingleFactorAuth final class CyanTest: XCTestCase { var singleFactoreAuth: SingleFactorAuth! - var singleFactorAuthArgs: SFAParams! + var singleFactorAuthArgs: Web3AuthOptions! let TORUS_TEST_EMAIL = "hello@tor.us" let TEST_VERIFIER = "torus-test-health" let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate" override func setUp() { - singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.CYAN)) + singleFactorAuthArgs = Web3AuthOptions(clientId: "CLIENT ID", web3AuthNetwork: .CYAN) singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) } @@ -31,9 +31,9 @@ final class CyanTest: XCTestCase { let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "223d982054fa1ad27d1497560521e4cce5b8c6438c38533c7bad27ff21ce0546" - let savedKey = try await singleFactoreAuth.initialize() - XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) - XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress()) + try await singleFactoreAuth.initialize() + XCTAssertEqual(requiredPrivateKey, singleFactoreAuth.getSessionData()!.getPrivateKey()) + XCTAssertEqual(torusKey.getPublicAddress(), singleFactoreAuth.getSessionData()!.getPublicAddress()) } func testAggregrateGetTorusKey() async throws { diff --git a/Tests/SingleFactorAuthTests/SapphireDevnetTests.swift b/Tests/SingleFactorAuthTests/SapphireDevnetTests.swift index 32f13a5..cf7dff9 100644 --- a/Tests/SingleFactorAuthTests/SapphireDevnetTests.swift +++ b/Tests/SingleFactorAuthTests/SapphireDevnetTests.swift @@ -1,19 +1,19 @@ import BigInt -import JWTKit -import XCTest import FetchNodeDetails +import JWTKit import SingleFactorAuth +import XCTest final class SapphireDevnetTests: XCTestCase { var singleFactoreAuth: SingleFactorAuth! - var singleFactorAuthArgs: SFAParams! + var singleFactorAuthArgs: Web3AuthOptions! let TORUS_TEST_EMAIL = "devnettestuser@tor.us" let TEST_VERIFIER = "torus-test-health" let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate" override func setUp() { - singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .sapphire(.SAPPHIRE_DEVNET)) + singleFactorAuthArgs = Web3AuthOptions(clientId: "CLIENT ID", web3AuthNetwork: .SAPPHIRE_DEVNET) singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) } @@ -30,11 +30,8 @@ final class SapphireDevnetTests: XCTestCase { func testInitialise() async throws { let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) - let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) - let savedKey = try await singleFactoreAuth.initialize() - let requiredPrivateKey = "230dad9f42039569e891e6b066ff5258b14e9764ef5176d74aeb594d1a744203" - XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) - XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress()) + let _ = try await singleFactoreAuth.connect(loginParams: loginParams) + try await singleFactoreAuth.initialize() } func testAggregrateGetTorusKey() async throws { diff --git a/Tests/SingleFactorAuthTests/SapphireMainnetTests.swift b/Tests/SingleFactorAuthTests/SapphireMainnetTests.swift index 16d7131..db4e5cb 100644 --- a/Tests/SingleFactorAuthTests/SapphireMainnetTests.swift +++ b/Tests/SingleFactorAuthTests/SapphireMainnetTests.swift @@ -1,19 +1,19 @@ import BigInt -import JWTKit -import XCTest import FetchNodeDetails +import JWTKit import SingleFactorAuth +import XCTest final class SapphireMainnetTests: XCTestCase { var singleFactoreAuth: SingleFactorAuth! - var singleFactorAuthArgs: SFAParams! + var singleFactorAuthArgs: Web3AuthOptions! let TORUS_TEST_EMAIL = "devnettestuser@tor.us" let TEST_VERIFIER = "torus-test-health" let TEST_AGGREGRATE_VERIFIER = "torus-aggregate-sapphire-mainnet" override func setUp() { - singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .sapphire(.SAPPHIRE_MAINNET)) + singleFactorAuthArgs = Web3AuthOptions(clientId: "CLIENT ID", web3AuthNetwork: .SAPPHIRE_MAINNET) singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) } @@ -31,10 +31,10 @@ final class SapphireMainnetTests: XCTestCase { let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) - let savedKey = try await singleFactoreAuth.initialize() + try await singleFactoreAuth.initialize() let requiredPrivateKey = "2c4b346a91ecd11fe8a02d111d00bd921bf9b543f0a1e811face91b5f28947d6" - XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) - XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress()) + XCTAssertEqual(requiredPrivateKey, singleFactoreAuth.getSessionData()!.getPrivateKey()) + XCTAssertEqual(torusKey.getPublicAddress(), singleFactoreAuth.getSessionData()!.getPublicAddress()) } func testAggregrateGetTorusKey() async throws { @@ -46,4 +46,13 @@ final class SapphireMainnetTests: XCTestCase { XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) XCTAssertEqual("0xA92E2C756B5b2abABc127907b02D4707dc085612", torusKey.getPublicAddress()) } + + func testLogout() async throws { + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) + let _ = try await singleFactoreAuth.connect(loginParams: loginParams) + + try await singleFactoreAuth.logout() + XCTAssertNil(singleFactoreAuth.getSessionData()) + } } diff --git a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift index f1d1c07..355efd0 100644 --- a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift +++ b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift @@ -5,14 +5,14 @@ import SingleFactorAuth final class SingleFactorAuthTests: XCTestCase { var singleFactoreAuth: SingleFactorAuth! - var singleFactorAuthArgs: SFAParams! + var singleFactorAuthArgs: Web3AuthOptions! let TORUS_TEST_EMAIL = "hello@tor.us" let TEST_VERIFIER = "torus-test-health" let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate" override func setUp() { - singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.TESTNET)) + singleFactorAuthArgs = Web3AuthOptions(clientId: "CLIENT ID", web3AuthNetwork: .TESTNET) singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) } @@ -29,10 +29,10 @@ final class SingleFactorAuthTests: XCTestCase { let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) - let savedKey = try await singleFactoreAuth.initialize() + try await singleFactoreAuth.initialize() let requiredPrivateKey = "296045a5599afefda7afbdd1bf236358baff580a0fe2db62ae5c1bbe817fbae4" - XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) - XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress()) + XCTAssertEqual(requiredPrivateKey, singleFactoreAuth.getSessionData()!.getPrivateKey()) + XCTAssertEqual(torusKey.getPublicAddress(), singleFactoreAuth.getSessionData()!.getPublicAddress()) } func testAggregrateGetTorusKey() async throws {