Skip to content

Commit

Permalink
catch up to v20 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed Sep 30, 2024
1 parent 576139c commit 06ad56d
Show file tree
Hide file tree
Showing 21 changed files with 246 additions and 204 deletions.
2 changes: 1 addition & 1 deletion CustomAuth.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ Pod::Spec.new do |spec|
spec.module_name = "CustomAuth"
spec.source = { :git => "https://github.com/torusresearch/customauth-swift-sdk.git", :tag => spec.version }
spec.source_files = "Sources/CustomAuth/*.{swift}","Sources/CustomAuth/**/*.{swift}"
spec.dependency 'Torus-utils', '~> 9.0.0'
spec.dependency 'Torus-utils', '~> 9.0.1'
spec.dependency 'JWTDecode', '~> 3.1.0'
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"originHash" : "18b632e2194394e0c1a608ed933754bf428e89a5fb32ba332befd79edc99e5f5",
"pins" : [
{
"identity" : "bigint",
Expand All @@ -20,21 +19,21 @@
}
},
{
"identity" : "customauth-swift-sdk",
"identity" : "fetch-node-details-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/torusresearch/customauth-swift-sdk/",
"location" : "https://github.com/torusresearch/fetch-node-details-swift",
"state" : {
"branch" : "features_update",
"revision" : "045d57c57c1bfcc5fa951dd510e6f840c5af8d88"
"revision" : "4bd96c33ba8d02d9e27190c5c7cedf09cfdfd656",
"version" : "6.0.3"
}
},
{
"identity" : "fetch-node-details-swift",
"identity" : "jwt-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/torusresearch/fetch-node-details-swift",
"location" : "https://github.com/vapor/jwt-kit.git",
"state" : {
"revision" : "4bd96c33ba8d02d9e27190c5c7cedf09cfdfd656",
"version" : "6.0.3"
"revision" : "c2595b9ad7f512d7f334830b4df1fed6e917946a",
"version" : "4.13.4"
}
},
{
Expand All @@ -46,15 +45,24 @@
"version" : "3.1.0"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "81bee98e706aee68d39ed5996db069ef2b313d62",
"version" : "3.7.1"
}
},
{
"identity" : "torus-utils-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/torusresearch/torus-utils-swift.git",
"state" : {
"revision" : "7788752bed699b3d34e78c2c51174f814d02610b",
"version" : "9.0.0"
"revision" : "eda55b8537a600e657d19d4c452c0a36f217883c",
"version" : "9.0.1"
}
}
],
"version" : 3
"version" : 2
}
52 changes: 52 additions & 0 deletions CustomAuthDemo/CustomAuthDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import CustomAuth
import SwiftUI

