Auth0Client 版本 0.0.12

Auth0Client 版本 0.0.12

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最后发布2014年12月

Hernan ZalazarSebastian IacomuzziEugenio Pace 维护。



Auth0Client 版本 0.0.12

  • Auth0

Auth0 iOS 客户端 License MIT

使用方法

  1. 将库安装到项目中

    如果您正在使用 CocoaPods,请将以下行添加到您的 Podfile

    pod 'Auth0Client'

    或者,按照以下步骤直接 git clone 并在您的项目中引用库

    git clone [email protected]:auth0/Auth0.iOS.git
    1. 转到您的项目
    2. 在 Frameworks 文件夹中右键单击,然后选择 将文件添加到 "您的项目名称"
    3. 转到 Auth0.iOS,选择 Auth0Client 文件夹,确保您的项目目标已选中,然后按 添加
  2. 实例化 Auth0Client

    #import "Auth0Client.h"
    
    // ...
    
    Auth0Client *client = [Auth0Client auth0Client:@"YOUR_AUTH0_DOMAIN" clientId:@"YOUR_CLIENT_ID"];
  3. 触发登录(使用小部件)

    [client loginAsync:self withCompletionHandler:^(NSMutableDictionary* error) {
    if (error) {
        NSLog(@"Error authenticating: %@", [error objectForKey:@"error"]);
    }
    else {
      // * Use client.auth0User to do wonderful things, e.g.:
      // - get user email => [client.auth0User.Profile objectForKey:@"email"]
      // - get facebook/google/twitter/etc access token => [[[client.auth0User.Profile objectForKey:@"identities"] objectAtIndex:0] objectForKey:@"access_token"]
      // - get Windows Azure AD groups => [client.auth0User.Profile objectForKey:@"groups"]
      // - etc.
    }
    }];

或者您可以使用连接作为参数(例如,这里我们使用 Windows Azure AD 账户进行登录)

[client loginAsync:self connection:@"auth0waadtests.onmicrosoft.com" withCompletionHandler:^(NSMutableDictionary* error) { ... }];

用户名/密码登录(不使用 WebView)

只有某些供应商支持此选项(数据库连接和活动目录/LDAP)。

[client loginAsync:self connection:@"my-db-connection"
                        username:@"username"
                        password:@"password"
                        withCompletionHandler:^(NSMutableDictionary* error) {
    if (error) {
        NSLog(@"Error authenticating: %@ - %@", [error objectForKey:@"error"], [error objectForKey:@"error_description"]);
    }
    else {
        // * Use client.auth0User to do wonderful things, e.g.:
        // - get user email => [client.auth0User.Profile objectForKey:@"email"]
        // - get facebook/google/twitter/etc access token => [[[client.auth0User.Profile objectForKey:@"identities"] objectAtIndex:0] objectForKey:@"access_token"]
        // - get Windows Azure AD groups => [client.auth0User.Profile objectForKey:@"groups"]
        // - etc.
    }
}];

可选地,您可以指定 作用域 参数。目前作用域有两个可能的值

  • scope:@"openid"(默认值)- 它将返回 access_token,还有 id_token,这是一个 Json Web Token (JWT)。JWT 只包含用户 id。
  • scope:@"openid profile":如果您想使整个用户配置文件成为 id_token 的一部分。

委托令牌请求

您可以通过指定目标客户端的 ID(targetClientId)以及可选的 NSMutableDictionary 对象(options),来获取一个委托令牌,以便包括自定义参数,如作用域或 id_token

NSMutableDictionary *options = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
    @"USER_ID_TOKEN", @"id_token",  // default: id_token of the authenticated user (client.auth0User.IdToken)
    @"openid profile", @"scope",    // default: openid
    nil];

[client getDelegationToken:targetClientId options:options withCompletionHandler:^(NSMutableDictionary* delegationResult)
{
    // [delegationResult objectForKey:@"id_token"]
}];

options 参数不得包含 client_idtarget 键。 targettargetClientId 获取,client_id 使用创建 Auth0Client 实例时使用的 id。

使用 Facebook App 原生登录对话框进行认证

  1. 安装并 配置您的应用程序 以与 iOS 的 Facebook SDK 一起工作。

  2. 在您的iOS应用中实现Facebook登录。有两种方式

  3. 用户通过Facebook App Native认证后,调用loginAsync方法,指定Facebook的access_token

NSString *fb_access_token = [[FBSession.activeSession accessTokenData] accessToken];

[client loginAsync:self connection:@"facebook"
                        accessToken:fb_access_token
                        withCompletionHandler:^(NSMutableDictionary* error) {
    if (error) {
        NSLog(@"Error authenticating: %@ - %@", [error objectForKey:@"error"], [error objectForKey:@"error_description"]);
    }
    else {
        // Use client.auth0User to do wonderful things
    }
}];

想了解更多详情,您可以查看我们的示例


什么是Auth0?

Auth0可以帮助您

  • 通过多种身份验证来源添加身份验证,无论是社交账号如Google、Facebook、Microsoft Account、LinkedIn、GitHub、Twitter,还是企业身份系统如Windows Azure AD、Google Apps、AD、ADFS或任何SAML身份提供者
  • 通过传统的添加身份验证。
  • 实现支持将不同的用户账号链接到同一个用户链接
  • 生成已签名的Json Web Tokens以安全调用您的API并将用户身份
  • 分析用户登录的何时、何地。
  • 通过JavaScript规则从其他来源拉取数据并将其添加到用户配置文件中。

在Auth0中创建免费账户

  1. 转到Auth0并点击注册。
  2. 使用Google、GitHub或Microsoft Account登录。