---
title: "How to use OAuth2 Tokens in Platform?"
canonical: "https://onesaitplatform-es.refined.site/space/OP/55967769/How%20to%20use%20OAuth2%20Tokens%20in%20Platform%3F"
format: markdown
---
<span style="color: #091e42">EN | </span>[<span style="color: #091e42">ES</span>](https://onesaitplatform.atlassian.net/wiki/spaces/OP/pages/56000785)

> Macro (toc)

<span style="color: #091e42">Onesait Platform is ready to work with OAuth2 authentication. Following we'll see the OAuth2 Token management flow.</span>

## <span style="color: #091e42">1. OAuth2 Realm configuration parameters</span>

<span style="color: #091e42">When creating or Updating a Realm, there are two parameters to be configured associated to the OAuth Tokens:</span>

![image]()

- KEY (Secret): key that will be used for clients's authentication..
- TOKEN VALIDITY TIME (S): configurable duration (in seconds) in which the generated token will be valid.

They are non-obligatory attributes. In case of not informing them, they will take default values ​​(onesaitplatform and 43200 (12 hours)).

## 2. Token Generation

There is an endpoint that allows OAuth2 token generation.

 The URL is like this: <span style="color: #0000ff">https://lab.onesaitplatform.com/oauth-server/oauth/token</span>

It's a POST request, and it must include:

- **Headers**:
  - Authorization: (client:secret b64)
  - Content-Type: application/x-www-form-urlencoded
- **Body**:
  - grant_type: password (User/Password request)
  - username: user's Id
  - password: user's password
  - clientId: client's Id for Token use
  - scope: Token scope

Using Postman to send this request, it will be something like this:


![image](media://bbfe7b6c-d5ce-4d6c-afcc-6fbdf165c31a)


![image](media://5a0bbfc9-7976-4642-abf6-6eab584b3328)

The response will be like this:

![image](media://834f7464-4031-4389-b799-e261b1dde4a6)

  
To Highlight: 

- access_token: Access token
- refresh_token: Refresh Token (one use)
- expires_in: Remaining validity time (seconds)
- authorities: Realms' roles asigned to the user

## 3. Chek Token

Service that verifies the validity of a token. The endpoint will be like this: <span style="color: #0000ff">https://lab.onesaitplatform.com</span><span style="color: #0000ff">/oauth-server/openplatform-oauth/check_token</span>  
  
The POST request must include:

- **Headers**:
  - Authorization: (client:secret b64)
- **Parameter**:
  - token: token to validate

Using postman:

![image](media://7a33d456-0c4d-48e3-9b87-b354a942bbb7)

<span style="color: #212121">If the token is valid, a response will be obtained in the form:</span>

![image](media://4d34b558-49cf-4e57-8598-39389b1a6d0b)

To Highlight: 

- <span style="color: #212121">exp: Expiration date</span>
- <span style="color: #212121">client_id: client for which the token was generated</span>
- <span style="color: #212121">authorities: Realm's Roles to which the token's user belongs</span>

## <span style="color: #212121">4. Refresh Token</span>

Service that regenerates the token in order to obtain another one. The endpoint is like this: [<span style="color: #0000ff">https://lab.onesaitplatform.com/oauth-server/oauth/token</span>](https://www.onesaitplatform.online/oauth-server/oauth/token)

(the same as for getting a new token, changes the grant-type).

The POST request must include:

- **Headers**:
  - Authorization: (client:secret b64)
  - Content-Type: application/x-www-form-urlencoded
- **Body**:
  - grant_type: refresh_token (for token refresh)
  - refresh_token: refresh token obtained when generating the token)

Using Postman:

![image](media://dacc024e-7399-431f-9ed2-556975d241d5)

![image](media://10c7871b-113e-4729-a737-53fe09be516e)

<span style="color: #212121">The result is the same as for a get token request:</span>

![image](media://e8a04aac-788d-44ce-a6d0-cc9bb26297bf)

The refresh token is one-use token. After regenerating the token, a new refresh token will be provided.

## <span style="color: #212121">5. Revoke Token</span>

<span style="color: #212121">As an addional service, a revoke token service has been included. It allows to disable (revoke) an existing access token associated to an user.</span>

<span style="color: #212121">The endpoint is like this: </span>[<span style="color: #0000ff">https://lab.onesaitplatform.com/oauth-server/openplatform-oauth/revoke_token</span>](https://www.onesaitplatform.online/oauth-server/oauth/token)

The POST request must include:

**Headers**:

- Authorization: (cliente:secret en b64)
- Content-Type: application/x-www-form-urlencoded

**Query Param:**

- token: (token to revoke)

In Postman:

![image](media://88dbd6f5-9397-4640-932b-7c3d390b58f5)

The result will indicate that the token is not longer valid:

![image](media://39c1a956-8ad4-41b0-8603-18bb8e196e75)

## 6. User info endpoints

An endpoint to retrieve user claims. The url is like this: [<span style="color: #505050">https://lab.onesaitplatform.com/oauth-server/user</span>](https://development.onesaitplatform.com/oauth-server/user)

The POST request must include:

**Headers**:

- Authorization: Bearer {jwt}

In postman:

![image](media://0fcf0cd0-df08-47bf-acff-73ec9e9fba40)


![image](media://4ac1bdc1-ee88-46b1-a55a-6bd5f5a5dec4)