iOS网络身份认证

文档

URL Session Programming Guide中重要的类如下:

在Networking Overview——Making HTTP and HTTPS Requests中的配置身份认证(Configuring Authentication),摘录如下:

对于NSURLSession,代理对象要实现URLSession:task:didReceiveChallenge:completionHandler:方法。
对于NSURLConnection类:

  • OS X v10.7和iOS5及以上的,代理对象要显示connection:willSendRequestForAuthenticationChallenge: 方法。
  • 在早期的版本中,代理对象要实现connection:canAuthenticateAgainstProtectionSpace:connection:didReceiveAuthenticationChallenge:方法。

URL Session Programming Guide中的Authentication Challenges and TLS Chain Validation有如何响应认证请求?(Deciding How to Respond to an Authentication Challenge)

  • NSURLSession: 所有的认证请求都被传递给代理对象,不论它是什么认证类型
  • NSURLConnection或者NSURLDownload: 代理对象收到connection:canAuthenticateAgainstProtectionSpace:(或者download:canAuthenticateAgainstProtectionSpace:)消息,允许代理,在尝试来验证它之前,来分析服务器的属性,包括它的协议和认证方法。如果代理不打算来验证服务器的protection space,返回NO。然后系统会尝试从用户的keychain来验证信息。
  • 如果NSURLConnection或者NSURLDownload 的代理没有实现connection:canAuthenticateAgainstProtectionSpace:(或者download:canAuthenticateAgainstProtectionSpace:)方法,protection space使用客户端证书认证(client certificate authentication)或者服务器信任认证server trust authentication),系统的行为就像你返回了NO。系统的行为就像对其它的所有的认证类型,你返回的是YES。

接下来,如果代理同意处理认证,并且没有可用的有效证书,无论是请求URL的一部分或者是NSURLCredentialStorage中的共享。代理会收到下面其中之一的消息:

URLSession:didReceiveChallenge:completionHandler:
URLSession:task:didReceiveChallenge:completionHandler:
connection:didReceiveAuthenticationChallenge:
download:didReceiveAuthenticationChallenge:

为了继续连接,代理有如下的三个选择:

  • 提供认证证书
  • 没有证书,尝试继续(Attempt to continue without credentials)
  • 取消认证请求

响应认证请求

响应connection:didReceiveAuthenticationChallenge: 代理方法,有如下3种方式

提供证书(Providing Credentials)

为了认证,要创建一个NSURLCredential对象。在提供的authentication challenge的protection space上调用authenticationMethod 方法,可以获取服务器的认证方法。NSURLCredential支持的认证方法有:

  • HTTP basic authentication (NSURLAuthenticationMethodHTTPBasic) 需要一个用户名和密码。使用
    credentialWithUser:password:persistence:方法创建NSURLCredential对象。
  • HTTP digest authentication (NSURLAuthenticationMethodHTTPDigest),与basic authentication类似,也需要一个用户名和密码。使用
    credentialWithUser:password:persistence:方法创建NSURLCredential对象。
  • Client certificate authentication (NSURLAuthenticationMethodClientCertificate) 需要system identity和需要与server进行身份验证的所有证书。使用credentialWithIdentity:certificates:persistence:创建NSURLCredential对象。
  • Server trust authentication (NSURLAuthenticationMethodServerTrust) 需要authentication challenge的protection space提供一个trust。使用credentialForTrust:来创建NSURLCredential对象。

创建NSURLCredential对象之后:

  • 对于NSURLSession,在completion handler中,把NSURLCredential对象传递给authentication challenge的sender。
  • 对于NSURLConnectionNSURLDownload,使用useCredential:forAuthenticationChallenge: 方法,把NSURLCredential对象传递给authentication challenge的sender。

不提供证书(Continuing Without Credentials)

如果代理选择不提供证书,可选择如下方式来继续:

  • 对于NSURLSession,传递如下之一的值给completion handler回调:

    • NSURLSessionAuthChallengePerformDefaultHandling 处理请求,就好像代理没有提供一个代理方法来处理认证请求
    • NSURLSessionAuthChallengeRejectProtectionSpace 拒接认证请求。基于服务器响应的认证类型,URL加载类可能会多次调用代理方法。
  • 对于 NSURLConnectionNSURLDownload,在[challenge sender] 上调用continueWithoutCredentialsForAuthenticationChallenge:方法。不提供证书的话,可能会导致连接失败,调用connectionDidFailWithError:方法 ,或者会返回一个不需要验证身份的替代的URL。

