You get a bonus - 1 coin for daily activity. Now you have 1 coin

OAuth 1 and 2 Authorization: Concept and Examples

Lecture



OAuth 1.0 — is an authorization protocol that allows an application to get limited access to a user's resources on another service without transmitting the login and password.
It was used, for example, in the early APIs of Twitter and LinkedIn.

The main idea of OAuth1

The application receives an access token
instead of the user's login and password.

Below is the sequence of OAuth 1.0 operation.

OAuth 1.0 authorization sequence

1. Application registration

The developer registers the application with the provider.

The application receives:

  • Consumer Key (client identifier)
  • Consumer Secret (client secret)

This is the equivalent of client_id / client_secret.

2. Obtaining a Request Token

The application sends a request to the provider:

POST /oauth/request_token

The following are passed:

  • consumer_key
  • signature (HMAC-SHA1)
  • timestamp
  • nonce
  • callback_url

Response:

oauth_token=REQUEST_TOKEN
oauth_token_secret=REQUEST_SECRET

This token has not yet been authorized by the user.

3. Redirecting the user

The user is sent to the provider's authorization page.

 https://provider.com/oauth/authorize?oauth_token=REQUEST_TOKEN

The user:

  1. logs in
  2. confirms access

4. Callback to the application

After confirmation, the provider performs a redirect:

callback_url?oauth_token=REQUEST_TOKEN&oauth_verifier=VERIFIER

The application receives:

  • oauth_token
  • oauth_verifier

5. Exchanging the Request Token → Access Token

The application sends a request:

 POST /oauth/access_token

The following are passed:

  • consumer_key
  • request_token
  • oauth_verifier
  • signature

Response:

oauth_token=ACCESS_TOKEN
oauth_token_secret=ACCESS_SECRET

6. Accessing the API

Now the application can make requests to the API:

GET /user/profile
Authorization: OAuth ...

Each request is signed with:

  • consumer_secret
  • access_token_secret

The signature is usually:

HMAC-SHA1
Simplified diagram

OAuth 1 and 2 Authorization: Concept and Examples

Key features of OAuth 1.0

Signing every request

Each HTTP request is signed with:

  • nonce
  • timestamp
  • consumer_secret
  • token_secret

This makes requests cryptographically protected.

No need to transmit the password

The user enters the password only on the provider's side.

Implementation complexity

OAuth1:

  • complex signatures
  • many parameters
  • difficult to implement

That's why OAuth 2.0 was created.

How OAuth 2.0 works


Like the first version, OAuth 2.0 is based on the use of basic web technologies: HTTP requests, redirects, etc. Therefore, OAuth can be used on any platform with access to the internet and a browser: on websites, in mobile and desktop applications, browser plugins…

The key difference from OAuth 1.0 is simplicity. The new version has no cumbersome signature schemes, and the number of requests required for authorization has been reduced.

The general operating scheme for an application using OAuth is as follows:

  1. obtaining authorization
  2. accessing protected resources


The result of authorization is an access token — a kind of key (usually just a set of characters), whose presentation grants access to protected resources. In the simplest case, accessing them happens over HTTPS with the obtained access token specified in the headers or as one of the parameters.

The protocol describes several authorization options suited to different situations:

  • authorization for applications with a server-side component (most often, websites and web applications)
  • authorization for fully client-side applications (mobile and desktop applications)
  • authorization by login and password
  • restoring a previous authorization

Authorization for applications with a server-side component


OAuth 1 and 2 Authorization: Concept and Examples

  1. Redirect to the authorization page
  2. On the authorization page, the user is asked to confirm the granting of rights
  3. If the user agrees, the browser is redirected to the URL specified when opening the authorization page, with a special key — the authorization code — added to the GET parameters
  4. The application server performs a POST request with the received authorization code as a parameter. This request returns an access token


This is the most complex authorization option, but it is the only one that lets the service unambiguously identify the application requesting authorization (this happens through server-to-server communication in the last step). In all other options, authorization happens entirely on the client, and for obvious reasons one application can be masked as another. This should be taken into account when implementing OAuth authentication in service APIs.

Example


Here and below, the examples are given for the intellect.icu API, but the logic is the same for all services — only the authorization page addresses change. Note that requests must be made over HTTPS.