struct ContentView: View {
@State private var inputDetail: String = ""

var body: some View {
VStack {
Button(action: {
Expand Down Expand Up @@ -133,6 +135,56 @@ struct ContentView: View {
}, label: {
Text("Email Password")
})

Label(
title: { Text("Hosted Passwordless") },
icon: { Image(systemName: "circle") }
)
TextField(
"Email or Phone Number",
text: $inputDetail
)
.disableAutocorrection(true)
.border(.secondary)

Button(action: {
Task {
do {
let sub = SingleLoginParams(typeOfLogin: .email_passwordless,
verifier: "torus-auth0-email-passwordless-lrc", clientId: "P7PJuBCXIHP41lcyty0NEb7Lgf7Zme8Q",
jwtParams: Auth0ClientOptions(verifierIdField: "name", login_hint: inputDetail, flow_type: .link))
let customAuthArgs = CustomAuthArgs(urlScheme: "tdsdk://tdsdk/oauthCallback", network: .legacy(.TESTNET), enableOneKey: true, web3AuthClientId: "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ")
let customAuth = try CustomAuth(config: customAuthArgs)
let torusLoginResponse = try await customAuth.triggerLogin(args: sub)
let encoded = try JSONEncoder().encode(torusLoginResponse)
print(String(data: encoded, encoding: .utf8)!)
} catch {
print(error)
}
}
}, label: {
Text("Email Passwordless")
})


Button(action: {
Task {
do {
let sub = SingleLoginParams(typeOfLogin: .sms_passwordless,
verifier: "torus-sms-passwordless-lrc", clientId: "P7PJuBCXIHP41lcyty0NEb7Lgf7Zme8Q",
jwtParams: Auth0ClientOptions(login_hint: inputDetail, flow_type: .code))
let customAuthArgs = CustomAuthArgs(urlScheme: "tdsdk://tdsdk/oauthCallback", network: .legacy(.TESTNET), enableOneKey: true, web3AuthClientId: "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ")
let customAuth = try CustomAuth(config: customAuthArgs)
let torusLoginResponse = try await customAuth.triggerLogin(args: sub)
let encoded = try JSONEncoder().encode(torusLoginResponse)
print(String(data: encoded, encoding: .utf8)!)
} catch {
print(error)
}
}
}, label: {
Text("SMS Passwordless")
})

Label(
title: { Text("Aggregate Verifiers") },
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/torusresearch/torus-utils-swift.git",
"state" : {
"revision" : "303dc2cf41db7c10f769855edad0e717ced2d554",
"version" : "9.0.0"
"revision" : "eda55b8537a600e657d19d4c452c0a36f217883c",
"version" : "9.0.1"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
targets: ["CustomAuth"])
],
dependencies: [
.package(url: "https://github.com/torusresearch/torus-utils-swift.git", from: "9.0.0"),
.package(url: "https://github.com/torusresearch/torus-utils-swift.git", from: "9.0.1"),
.package(url: "https://github.com/auth0/JWTDecode.swift.git", from: "3.1.0"),
// NB: jwt-kit may only be a test dependency or it will break cocoapods support
.package(url: "https://github.com/vapor/jwt-kit.git", from: "4.13.0"),
Expand Down
10 changes: 7 additions & 3 deletions Sources/CustomAuth/Common/LoginParams/Auth0ClientOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class Auth0ClientOptions: BaseLoginOptions {
public let id_token: String?
public let access_token: String?
public let user_info_route: String?
public let login_hint: String?
public var login_hint: String?
public let flow_type: EmailFlowType?

private enum CodingKeys: CodingKey {
case domain,
Expand All @@ -22,10 +23,11 @@ public class Auth0ClientOptions: BaseLoginOptions {
id_token,
access_token,
user_info_route,
login_hint
login_hint,
flow_type
}

public init(display: String? = nil, prompt: String? = nil, max_age: Int? = nil, ui_locales: String? = nil, id_token_hint: String? = nil, arc_values: String? = nil, scope: String? = nil, audience: String? = nil, connection: String? = nil, domain: String? = nil, client_id: String? = nil, redirect_url: String? = nil, leeway: Int? = nil, verifierIdField: String? = nil, isVerifierIdCaseSensitive: Bool = false, id_token: String? = nil, access_token: String? = nil, user_info_route: String? = nil, login_hint: String? = nil) {
public init(display: String? = nil, prompt: String? = nil, max_age: Int? = nil, ui_locales: String? = nil, id_token_hint: String? = nil, arc_values: String? = nil, scope: String? = nil, audience: String? = nil, connection: String? = nil, domain: String? = nil, client_id: String? = nil, redirect_url: String? = nil, leeway: Int? = nil, verifierIdField: String? = nil, isVerifierIdCaseSensitive: Bool = false, id_token: String? = nil, access_token: String? = nil, user_info_route: String? = nil, login_hint: String? = nil, flow_type: EmailFlowType? = nil) {

// This is for convenience in case a full url gets passed in
// https:// is added back in later when it is constructed from
Expand All @@ -45,6 +47,7 @@ public class Auth0ClientOptions: BaseLoginOptions {
self.user_info_route = user_info_route
self.client_id = client_id
self.login_hint = login_hint
self.flow_type = flow_type

super.init(display: display, prompt: prompt, max_age: max_age, ui_locales: ui_locales, id_token_hint: id_token_hint, arc_values: arc_values, scope: scope, audience: audience, connection: connection)
}
Expand All @@ -66,6 +69,7 @@ public class Auth0ClientOptions: BaseLoginOptions {
try container.encodeIfPresent(self.id_token, forKey: .id_token)
try container.encodeIfPresent(self.client_id, forKey: .client_id)
try container.encodeIfPresent(self.login_hint, forKey: .login_hint)
try container.encodeIfPresent(self.flow_type, forKey: .flow_type)
}
}

Expand Down
6 changes: 6 additions & 0 deletions Sources/CustomAuth/Common/LoginParams/EmailFlowType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation

public enum EmailFlowType: String, Equatable, Hashable, Codable {
case link
case code
}
5 changes: 4 additions & 1 deletion Sources/CustomAuth/Common/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ internal class State: Codable {
public var typeOfLogin: String
public var redirectUri: String
public var redirectToAndroid: String = "true"
public var client: String?

public init(instanceId: String, verifier: String, typeOfLogin: String, redirectUri: String, customState: TorusGenericContainer? = nil) {
public init(instanceId: String, verifier: String, typeOfLogin: String, redirectUri: String, customState: TorusGenericContainer? = nil, client: String?) {
self.customState = customState
self.instanceId = instanceId
self.verifier = verifier
self.typeOfLogin = typeOfLogin
self.redirectUri = redirectUri
self.client = client
}

public func encode(to encoder: any Encoder) throws {
Expand All @@ -24,5 +26,6 @@ internal class State: Codable {
try container.encode(typeOfLogin, forKey: .typeOfLogin)
try container.encode(redirectToAndroid, forKey: .redirectToAndroid)
try container.encode(redirectUri, forKey: .redirectUri)
try container.encodeIfPresent(client, forKey: .client)
}
}
4 changes: 2 additions & 2 deletions Sources/CustomAuth/CustomAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class CustomAuth {
///
/// - Throws: `CASDKError`, `TorusUtilError`
public func triggerLogin(args: SingleLoginParams) async throws -> TorusLoginResponse {
let loginHandler = try HandlerFactory.createHandler(params: CreateHandlerParams(typeOfLogin: args.typeOfLogin, verifier: args.verifier, clientId: args.clientId, urlScheme: config.urlScheme, redirectURL: args.redirectURL, jwtParams: args.jwtParams, customState: args.customState))
let loginHandler = try HandlerFactory.createHandler(params: CreateHandlerParams(typeOfLogin: args.typeOfLogin, verifier: args.verifier, clientId: args.clientId, urlScheme: config.urlScheme, redirectURL: args.redirectURL, jwtParams: args.jwtParams, customState: args.customState, web3AuthNetwork: self.config.network, web3AuthClientId: self.config.web3AuthClientId))

var loginParams: LoginWindowResponse
if args.hash != nil && args.queryParams != nil {
Expand Down Expand Up @@ -98,7 +98,7 @@ public class CustomAuth {
var loginParamsArray: [LoginWindowResponse] = []
var userInfoArray: [UserInfo] = []
for subverifierDetail in args.subVerifierDetailsArray {
let loginHandler = try HandlerFactory.createHandler(params: CreateHandlerParams(typeOfLogin: subverifierDetail.typeOfLogin, verifier: subverifierDetail.verifier, clientId: subverifierDetail.clientId, urlScheme: config.urlScheme, redirectURL: subverifierDetail.redirectURL, jwtParams: subverifierDetail.jwtParams, customState: subverifierDetail.customState))
let loginHandler = try HandlerFactory.createHandler(params: CreateHandlerParams(typeOfLogin: subverifierDetail.typeOfLogin, verifier: subverifierDetail.verifier, clientId: subverifierDetail.clientId, urlScheme: config.urlScheme, redirectURL: subverifierDetail.redirectURL, jwtParams: subverifierDetail.jwtParams, customState: subverifierDetail.customState, web3AuthNetwork: self.config.network, web3AuthClientId: self.config.web3AuthClientId))
var loginParams: LoginWindowResponse
var userInfo: UserInfo
if subverifierDetail.hash != nil && subverifierDetail.queryParams != nil {
Expand Down
14 changes: 7 additions & 7 deletions Sources/CustomAuth/Handlers/DiscordLoginHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ internal class DiscordLoginHandler: AbstractLoginHandler {
private var scope: String = "identify email"
private var prompt: String = "none"

override public init(clientId: String, verifier: String, urlScheme: String, redirectURL: String, typeOfLogin: LoginType, jwtParams: Auth0ClientOptions? = nil, customState: TorusGenericContainer? = nil) throws {
try super.init(clientId: clientId, verifier: verifier, urlScheme: urlScheme, redirectURL: redirectURL, typeOfLogin: typeOfLogin, jwtParams: jwtParams, customState: customState)
override public init(params: CreateHandlerParams) throws {
try super.init(params: params)
try setFinalUrl()
}

Expand All @@ -23,15 +23,15 @@ internal class DiscordLoginHandler: AbstractLoginHandler {

var params: [String: String] = [:]

if jwtParams != nil {
params = try (JSONSerialization.jsonObject(with: try JSONEncoder().encode(jwtParams), options: []) as! [String: String])
if self.params.jwtParams != nil {
params = try (JSONSerialization.jsonObject(with: try JSONEncoder().encode(self.params.jwtParams), options: []) as! [String: String])
}

params.merge([
"state": try state(),
"response_type": response_type,
"client_id": clientId,
"redirect_uri": redirectURL,
"client_id": self.params.clientId,
"redirect_uri": self.params.redirectURL,
"prompt": prompt,
"scope": scope], uniquingKeysWith: { _, new in new })
urlComponents.scheme = "https"
Expand All @@ -56,6 +56,6 @@ internal class DiscordLoginHandler: AbstractLoginHandler {
let profileImage = result.avatar == nil ? "https://cdn.discordapp.com/embed/avatars/" + String(Int(result.discriminator)! % 5) + ".png" :
"https://cdn.discordapp.com/avatars/${id}/" + result.avatar! + ".png?size=2048"

return TorusVerifierResponse(email: result.email, name: result.username + "#" + result.discriminator, profileImage: profileImage, verifier: verifier, verifierId: result.id, typeOfLogin: typeOfLogin)
return TorusVerifierResponse(email: result.email, name: result.username + "#" + result.discriminator, profileImage: profileImage, verifier: self.params.verifier, verifierId: result.id, typeOfLogin: self.params.typeOfLogin)
}
}
14 changes: 7 additions & 7 deletions Sources/CustomAuth/Handlers/FacebookLoginHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ internal class FacebookLoginHandler: AbstractLoginHandler {
private var response_type: String = "token"
private var scope: String = "public_profile email"

override public init(clientId: String, verifier: String, urlScheme: String, redirectURL: String, typeOfLogin: LoginType, jwtParams: Auth0ClientOptions? = nil, customState: TorusGenericContainer? = nil) throws {
try super.init(clientId: clientId, verifier: verifier, urlScheme: urlScheme, redirectURL: redirectURL, typeOfLogin: typeOfLogin, jwtParams: jwtParams, customState: customState)
override public init(params: CreateHandlerParams) throws {
try super.init(params: params)
try setFinalUrl()
}

Expand All @@ -29,15 +29,15 @@ internal class FacebookLoginHandler: AbstractLoginHandler {

var params: [String: String] = [:]

if jwtParams != nil {
params = try (JSONSerialization.jsonObject(with: try JSONEncoder().encode(jwtParams), options: []) as! [String: String])
if self.params.jwtParams != nil {
params = try (JSONSerialization.jsonObject(with: try JSONEncoder().encode(self.params.jwtParams), options: []) as! [String: String])
}

params.merge([
"state": try state(),
"response_type": response_type,
"client_id": clientId,
"redirect_uri": redirectURL,
"client_id": self.params.clientId,
"redirect_uri": self.params.redirectURL,
"scope": scope], uniquingKeysWith: { _, new in new })
urlComponents.scheme = "https"
urlComponents.host = "www.facebook.com"
Expand All @@ -59,6 +59,6 @@ internal class FacebookLoginHandler: AbstractLoginHandler {

let result = try JSONDecoder().decode(FacebookInfo.self, from: data)

return TorusVerifierResponse(email: result.email, name: result.name, profileImage: result.picture.data.url, verifier: verifier, verifierId: result.id, typeOfLogin: typeOfLogin)
return TorusVerifierResponse(email: result.email, name: result.name, profileImage: result.picture.data.url, verifier: self.params.verifier, verifierId: result.id, typeOfLogin: self.params.typeOfLogin)
}
}
14 changes: 7 additions & 7 deletions Sources/CustomAuth/Handlers/GoogleLoginHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ internal class GoogleLoginHandler: AbstractLoginHandler {
private var scope: String = "profile email openid"
private var prompt: String = "select_account"

override public init(clientId: String, verifier: String, urlScheme: String, redirectURL: String, typeOfLogin: LoginType, jwtParams: Auth0ClientOptions? = nil, customState: TorusGenericContainer? = nil) throws {
try super.init(clientId: clientId, verifier: verifier, urlScheme: urlScheme, redirectURL: redirectURL, typeOfLogin: typeOfLogin, jwtParams: jwtParams, customState: customState)
override public init(params: CreateHandlerParams) throws {
try super.init(params: params)
try setFinalUrl()
}

Expand All @@ -21,16 +21,16 @@ internal class GoogleLoginHandler: AbstractLoginHandler {

var params: [String: String] = [:]

if jwtParams != nil {
params = try (JSONSerialization.jsonObject(with: try JSONEncoder().encode(jwtParams), options: []) as! [String: String])
if self.params.jwtParams != nil {
params = try (JSONSerialization.jsonObject(with: try JSONEncoder().encode(self.params.jwtParams), options: []) as! [String: String])
}

params.merge([
"state": try state(),
"response_type": response_type,
"client_id": clientId,
"client_id": self.params.clientId,
"prompt": prompt,
"redirect_uri": redirectURL,
"redirect_uri": self.params.redirectURL,
"scope": scope,
"nonce": nonce,
], uniquingKeysWith: { _, new in new })
Expand All @@ -54,6 +54,6 @@ internal class GoogleLoginHandler: AbstractLoginHandler {

let result = try JSONDecoder().decode(GoogleInfo.self, from: data)

return TorusVerifierResponse(email: result.email, name: result.name, profileImage: result.picture, verifier: verifier, verifierId: result.email.lowercased(), typeOfLogin: typeOfLogin)
return TorusVerifierResponse(email: result.email, name: result.name, profileImage: result.picture, verifier: self.params.verifier, verifierId: result.email.lowercased(), typeOfLogin: self.params.typeOfLogin)
}
}
Loading

0 comments on commit 06ad56d

Please sign in to comment.