取消连接(Canceling the Connection)

代理也可以选择取消认证请求(authentication challenge)

  • 对于NSURLSession,传递NSURLSessionAuthChallengeCancelAuthenticationChallenge 到提供的completion handler中。
  • 对于 NSURLConnectionNSURLDownload,在[challenge sender]上调用cancelAuthenticationChallenge: 。代理会收到connection:didCancelAuthenticationChallenge:消息。

身份认证例子

下面的例子使用预先设置的用户名和密码来创建NSURLCredential。如果身份认证先前失败过,就取消认证请求并通知用户。

-(void)connection:(NSURLConnection *)connectiondidReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{if ([challenge previousFailureCount] == 0) {NSURLCredential *newCredential;newCredential = [NSURLCredential credentialWithUser:[self preferencesName]password:[self preferencesPassword]persistence:NSURLCredentialPersistenceNone];[[challenge sender] useCredential:newCredentialforAuthenticationChallenge:challenge];} else {[[challenge sender] cancelAuthenticationChallenge:challenge];// inform the user that the user name and password// in the preferences are incorrect[self showPreferencesCredentialsAreIncorrectPanel:self];}
}

如果代理没有实现connection:didReceiveAuthenticationChallenge:方法,并且请求需要认证,URL credential storage中需存在有效的证书或者必须在请求的URL中提供。如果没有证书或者验证失败,代理会调用continueWithoutCredentialForAuthenticationChallenge: 方法。

执行自定义TLS链验证(Performing Custom TLS Chain Validation)

除了提供证书到服务器,app还可以检查在TLS握手过程中服务器提供的证书,然后告知URL加载系统,是否应该接受或者拒接这些证书。
如果你需要以非标准方式来执行TLS链验证(例如接受一个自签名证书来测试),app应该这样做:

  • 对于NSURLSession,实现URLSession:didReceiveChallenge:completionHandler: 或者URLSession:task:didReceiveChallenge:completionHandler:代理方法。如果你两个都实现,session-level方法负责处理身份认证。
  • 对于NSURLConnectionNSURLDownload,实现connection:canAuthenticateAgainstProtectionSpace:或者download:canAuthenticateAgainstProtectionSpace: 方法,如果 protection space有一个认证类型是NSURLAuthenticationMethodServerTrust,返回YES。
    然后,实现connection:didReceiveAuthenticationChallenge: 或者download:didReceiveAuthenticationChallenge: 来处理身份认证。

在身份认证处理方法中,要检查challenge的protection space是否有NSURLAuthenticationMethodServerTrust类型,如果有,就从protection space中获取serverTrust信息。

更多详细信息,请参考Overriding TLS Chain Validation Correctly

类介绍

相关内容请参加HTTPS Server Trust Evaluation

NSURLProtectionSpace

protection space (also realm) — An HTTP or HTTPS server, or an area on such a server, that requires authentication. Within Foundation this is represented by the NSURLProtectionSpace class. See also authentication challenge.

NSURLProtectionSpace表示需要认证的服务器或者域。
《iOS网络高级编程:iPhone和iPad的企业应用开发》书中介绍如下:
“最佳实践是使用NSURLProtectionSpace 验证手机银行应用的用户与安全的银行服务器进行通信,特别是在发出的请求会操纵后端数据时更是如此”
创建方式如下:

    NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:@"67.205.6.121"port:443protocol:NSURLProtectionSpaceHTTPSrealm:@"mobilebanking"authenticationMethod:NSURLAuthenticationMethodClientCertificate];

所有支持的认证方法有:

  • NSURLAuthenticationMethodDefault
  • NSURLAuthenticationMethodHTTPBasic
  • NSURLAuthenticationMethodHTTPDigest
  • NSURLAuthenticationMethodHTMLForm
  • NSURLAuthenticationMethodNTLM
  • NSURLAuthenticationMethodNegotiate
  • NSURLAuthenticationMethodClientCertificate
  • NSURLAuthenticationMethodServerTrust

