An Introduction To OAuth 2
An Introduction To OAuth 2
NEW App Platform: reimagining PaaS to make it simpler for you to build, deploy, and scale apps.
TUTORIAL
An Introduction to OAuth 2
Security API Conceptual
By Mitchell Anicas
Published on July 21, 2014 English
1.8m
Introduction
OAuth 2 is an authorization framework that enables applications to obtain limited access to
user accounts on an HTTP service, such as Facebook, GitHub, and DigitalOcean. It works by
delegating user authentication to the service that hosts the user account, and authorizing
third-party applications to access the user account. OAuth 2 provides authorization flows for
web and desktop applications, and mobile devices.
OAuth Roles
OAuth defines four roles:
Resource Owner
Client
Resource Server
Authorization Server
SCROLL TO TOP
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 1/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
From an application developer’s point of view, a service’s API fulfills both the resource and
authorization server roles. We will refer to both of these roles combined, as the Service or
API role.
Client: Application
The client is the application that wants to access the user’s account. Before it may do so, it
must be authorized by the user, and the authorization must be validated by the API.
SCROLL TO TOP
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 2/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
1. The application requests authorization to access service resources from the user
2. If the user authorized the request, the application receives an authorization grant
3. The application requests an access token from the authorization server (API) by presenting
authentication of its own identity, and the authorization grant
4. If the application identity is authenticated and the authorization grant is valid, the
authorization server (API) issues an access token to the application. Authorization is
complete.
5. The application requests the resource from the resource server (API) and presents the
access token for authentication
6. If the access token is valid, the resource server (API) serves the resource to the application
The actual flow of this process will differ depending on the authorization grant type in use,
but this is the general idea. We will explore different grant types in a later section.
Application Registration
SCROLL TO TOP
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 3/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
Before using OAuth with your application, you must register your application with the
service. This is done through a registration form in the “developer” or “API” portion of the
service’s website, where you will provide the following information (and probably details
about your application):
Application Name
Application Website
The redirect URI is where the service will redirect the user after they authorize (or deny) your
application, and therefore the part of your application that will handle authorization codes or
access tokens.
Authorization Grant
In the Abstract Protocol Flow above, the first four steps cover obtaining an authorization
grant and access token. The authorization grant type depends on the method used by the
application to request authorization, and the grant types supported by the API. OAuth 2
defines four grant types, each of which is useful in different cases:
Implicit: used with Mobile Apps or Web Applications (applications that run on the user’s
device)
Resource Owner Password Credentials: used with trusted Applications, such as those
owned by the service itself
SCROLL TO TOP
Client Credentials: used with Applications API access
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 4/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
Now we will describe grant types in more detail, their use cases and flows, in the following
sections.
SCROLL TO TOP
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 5/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
client_id= client_id : the application’s client ID (how the API identifies the application)
scope= read : specifies the level of access that the application is requesting
This particular screenshot is of DigitalOcean’s authorization screen, and we can see that
“Thedropletbook App” is requesting authorization for “read” access to the account of
“[email protected]”.
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 6/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
authorization code. The redirect would look something like this (assuming the application is
“dropletbook.com”):
https://dropletbook.com/callback?code= AUTHORIZATION_CODE
Now the application is authorized! It may use the token to access the user’s account via the
service API, limited to the scope of access, until the token expires or is revoked. If a refresh
token was issued, it may be used to request new access tokens if the original token has
expired.
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 7/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
user’s device. Also, this flow does not authenticate the identity of the application, and relies
on the redirect URI (that was registered with the service) to serve this purpose.
The implicit grant flow basically works as follows: the user is asked to authorize the
application, then the authorization server passes the access token back to the user-agent,
which passes it to the application. If you are curious about the details, read on.
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 8/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
When the user clicks the link, they must first log in to the service, to authenticate their
identity (unless they are already logged in). Then they will be prompted by the service to
authorize or deny the application access to their account. Here is an example authorize
application prompt:
We can see that “Thedropletbook App” is requesting authorization for “read” access to the
account of “[email protected]”.
https://dropletbook.com/callback#token= ACCESS_TOKEN
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 9/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
The user-agent executes the provided script and passes the extracted access token to the
application.
Now the application is authorized! It may use the token to access the user’s account via the
service API, limited to the scope of access, until the token expires or is revoked.
If the user credentials check out, the authorization server returns an access token to the
application. Now the application is authorized!
Note: DigitalOcean does not currently support the password credentials grant type, so the
link points to an imaginary authorization server at “oauth.example.com”.
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 10/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
https://oauth.example.com/token?grant_type=client_credentials&client_id=CLIENT_ID&client_secre
If the application credentials check out, the authorization server returns an access token to
the application. Now the application is authorized to use its own account!
Note: DigitalOcean does not currently support the client credentials grant type, so the link
points to an imaginary authorization server at “oauth.example.com”.
Here is an example of an API request, using curl . Note that it includes the access token:
Assuming the access token is valid, the API will process the request according to its API
specifications. If the access token is expired or otherwise invalid, the API will return an
“invalid_request” error.
Conclusion
That concludes this OAuth 2 guide. You should now have a good idea of how OAuth 2
works, and when a particular authorization flow should be used.
If you want to learn more about OAuth 2, check out these valuable resources:
Report an issue
Mitchell Anicas
Software Engineer @ DigitalOcean.
Former Señor Technical Writer (I no
longer update articles or respond to
comments).
SCROLL TO TOP
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 12/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
RELATED
How To Create an Intelligent Chatbot in Python Using the spaCy NLP Library
Tutorial
Comments
38 Comments
Leave a comment...
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 13/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
5 Nice Job.
Reply Report
1 Which flow is most suitable for system to system communication using REST APIs?
Reply Report
1
I agree. Confused.
Reply Report
Reply Report
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 14/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
3 @author @manicas Why are you sending sensitive data as Query parameters (in URL), even
though it isn’t recommended by the OAuth2 specification itself ?
See the last point.
<^>
Don’t pass bearer tokens in page URLs: Bearer tokens SHOULD NOT be
passed in page URLs (for example as query string parameters).
Instead, bearer tokens SHOULD be passed in HTTP message headers or
message bodies for which confidentiality measures are taken.
Browsers, web servers, and other software may not adequately
secure URLs in the browser history, web server logs, and other
data structures. If bearer tokens are passed in page URLs,
attackers might be able to steal them from the history data, logs,
or other unsecured locations.
<^>
Reply Report
1 Thank you guys. This tutorial really helped me understand how OAUTH works. I have a little
question though I will like to ask what are the steps or how can I generate a signature for my
OAUTH requests as I have read that requests without signature may not be so secured.
Thanks.
Reply Report
1 Very good explanation of the concepts. The way content is ordered helped me getting first
about the OAuth 2.0 and then each grant type helped me to understand the required grant
typed for my project.
Thanks.
Reply Report
1 Good job
Reply Report
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 15/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
1 In general, this tutorial is pretty good in contents, technical level and formatting. In the
beginning, I really liked it and I wanted to finish it. But the more I read, the more I got confused.
For example, these terms “application”, “client”, “user” should be explained in more details. In
some context, “application” actually meant to be “client”. In step 2 of Authorization Code
section, a “user” actually means “client” instead of resource owner. In other context, “user”
means resource owner. Thanks a lot for the work!
Reply Report
1 I really love this explanation, concise, clear, and complete. I’ve printed this out and put into a
folder called “oauth bible”. Thanks for the great job!
Reply Report
0 good work
Reply Report
1 Should client_secret not be supplied along with client_id, username and password in case of
resource owner password credential grant ?
Reply Report
0 Excellent article.
Example URLs does not show state parameters. Might be good to mention this so that readers
are aware of it and developers make use of it to protect against CSRF attacks
Reply Report
SCROLL TO TOP
Load More Comments
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 16/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 17/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
BECOME A CONTRIBUTOR
DigitalOcean Products Virtual Machines Managed Databases Managed Kubernetes Block Storage
Object Storage Marketplace VPC Load Balancers
Learn More
SCROLL TO TOP
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 18/19
1/28/2021 An Introduction to OAuth 2 | DigitalOcean
Company
About
Leadership
© 2021 DigitalOcean, LLC. All rights reserved.
Blog
Careers
Partners
Referral Program
Press
Legal
Security & Trust Center
SCROLL TO TOP
https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2 19/19