IBMCloudAppID 6.0.3

IBMCloudAppID 6.0.3

由以下人员维护:Vladimir AtanasovTal AvielVitaly MRotem podAnton AleksandrovMoty Drimer



 
依赖
BMSCore>= 0
JOSESwift>= 0
 

  • IBM Cloud Services Mobile SDK 提供

IBM Cloud App ID iOS Swift SDK

IBM Cloud powered Travis Coveralls Codacy License

GithubWatch GithubStars GithubForks

要求

  • Xcode 9.0 或更高版本
  • CocoaPods 1.1.0 或更高版本
  • MacOS 10.11.5 或更高版本
  • iOS 10.0 或更高版本

安装 SDK

  1. 将 'IBMCloudAppID' 依赖项添加到你的 Podfile,例如

    target <yourTarget> do
       use_frameworks!
         pod 'IBMCloudAppID'
    end
  2. 从终端运行

    pod install --repo-update

初始化 App ID 客户端 SDK

  1. 打开你的 Xcode 项目,并启用 Keychain Sharing(在项目设置 > 功能 > Keychain sharing 中)

  2. 在项目设置 > 信息 > URL 类型下,将 $(PRODUCT_BUNDLE_IDENTIFIER) 添加为 URL Scheme

  3. 将以下导入添加到你的 AppDelegate.swift 文件中

    import IBMCloudAppID
  4. 通过将tenantId和region参数传递给initialize方法初始化客户端SDK。通常,将初始化代码放在AppDelegate的应用程序中的didFinishLaunchingWithOptions:方法中是一个常见的做法,即使这不是强制性的。

    AppID.sharedInstance.initialize(tenantId: <tenantId>, region: AppID.REGION_UK)
    • 将"tenantId"替换为App ID服务的tenantId。
    • AppID.REGION_UK替换为您的App ID区域(AppID.REGION_US_SOUTHAppID.REGION_SYDNEY)。
  5. 将以下代码添加到您的AppDelegate文件中

    func application(_ application: UIApplication, open url: URL, options :[UIApplicationOpenURLOptionsKey : Any]) -> Bool {
        return AppID.sharedInstance.application(application, open: url, options: options)
    }

使用登录小工具

在App ID客户端SDK初始化后,您可以通过启动登录小工具来开始用户认证。

  1. 将以下导入添加到您想要使用登录小工具的文件中
import IBMCloudAppID
  1. 将以下代码添加到同一文件中
class delegate : AuthorizationDelegate {
    public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
        //User authenticated
    }

    public func onAuthorizationCanceled() {
        //Authentication canceled by the user
    }

    public func onAuthorizationFailure(error: AuthorizationError) {
        //Exception occurred
    }
}

AppID.sharedInstance.loginWidget?.launch(delegate: delegate())

说明:

  • 默认情况下,App ID配置为使用Facebook、Google和Cloud Directory作为身份提供者。如果您将身份提供者设置更改为仅提供一种选项,那么将不再需要登录小工具,并且它将不会显示。用户将被重定向到您选择的身份提供者的认证屏幕。
  • 当使用Cloud Directory,并且"电子邮件验证"配置为不允许用户在没有电子邮件验证的情况下登录时,则"AuthorizationListener"的"onAuthorizationSuccess"将不会调用令牌。

使用iOS Swift SDK管理Cloud Directory

使用资源所有者密码登录

通过提供最终用户的用户名和密码,您可以获取访问令牌和id令牌。

class delegate : TokenResponseDelegate {
    public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
    //User authenticated
    }

    public func onAuthorizationFailure(error: AuthorizationError) {
    //Exception occurred
    }
}

AppID.sharedInstance.signinWithResourceOwnerPassword(username: username, password: password, delegate: delegate())

使用刷新令牌登录

建议本地存储刷新令牌,以便无需用户再次输入凭据即可使用刷新令牌登录。

class delegate : TokenResponseDelegate {
    public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
    //User authenticated
    }

    public func onAuthorizationFailure(error: AuthorizationError) {
    //Exception occurred
    }
}

AppID.sharedInstance.signInWithRefreshToken(refreshTokenString: refreshTokenString, delegate: delegate())

注册

务必确保在云目录设置中,将“允许用户注册和重置密码”设置为开启

使用LoginWidget类来启动注册流程。

class delegate : AuthorizationDelegate {
  public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
     if accessToken == nil && identityToken == nil {
      //email verification is required
      return
     }
   //User authenticated
  }

  public func onAuthorizationCanceled() {
      //Sign up canceled by the user
  }

  public func onAuthorizationFailure(error: AuthorizationError) {
      //Exception occurred
  }
}

