用最简单的方法实现 Facebook 和 Google 授权登录

4,558 阅读2分钟
原文链接: github.com

Simplicity

Version
License
Platform codebeat badge

Simplicity is a simple way to implement Facebook and Google login in your iOS and OS X apps. Written by Edward Jiang at Stormpath.

Simplicity can be easily extended to support other external login providers, including OAuth2, OpenID, SAML, and other custom protocols, and will support more in the future. We always appreciate pull requests!

Why use Simplicity?

Facebook and Googles SDKs are heavyweight, and take time to set up and use. You can use Simplicity and only have to manage one SDK for logging in with an external provider in your app. Simplicity adds just 200KB to your apps binary, compared to 5.4MB when using the Facebook & Google SDKs.

Logging in with Simplicity is as easy as:

Simplicity.login(Facebook()) { (accessToken, error) in
  // Handle access token here
}

Stormpath

Simplicity is maintained by Stormpath, an API service for authentication, authorization, and user management. If youre building a backend API for your app, consider using Stormpath to help you implement a secure REST API. Read our tutorial on how to build a REST API for your mobile apps using Node.js.

Installation

Requires XCode 7.3+ / Swift 2.3+

To install Simplicity, we use CocoaPods. To install it, simply add the following line to your Podfile:

pod Simplicity

Carthage

To use Simplicity with Carthage, specify it in your Cartfile:

github SimplicityMobile/Simplicity

Add the link handlers to the AppDelegate

When a user finishes their log in flow, Facebook or Google will redirect back into the app. Simplicity will listen for the access token or error. You need to add the following lines of code to AppDelegate.swift:

import Simplicity

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

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
  return Simplicity.application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

Usage

Simplicity is very flexible and supports a number of configuration options for your login providers. To view, please see the full API docs on CocoaDocs.

Using Facebook Login

To get started, you first need to register an application with Facebook. After registering your app, go into your app dashboards settings page. Click Add Platform, and fill in your Bundle ID, and turn Single Sign On on.

Finally, open up your Apps Xcode project and go to the projects info tab. Under URL Types, add a new entry, and in the URL schemes form field, type in fb[APP_ID_HERE], replacing [APP_ID_HERE] with your Facebook App ID.

Then, you can initiate the login screen by calling:

Simplicity.login(Facebook()) { (accessToken, error) in
  // Handle access token here
}

Using Google Login

To get started, you first need to register an application with Google. Click Enable and Manage APIs, and then the credentials tab. Create an OAuth Client ID for iOS.

Next, open up your Apps Xcode project and go to the projects info tab. Under URL Types, add a new entry, and in the URL schemes form field, type in your Google iOS Clients iOS URL scheme from the Google Developer Console.

Then, you can initiate the login screen by calling:

Simplicity.login(Google()) { (accessToken, error) in
  // Handle access token here
}

Requesting Scopes

If you need custom scopes, you can modify the Facebook or Google object to get them.

let facebook = Facebook()
facebook.scopes = [public_profile, email, user_friends]

Simplicity.login(facebook) { (accessToken, error) in
  // Handle access token here
}

Twitter, LinkedIn, and GitHub

We cant implement Twitter, GitHub, LinkedIn, Slack, or other login types because we cant do authorization_code grants without a client secret. Client secrets are fundamentally insecure on mobile clients, so we need to create a companion server to help with the authentication request.

If this is something youd like to see, please +1 or follow this GitHub Issue to create a companion server so I know that theres demand for this.

Other External Login Providers

Want another external login provider implemented? Please open a GitHub issue so I know its in demand, or consider contributing to this project!

Contributing

Please send a pull request with your new LoginProvider implemented. LoginProviders should try to autoconfigure where possible.

License

Simplicity is available under the Apache 2.0 license. See the LICENSE file for more info.