Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
Check defaults for CustomAuthArgs
Check login types, implement caseSensitiveField helper function, use helper in getVerifierId
  • Loading branch information
metalurgical committed Sep 5, 2024
1 parent dfa6cb5 commit caf4c58
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Sources/CustomAuth/Common/LoginParams/LoginType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ public enum LoginType: String, Equatable, Hashable, Codable {
case line
case email_password
case passwordless
case email_passwordless
case sms_passwordless
case jwt
}
4 changes: 2 additions & 2 deletions Sources/CustomAuth/CustomAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class CustomAuth {
let torusUtils = try TorusUtils(params: torusOptions)
torus = torusUtils

torus.setApiKey(apiKey: config.apiKey ?? "")
torus.setApiKey(apiKey: config.apiKey)
}

/// Initiates a login using a single verifier
Expand Down Expand Up @@ -219,7 +219,7 @@ public class CustomAuth {
/// - Returns: `TorusKey`
///
/// - Throws: `CASDKError`, `TorusUtilError`, `FetchNodeError`
func getTorusKey(verifier: String, verifierParams: VerifierParams, idToken: String) async throws -> TorusKey {
func getTorusKey(verifier: String, verifierParams: VerifierParams, idToken: String, extraParams: TorusUtilsExtraParams? = nil) async throws -> TorusKey {
let nodeDetails = try await nodeDetailManager.getNodeDetails(verifier: verifier, verifierID: verifierParams.verifier_id)

return try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierParams: verifierParams, idToken: idToken)
Expand Down
21 changes: 17 additions & 4 deletions Sources/CustomAuth/CustomAuthArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@ import Foundation

public class CustomAuthArgs {
public let urlScheme: String
public let metadataUrl: String?
public let metadataUrl: String
public let network: TorusNetwork
public let enableLogging: Bool
public let enableOneKey: Bool
public let apiKey: String?
public let apiKey: String
public let popupFeatures: String?
public let storageServerUrl: String?
public let storageServerUrl: String
public let web3AuthClientId: String
public let serverTimeOffset: Int
public let useDkg: Bool // TODO: Implement usage of this

public init(urlScheme: String, metadataUrl: String? = nil, network: TorusNetwork, enableLogging: Bool = false, enableOneKey: Bool, apiKey: String? = nil, popupFeatures: String? = nil, storageServerUrl: String? = nil, web3AuthClientId: String, serverTimeOffset: Int = 0, legacyMetadataHost: String? = nil) {
public init(urlScheme: String,
network: TorusNetwork,
metadataUrl: String = "https://metadata.tor.us",
enableLogging: Bool = false,
apiKey: String = "torus-default",
storageServerUrl: String = "https://session.web3auth.io",
enableOneKey: Bool = false,
web3AuthClientId: String,
useDkg: Bool,
serverTimeOffset: Int = 0,
popupFeatures: String? = nil
) {
self.urlScheme = urlScheme
self.metadataUrl = metadataUrl
self.network = network
Expand All @@ -24,5 +36,6 @@ public class CustomAuthArgs {
self.storageServerUrl = storageServerUrl
self.web3AuthClientId = web3AuthClientId
self.serverTimeOffset = serverTimeOffset
self.useDkg = useDkg
}
}
37 changes: 30 additions & 7 deletions Sources/CustomAuth/Handlers/HandlerFactory.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import Foundation

internal class HandlerFactory {
internal class HandlerFactory {
static func createHandler(
params: CreateHandlerParams
) throws -> ILoginHandler {
if params.verifier.isEmpty {
throw CASDKError.invalidVerifier
}

if params.clientId.isEmpty {
throw CASDKError.invalidClientID
}

let domain = params.jwtParams?.domain
let hint = params.jwtParams?.login_hint
let idToken = params.jwtParams?.id_token
let accessToken = params.jwtParams?.access_token

switch params.typeOfLogin {
case .google:
return try GoogleLoginHandler(clientId: params.clientId, verifier: params.verifier, urlScheme: params.urlScheme, redirectURL: params.redirectURL, typeOfLogin: params.typeOfLogin, jwtParams: params.jwtParams, customState: params.customState)
case .facebook:
return try FacebookLoginHandler(clientId: params.clientId, verifier: params.verifier, urlScheme: params.urlScheme, redirectURL: params.redirectURL, typeOfLogin: params.typeOfLogin, jwtParams: params.jwtParams, customState: params.customState)
case .reddit:
if idToken != nil || accessToken != nil {
return try MockLoginHandler(clientId: params.clientId, verifier: params.verifier, urlScheme: params.urlScheme, redirectURL: params.redirectURL, typeOfLogin: params.typeOfLogin, jwtParams: params.jwtParams, customState: params.customState)
}
return try RedditLoginHandler(clientId: params.clientId, verifier: params.verifier, urlScheme: params.urlScheme, redirectURL: params.redirectURL, typeOfLogin: params.typeOfLogin, jwtParams: params.jwtParams, customState: params.customState)
case .twitch:
return try TwitchLoginHandler(clientId: params.clientId, verifier: params.verifier, urlScheme: params.urlScheme, redirectURL: params.redirectURL, typeOfLogin: params.typeOfLogin, jwtParams: params.jwtParams, customState: params.customState)
Expand All @@ -30,15 +38,30 @@ internal class HandlerFactory {
case .weibo: break
case .line: break
case .email_password: break
case .passwordless: return try PasswordlessLoginHandler(clientId: params.clientId, verifier: params.verifier, urlScheme: params.urlScheme, redirectURL: params.redirectURL, typeOfLogin: params.typeOfLogin, jwtParams: params.jwtParams, customState: params.customState)
case .passwordless:
if domain == nil || hint == nil {
throw CASDKError.invalidAuth0Options
}
return try PasswordlessLoginHandler(clientId: params.clientId, verifier: params.verifier, urlScheme: params.urlScheme, redirectURL: params.redirectURL, typeOfLogin: params.typeOfLogin, jwtParams: params.jwtParams, customState: params.customState)
case .email_passwordless:
if domain == nil || hint == nil {
throw CASDKError.invalidAuth0Options
}
throw CASDKError.invalidAuth0Options
// TODO: implement web3authpasswordlesshandler for this
case .sms_passwordless:
if hint == nil {
throw CASDKError.invalidAuth0Options
}
throw CASDKError.invalidAuth0Options
// TODO: implement web3authpasswordlesshandler for this
case .jwt: break
}


if params.jwtParams?.id_token != nil || params.jwtParams?.access_token != nil {
if idToken != nil || accessToken != nil {
return try MockLoginHandler(clientId: params.clientId, verifier: params.verifier, urlScheme: params.urlScheme, redirectURL: params.redirectURL, typeOfLogin: params.typeOfLogin, jwtParams: params.jwtParams, customState: params.customState)
}

let domain = params.jwtParams?.domain

if domain == nil {
throw CASDKError.invalidAuth0Options
}
Expand Down
16 changes: 13 additions & 3 deletions Sources/CustomAuth/Helpers/Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ internal func loginToConnection(loginType: LoginType) -> String {
case .line: break
case .email_password: return "Username-Password-Authentication"
case .passwordless: return "email"
case .email_passwordless: return "email"
case .sms_passwordless: return "sms"
case .jwt: break
}
return loginType.rawValue
}

internal func caseSensitiveField(field: String, isCaseSensitive: Bool) -> String{
return isCaseSensitive ? field : field.lowercased()
}

internal func getVerifierId(
userInfo: Auth0UserInfo,
typeOfLogin: LoginType,
Expand All @@ -32,10 +39,14 @@ internal func getVerifierId(
let json = try JSONSerialization.jsonObject(with: encoded, options: []) as! [String: String]

if verifierIdField != nil {
return json[isVerifierIdCaseSensitive ? verifierIdField!.lowercased() : verifierIdField!]!
return json[caseSensitiveField(field: verifierIdField!, isCaseSensitive: isVerifierIdCaseSensitive)]!
}

switch typeOfLogin {
case .passwordless: return name
case .email_password: return name
case .email_passwordless: return name
case .sms_passwordless: return caseSensitiveField(field: name, isCaseSensitive: isVerifierIdCaseSensitive)
case .google: return sub
case .facebook: return sub
case .reddit: return sub
Expand All @@ -47,8 +58,7 @@ internal func getVerifierId(
case .twitter: return sub
case .weibo: return sub
case .line: return sub
case .email_password: return name
case .passwordless: return name
case .jwt: return caseSensitiveField(field: sub, isCaseSensitive: isVerifierIdCaseSensitive)
}
}

Expand Down

0 comments on commit caf4c58

Please sign in to comment.