测试测试 | ✓ |
Lang语言 | Obj-CObjective C |
许可 | MIT |
发布最新发布 | 2014年12月 |
由 Hernan Zalazar 维护。
依赖 | |
libextobjc | ~> 0.4 |
CocoaLumberjack | ~> 1.9 |
ObjectiveSugar | ~> 1.1 |
Auth0 是一种身份验证代理,支持社交身份提供者以及企业身份提供者,如 Active Directory、LDAP、Google Apps 和 Salesforce。
iOS 7+。如果您需要在使用较旧版本中使用我们的 SDK,请使用我们之前的 SDK pod Auth0Client
或检查此存储库的 old-sdk 分支。
Auth0.iOS pod 通过 CocoaPods 提供。要安装它,请简单地将以下行添加到您的 Podfile 中
pod "Auth0.iOS", "~> 1.3"
然后,在您的项目的 Info.plist
文件中添加以下条目
YOUR_AUTH0_APP_CLIENT_ID
YOUR_AUTH0_TENANT_NAME
例如
您可以使用 Auth0.iOS 与我们的本地小部件来处理您的身份验证。它获取 Auth0 应用配置并据此进行配置。
要开始,将此文件导入您的 AppDelegate.m
文件。
#import <Auth0.iOS/Auth0.h>
并添加以下方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
A0TwitterAuthenticator *twitter = [A0TwitterAuthenticator newAuthenticationWithKey:@"???" andSecret:@"????"];
A0FacebookAuthenticator *facebook = [A0FacebookAuthenticator newAuthenticationWithDefaultPermissions];
[[A0IdentityProviderAuthenticator sharedInstance] registerSocialAuthenticatorProviders:@[twitter, facebook]];
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[A0IdentityProviderAuthenticator sharedInstance] handleURL:url sourceApplication:sourceApplication];
}
有关如何配置 Facebook & Twitter 的更多信息,请转到 身份提供者身份验证
在您想要显示我们的本地小部件的类中导入以下头文件
#import <Auth0.iOS/Auth0.h>
#import <libextobjc/EXTScope.h>
以将我们的小部件作为模态视图控制器显示
A0AuthenticationViewController *controller = [[A0AuthenticationViewController alloc] init];
@weakify(self);
controller.onAuthenticationBlock = ^(A0UserProfile *profile, A0Token *token) {
@strongify(self);
// Do something with token & profile. e.g.: save them.
// Auth0.iOS will not save the Token and the profile for you. Please read below
// And dismiss the ViewController
[self dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:controller animated:YES completion:nil];
如果您需要保存和刷新用户的 JWT 令牌,请阅读我们 Wiki 中的以下指南如何在 Wiki 中保存和刷新 JWT 令牌。
您还可以查看我们的Swift和Objective-C示例应用程序。有关如何使用Auth0.iOS与Swift结合使用的更多信息,请参阅此指南
在使用来自其他身份提供者的认证之前,例如Twitter或Facebook,您需要遵循一些步骤。
首先,在您的AppDelegate.m
中添加以下方法
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[A0SocialAuthenticator sharedInstance] handleURL:url sourceApplication:sourceApplication];
}
这将允许Auth0.iOS处理来自Facebook、Twitter或其他身份提供者的成功登录。最后,您需要定义一个新的Auth0 URL类型,该类型具有以下自定义方案格式:a0${AUTH0_CLIENT_ID}
,您可以在Xcode中的应用程序目标中(在“信息”部分下)或直接在您的应用程序信息属性列表文件中完成此操作。此自定义方案由Auth0.iOS用于处理所有需要使用网络浏览器(Safari或UIWebView)进行认证的操作。
默认情况下,Auth0.iOS包含Twitter & Facebook集成(及其依赖项),但您可以丢弃您不需要的内容。如果您只想使用Facebook认证,只需将其添加到您的Podfile中
pod "Auth0.iOS/Core"
pod "Auth0.iOS/Facebook"
pod "Auth0.iOS/UI"
Auth0.iOS使用Facebook iOS SDK获取用户的访问令牌,因此您需要使用您的Facebook应用程序信息进行配置
首先,将以下条目添加到Info.plist
YOUR_FACEBOOK_APP_ID
YOUR_FACEBOOK_DISPLAY_NAME
注册一个以格式fb
的自定义URL类型。有关更多信息,请参阅Facebook入门指南。
以下是如何显示条目的示例
最后,您需要在应用程序中某处注册Auth0 Facebook提供者。您可以在AppDelegate.m
文件中这样做,例如
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
A0FacebookAuthenticator *facebook = [A0FacebookAuthenticator newAuthenticationWithDefaultPermissions];
[[A0SocialAuthenticator sharedInstance] registerSocialAuthenticatorProvider:facebook];
}
Twitter认证是通过反向认证完成的,以获取可用于发送到Auth0服务器并验证用户的有效访问令牌。默认情况下,我们使用iOS Twitter集成,但如果用户在其Apple设备上没有配置账户,我们支持OAuth Web流程(使用Safari)作为后备机制。
要支持Twitter认证,您需要配置Twitter认证提供者
NSString *twitterApiKey = ... //Remember to obfuscate your api key
NSString *twitterApiSecret = ... //Remember to obfuscate your api secret
A0TwitterAuthenticator *twitter = [A0TwitterAuthenticator newAuthenticationWithKey:twitterApiKey andSecret:twitterApiSecret];
[[A0SocialAuthenticator sharedInstance] registerSocialAuthenticatorProvider:twitter];
我们需要您的Twitter应用程序的密钥和密钥诀窍来对反向认证请求进行签名。有关更多信息,请阅读与授权请求和反向认证相关的Twitter文档。
您可以使用Auth0.iOS完成的非常酷的事情之一是使用SSO。想象一下,您想创建两个应用程序。然而,您想要的是,如果用户已在应用程序A中登录,则他在应用程序B中也将自动登录。这与Messanger和Facebook以及Foursquare和Swarm中发生的事情类似。
阅读此指南以学习如何使用此库完成此操作。
- (instancetype)init;
使用来自info.plist文件的Auth0ClientId
和Auth0Tenant
初始化'A0AuthenticationViewController'。
A0AuthenticationViewController *controller = [[A0AuthenticationViewController alloc] init];
@property (copy, nonatomic) void(^onAuthenticationBlock)(A0UserProfile *profile, A0Token *token);
成功认证时调用的块。具有两个参数:profile和token,除非登录在注册后被禁用,否则这两个值不会为nil。
controller.onAuthenticationBlock = ^(A0UserProfile *profile, A0Token *token) {
NSLog(@"Auth successful: profile %@, token %@", profile, token);
};
@property (copy, nonatomic) void(^onUserDismissBlock)();
当用户关闭登录界面时调用的块。只有当closable
属性为YES
时。
controller.onUserDismissBlock = ^() {
NSLog(@"User dismissed login screen.");
};
@property (assign, nonatomic) BOOL usesEmail;
在所有Auth0界面上启用用户名作为电子邮件处理(以及验证为电子邮件)。默认值为YES
。
controller.usesEmail = NO;
@property (assign, nonatomic) BOOL closable;
允许通过添加按钮关闭A0AuthenticationViewController
。默认值为NO
。
controller.closable = YES;
@property (assign, nonatomic) BOOL loginAfterSignUp;
注册成功后,如果此属性为YES
,则A0AuthenticationViewController将尝试登录用户;否则,将使用两个参数都为nil的onAuthenticationBlock
。默认值为YES
。
controller.loginAfterSignup = NO;
@property (assign, nonatomic) A0AuthParameters *authenticationParameters;
用于每个认证请求的Auth0 API的可选参数列表。默认情况下,它只包含'openid'和'offline_access'作用域值。更多信息请参阅我们的Wiki
controller.authenticationParameters.scopes = @[A0ScopeOfflineAccess, A0ScopeProfile];
@property (strong, nonatomic) UIView *signUpDisclaimerView;
出现在注册屏幕底部的视图。应将其用于显示应用程序的条款和条件。
UIView *view = //..
controller.signupDisclaimerView = view;
@property (assign, nonatomic) BOOL useWebView;
当认证需要打开网页登录时,例如LinkedIn,它将使用嵌入的UIWebView而不是Safari(如果设置为YES
)。我们建议使用Safari进行认证,因为这将始终保存用户会话。这意味着如果用户已经在LinkedIn上登录,例如点击LinkedIn按钮,它将正常工作。默认值是NO
。
controller.useWebView = YES
Auth0.iOS使用CocoaLumberjack记录多个调试信息。默认情况下,所有日志消息都被禁用,但您可以按照以下步骤启用它们
切换到A0Logging.h
并将auth0LogLevel
变量更改为您想要看到的日志级别。例如
static const int auth0LogLevel = LOG_LEVEL_ALL;
然后您需要配置CocoaLumberjack(如果您尚未为您的应用程序做此操作)。您只需要做一次,我们建议在您的AppDelegate
中完成此操作
#import <CocoaLumberjack/DDASLLogger.h>
#import <CocoaLumberjack/DDTTYLogger.h>
#import <CocoaLumberjack/DDLog.h>
@implementation A0AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[DDLog addLogger:[DDASLLogger sharedInstance]];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
return YES;
}
@end
Auth0帮助您
Auth0
Auth0.iOS 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。