iOS SDK
概述
AppAuth.framework为iPhone手机开发者提供了多种登录认证模块的SDK包,辅助开发者快速接入登录、注册、生物认证等功能。 本SDK基于Objective-C开发,SDK接入也通过OC的语法进行。
最新SDK包
| 模块名 | SDK包 | 最新版本 | 文件大小 | 下载链接 |
|---|---|---|---|---|
| 认证SDK | AppAuth.framework | 1.0 | 1,975,419bytes | 下载地址 |
SDK包介绍
AppAuth.framework基于开源代码AppAuth,对AppAuth做了一层封装,因适配统一身份认证服务端,与AppAuth的原始SDK有一定区别,在接入接口的使用上,也有些许区别。
iOS SDK支持的版本
| iOS版本 | SDK描述 |
|---|---|
| > iOS9 | uses the in-app browser tab pattern (via SFSafariViewController) |
| >= iOS7 | system browser |
接入SDK工程配置:
在主工程中添加framework框架: 【工程】-【General】-【frameworks,library,and Embedded Content】下Insert AppAuth.framework
头文件配置 【工程】-【Build Settings】-【Header Search Paths】 添加"$../SDK/Libs/AppAuth.framework/Headers",并设置“recursive”=true
SDK内部会通过shceme形式接受认证中的浏览器的通信数据,scheme的配置在APP层级,需要在项目的info.plist的URL types中新增Item,在URL Schemes配置一个scheme,scheme值如下:
net.rj.appauth
iOS SDK接口使用
认证参数配置
- 手动指定参数信息:
NSURL *authorizationEndpoint =
[NSURL URLWithString:@"https://accounts.google.com/o/oauth2/v2/auth"];
NSURL *tokenEndpoint =
[NSURL URLWithString:@"https://www.googleapis.com/oauth2/v4/token"];
OIDServiceConfiguration *configuration =
[[OIDServiceConfiguration alloc]
initWithAuthorizationEndpoint:authorizationEndpoint
tokenEndpoint:tokenEndpoint];
// perform the auth request...
OIDAuthorizationRequest *request =
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
clientId:clientID
clientSecret:clientSecret
scopes:@[ OIDScopeOpenID, OIDScopeProfile ]
redirectURL:redirectURI
responseType:OIDResponseTypeCode
additionalParameters:additionalParameters];
其中 authorizationEndpoint 、 tokenEndpoint的值为示例,实际取值为: 必须接口中参数来源说明(关于url域名的绝对地址拼装,参考【概述】-【域名地址配置说明】):
| 参数名 | 参数来源/值 |
|---|---|
| clientID | 统一身份认证后台 |
| scopes | 值:openid |
| redirectURI | 统一身份认证后台 |
| tokenEndpoint | 域名,相对路径 : /accessToken |
| authorizationEndpoint | 域名,相对路径 : /authorize |
- 通过discovery自动获取参数
NSURL *issuer = [NSURL URLWithString:@"https://accounts.google.com"];
[OIDAuthorizationService discoverServiceConfigurationForIssuer:issuer
completion:^(OIDServiceConfiguration *_Nullable configuration,
NSError *_Nullable error) {
if (!configuration) {
NSLog(@"Error retrieving discovery document: %@",
[error localizedDescription]);
return;
}
// perform the auth request...
}];
其中 issuer的值为示例,实际取值为: 必须接口中参数来源说明(关于url域名的绝对地址拼装,参考【概述】-【域名地址配置说明】):
| 参数名 | 参数来源/值 |
|---|---|
| issuer | 域名,相对路径 : /oidc |
以上都是Auth标准的输入参数,本SDK内置了第三方的跳转页面,需要 转换登录/登出的redirect url和authorizationEndpoint,转换格式如下(添加为参数), 请在发起authorize、accessToken之前转换:
Url request 额外参数:
| 参数 | 类型 | 必填 | 定义 |
|---|---|---|---|
| launch | string | require | Scheme + host,跳转回APP |
| jumpfrom | string | require | appid/package id |
| platform | string | require | Ios / android |
| client_id | string | require |
scheme与info.plist的相同。
登录接口
首先,您需要在 UIApplicationDelegate 实现中有一个属性来保存会话,以便从重定向继续授权流程。在我们的例子中,这个委托的实现是一个名为 AppDelegate 的类。
@interface AppDelegate : UIResponder <UIApplicationDelegate>
// property of the app's AppDelegate
@property(nonatomic, strong, nullable) id<OIDExternalUserAgentSession> currentAuthorizationFlow;
@end
在工程主类中,定义 OIDAuthState
// property of the containing class
@property(nonatomic, strong, nullable) OIDAuthState *authState;
授权请求。通过使用authStateByPresentingAuthorizationRequest便捷方法,令牌交换将自动执行,并且一切都将使用 PKCE进行保护。AppAuth 也允许你手动执行这些请求。
// builds authentication request
OIDAuthorizationRequest *request =
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
clientId:kClientID
scopes:@[OIDScopeOpenID,
OIDScopeProfile]
redirectURL:kRedirectURI
responseType:OIDResponseTypeCode
additionalParameters:nil];
// performs authentication request
AppDelegate *appDelegate =
(AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.currentAuthorizationFlow =
[OIDAuthState authStateByPresentingAuthorizationRequest:request
presentingViewController:self
callback:^(OIDAuthState *_Nullable authState,
NSError *_Nullable error) {
if (authState) {
NSLog(@"Got authorization tokens. Access token: %@",
authState.lastTokenResponse.accessToken);
[self setAuthState:authState];
} else {
NSLog(@"Authorization error: %@", [error localizedDescription]);
[self setAuthState:nil];
}
}];
处理重定向: 授权响应 URL 通过 iOS 的openURL应用委托方法返回到应用程序
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<NSString *, id> *)options {
// Sends the URL to the current authorization flow (if any) which will
// process it if it relates to an authorization response.
if ([_currentAuthorizationFlow resumeExternalUserAgentFlowWithURL:url]) {
_currentAuthorizationFlow = nil;
return YES;
}
// Your additional URL handling (if any) goes here.
return NO;
}
登出接口
//builds ending authentication request
id<OIDExternalUserAgent> externalUserAgent = [[OIDExternalUserAgentIOS alloc] initWithPresentingViewController:presentingViewController];;
AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
appDelegate.currentAuthorizationFlow = [OIDAuthorizationService presentEndSessionRequest:request externalUserAgent:externalUserAgent callback:callback];