Introduction

When developing web applications using a server-side framework, use the following procedures to set up your authorization.

IMPORTANT

Keep your client credentials, i.e., client ID, client secret, and access tokens, secure. Do not include them in publicly accessible code where they could be discovered. Instead, store them safely on a backend server.

Before you begin

This guide assumes you have:

  • Registered a web app with the necessary scopes for your use case. If you have not created an application, open the My Apps page under your Profile menu to create one. For this guide, the application type is Web App.
  • The client_id and the client_secret supplied when you created your application.
  • This tutorial requires a redirect URI of https://developer.bentley.com/redirect-tutorial. Please add this redirect URI to the App Details for your application. The App Details page can be accessed through the My Apps page.

Note

There is an optional step at the end of the tutorial that enables you to validate the token you received beyond the successful HTTP response. If you would like to perform this step, your app must have the users:read scope assigned.

User Authorization Code Flow

Web applications can use the OAuth 2.0 Authorization Code Flow to enable data owners to authorize third-party apps to access user data. This flow includes features like customer login and consent handling to obtain authorization from the resource owner.

The following steps provide an overview of the process.

  1. The application makes a request to the authorization server endpoint.
  2. The end-user provides their authentication information and consents for the application to access resources on their behalf.
  3. The authorization server returns an authorization code to the Redirect URI specified when creating the application.
  4. Exchange the authorization code for an access token.
  5. Use the access token to call the API on behalf of the user.
sequenceDiagram participant ro as Resource Owner participant app as Application participant as as Authorization Server participant api as iTwin API ro->>+app: Connect to application app->>+as: Request authorization code as->>ro: Redirect to login and consent ro->>as: Sign in and consent as->>-app: Authorization code app->>+as: Exchange authorization code for an access_token as->>as: Validate client_id, client_secret, scope and redirect_uri as->>-app: Granted access token app->>-ro: Signed in ro->>+app: Perform action app->>+api: API request with the access token api->>-app: API response app->>-ro: Render content

Set up Authorization for your App

The following steps guide you through setting up your application and retrieving an access token with the needed permissions for your web application.

Register a web application

  1. Click your profile button in the upper-right corner of the page. You must be logged in to the Developer Portal.
  2. Select My Apps from the drop-down.
  3. Click the Register New button.
  4. Enter the name of your application. Web apps have a maximum of 128 characters, the following characters are not allowed: ,;[]=\<>?"{}|+!@#\$%\^*`~
  5. Select the appropriate scopes for your application in API associations.
    Selected scopes are listed in the Allowed scope list box.
  6. In Application Type, click Web App to indicate you are creating a web application.
  7. In Redirect URIs, enter the callback URL to use during the authorization process. This is the URL to which the authorization code is sent.
    Optionally, you can set a Post logout URI to which the user will be redirected when logging out of the app.
  8. Click Save.
  9. On the Success dialog, click Copy to copy the client_id and client_secret created for your app. Make sure you save this data in a secure location. It is required to obtain a token.
    Close the dialog by clicking the X in the upper-right corner or clicking outside the box.

Obtain an Access Token

To obtain an access token for your app, follow these steps:

  1. When the user opens the Web app, redirect them to the authorization server endpoint.

    https://ims.bentley.com/connect/authorize?client_id=<client_id>&redirect_uri=<redirect_uri>&scope=<scopes>+offline_access&response_type=code

    The request for an authorization code requires the following parameters:

    Parameter
    Description
    response_type
    Set to code to indicate that an authorization code is needed.
    client_id
    The ID of the app you created. If you forgot the ID, find it on the My Apps page. Locate your app in the list, and the Client ID is in the same-named column.
    redirect_uri
    The callback URL you entered when registering your application. The returned authorization code is sent to this URL.
    scope
    Add the scopes assigned to your app during registration. Separate multiple scopes with a space. Your end user will consent to the app accessing this information on their behalf during the login process. When requesting a refresh token, include the scope offline_access. For all other scopes, check the reference documentation for each respective API.
    state
    (Optional) Used by the client to maintain state between a request and a callback. Recommended to prevent cross-site request forgery. The authorization server includes this value when redirecting the user-agent to the client.
    Redirect
  2. The user signs in to authenticate and consents for the application to access resources on their behalf. This page is managed by the Bentley authorization server and does not require any implementation in your application.

    User interface for facing Bentley authorization server

    Once authenticated, the Bentley authorization server returns an authorization code to the Redirect URI registered with your application. Extract the code in the response from the redirect_uri field.

    Note

    Authorization codes are time-sensitive. If your code expires, send a new request to the authorization server for a new code.

  3. Send a request to the token endpoint to exchange the authorization code for an access token.

    POST
    https://ims.bentley.com/connect/token

    The authorization request requires the following parameters:

    Field Name
    Description
    client_id
    Identification generated during application creation. Found in the My Apps page or in the first step if generated during the tutorial.
    client_secret
    Secret created during application creation.
    grant_type
    Set to authorization_code Indicates the type of grant being used. This tells the service you are exchanging the code for a token.
    code
    This is the authorization code returned in the previous request.
    redirect_uri
    The callback URL you entered when registering your application. This URL must match the URL provided in the initial request.
  4. The authorization server confirms the information sent in the request and returns an access token. Bentley’s authorization server completes this step. There is no implementation needed in your application. A successful response includes the access_token, an expiry of 3600 seconds, and a refresh_token. Tokens are Bearer type, which must be specified in your API calls.

  5. Use the access token to call the API. Tokens are added to the Authorization header with Bearer type in subsequent requests.
    Successfully sending a request to an API requires the end user to have the correct account permissions. Use the Access Control API to ensure your user can access the required resources.

Request a new access token with a refresh token

When your access token is close to the expiry time, you must request a new one using the refresh token provided in the original request. The response contains a new token and a new refresh token. Tokens are Bearer type, which must be specified in your API calls.

POST
https://ims.bentley.com/connect/token
Field Name
Description
client_id
Identification generated during application creation. Found in the My Apps page or in the first step if generated during the tutorial.
client_secret
Secret created during application creation.
grant_type
Set to refresh_token Indicates the type of grant being used. This tells the service you are requesting a new access_token with a refresh_token.
refresh_token
The refresh token. Ensure the refresh_token you include is the same one received from the previous request.

Validate your token

If you received a 200 OK response to your request for a token, it is not necessary to validate it. However, the users/me endpoint is provided if you would like to do so. This request returns the profile information for the user account associated with the token received.

Remember, the iTwin Platform Base URI is api.bentley.com.

More resources that you may like