Skip to content

Commit

Permalink
review comment: add TorusUtilsExtraParams class
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed Jul 22, 2024
1 parent a3e8ae1 commit d1b9ae0
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 14 deletions.
68 changes: 62 additions & 6 deletions Sources/TorusUtils/Helpers/NodeUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal class NodeUtils {
idToken: String,
importedShares: [ImportedShare]?,
apiKey: String = "torus-default",
extraParams: [String: Codable] = [:]
extraParams: TorusUtilsExtraParams
) async throws -> TorusKey {
let threshold = Int(trunc(Double((endpoints.count / 2) + 1)))

Expand Down Expand Up @@ -218,7 +218,7 @@ internal class NodeUtils {

var thresholdNonceData: GetOrSetNonceResult?

let sessionExpiry: Int? = extraParams["session_token_exp_second"] as? Int
let sessionExpiry = extraParams.session_token_exp_second

var shareImportSuccess = false

Expand Down Expand Up @@ -246,11 +246,39 @@ internal class NodeUtils {
key_type: importShare.key_type,
nonce_data: importShare.nonce_data,
nonce_signature: importShare.nonce_signature,
// extra_params: extraData
sub_verifier_ids: verifierParams.sub_verifier_ids,
session_token_exp_second: sessionExpiry,
verify_params: verifierParams.verify_params,
sss_endpoint: endpoints[j]
sss_endpoint: endpoints[j],

enable_verifier_id_hash: extraParams.enable_verifier_id_hash,
app_s: extraParams.app_s,
app_id: extraParams.app_id,
domain: extraParams.domain,
nonce: extraParams.nonce,
message: extraParams.message,
signature: extraParams.signature,
clientDataJson: extraParams.clientDataJson,
authenticatorData: extraParams.authenticatorData,
publicKey: extraParams.publicKey,
challenge: extraParams.challenge,
rpOrigin: extraParams.rpOrigin,
rpId: extraParams.rpId,
jwk_endpoint: extraParams.jwk_endpoint,
default_node_set: extraParams.default_node_set,
jwt_verifier_id_field: extraParams.jwt_verifier_id_field,
jwt_verifier_id_case_sensitive: extraParams.jwt_verifier_id_case_sensitive,
jwk_keys: extraParams.jwk_keys,
jwt_validation_fields: extraParams.jwt_validation_fields,
jwt_validation_values: extraParams.jwt_validation_values,
index: extraParams.index,
email: extraParams.email,
id: extraParams.id,
correct_id_token: extraParams.correct_id_token,
verify_param: extraParams.verify_param,
threshold: extraParams.threshold,
pub_k_x: extraParams.pub_k_x,
pub_k_y: extraParams.pub_k_y
)

importedItems.append(shareRequestItem)
Expand Down Expand Up @@ -295,10 +323,38 @@ internal class NodeUtils {
extended_verifier_id: verifierParams.extended_verifier_id,
idtoken: idToken,
nodesignatures: nodeSigs,
// extra_params: extraData
sub_verifier_ids: verifierParams.sub_verifier_ids,
session_token_exp_second: sessionExpiry,
verify_params: verifierParams.verify_params
verify_params: verifierParams.verify_params,

enable_verifier_id_hash: extraParams.enable_verifier_id_hash,
app_s: extraParams.app_s,
app_id: extraParams.app_id,
domain: extraParams.domain,
nonce: extraParams.nonce,
message: extraParams.message,
signature: extraParams.signature,
clientDataJson: extraParams.clientDataJson,
authenticatorData: extraParams.authenticatorData,
publicKey: extraParams.publicKey,
challenge: extraParams.challenge,
rpOrigin: extraParams.rpOrigin,
rpId: extraParams.rpId,
jwk_endpoint: extraParams.jwk_endpoint,
default_node_set: extraParams.default_node_set,
jwt_verifier_id_field: extraParams.jwt_verifier_id_field,
jwt_verifier_id_case_sensitive: extraParams.jwt_verifier_id_case_sensitive,
jwk_keys: extraParams.jwk_keys,
jwt_validation_fields: extraParams.jwt_validation_fields,
jwt_validation_values: extraParams.jwt_validation_values,
index: extraParams.index,
email: extraParams.email,
id: extraParams.id,
correct_id_token: extraParams.correct_id_token,
verify_param: extraParams.verify_param,
threshold: extraParams.threshold,
pub_k_x: extraParams.pub_k_x,
pub_k_y: extraParams.pub_k_y
)

let params = ShareRequestParams(encrypted: "yes", item: [shareRequestItem], client_time: String(Int(trunc(Double((serverTimeOffset ?? 0) + Int(Date().timeIntervalSince1970))))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,41 @@ internal struct ShareRequestParams: Codable {
public var key_type: TorusKeyType?
public var nonce_data: String?
public var nonce_signature: String?
// [key: string]; This should be strongly typed
public var sub_verifier_ids: [String]?
public var session_token_exp_second: Int?
public var verify_params: [VerifyParams?]?
public var sss_endpoint: String?

// TODO: This is a bit of a mess from here due to legacy reasons and should be cleaned up in future.
// Note: Nil values by default are excluded from serialization
public var enable_verifier_id_hash: Bool? // most
public var app_s: String? // meta
public var app_id: String? // meta
public var domain: String? // farcaster
public var nonce: String? // farcaster
public var message: String? // farcaster
public var signature: String? // farcaster, passkey, webauthn
public var clientDataJson: String? // passkey, webauthn
public var authenticatorData: String? // passkey, webauthn
public var publicKey: String? // passkey, webauthn
public var challenge: String? // passkey, webauthn
public var rpOrigin: String? // passkey, webauthn
public var rpId: String? // passkey, webauthn
public var jwk_endpoint: String? // passkey, jwt
public var default_node_set: [String]? // passkey, jwt
public var jwt_verifier_id_field: String? // passkey, jwt
public var jwt_verifier_id_case_sensitive: Bool? // passkey, jwt
public var jwk_keys: String? // passkey, jwt
public var jwt_validation_fields: [String]? // passkey, jwt
public var jwt_validation_values: [String]? // passkey, jwt
public var index: Int? // demo
public var email: String? // demo
public var id: String? // test, jwt, passkey
public var correct_id_token: String? // test
public var verify_param: String? // OrAggregate
public var threshold: Int? // SingleID
public var pub_k_x: String? // Signature
public var pub_k_y: String? // Signature
}

public var encrypted: String = "yes"
Expand Down
20 changes: 13 additions & 7 deletions Sources/TorusUtils/TorusUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ public class TorusUtils {
endpoints: [String],
verifier: String,
verifierParams: VerifierParams,
idToken: String
idToken: String,
extraParams: TorusUtilsExtraParams = TorusUtilsExtraParams()
) async throws -> TorusKey {
// This has to be done here as retrieveOrImport share does not have a reference to self
var params: [String: Codable] = [:]
params.updateValue(sessionTime, forKey: "session_token_exp_second")
if extraParams.session_token_exp_second == nil {
extraParams.session_token_exp_second = sessionTime
}

return try await NodeUtils.retrieveOrImportShare(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset, enableOneKey: enableOneKey, allowHost: allowHost, network: network, clientId: clientId, endpoints: endpoints, verifier: verifier, verifierParams: verifierParams, idToken: idToken, importedShares: [], apiKey: apiKey, extraParams: params)
return try await NodeUtils.retrieveOrImportShare(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset, enableOneKey: enableOneKey, allowHost: allowHost, network: network, clientId: clientId, endpoints: endpoints, verifier: verifier, verifierParams: verifierParams, idToken: idToken, importedShares: [], apiKey: apiKey, extraParams: extraParams)
}

/// Retrieves user information, defaulting the user type to .v2
Expand Down Expand Up @@ -170,7 +171,8 @@ public class TorusUtils {
verifier: String,
verifierParams: VerifierParams,
idToken: String,
newPrivateKey: String
newPrivateKey: String,
extraParams: TorusUtilsExtraParams = TorusUtilsExtraParams()
) async throws -> TorusKey {
let nodePubs = TorusNodePubModelToINodePub(nodes: nodePubKeys)
if endpoints.count != nodeIndexes.count {
Expand All @@ -179,7 +181,11 @@ public class TorusUtils {

let sharesData = try KeyUtils.generateShares(serverTimeOffset: serverTimeOffset ?? 0, nodeIndexes: nodeIndexes, nodePubKeys: nodePubs, privateKey: newPrivateKey)

return try await NodeUtils.retrieveOrImportShare(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset ?? 0, enableOneKey: enableOneKey, allowHost: allowHost, network: network, clientId: clientId, endpoints: endpoints, verifier: verifier, verifierParams: verifierParams, idToken: idToken, importedShares: sharesData)
if extraParams.session_token_exp_second == nil {
extraParams.session_token_exp_second = sessionTime
}

return try await NodeUtils.retrieveOrImportShare(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset ?? 0, enableOneKey: enableOneKey, allowHost: allowHost, network: network, clientId: clientId, endpoints: endpoints, verifier: verifier, verifierParams: verifierParams, idToken: idToken, importedShares: sharesData, extraParams: extraParams)
}

/// Retrieves user information
Expand Down
69 changes: 69 additions & 0 deletions Sources/TorusUtils/TorusUtilsExtraParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Foundation

// TODO: This class is a bit of a mess for legacy reasons and should be cleaned up in future.

public class TorusUtilsExtraParams: Codable {
var enable_verifier_id_hash: Bool? // most
var app_s: String? // meta
var app_id: String? // meta
var domain: String? // farcaster
var nonce: String? // farcaster
var message: String? // farcaster
var signature: String? // farcaster, passkey, webauthn
var clientDataJson: String? // passkey, webauthn
var authenticatorData: String? // passkey, webauthn
var publicKey: String? // passkey, webauthn
var challenge: String? // passkey, webauthn
var rpOrigin: String? // passkey, webauthn
var rpId: String? // passkey, webauthn
var jwk_endpoint: String? // passkey, jwt
var default_node_set: [String]? // passkey, jwt
var jwt_verifier_id_field: String? // passkey, jwt
var jwt_verifier_id_case_sensitive: Bool? // passkey, jwt
var jwk_keys: String? // passkey, jwt
var jwt_validation_fields: [String]? // passkey, jwt
var jwt_validation_values: [String]? // passkey, jwt
var index: Int? // demo
var email: String? // demo
var id: String? // test, jwt, passkey
var correct_id_token: String? // test
var verify_param: String? // OrAggregate
var session_token_exp_second: Int?
var threshold: Int? // SingleID
var pub_k_x: String? // Signature
var pub_k_y: String? // Signature

public init() {}

public init(enable_verifier_id_hash: Bool? = nil, app_s: String? = nil, app_id: String? = nil, domain: String? = nil, nonce: String? = nil, message: String? = nil, signature: String? = nil, clientDataJson: String? = nil, authenticatorData: String? = nil, publicKey: String? = nil, challenge: String? = nil, rpOrigin: String? = nil, rpId: String? = nil, jwk_endpoint: String? = nil, default_node_set: [String]? = nil, jwt_verifier_id_field: String? = nil, jwt_verifier_id_case_sensitive: Bool? = nil, jwk_keys: String? = nil, jwt_validation_fields: [String]? = nil, jwt_validation_values: [String]? = nil, index: Int? = nil, email: String? = nil, id: String? = nil, correct_id_token: String? = nil, verify_param: String? = nil, session_token_exp_second: Int? = nil, threshold: Int? = nil, pub_k_x: String? = nil, pub_k_y: String? = nil) {
self.enable_verifier_id_hash = enable_verifier_id_hash
self.app_s = app_s
self.app_id = app_id
self.domain = domain
self.nonce = nonce
self.message = message
self.signature = signature
self.clientDataJson = clientDataJson
self.authenticatorData = authenticatorData
self.publicKey = publicKey
self.challenge = challenge
self.rpOrigin = rpOrigin
self.rpId = rpId
self.jwk_endpoint = jwk_endpoint
self.default_node_set = default_node_set
self.jwt_verifier_id_field = jwt_verifier_id_field
self.jwt_verifier_id_case_sensitive = jwt_verifier_id_case_sensitive
self.jwk_keys = jwk_keys
self.jwt_validation_fields = jwt_validation_fields
self.jwt_validation_values = jwt_validation_values
self.index = index
self.email = email
self.id = id
self.correct_id_token = correct_id_token
self.verify_param = verify_param
self.session_token_exp_second = session_token_exp_second
self.threshold = threshold
self.pub_k_x = pub_k_x
self.pub_k_y = pub_k_y
}
}

0 comments on commit d1b9ae0

Please sign in to comment.