使用服务器的属性创建了保护空间后,就可以用于验证连接。在代码向需要认证的服务器请求资源时,服务器会使用HTTP状态码401进行响应,即访问拒绝。NSURLConnection会接收到响应并立刻使用认证challenge的一份副本来发送一条willSendRequestForAuthenticationChallenge:委托消息。过程如下所示:

NSURLAuthenticationChallenge

NSURLAuthenticationChallenge encapsulates a challenge from a server requiring authentication from the client.

authentication challenge — An HTTP or HTTPS response indicating that the server requires authentication information from the client(服务器从客户端请求的验证信息). Foundation represents this with the NSURLAuthenticationChallenge class, and it also uses this infrastructure to support custom HTTPS server trust evaluation. An authentication challenge originates from a protection space.

NSURLCredential

NSURLCredential代表的是一个身份验证证书。URL Loading系统支持3种类型的证书:password-based user credentials, certificate-based user credentials, and certificate-based server credentials。
NSURLCredential适合大多数的认证请求,因为它可以表示由用户名/密码组合、客户端证书及服务器信任创建的认证信息。
认证信息有三种持久化选项:

  • NSURLCredentialPersistenceNone :要求 URL 载入系统 “在用完相应的认证信息后立刻丢弃”。
  • NSURLCredentialPersistenceForSession :要求 URL 载入系统 “在应用终止时,丢弃相应的 credential ”。
  • NSURLCredentialPersistencePermanent :要求 URL 载入系统 “将相应的认证信息存入钥匙串(keychain),以便其他应用也能使用。

HTTP认证

认证指的是确认访问系统的用户身份的过程。对于手机银行应用的服务层来说,最重要的事情就是要能分辨出真实用户与虚假用户之间的差别。
手机银行应用有两种认证模式:

  • 标准验证:提示用户输入用户名和密码
  • 快速验证:让用户注册设备,然后使用PIN进行验证,每次验证无需用户名和密码。如果用户选择在给定的认证请求中注册设备,那么服务器响应就需要包含一个额外的属性,即用户的证书。应用会存储这个证书,并在随后启动时检查,从而确定应该显示哪个认证视图。

手机银行应用的标准认证模式使用HTTP Basic认证,而快速认证则使用从Web Service下载的客户端证书。

HTTP Basic, HTTP Digest, 与 NTLM认证

Basic、Digest与NTLM认证都是基于用户名/密码的认证。他们认证的响应逻辑是相同的。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {// validate that the authentication challenge came from a whitelisted protection spaceif (![[[Model sharedModel] validProtectionSpaces] containsObject:challenge.protectionSpace]) {// dispatch alert view message to the main threaddispatch_async(dispatch_get_main_queue(), ^{[[[UIAlertView alloc] initWithTitle:@"Unsecure Connection"message:@"We're unable to establish a secure connection. Please check your network connection and try again."delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil] show];});// cancel authentication[challenge.sender cancelAuthenticationChallenge:challenge];}// respond to basic authentication requests// others that follow this pattern include DIGEST and NTLM authenticationif (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic) {// proceed with authenticationif (challenge.previousFailureCount == 0) {NSURLCredential *creds = [[NSURLCredential alloc] initWithUser:_usernamepassword:_password persistence:NSURLCredentialPersistenceForSession];[challenge.sender useCredential:creds forAuthenticationChallenge:challenge];// authentication has previously failed. depending on authentication configuration, too// many attempts here could lead to a poor user experience via locked accounts} else {// cancel the authentication attempt[[challenge sender] cancelAuthenticationChallenge:challenge];// alert observers of the failed attemptdispatch_async(dispatch_get_main_queue(), ^{[[NSNotificationCenter defaultCenter] postNotificationName:kNormalLoginFailedNotification object:nil];});}}
}

基本认证请参考HTTP协议 (二) 基本认证

参考文档

  • iOS安全系列之一:HTTPS
  • iOS安全系列之二:HTTPS进阶

