Swift toolkit that lets you communicate efficiently with many of the Auth0 API functions and enables you to seamlessly integrate the Auth0 login.
- iOS 9 or later
- Xcode 8
- Swift 3.0
If you are using Carthage, add the following lines to your Cartfile
:
pod "Auth0", '~> 1.6'
Then run carthage bootstrap
.
For more information about Carthage usage, check their official documentation.
If you are using Cocoapods, add these lines to your Podfile
:
use_frameworks!
pod 'Auth0', '~> 1.6'
Then, run pod install
.
For further reference on Cocoapods, check their official documentation.
- Import Auth0 into your project.
import Auth0
- Present the hosted login page.
Auth0
.webAuth()
.start { result in
switch result {
case .success(let credentials):
print("Obtained credentials: \(credentials)")
case .failure(let error):
print("Failed with \(error)")
}
}
- Allow Auth0 to handle authentication callbacks. In your
AppDelegate.swift
add the following:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
return Auth0.resumeAuth(url, options: options)
}
In order to use Auth0 you need to provide your Auth0 ClientId and Domain.
Auth0 ClientId & Domain can be found in your Auth0 Dashboard
In your application bundle add a plist
file named Auth0.plist
with the following information.
<?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>
Callback URLs are the URLs that Auth0 invokes after the authentication process. Auth0 routes your application back to this URL and appends additional parameters to it, including a token. Since callback URLs can be manipulated, you will need to add your application's URL to your client's Allowed Callback URLs for security. This will enable Auth0 to recognize these URLs as valid. If omitted, authentication will not be successful.
In your application's Info.plist
file, register your iOS Bundle Identifer as a custom scheme:
<!-- Info.plist -->
<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>
Finally, go to your Auth0 Dashboard and make sure that Allowed Callback URLs contains the following:
{YOUR_BUNDLE_IDENTIFIER}://${YOUR_DOMAIN}/ios/{YOUR_BUNDLE_IDENTIFIER}/callback
Check out the iOS Swift QuickStart Guide to find out more about the Auth0.swift toolkit and explore our tutorials and sample projects.
Auth0
.authentication()
.userInfo(token: accessToken)
.start { result in
switch result {
case .success(let profile):
print("User Profile: \(profile)")
case .failure(let error):
print("Failed with \(error)")
}
}
Renewal of credentials can be achieved using a Refresh Token, it's recommended that you read and understand the refresh token process before implementing.
Auth0
.authentication()
.renew(withRefreshToken: refreshToken)
.start { result in
switch(result) {
case .success(let credentials):
print("Obtained new credentials: \(credentials)")
case .failure(let error):
print("Failed with \(error)")
}
}
let credentialsManager = CredentialsManager(authentication: Auth0.authentication())
Store user credentials securely in the KeyChain.
credentialsManager.store(credentials: credentials)
credentialsManager.credentials { error, credentials in
guard error == nil else { return print("Failed with \(error)") }
print("Obtained credentials: \(credentials)")
}
The Authentication API exposes the identity functionality of Auth0, as well as the supported identity protocols like OpenID Connect, OAuth 2.0, and SAML.
Most users consume this API through our Quickstarts, the hosted login or the Lock widget. However, if you are building all of your authentication UI manually you can interact with this API directly.
Auth0
.authentication()
.login(
usernameOrEmail: "support@auth0.com",
password: "secret-password",
realm: "Username-Password-Authentication",
scope: "openid")
.start { result in
switch result {
case .success(let credentials):
print("Obtained credentials: \(credentials)")
case .failure(let error):
print("Failed with \(error)")
}
}
Auth0
.authentication()
.createUser(
email: "support@auth0.com",
password: "secret-password",
connection: "Username-Password-Authentication",
userMetadata: ["first_name": "First",
"last_name": "Last"]
)
.start { result in
switch result {
case .success(let user):
print("User Signed up: \(user)")
case .failure(let error):
print("Failed with \(error)")
}
}
Passwordless connections in Auth0 allow users to login without the need to remember a password. This is a two step process:
- Request a OTP to be sent to the user by email or SMS
- Perform the login using the OTP
Auth0
.authentication()
.startPasswordless(
email: "support@auth0.com",
type: .Code,
connection: "email",
parameters: [:])
.start { result in
switch result {
case .success:
print("OTP Sent")
case .failure(let error):
print("Failed with \(error)")
}
}
Auth0
.authentication()
.login(
usernameOrEmail: "support@auth0.com",
password: "OTP Code",
realm: "email",
scope: "openid")
.start { result in
switch result {
case .success(let credentials):
print("Obtained credentials: \(credentials)")
case .failure(let error):
print("Failed with \(error)")
}
}
Auth0
.authentication()
.startPasswordless(
phoneNumber: "01234567890"
type: .Code,
connection: "sms",
parameters: [:])
.start { result in
switch result {
case .success:
print("OTP Sent")
case .failure(let error):
print("Failed with \(error)")
}
}
Auth0
.authentication()
.login(
usernameOrEmail: "01234567890",
password: "OTP Code",
realm: "sms",
scope: "openid")
.start { result in
switch result {
case .success(let credentials):
print("Obtained credentials: \(credentials)")
case .failure(let error):
print("Failed with \(error)")
}
}
Auth0
.users(token: idToken)
.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("Failed with \(error)")
}
}
Auth0
.users(token: idToken)
.link("user identifier", withOtherUserToken: "another user token")
.start { result in
switch result {
case .success(let userInfo):
print("User: \(userInfo)")
case .failure(let error):
print("Failed with \(error)")
}
}
To enable Auth0.swift to log HTTP request and OAuth2 flow for debugging you can call the following method in either WebAuth
, Authentication
or Users
object:
var auth0 = Auth0.authentication()
auth0.logging(enabled: true)
Then for a OAuth2 authentication you'll see something similar to the following:
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.
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.
- Go to Auth0 and click Sign Up.
- Use Google, GitHub or Microsoft Account to login.
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.
This project is licensed under the MIT license. See the LICENSE file for more info.