Redirecting the user's browser to the authorization page:

> GET /oauth/authorize?response_type=code&client_id=1111&
      redirect_uri=http%3A%2F%2Fintellect.icu%2Fcb%2F123 HTTP/1.1
> Host: connect.intellect.icu


Here and below, client_id and client_secret are the values obtained when registering the application on the platform.

After the user grants permissions, a redirect occurs to the specified redirect_uri:

< HTTP/1.1 302 Found
< Location: http://example.com/cb/123?code=gfjfgjgfjb0y



Note that if you are implementing site login via OAuth, it is recommended to add to redirect_uri a unique identifier for each user to prevent CSRF attacks (in the example this is 123). When receiving the code, you should verify that this identifier has not changed and corresponds to the current user.

We use the obtained code to get the access_token, by performing a request from the server:

> POST /oauth/token HTTP/1.1
> Host: connect.intellect.icu
> Content-Type: application/x-www-form-urlencoded
>
> grant_type=authorization_code&client_id=49&client_secret=dgfhfgef&code=gfjfgjgfjb0y&
  redirect_uri=http%3A%2F%2Fexample.com%2Fcb%2F123

< HTTP/1.1 200 OK
< Content-Type: application/json
<
< {
<    "access_token":"tfrtgu",
<    "token_type":"bearer",
<    "expires_in":86400,
<    "refresh_token":"thdtthTDHtrht",
< }



Note that this last request uses client_secret, which in this case is stored on the application server and confirms that the request has not been forged.

As a result of the last request, we obtain the access key itself (access_token), its expiration time (expires_in), the key type, which determines how it should be used (token_type), and refresh_token, which will be discussed in more detail below. Next, the obtained data can be used to access protected resources, for example, the intellect.icu API:

> GET /platform/api?oauth_token=tbhuiGDSGGGg76rf&client_id=5119&format=json&method=users.getInfo&
      sig=... HTTP/1.1
> Host: apps.intellect.icu


Described in the specification

Authorization for fully client-side applications


