神刀安全网

[iOS] SwiftyOAuth:极为小巧、易用的 OAuth 授权类库

[iOS] SwiftyOAuth:极为小巧、易用的 OAuth 授权类库

[iOS] SwiftyOAuth:极为小巧、易用的 OAuth 授权类库 [iOS] SwiftyOAuth:极为小巧、易用的 OAuth 授权类库 [iOS] SwiftyOAuth:极为小巧、易用的 OAuth 授权类库

SwiftyOAuthis a small OAuth library with a built-in set of providers and a nice API to add your owns.

let github: Provider = .GitHub(clientID: "***", clientSecret: "***", redirectURL: "foo://callback")  github.authorize { result in     print(result) // Success(Token(accessToken: "abc123", tokenType: "bearer", scope: "")) }

Usage•References•Installation•License

Usage

Provider

Initialize a provider with the custom URL scheme that you defined:

let provider = Provider(     clientID: "***",     clientSecret: "***",     authorizeURL: "https://example.com/authorize",     tokenURL: "https://example.com/authorize/token",     redirectURL: "foo://callback" )

Alternatively, you can use a built-in provider:

let github = .GitHub(     clientID: "***",     clientSecret: "***",     redirectURL: "foo://callback" )

Optionally set the state and scope properties:

github.state = "asdfjkl;" // An random string used to protect against CSRF attacks. github.scope = "public_repo"

Define additional parameters for the authorization request or the token request with additionalParamsForAuthorization and additionalParamsForTokenRequest respectively:

github.additionalParamsForAuthorization["allow_signup"] = false

Handle the incoming requests in your AppDelegate :

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {     github.handleURL(url, options: options)      return true }

Finally, ask for authorization. SwiftyOAuth will either present a SFSafariViewController (iOS 9) or open mobile safari.

github.authorize { (result: Result<Token, Error>) -> Void in     switch result {     case .Success(let token):         print(token)     case .Failure(let error):         print(error)     }  }

Token

The access_token , token_type and scope are available as Token properties:

token.accessToken // abc123 token.tokenType // bearer token.scope // public_repo

Additionally, you can access all the response data via the dictionary property:

token.dictionary // ["access_token": "abc123, "token_type": "bearer", "scope": "public_repo"]

Error

Erroris a enum that conforms to the ErrorType protocol.

  • Cancel The user cancelled the authorization process by closing the web browser window.

  • ApplicationSuspended The OAuth application you set up has been suspended.

  • RedirectURIMismatch The provided redirectURL that doesn’t match what you’ve registered with your application.

  • AccessDenied The user rejects access to your application.

  • IncorrectClientCredentials The clientID and or clientSecret you passed are incorrect.

  • BadVerificationCode The verification code you passed is incorrect, expired, or doesn’t match what you received in the first request for authorization.

  • Other The application emitted a response in the form of {"error": "xxx", "error_description": "yyy"} but SwiftyOAuth doesn’t have a enum for it. The data is available in the associated values.

  • Unknown The application emitted a response that is neither in the form of a success one ( {"access_token": "xxx"...} ) nor in the form of a failure one ( {"error": "xxx"...} ). The data is available in the associated value.

  • NSError An error triggered when making network requests or parsing JSON. The data is available in the associated value.

Providers

  • GitHub -code, doc
  • Dribbble -code, doc
  • More to come…

Roadmap

  • Store the token in the Keychain
  • Support for Client Flow
  • Refresh token (when available)
  • More providers

References

Token

Provider access_token token_type scope
GitHub yes yes yes
Dribbble yes yes yes

Parameters

Authorize request params

Provider client_id redirect_uri scope state Additional parameters
GitHub required optional optional optional allow_signup
Dribbble required optional optional optional

Token request params

Provider code client_id client_secret redirect_uri state
GitHub required required required optional optional
Dribbble required required required optional

Errors

Authorize request errors

Provider .ApplicationSuspended .RedirectURIMismatch .AccessDenied
GitHub application_suspended redirect_uri_mismatch access_denied
Dribbble application_suspended redirect_uri_mismatch access_denied

Token request errors

Provider .IncorrectClientCredentials .RedirectURIMismatch .BadVerificationCode
GitHub incorrect_client_credentials redirect_uri_mismatch bad_verification_code
Dribbble invalid_client invalid_grant invalid_grant

Installation

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update $ brew install carthage

To integrate SwiftyOAuth into your Xcode project using Carthage, specify it in your Cartfile :

github "delba/SwiftyOAuth" >= 0.1 

CocoaPods

CocoaPods is a dependency manager for Cocoa projects.

You can install it with the following command:

$ gem install cocoapods

To integrate SwiftyOAuth into your Xcode project using CocoaPods, specify it in your Podfile :

use_frameworks!  pod 'SwiftyOAuth', '~> 0.1'

License

Copyright (c) 2016 Damien ( http://delba.io )

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » [iOS] SwiftyOAuth:极为小巧、易用的 OAuth 授权类库

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址