iOS网络——身份认证相关推荐

  1. [附源码]java毕业设计网络身份认证技术及方法

    项目运行 环境配置: Jdk1.8 + Tomcat7.0 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclis ...

  2. 信息系统安全实验(七):使用Kerberos实现网络身份认证

    这是信息系统安全实验系列的第七篇~ 1. 背景知识 (1)概述 Kerberos又指麻省理工学院为这个协议开发的一套计算机网络安全系统.系统设计上采用客户端/服务器结构与DES加密技术,并且能够进行相 ...

  3. 人脸识别很危险!美斯坦福大学攻破,千万别拿来当网络身份认证

    人脸识别很危险!美斯坦福大学攻破,千万别拿来当网络身份认证 http://mp.weixin.qq.com/s?__biz=MzIxMDIwODM2MA==&mid=2653906207&am ...

  4. 身份认证云平台商丘_网络身份认证平台

    在线身份认证解决方案 背景介绍 近年来互联网金融的迅猛发展,让我们看到了金融与互联网结合之后迸发的巨大能量.互联网因其便利.快捷.覆盖面广.快速响应客户需求.强调用户使用感受等特点,在与金融业务结合之 ...

  5. 安全网络身份认证系统的设计与实现

    本文章源码地址:https://gitee.com/sukels/shirohttps://gitee.com/sukels/shiro 摘  要 随着互联网的飞速发展,Web应用的安全问题日益凸显. ...

  6. iOS网络请求认证挑战

    一.引言 Http请求中认证挑战相关的代理如下: 1.将要发送一个认证挑战的请求 - connection:willSendRequestForAuthenticationChallenge: 2.是 ...

  7. 破坏网络可信身份认证,黑灰产业链正在兴起

    <2015网络可信身份发展年报> 阿里移动安全 第一章  2015年网络可信身份发展现状 1.1 网络可信身份发展的需求产生新的变化 2002年我国开始了网络实名制建设的探讨,其本质目的在 ...

  8. 浅析Windows域环境身份认证与攻击思路

    文章目录 前言 Kerberos协议 第1步-AS认证获取TGT 第2步-TGS认证获取ST 第3步-服务端服务认证 NTLM 认证 本地认证模式 网络认证模式 内网横向渗透 哈希凭证窃取 内网远程连 ...

  9. 分布式身份认证——未来信任生态的基石

    一年前,微软在其官方博客中宣布,将在旗下微软身份验证(Microsoft Authenticator)应用程序内整合基于区块链的去中心化 ID 验证技术,标志着微软正式开始进入DID(分布式身份验证) ...

最新文章

  1. UI设计培训:UI设计师离不开的基本版式设计
  2. linux内核线程创建销毁机制
  3. 如何使用Maven scope
  4. SAP智能机器人流程自动化解决方案
  5. rvm RuvyGem Cocoapods brew
  6. (43)FPGA状态机三段式
  7. sed在行首(行尾)添加字符串;在某行后添加多行字符串-转
  8. 软件测试中的风控,测试过程中会遇到哪些风险和机会?
  9. 全网通是4g显示无服务器,4G+时代的全网通?可没有那么简单!
  10. f2fs系列之五:冷热数据分离
  11. AngularJS------Error: Cannot find module '@angular-devkit/core'
  12. jdk动态代理的实现流程(事务处理)
  13. 基于SSM小说阅读网站设计带爬虫功能
  14. android wifi已停用,为什么手机连接wifi时总显示已停用
  15. 命令行录制工具 asciinema
  16. 2019年CSDN博客排名前10名
  17. python做一个大鱼吃小鱼_Python精灵模块制作的大鱼吃小鱼游戏
  18. GUI编程入门到游戏实战
  19. 国家住宅装饰装修工程施工规范
  20. js如何截取某个字符前面所有的字符串

热门文章

  1. linux命令stat和top,用top,du,df,ls,free,tree,stat命令更人性化显示数据
  2. 我的创作一周年纪念日
  3. Word中添加图表目录
  4. QTP简单框架(5)之脚本编写规范
  5. 中国移动原董事长王建宙:运营商共建共享网络是未来趋势,5G手机降价是必然...
  6. Value Results,Not Just Effort
  7. Android游戏接入——华为篇
  8. 弘辽科技:做京东还不会开快车?教你怎么开快车把流量拉起飞
  9. zzulioj 1205: 你爱我么?
  10. IDEA中一个项目如何导入另一个依赖的项目?