LogonLabs iOS
官方 LogonLabs iOS 客户端库。
下载
CocoaPods
将这些行添加到您的 Podfile
use_frameworks!
pod 'LogonLabs', '~> 1.5.0'
LogonLabs API
-
在编码之前,需要在 https://app.logonlabs.com/app/#/app-settings 进行一些配置。
-
有关完整的开发者文档,请访问:https://app.logonlabs.com/api/
创建新的客户端实例
- 您的
APP_ID
可在 应用设置 中找到 LOGONLABS_API_ENDPOINT
应设置为https://api.logonlabs.com
DESTINATION_URL
应设置为您的应用程序的自定义 URL 方案- 备注:必须通过应用设置将此URL添加到您的App的目标URL白名单。
创建一个LogonClient
的新实例。
import LogonLabs;
let logonClient = LogonClient(baseUri: "{LOGONLABS_API_ENDPOINT}", appId: "{APP_ID}");
SSO登录快速入门
iPhone库中的StartLogin函数开始LogonLabs管理的SSO过程。
第一步
以下示例演示了当系统使用回调URL
将用户重定向回您的页面后应该怎么做。
var body: some View {
VStack {
Button(action: {
logonClient.startLogin(identity_provider: "google", destinationUrl: "") {error in
if(error != nil) {
print(error.description!)
}
}
}){
Text("Start SSO Workflow")
}
}
}
第二步
用户将被重定向到其iPhone浏览器,并提示使用指定的身份提供者登录。最后,用户将被重定向到您的后端服务器。调用ValidateLogin后,将会有一个destination_url参数,应使用名为payload
的查询参数进行重定向。这可以包含您移动应用需要的任何内容,并应进行Base64编码。
第三步
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
var payload : String?
logonClient.parsePayload(urlContexts: URLContexts) { result in
switch result {
case .failure(let e ):
payload = e.description
case .success(let p):
payload = p
}
}
//An example of passing the information to the view but
let view = AuthenticatedView(data: payload)
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: view)
self.window = window
window.makeKeyAndVisible()
}
}
辅助方法
GetProviders
此方法用于检索应用程序启用的所有提供者的列表。如果向该方法传递电子邮件地址,它将返回该电子邮件域可用的提供者列表。如果已经配置了企业身份提供者,则还会在 enterprise_identity_providers 中返回一组匹配的提供者。
import LogonLabs;
logonClient.getProviders(emailAddress: "[email protected]") {result in
switch result {
case .success(let providerData):
if let suggestedProvider = providerData.suggestedProvider {
print(suggestedProvider.rawValue) //google or microsoft
}
for socialProvider in providerData.socialIdentityProviders {
print(socialProvider.type.rawValue) //google, microsoft, okta, etc
}
for enterpriseProvider in providerData.enterpriseIdentityProviders {
print("Provider Name: \(enterpriseProvider.name)")
print("Provider Id: \(enterpriseProvider.identityProviderId)")
print("Provider Type: \(enterpriseProvider.type.rawValue)")
}
case .failure(let error):
print(error.description!)
}
}