CocOAuth 0.1.7

CocOAuth 0.1.7

Marko Seifert 维护。



CocOAuth 0.1.7

  • 开源项目:CocOAuth

CocOAuth_Logo_262x2632

CocOAuth

CocOAuth 是用 Swift 4.0 编写的 iOS 上 OAuth2 框架。

Build Status Badge w/ Platform Swift 4.0

  • 安全性
  • 企业级
  • 易于使用

用法

集成

  • CocoaPods
pod 'CocOAuth', '~> 0.1.4'

主要概念:认证器

认证器封装了一个OAuth2身份。这意味着在某个租户的上下文中一个用户或设备。您可以创建任意数量的认证器。我们在身份卡应用中使用此功能。每个身份卡都有一个自己的认证器,其在共享平台上有自己的OAuth2服务器或租户。

Authenticator Big Picture

配置

在使用CocOAuth库进行任何认证请求之前,您需要对其配置。

let config = OAuth2Config(
  tokenURL: URL(string: <YOUR OAuth2 Identity Provider Token URL>)!, 
  clientID: <YOUR_CLIENT_ID>, 
  clientSecret: <YOUR_CLIENT_SECRET>)

let authenticator = Authenticator(config: config)

高级配置

  OAuth2Config(
      ...
      additionalHeader: <YOUR_ADDITIONAL_HEADER>, 
      scopes: <YOUR_SCOPES_>, 
      timeout: <TIMEOUT>)

在我们的情况下,我们将tenantId传输到附加的头部信息。

身份验证

身份验证API提供以下服务

  • authenticateWithClientCredentials
  • authenticateWithUsername
  if let username = username.text, let password = password.text {
    authenticator.authenticateWithUsername(username, password: password) {success, error in
      if(success){
        self.message.text = "success"
      }else{
        if let err = error{
          self.message.text = err.localizedDescription
        }
      }
    }
  }

在“authenticateWithClientCredentials”的情况下,客户端凭据将被存储在凭据存储中,如果令牌过期,将自动重用。在其他情况下(authenticateWithUsername),除了OAuth2刷新令牌也将被存储和重用。

检索访问令牌

此方法返回OAuth2访问令牌。令牌如果过期,将自动续订。

  authenticator.retrieveAccessToken(handler: { (token, error) in
    if let accessToken = token{
      let token = accessToken
    }else if let e = error{
      self.message.text = e.localizedDescription
    }
  })

持久性

内置的凭据存储是一个简单的内存存储。您可以实施和配置自己的存储。例如,使用iOS密钥链来存储凭据。

  • 实现凭据存储协议
  public protocol CredentialsStore{
    
    func storeCredentials(_ credentials:Credentials)
    func loadCredentials() -> Credentials?
    
  }
  
  import CocOAuth
  import KeychainAccess

  class KeychainCredentialsStore: CredentialsStore {
    
      var keychain: Keychain!
    
      init(storeName: String, userAuth: Bool=true ) {
          keychain = Keychain(service: storeName).accessibility(.whenUnlocked)
          if userAuth {
              keychain = self.keychain.accessibility(.whenPasscodeSetThisDeviceOnly, authenticationPolicy: .userPresence).authenticationPrompt("Authenticate to update your access token")
          }   
      }
      ...
  }
  • 在配置中设置自己的存储
  config.credentialsStore = KeychainCredentialsStore(storeName: tenantID, userAuth: userAuth)

错误处理

OAuth 2特定错误类型 https://tools.ietf.org/html/rfc6749#section-5.2

错误 描述 映射
invalid_request 请求缺少所需参数,包含不支持参数值(除授权类型外),重复参数,包含多个凭据,使用多个机制进行客户端认证,或格式不正确。
invalid_client 客户端认证失败(例如,未知客户端,未包含客户端认证,或不支持认证方法)。授权服务器可以返回HTTP 401(未授权)状态码来指示哪些HTTP认证方案是受支持的。如果客户端尝试通过“Authorization”请求头字段进行认证,则授权服务器必须以HTTP 401(未授权)状态码响应,并包含与客户端使用的认证方案匹配的“WWW-Authenticate”响应头字段。
invalid_callback 客户端从服务器发送的重定向URI无效。
invalid_grant 提供的授权授予(例如,授权代码、资源所有者凭据)或刷新令牌无效、过期、已吊销、不匹配授权请求中使用的重定向URI,或颁发给另一个客户端。
unauthorized_client 已认证的客户端未授权使用此授权授予类型。
unsupported_grant_type 授权服务器不支持当前授权类型。
invalid_scope 请求的作用域无效、未知、格式错误,或者超出了资源拥有者授予的作用域。
access_denied 资源拥有者或授权服务器拒绝了请求。
unsupported_response_type 授权服务器不支持使用此方法来获取授权码。
server_error 授权服务器遇到了意外的条件,导致无法完成请求。(此错误代码需要,因为500内部服务器错误HTTP状态码不能通过HTTP重定向返回给客户端。)
temporarily_unavailable 授权服务器因暂时过载或维护而目前无法处理请求。(此错误代码需要,因为503服务不可用HTTP状态码不能通过HTTP重定向返回给客户端。)

安全注意事项

许可证

此代码置于Apache 2.0许可证之下。