OAuth 1 and 2 Authorization: Concept and Examples

  1. Opening an embedded browser with the authorization page
  2. The user is asked to confirm the granting of rights
  3. If the user agrees, the browser is redirected to a placeholder page, with the access token appended to the fragment (after #) of its URL
  4. The application intercepts the redirect and extracts the access token from the page address


This option requires opening a browser window within the application, but does not require a server-side component or an additional server-to-server call to exchange the authorization code for an access token.

Example


Opening the browser with the authorization page:

> GET /oauth/authorize?response_type=token&client_id=454119 HTTP/1.1
> Host: connect.intellect.icu



After the user grants permissions, a redirect occurs to the standard placeholder page, which for intellect.icu is connect.intellect.icu/oauth/success.html:

< HTTP/1.1 302 Found
< Location: http://connect.intellect.icu/oauth/success.html#access_token=jhgbujgjyhg&token_type=bearer&
            expires_in=16400&refresh_token=thdtthTDHtrht



The application must intercept this last redirect, extract the acess_token from the address, and use it to access protected resources.

Authorization by login and password


Authorization by login and password is a simple POST request that returns an access token. This scheme is nothing new, but it is included in the standard for completeness and is recommended only when other authorization options are not available.

Example

> POST /oauth/token HTTP/1.1
> Host: connect.intellect.icu
> Content-Type: application/x-www-form-urlencoded
>
> grant_type=password&client_id=31337&client_secret=deadbeef&username=api@corp.intellect.icu&
  password=qwerty

< HTTP/1.1 200 OK
< Content-Type: application/json
<
< {
<    "access_token":"hgfvyfyujfygjftyf32",
<    "token_type":"bearer",
<    "expires_in":96400,
<    "refresh_token":"thdtthTDHtrht",
< }

Restoring a previous authorization


Usually, access token has a limited lifetime. This can be useful, for example, if it is transmitted over open channels. To avoid forcing the user to go through authorization again once the access token's lifetime expires, in all of the options listed above, in addition to the access token, a refresh token may also be returned. It can be used to obtain a new access token via an HTTP request, similarly to authorization by login and password.

Example

> POST /oauth/token HTTP/1.1
> Host: connect.intellect.icu
> Content-Type: application/x-www-form-urlencoded
>
> grant_type=refresh_token&client_id=31337&client_secret=dfhdfhdfhdfh&refresh_token=thdtthTDHtrht

< HTTP/1.1 200 OK
< Content-Type: application/json
<
< {
<    "access_token":"JRTr5677",
<    "token_type":"bearer",
<    "expires_in":86400,
<    "refresh_token":"thdtthTDHtrht",
< }

What is OAuth 2.0

OAuth 2.0 — is a delegated authorization protocol that allows one application to access a user's resources without transmitting the password.

The main idea:
the user grants the application access through a trusted authorization server.

Example:
You log into a site through Google / GitHub / Facebook — the site does not receive your password, but receives an access token.

Purpose of OAuth2

The main goals of the protocol:

  1. Security
    the user's password is not transmitted to the third-party application.
  2. Delegated access
    the user can grant access to only specific data (scope).
  3. Access control
    the user can revoke permission.
  4. Standardized API authorization

OAuth2 participants

4 roles participate in the process:

Role Description
Resource Owner the user
Client the application (website / mobile app)
Authorization Server the authorization server
Resource Server the API with protected data

3 OAuth2 schemes:

1 Full OAuth2 Authorization Code scheme
2 OAuth2 + PKCE (as in SPA)
3 OAuth2 + JWT tokens

OAuth 1 and 2 Authorization: Concept and Examples

OAuth 1 and 2 Authorization: Concept and Examples

OAuth2 authorization sequence (Authorization Code Flow)

This is the most common and secure flow.

1. The user opens the application

The client sends the user to the Authorization Server.

GET /authorize
client_id=123
redirect_uri=https://client.app/callback
response_type=code
scope=profile email

2. The user goes through authentication

On the authorization server, the user:

  • enters a login/password
  • confirms access

3. The server returns an Authorization Code

After confirmation, the user is redirected back:

https://client.app/callback?code=AUTH_CODE

4. The client obtains an Access Token

The client sends a request to the authorization server:

POST /token
grant_type=authorization_code
code=AUTH_CODE
client_id=123
client_secret=abc

Response:

{
"access_token": "TOKEN",
"refresh_token": "REFRESH",
"expires_in": 3600
}

5. The client accesses the API

Now the application can access the Resource Server:

GET /user/profile
Authorization: Bearer ACCESS_TOKEN

What is an Access Token

Access Token — is the key for accessing the API.

Usually it is:

  • a JWT
  • a string of random characters

It has:

  • a lifetime
  • a set of permissions (scope)

Refresh Token

If the Access Token has expired, the Refresh Token is used:

POST /token
grant_type=refresh_token
refresh_token=XXXX

Main OAuth2 Grant Types

Type Purpose
Authorization Code web applications
PKCE SPA and mobile applications
Client Credentials server-to-server
Device Code

browserless devices

In short:

OAuth2 — is a delegated authorization protocol that allows an application to access a user's data via an access token, without disclosing the password.

Comparison of OAuth 1.0 and OAuth 2.0

OAuth 1.0 OAuth 2.0
Signature HMAC-SHA1 usually none
Tokens request + access access + refresh
Complexity high simpler
Security cryptographic signatures HTTPS
complex cryptography simpler
request signatures access token
complex implementation simpler to implement

See also

  • SOAP [[b9827]]
  • [[b9152]]
  • [[b4708]]
  • [[b5768]]
  • [[b14138]]
  • [[b11851]]
  • [[b8651]]
  • [[b768]]
  • [[b6939]]
  • [[b5973]]
  • WSDL
  • WDDX
  • AMQP
  • Data Terminal Equipment (DTE)
  • Point of call
  • End system
  • Host (network)
  • Node (network)
  • Terminal (telecommunications)
  • Unified identification and authentication system
  • Cyrus SASL
  • Token (authorization)
  • One-time password
  • Needham–Schroeder authentication protocol
  • Account
  • Login
  • OpenID
  • Unified identification and authentication system

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Running server side scripts using PHP as an example (LAMP)"

Terms: Running server side scripts using PHP as an example (LAMP)