Skip to content

Commit

Permalink
update CI and xcformat files
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed Jun 12, 2024
1 parent 83eede4 commit 55bf04c
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ jobs:
platform:
- iOS
steps:
- uses: actions/checkout@v2.3.4
- uses: actions/checkout@v4
- name: Remove Development.xcworkspace to avoid tooling confusion
run: rm -rf Development.xcworkspace
- uses: mxcl/xcodebuild@v1
- uses: mxcl/xcodebuild@v3.0.0
with:
platform: ${{ matrix.platform }}
action: test
Expand Down
1 change: 0 additions & 1 deletion Sources/CustomAuth/Common/HashParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ public class HashParams: Codable {
self.id_token = id_token
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Auth0ClientOptions: BaseLoginOptions {

super.init(customParams: customParams, 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)
}

required init(from decoder: any Decoder) throws {
fatalError("init(from:) has not been implemented")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
public class HybridAggregateLoginParams: Codable {
public let singleLogin: SingleLoginParams
public let aggregateLoginParams: AggregateLoginParams

public init(singleLogin: SingleLoginParams, aggregateLoginParams: AggregateLoginParams) {
self.singleLogin = singleLogin
self.aggregateLoginParams = aggregateLoginParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ public class TorusLoginResponse: Codable {
self.torusKey = torusKey
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public class State: Codable {
self.typeOfLogin = typeOfLogin
self.redirectUri = redirectUri
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(self.customState, forKey: .customState)
try container.encode(self.instanceId, forKey: .instanceId)
try container.encode(self.verifier, forKey: .verifier)
try container.encode(self.typeOfLogin, forKey: .typeOfLogin)
try container.encode(self.redirectToAndroid, forKey: .redirectToAndroid)
try container.encode(self.redirectUri, forKey: .redirectUri)
try container.encodeIfPresent(customState, forKey: .customState)
try container.encode(instanceId, forKey: .instanceId)
try container.encode(verifier, forKey: .verifier)
try container.encode(typeOfLogin, forKey: .typeOfLogin)
try container.encode(redirectToAndroid, forKey: .redirectToAndroid)
try container.encode(redirectUri, forKey: .redirectUri)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ public class TorusAggregateVerifierResponse: Codable {
self.loginResponse = loginResponse
}
}

45 changes: 19 additions & 26 deletions Sources/CustomAuth/CustomAuth.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import FetchNodeDetails
import Foundation
import OSLog
import FetchNodeDetails
import TorusUtils
#if canImport(curveSecp256k1)
import curveSecp256k1
Expand Down Expand Up @@ -57,24 +57,22 @@ public class CustomAuth {

return TorusLoginResponse(singleVerifierResponse: TorusSingleVerifierResponse(userInfo: returnedInfo, loginResponse: loginParams), torusKey: torusKey)
}

public func triggerAggregateLogin(args: AggregateLoginParams) async throws -> TorusAggregateLoginResponse? {

if args.subVerifierDetailsArray.isEmpty {
throw CASDKError.unknownError
}
if args.subVerifierDetailsArray.count == 1 && args.aggregateVerifierType == AggregateVerifierType.single_id_verifier {
throw CASDKError.unknownError
}

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))
var loginParams: LoginWindowResponse
var userInfo: UserInfo
if subverifierDetail.hash != nil && subverifierDetail.queryParams != nil {

let (error, hashParams, instanceParams) = try handleRedirectParameters(hash: subverifierDetail.hash!, queryParameters: subverifierDetail.queryParams!)
if !error.isEmpty {
throw CASDKError.unknownError
Expand All @@ -83,22 +81,22 @@ public class CustomAuth {
} else {
loginParams = try await loginHandler.handleLoginWindow(popupFeatures: config.popupFeatures)
}

let info = try await loginHandler.getUserInfo(params: loginParams, storageServerUrl: nil)
userInfo = UserInfo(email: info.email, name: info.name, profileImage: info.profileImage, aggregateVerifier: args.verifierIdentifier, verifier: info.verifier, verifierId: info.verifierId, typeOfLogin: info.typeOfLogin, ref: info.ref, extraVerifierParams: info.extraVerifierParams, accessToken: loginParams.accessToken, idToken: loginParams.idToken, extraParams: loginParams.extraParams, extraParamsPassed: loginParams.extraParamsPassed, state: loginParams.state)
loginParamsArray.append(loginParams)
userInfoArray.append(userInfo)
}

var subVerifierIds: [String] = []
var aggregateVerifierParams: [VerifyParams] = []
var aggregateIdTokenSeeds: [String] = []
var aggregateVerifierId: String = ""
for i in 0..<args.subVerifierDetailsArray.count {

for i in 0 ..< args.subVerifierDetailsArray.count {
let loginParams = loginParamsArray[i]
let userInfo = userInfoArray[i]

aggregateVerifierParams.append(VerifyParams(verifier_id: userInfo.verifierId, idtoken: loginParams.idToken!))
aggregateIdTokenSeeds.append(loginParams.idToken ?? loginParams.accessToken!)
subVerifierIds.append(userInfo.verifier)
Expand All @@ -108,31 +106,27 @@ public class CustomAuth {
let joined = aggregateIdTokenSeeds.joined(separator: "\u{29}").data(using: .utf8)!
let aggregateIdToken = try keccak256(data: joined)
let aggregateParams: VerifierParams = VerifierParams(verifier_id: aggregateVerifierId, extended_verifier_id: nil, sub_verifier_ids: subVerifierIds, verify_params: aggregateVerifierParams)

let aggregateTorusKey = try await getTorusKey(verifier: aggregateVerifierId, verifierParams: aggregateParams, idToken: String(data: aggregateIdToken, encoding: .utf8)!)

var aggregateVerifierResponses: [TorusAggregateVerifierResponse] = []
for i in 0..<userInfoArray.count {
for i in 0 ..< userInfoArray.count {
let loginParams = loginParamsArray[i]
let userInfo = userInfoArray[i]
aggregateVerifierResponses.append(TorusAggregateVerifierResponse(userInfo: userInfo, loginResponse: loginParams))

}

return TorusAggregateLoginResponse(torusAggregateVerifierResponse: aggregateVerifierResponses, torusKey: aggregateTorusKey)

}

public func triggerHybridAggregateLogin(args: HybridAggregateLoginParams) async throws -> TorusHybridAggregateLoginResponse? {

if args.aggregateLoginParams.subVerifierDetailsArray.isEmpty {
throw CASDKError.unknownError
}
if args.aggregateLoginParams.subVerifierDetailsArray.count == 1 && args.aggregateLoginParams.aggregateVerifierType == AggregateVerifierType.single_id_verifier {
throw CASDKError.unknownError
}



let loginHandler = try HandlerFactory.createHandler(params: CreateHandlerParams(typeOfLogin: args.singleLogin.typeOfLogin, verifier: args.singleLogin.verifier, clientId: args.singleLogin.clientId, urlScheme: config.urlScheme, redirectURL: args.singleLogin.redirectURL, jwtParams: args.singleLogin.jwtParams, customState: args.singleLogin.customState))

var loginParams: LoginWindowResponse
Expand All @@ -154,12 +148,11 @@ public class CustomAuth {

let returnedInfo = UserInfo(email: userInfo.email, name: userInfo.name, profileImage: userInfo.profileImage, aggregateVerifier: userInfo.aggregateVerifier, verifier: userInfo.verifier, verifierId: userInfo.verifierId, typeOfLogin: userInfo.typeOfLogin, ref: userInfo.ref, extraVerifierParams: userInfo.extraVerifierParams, accessToken: loginParams.accessToken, idToken: loginParams.idToken, extraParams: loginParams.extraParams, extraParamsPassed: loginParams.extraParamsPassed, state: loginParams.state)


var subVerifierIds: [String] = []
var aggregateVerifierParams: [VerifyParams] = []
var aggregateIdTokenSeeds: [String] = []
var aggregateVerifierId: String = ""
for i in 0..<args.aggregateLoginParams.subVerifierDetailsArray.count {
for i in 0 ..< args.aggregateLoginParams.subVerifierDetailsArray.count {
let sub = args.aggregateLoginParams.subVerifierDetailsArray[i]
aggregateVerifierParams.append(VerifyParams(verifier_id: userInfo.verifierId, idtoken: loginParams.idToken!))
aggregateIdTokenSeeds.append(loginParams.idToken ?? loginParams.accessToken!)
Expand All @@ -170,13 +163,13 @@ public class CustomAuth {
let joined = aggregateIdTokenSeeds.joined(separator: "\u{29}").data(using: .utf8)!
let aggregateIdToken = try keccak256(data: joined)
let aggregateParams: VerifierParams = VerifierParams(verifier_id: aggregateVerifierId, extended_verifier_id: nil, sub_verifier_ids: subVerifierIds, verify_params: aggregateVerifierParams)

let aggregateTorusKey = try await getTorusKey(verifier: args.aggregateLoginParams.verifierIdentifier, verifierParams: aggregateParams, idToken: String(data: aggregateIdToken, encoding: .utf8)!)

let aggregateResponse = TorusAggregateVerifierResponse(userInfo: returnedInfo, loginResponse: loginParams)

let aggregateLogin = TorusAggregateLoginResponse(torusAggregateVerifierResponse: [aggregateResponse], torusKey: torusKey)

return TorusHybridAggregateLoginResponse(singleLogin: aggregateLogin, aggregateLogins: [aggregateTorusKey])
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/CustomAuth/Extension/Data+extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

extension Data {
func toBase64URL() -> String {
var result = self.base64EncodedString()
var result = base64EncodedString()
result = result.replacingOccurrences(of: "+", with: "-")
result = result.replacingOccurrences(of: "/", with: "_")
result = result.replacingOccurrences(of: "=", with: "")
Expand Down
3 changes: 1 addition & 2 deletions Sources/CustomAuth/Handlers/AuthenticationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import AuthenticationServices
import UIKit

public class AuthenticationManager: NSObject, ASWebAuthenticationPresentationContextProviding {

public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
let window = UIApplication.shared.windows.first { $0.isKeyWindow }
return window ?? ASPresentationAnchor()
Expand All @@ -17,7 +16,7 @@ public class AuthenticationManager: NSObject, ASWebAuthenticationPresentationCon
completion(.success(url))
}
}

DispatchQueue.main.async { [weak self] in
guard let self = self else {
return
Expand Down
2 changes: 1 addition & 1 deletion Sources/CustomAuth/Handlers/DiscordLoginHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class DiscordLoginHandler: AbstractLoginHandler {
throw CASDKError.accessTokenNotProvided
}

var urlRequest = makeUrlRequest(url:"https://discord.com/api/users/@me", method: "GET")
var urlRequest = makeUrlRequest(url: "https://discord.com/api/users/@me", method: "GET")
urlRequest.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")

let (data, _) = try await URLSession.shared.data(for: urlRequest)
Expand Down
4 changes: 2 additions & 2 deletions Sources/CustomAuth/Handlers/FacebookLoginHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class FacebookLoginHandler: AbstractLoginHandler {
throw CASDKError.accessTokenNotProvided
}

var urlRequest = makeUrlRequest(url:"https://graph.facebook.com/me?fields=name,email,picture.type(large)", method: "GET")
var urlRequest = makeUrlRequest(url: "https://graph.facebook.com/me?fields=name,email,picture.type(large)", method: "GET")
urlRequest.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")

let (data, _) = try await URLSession.shared.data(for: urlRequest)

let result = try JSONDecoder().decode(FacebookInfo.self, from: data)
Expand Down
2 changes: 1 addition & 1 deletion Sources/CustomAuth/Handlers/GoogleLoginHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GoogleLoginHandler: AbstractLoginHandler {
throw CASDKError.accessTokenNotProvided
}

var urlRequest = makeUrlRequest(url:"https://www.googleapis.com/userinfo/v2/me", method: "GET")
var urlRequest = makeUrlRequest(url: "https://www.googleapis.com/userinfo/v2/me", method: "GET")
urlRequest.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")

let (data, _) = try await URLSession.shared.data(for: urlRequest)
Expand Down
8 changes: 3 additions & 5 deletions Sources/CustomAuth/Handlers/JWTLoginHandler.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation


public class Auth0UserInfo: Codable {
public let picture: String
public let email: String
Expand All @@ -15,7 +14,7 @@ class JWTLoginHandler: AbstractLoginHandler {
private var prompt: String = "Login"

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)
try super.init(clientId: clientId, verifier: verifier, urlScheme: urlScheme, redirectURL: redirectURL, typeOfLogin: typeOfLogin, jwtParams: jwtParams, customState: customState)
try setFinalUrl()
}

Expand All @@ -25,7 +24,7 @@ class JWTLoginHandler: AbstractLoginHandler {
if jwtParams == nil {
throw CASDKError.unknownError
}

var params: [String: String] = try (JSONSerialization.jsonObject(with: try JSONEncoder().encode(jwtParams), options: []) as! [String: String])
params.merge([
"state": try state(),
Expand Down Expand Up @@ -60,10 +59,9 @@ class JWTLoginHandler: AbstractLoginHandler {
urlComponents.host = domain
urlComponents.path = user_route_info ?? ""


var urlRequest = makeUrlRequest(url: urlComponents.string!, method: "GET")
urlRequest.addValue("Bearer \(accessToken!)", forHTTPHeaderField: "Authorization")

let (data, _) = try await URLSession.shared.data(for: urlRequest)

let result = try JSONDecoder().decode(Auth0UserInfo.self, from: data)
Expand Down
2 changes: 1 addition & 1 deletion Sources/CustomAuth/Handlers/MockLoginHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

class MockLoginHandler: AbstractLoginHandler {
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)
try super.init(clientId: clientId, verifier: verifier, urlScheme: urlScheme, redirectURL: redirectURL, typeOfLogin: typeOfLogin, jwtParams: jwtParams, customState: customState)
try setFinalUrl()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class AbstractLoginHandler: ILoginHandler {
public var nonce: String

public var finalUrl: URLComponents

public var urlScheme: String

public var redirectURL: String

public var verifier: String
Expand Down Expand Up @@ -53,7 +53,7 @@ public class AbstractLoginHandler: ILoginHandler {
let access_token = tokenInfo["access_token"]
let id_token = tokenInfo["id_token"]
let ref = tokenInfo["ref"]

tokenInfo.removeValue(forKey: "access_token")
tokenInfo.removeValue(forKey: "id_token")
tokenInfo.removeValue(forKey: "ref")
Expand Down
2 changes: 1 addition & 1 deletion Sources/CustomAuth/Handlers/RedditLoginHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RedditLoginHandler: AbstractLoginHandler {
throw CASDKError.accessTokenNotProvided
}

var urlRequest = makeUrlRequest(url:"https://oauth.reddit.com/api/v1/me", method: "GET")
var urlRequest = makeUrlRequest(url: "https://oauth.reddit.com/api/v1/me", method: "GET")
urlRequest.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")

let (data, _) = try await URLSession.shared.data(for: urlRequest)
Expand Down
6 changes: 3 additions & 3 deletions Sources/CustomAuth/Handlers/TwitchLoginHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class TwitchLoginHandler: AbstractLoginHandler {
guard let accessToken = params.accessToken else {
throw CASDKError.accessTokenNotProvided
}
var urlRequest = makeUrlRequest(url:"https://api.twitch.tv/helix/users", method: "GET")

var urlRequest = makeUrlRequest(url: "https://api.twitch.tv/helix/users", method: "GET")
urlRequest.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")

let (data, _) = try await URLSession.shared.data(for: urlRequest)

let result = try JSONDecoder().decode(TwitchRootInfo.self, from: data)
Expand Down
10 changes: 10 additions & 0 deletions Tests/CustomAuthTests/CustomAuthTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation
import JWTDecode
import XCTest

final class CustomAuthTests: XCTestCase {
func test_jwtDecodeTest() {
let idToken = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3RvcnVzLmF1LmF1dGgwLmNvbS8iLCJhdWQiOiJLRzd6azg5WDNRZ3R0U3lYOU5KNGZHRXlGTmhPY0pUdyIsIm5hbWUiOiJkaHJ1dkB0b3IudXMiLCJlbWFpbCI6ImRocnV2QHRvci51cyIsInNjb3BlIjoib3BlbmlkIHByb2ZpbGUgZW1haWwiLCJpYXQiOjE2NTQ2NzcwMTQsImVhdCI6MTY1NDY3NzMxNCwiZXhwIjoxNjU0Njc3MzE0fQ.3nzDGeSiQwfTVmL4T4-e5N19eD280GjtosFzcGjhWv_sUCV2YkM3i7iFIpUq7AxoPXjai5v7GTTPRu1zHPL6bg"
let _ = try! decode(jwt: idToken)
}
}

0 comments on commit 55bf04c

Please sign in to comment.