customauth-swift-sdk
简介
此仓库允许 iOS 应用程序直接检索存储在 Torus 网络中的密钥。Torus 网络的验证层是一般的,以下是使用 Google 通过 SDK 访问密钥的示例。您可以在此处了解有关 Torus 网络交互的更多信息 这里。
功能
- 所有 API 都返回 Promise(mxcl/PromiseKit)。您可以导入 "yannickl/AwaitKit" 以将 API 转换为 async/await 格式。
🩹 示例
请查看我们的 CustomAuthDemo 目录 中 CustomAuth iOS/Swift SDK
的示例。
使用方法
1. 安装
Swift 包管理器
在项目设置中,将 Github 网址添加为 Swift 包依赖。
import PackageDescription
let package = Package(
name: "CustomAuth",
dependencies: [
.package(name: "CustomAuth", url: "https://github.com/torusresearch/customauth-swift-sdk", from: "2.4.0"))
]
)
Cocoapods
pod 'CustomAuth', '~> 5.0.0'
手动导入或其他包
如果您需要除SPM或Cocoapods之外的包管理器,请联系 [email protected] 或手动克隆仓库,将其作为框架导入到项目中。
2. 初始化
根据您所需的登录方式初始化SDK。以下示例演示了如何为单个Google登录执行此操作。《redirectURL》指的是用于登录流程回退到您的应用程序的URL,它应该由您的应用程序注册的方案,例如《com.mycompany.myapp://redirect》。《browserRedirectURL》指的是浏览器在登录流程中应使用的页面,它应该有http或https方案。
import CustomAuth
let sub = SubVerifierDetails(loginType: .installed, // default .web
loginProvider: .google,
clientId: "<your-client-id>",
verifierName: "<verifier-name>",
redirectURL: "<your-redirect-url>",
browserRedirectURL: "<your-browser-redirect-url>")
let tdsdk = CustomAuth(aggregateVerifierType: "<type-of-verifier>", aggregateVerifierName: "<verifier-name>", subVerifierDetails: [sub], network: <etherum-network-to-use>)
// controller is used to present a SFSafariViewController.
tdsdk.triggerLogin(controller: <UIViewController>?, browserType: <method-of-opening-browser>, modalPresentationStyle: <style-of-modal>).done{ data in
print("private key rebuild", data)
}.catch{ err in
print(err)
}
登录取决于验证脚本/验证器。还有其他验证器,包括《single_id_verifier》、《and_aggregate_verifier》、《or_aggregate_verifier》和《single_logins》,您可能需要使用这些验证器。要设置您的应用程序的验证器脚本,请联系 [email protected] 或阅读有关验证器的更多信息,请访问《https://docs.tor.us/customauth/supported-authenticators-verifiers》。
3. 处理OAuth/身份验证URL重定向
您可以通过两种方式设置重定向:URL方案或者通用链接。通常我们建议用户使用URL方案,因为通用链接需要额外的用户交互。《handle(url: URL)》类方法实现了一个NSNotification来处理URL回调。
设置URL方案
在目标的信息标签中,添加您的应用程序名称(例如:my-wallet-app)。在OAuth提供商设置页面的允许重定向URL列表中添加重定向URL。
- 对于SwiftUI,不使用代理(iOS 14+)
.onOpenURL { url in
guard let url = URLContexts.first?.url else {
return
}
CustomAuth.handle(url: url)
}
- 对于SwiftUI,在您的SceneDelegate中实现以下内容
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return
}
CustomAuth.handle(url: url)
}
- 对于Storyboard,在您的app AppDelegate中实现以下内容
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if url.host == "my-wallet-app" {
CustomAuth.handle(url: url)
}
return true
}
通用链接
通用链接允许用户智能地跟随链接到您的应用内部的内容或到您的网站。查看文档了解实现方法。
- 对于Swift UI
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb, let urlToOpen = userActivity.webpageURL else {
return
}
CustomAuth.handle(url: urlToOpen)
}
- 对于Storyboard
func application(_ application: UIApplication, continue userActivity: UIUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
{
// Get URL components from the incoming user activity
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
return false
}
CustomAuth.handle(url: incomingURL)
}
完成这些后,您就可以开始了!请联系[email protected],今天就在测试网上启动验证器!
要求
- Swift 5
使用CustomAuthFactory
CASDKFactoryProtocol
提供了一种修改在FetchNodeDetails
中发现torus节点机制和在TorusUtils
中执行密钥检索机制的方法,这在模拟或高级定制场景中非常有用。想要使用此机制的开发者应在Sources/CustomAuth/CustomAuth.swift中实现CASDKFactoryProtocol
,然后将实例传递给CustomAuth
的init
,例如:
let tdsdk = CustomAuth(
aggregateVerifierType: "<type-of-verifier>",
aggregateVerifierName: "<verifier-name>",
subVerifierDetails: [sub],
factory: customFactory,
network: myNetworkm
loglevel: myLoglevel
)