AppID.sharedInstance.loginWidget?.launchSignUp(delegate: delegate())

忘记密码

务必确保在云目录设置中,将“允许用户注册和重置密码”以及“忘记密码邮箱”都设置为开启

使用LoginWidget类来启动忘记密码流程。

class delegate : AuthorizationDelegate {
   public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
      //forgot password finished, in this case accessToken and identityToken will be null.
   }

   public func onAuthorizationCanceled() {
       //forgot password canceled by the user
   }

   public func onAuthorizationFailure(error: AuthorizationError) {
       //Exception occurred
   }
}

AppID.sharedInstance.loginWidget?.launchForgotPassword(delegate: delegate())

更改详情

务必确保在云目录设置中,将“允许用户注册和重置密码”设置为开启

使用LoginWidget类来启动更改详情流程。此API只能在用户使用云目录身份提供商登录时使用。

 class delegate : AuthorizationDelegate {
     public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
        //User authenticated, and fresh tokens received
     }

     public func onAuthorizationCanceled() {
         //changed details canceled by the user
     }

     public func onAuthorizationFailure(error: AuthorizationError) {
         //Exception occurred
     }
 }

 AppID.sharedInstance.loginWidget?.launchChangeDetails(delegate: delegate())

更改密码

务必确保在云目录设置中,将“允许用户注册和重置密码”设置为开启

使用LoginWidget类来启动更改密码流程。此API只能在用户使用云目录身份提供商登录时使用。

 class delegate : AuthorizationDelegate {
     public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
         //User authenticated, and fresh tokens received
     }

     public func onAuthorizationCanceled() {
         //change password canceled by the user
     }

     public func onAuthorizationFailure(error: AuthorizationError) {
          //Exception occurred
     }
  }

  AppID.sharedInstance.loginWidget?.launchChangePassword(delegate: delegate())

注销

调用注销函数以清除存储的令牌。

 AppID.sharedInstance.logout()

用户资料

使用App ID UserProfileManager,您可以在用户的资料中创建、读取和删除属性,同时还可以检索有关用户的更多信息。

let key = "attrKey"
let value = "attrValue"
let accessToken = "<access token>"
let idToken = "<id token>"

let userProfileManager = AppID.sharedInstance.userProfileManager

// Handle attribute response
func attributeHandler(error: Error?, response: [String: Any]) {}

/// If no tokens are passed, App ID will attempt to use the latest stored access and identity tokens

// Set Attributes
userProfileManager?.setAttribute(key: key, value: value, completionHandler: attributeHandler)
userProfileManager?.setAttribute(key: key, value: value, accessTokenString: accessToken)

// Get particular attribute
userProfileManager?.getAttribute(key: key, completionHandler: attributeHandler)
userProfileManager?.getAttribute(key: key, accessTokenString: accessToken, completionHandler: attributeHandler)

// Get all attributes
userProfileManager?.getAttributes(completionHandler: attributeHandler)
userProfileManager?.getAttributes(accessTokenString: accessToken, completionHandler: attributeHandler)

// Delete an Attribute
userProfileManager?.deleteAttribute(key: key, completionHandler:attributeHandler)
userProfileManager?.deleteAttribute(key: key, accessTokenString: accessToken, completionHandler: attributeHandler)

// Retrieve additional information about a user using the stored access/identity tokens
userProfileManager?.getUserInfo { (error: Error?, info: [String: Any]?) in

}

// Retrieve additional information about a user using the provided access and identity token
// If an identityToken is provided (recommended), we will validate the user info response
userProfileManager?.getUserInfo(accessTokenString: accessToken, identityTokenString: idToken { (error: Error?, info: [String: Any]?) in

}

调用受保护资源

将以下导入添加到您要调用受保护资源请求的文件中:

import BMSCore
import IBMCloudAppID

然后添加以下代码:

BMSClient.sharedInstance.initialize(region: AppID.REGION_UK)
BMSClient.sharedInstance.authorizationManager = AppIDAuthorizationManager(appid:AppID.sharedInstance)
var request:Request =  Request(url: "<your protected resource url>")
request.send(completionHandler: {(response:Response?, error:Error?) in
    //code handling the response here
})

设置钥匙串可访问性

在极少数情况下,如果您的应用在后台运行时需要刷新App ID令牌,您可以使用此API设置所需的关键权限。

AppID.secAttrAccess = .accessibleAlways

有问题吗?

加入我们的Slack团队以获取帮助Slack,与我们的开发者团队交谈。

许可证

此软件包包含根据Apache许可证版本2.0(“许可证”)许可的代码。您可以在https://apache.ac.cn/licenses/LICENSE.html处获取许可证副本,并可以在此软件包的LICENSE文件中查看许可证。