---
title: "How to use OAuth2 Tokens in Platform?"
canonical: "https://onesaitplatform.refined.site/space/DOCT/2220846623/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)

Onesait Platform is ready to work with OAuth2 authentication. The Oauth2 token management flow is explained below.

## <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](media://385c6ef3-3492-419e-b65f-45d445b7d193)

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

They are optional attributes. If not informed, they will take default values ​​defined in the platform deployment (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: id of the client requesting the token
  - scope: Token scope

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


![image](media://aee5c30d-86ed-4db8-9608-4d0407b79095)


![image](media://47fb4414-4538-4b89-9eb4-2d2dd1c7a5fd)

The response will have this format:

![image](media://18b6b79a-b264-4fc4-af27-5b982e211ac0)

  
Highlights: 

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

## 3. Check 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://c418cbbd-6e73-44bc-a251-333e5a2863c8)

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

![image](media://3ac5303d-ec5a-4a00-9c36-cbedc239ce46)

Highlights: 

- <span style="color: #212121">principal/name: user for which the token was generated.</span>
- <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 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 attribute).

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 the access token was generated.

Using Postman:

![image](media://08229e92-ba9e-40d5-9f4f-80e4c9921c36)

![image](media://9ab41e88-d9cf-4dd0-9907-f52c257c67a7)

The result will have the same structure as when a token generation request is made.

![image](media://f377023c-de23-493c-8053-8e5d934f5a0c)

The refresh token is one-use only. After using it, a new one will be provided along with the new access token.

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

<span style="color: #212121">As an additional service, a token revocation service is included. Allows you to disable (revoke) an access token associated with a given 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://8238767f-3749-4103-ba23-0bb79c13a8a8)

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

![image](media://cd6f1269-eb00-4d57-b905-b400df1eae57)

## 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://51e38226-167c-438c-917f-4a43e637879e)


![image](media://4e0bcb74-bb61-4851-8b33-6699eaf2a9f8)