DreamFactory 2.0 supports authentication using OAuth services. Developers can now allow end users to login with credentials from sites like Facebook, Twitter (yes, OAuth 1.0 is supported!), Google, and GitHub.
DreamFactory 2.0 makes it super easy to add OAuth services to your apps without any server-side coding or token handling. The currently supported OAuth service providers are Facebook, Twitter, Google, and GitHub. We’ll be adding more OAuth providers in the future.
There are different ways OAuth can be implemented, depending on the type of applications utilizing it. There are four main “grant types” for OAuth: authorization code grant, implicit grant, resource owner credentials grant, and client credentials grant. DreamFactory 2.0 uses the “Authorization Code” grant type. This is the type that is commonly used for web-based applications. You can find out more about the grant types on Alex Bilbie's blog and Aaron Parecki's blog.
Here is a quick workflow of Authorization code grant type.
In the figure above:
OAuth in DreamFactory 2.0 works just like other services in DreamFactory. It starts with provisioning an OAuth service using the Services tab in the DreamFactory 2.0 admin console. While provisioning the service you simply specify your OAuth service details such as key, secret, redirect url etc., as well as a default role. This role will be used for any users authenticating using your OAuth service. You can easily create a role in a DreamFactory 2.0 instance using the admin console.
Here is a short how-to guide on setting up an OAuth service with DreamFactory 2.0. We will start with setting up a role for our OAuth service.
The ‘facebook’ OAuth service that we just created is now ready for your users to log into your app with their Facebook credentials. Users simply click on the ‘Login with Facebook’ button on your app. This should trigger the following POST call to your DreamFactory instance.
curl -i -k -X POST https://example.com/api/v2/user/session -d {“service”:”facebook”}
Or alternatively…
curl -i -k -X POST https://example.com/api/v2/user/session?service=facebook
If all goes well your app will get a response back that looks like below.
{
"response": {
"redirect": true,
"url": "https://www.facebook.com/v2.4/dialog/oauth?client_id=15..."
}
}
The response includes a URL that redirects your users to the Facebook login page. Your app needs to handle this redirect in order to present the Facebook login page to your users. Once the user authenticates and authorizes access to their Facebook resources (currently just user information) the provider will redirect the user back to the app using the Redirect URL with the ‘Authorization Code’ as part of URL query string. Your app will need to extract this entire query string and make the following POST call to get a JWT (JSON Web Token) and successfully log into your DreamFactory instance.
curl -i -k -X POST https://example.com/api/v2/user/session?oauth_callback=true&<query_string_from_callback>
Your DreamFactory instance will then make two requests to the OAuth provider. First, it will use the Authorization Code (from the query string) to request the Access Token. Second, it will use the Access Token to make a request to fetch the basic user information. DreamFactory 2.0 will create a shadow user (if not already existing) using the user information from the OAuth provider’s site. Once the user is there it will then create a JWT for this user and establish an internal session and respond back with the following data.
{
"session_token": “abc.123abc.efg”,
"session_id": “abc.123abc.efg”,
"id": 1,
"name": "John",
"first_name": "John",
"last_name": "Doe",
"email": "jdoe@gmail.com",
"is_sys_admin": false,
"last_login_date": "2015-06-30 16:46:59",
"host": "example.com"
}
The app needs to save the session_token from this response locally. This is the JWT. The app will need to use this for every request it makes to your DreamFactory instance. Your app can pass this JWT in three different ways:
DreamFactory 2.0 also includes all active OAuth services in the system environment data. You can get the system environment data using a simple GET call …
curl -i -k -X GET https://example.com/api/v2/system/environment
The response from this call includes system authentication information, which shows the APIs and services available in the system for logging in. This will help you create a dynamic login UI based on the services available in the system. Here is a sample response from ‘system/environment’ call showing the OAuth services data.
{
…..
"authentication": {
…..
"oauth": [
{
"path": "user/session?service=facebook",
"name": "facebook",
"label": "Facebook OAuth",
"verb": [
"GET",
"POST"
],
"type": "oauth_facebook",
"icon_class": "fa-facebook"
}
]
},
}
With widespread adoption of OAuth as an authorization framework, DreamFactory's OAuth integration enables rapid and seamless connections to a wide variety of third party services.
Read about when we released DreamFactory 2.0!
https://blog.dreamfactory.com/dreamfactory-2-now-available/