Skip to content

volbap/Auth0.swift

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Auth0.swift

CI Status Coverage Status Version License Platform Carthage compatible

Swift toolkit for Auth0 API

Requirements

iOS 9+ and Xcode 7.3 (Swift 2.2)

Installation

###CocoaPods

Auth0.swift is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Auth0", '~> 1.0.0-beta.2'

###Carthage

In your Cartfile add this line

github "auth0/Auth0.swift"

Usage

Auth0.plist

To avoid specifying clientId & domain you can add a Auth0.plist file to your main bundle. Here is an example of the file contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>ClientId</key>
   <string>{YOUR_CLIENT_ID}</string>
   <key>Domain</key>
   <string>{YOUR_DOMAIN}</string>
</dict>
</plist>

This will load clientId & domain in authentication API & OAuth2 methods, and only domain for management API methods.

Authentication API

Login with database connection

Auth0
   .authentication()
   .login(
       "support@auth0.com", 
       password: "a secret password", 
       connection: "Username-Password-Authentication"
       )
   .start { result in
       switch result {
       case .Success(let credentials):
           print("access_token: \(credentials.accessToken)")
       case .Failure(let error):
           print(error)
       }
   }

Passwordless Login

Auth0
   .authentication()
   .startPasswordless(email: "support@auth0.com", connection: "email")
   .start { result in
       switch result {
       case .Success:
           print("Sent OTP to support@auth0.com!")
       case .Failure(let error):
           print(error)
       }
   }
Auth0
   .authentication()
   .login(
       "support@auth0.com", 
       password: "email OTP", 
       connection: "email"
       )
   .start { result in
       switch result {
       case .Success(let credentials):
           print("access_token: \(credentials.accessToken)")
       case .Failure(let error):
           print(error)
       }
   }

Sign Up with database connection

Auth0
   .authentication()
   .signUp(
       "support@auth0.com", 
       password: "a secret password", 
       connection: "Username-Password-Authentication"
       )
   .start { result in
       switch result {
       case .Success(let credentials):
           print("access_token: \(credentials.accessToken)")
       case .Failure(let error):
           print(error)
       }
   }

Get user information

Auth0
   .authentication()
   .tokenInfo("user id_token")
   .start { result in
       switch result {
       case .Success(let profile):
           print("profile email: \(profile.email)")
       case .Failure(let error):
           print(error)
       }
   }

Management API (Users)

Update user_metadata

Auth0
    .users(token: "user token")
    .patch("user identifier", userMetadata: ["first_name": "John", "last_name": "Doe"])
    .start { result in
        switch result {
        case .Success(let userInfo):
            print("user: \(userInfo)")
        case .Failure(let error):
            print(error)
        }
    }

Link users

Auth0
   .users(token: "user token")
   .link(userId, withOtherUserToken: "another user token")
   .start { result in
      switch result {
      case .Success(let userInfo):
         print("user: \(userInfo)")
      case .Failure(let error):
         print(error)
      }
   }

Web-based Auth (iOS Only)

First go to Auth0 Dashboard and go to application's settings. Make sure you have in Allowed Callback URLs a URL with the following format:

{YOUR_BUNDLE_IDENTIFIER}://{YOUR_AUTH0_DOMAIN}/ios/{YOUR_BUNDLE_IDENTIFIER}/callback

In your application's Info.plist file register your iOS Bundle Identifier as a custom scheme like this:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>None</string>
        <key>CFBundleURLName</key>
        <string>auth0</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>{YOUR_BUNDLE_IDENTIFIER}</string>
        </array>
    </dict>
</array>

Auth0.swift will only handle URLs with your Auth0 domain as host, e.g. com.auth0.MyApp://samples.auth0.com/ios/com.auth0.MyApp/callback

and add the following method in your application's AppDelegate

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

Authenticate with any Auth0 connection

Auth0
    .webAuth()
    .connection("facebook")
    .start { result in
        switch result {
        case .Success(let credentials):
            print("credentials: \(credentials)")
        case .Failure(let error):
            print(error)
        }
    }

Specify scope

Auth0
    .webAuth()
    .scope("openid email")
    .connection("google-oauth2")
    .start { result in
        switch result {
        case .Success(let credentials):
            print("credentials: \(credentials)")
        case .Failure(let error):
            print(error)
        }
    }

Authenticate with Auth0 hosted login page

Auth0
    .webAuth()
    .start { result in
        switch result {
        case .Success(let credentials):
            print("credentials: \(credentials)")
        case .Failure(let error):
            print(error)
        }
    }

Logging

To enable Auth0.swift to log HTTP request and OAuth2 flow for debugging just add the following:

Auth0.enableLogging()

Then for a OAuth2 authentication you'll see in the console:

Safari: https://samples.auth0.com/authorize?.....
URL: com.auth0.myapp://samples.auth0.com/ios/com.auth0.MyApp/callback?...
POST https://samples.auth0.com/oauth/token HTTP/1.1
Content-Type: application/json

{"code":"...","client_id":"...","grant_type":"authorization_code","redirect_uri":"com.auth0.MyApp:\/\/samples.auth0.com\/ios\/com.auth0.MyApp\/callback","code_verifier":"..."}

HTTP/1.1 200
Pragma: no-cache
Content-Type: application/json
Strict-Transport-Security: max-age=3600
Date: Thu, 09 Jun 2016 19:04:39 GMT
Content-Length: 57
Cache-Control: no-cache
Connection: keep-alive

{"access_token":"...","token_type":"Bearer"}

Only set this flag for DEBUG only or you'll be leaking user's credentials in the device log.

What is Auth0?

Auth0 helps you to:

  • Add authentication with multiple authentication sources, either social like Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, amont others, or enterprise identity systems like Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider.
  • Add authentication through more traditional username/password databases.
  • Add support for linking different user accounts with the same user.
  • Support for generating signed Json Web Tokens to call your APIs and flow the user identity securely.
  • Analytics of how, when and where users are logging in.
  • Pull data from other sources and add it to the user profile, through JavaScript rules.

Create a free Auth0 Account

  1. Go to Auth0 and click Sign Up.
  2. Use Google, GitHub or Microsoft Account to login.

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.

Author

Auth0

License

This project is licensed under the MIT license. See the LICENSE file for more info.

About

Swift toolkit for Auth0 API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 85.8%
  • Objective-C 7.9%
  • Shell 5.1%
  • Ruby 1.2%