> ## Documentation Index
> Fetch the complete documentation index at: https://help.lobyco.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Multi-tenant authentication using client credentials flow​

> Import the Lobyco multi-tenant app into your own Azure AD so you keep full control of the secrets.

## Intro

Azure AD multi-tenant authentication using the OAuth 2.0 client credentials flow enables secure and scalable access to APIs across multiple Azure AD tenants. This setup is ideal for scenarios where customers operate their own Azure AD tenants.

A key advantage is that customers have control over the secrets associated with their own tenant. This control is crucial for meeting compliance and regulatory requirements, as it allows customers to enforce their own security policies and practices.

## Step by step guide to setup multi-tenant Azure AD authentication

<Steps>
  <Step>
    ### Contact Lobyco to receive authentication configuration

    Lobyco needs to create multi-tenant app on it's end and share following configuration values:

    * `lobyco_app_id`
    * `lobyco_app_name`
    * `lobyco_app_uri`
  </Step>

  <Step>
    ### Import Lobyco multi-tenant app into your active directory

    In order to see available roles of Lobyco multi-tenant application, service principal should be created in the client's tenant that represents the application from the Lobyco tenant. It can be done using PowerShell script:

    ```powershell theme={"system"}
    New-AzureADServicePrincipal -AppId <lobyco_app_id>
    ```

    Created service principal can be found by `lobyco_app_name` in Enterprise applications blade in Azure Portal.
  </Step>

  <Step>
    ### Create single-tenant app in your active directory

    **Create new app registration**

    The single-tenant app, which represents the client application in Azure AD, should be registered. Go to App registrations blade in Azure Portal and create new registration.

    <img src="https://mintcdn.com/lobyco-4c9fb3ad/wpdZgjvGMgh4Gbhy/images/60058505b84fb55462bc22c00e103d6c21396a1a.png?fit=max&auto=format&n=wpdZgjvGMgh4Gbhy&q=85&s=448b26678ba60ff1bf9e2720eea521de" alt="" width="1071" height="971" data-path="images/60058505b84fb55462bc22c00e103d6c21396a1a.png" />

    When you create the application, choose following options:

    **Name**: Enter a meaningful application name, like `lobyco-api-client-app-{environmentName}`

    **Account** type: Accounts in this organizational directory only

    **Redirect URI**: not needed, the client credentials flow doesn't use it.

    Select **Register** to create the application. On the app **Overview** page, find the **Application (client) ID** value and record it as `client_app_id` for later. You'll need it to pass as parameter to get token endpoint.

    **Set app credentials: certificate or secret**

    As with any confidential client application, you need to add a secret or certificate to act as that application's credentials so it can authenticate as itself, without user interaction.

    Example: Created client secret

    <img src="https://mintcdn.com/lobyco-4c9fb3ad/wpdZgjvGMgh4Gbhy/images/9491944d3cfa03049c637eaf455ebd04a04c08ed.png?fit=max&auto=format&n=wpdZgjvGMgh4Gbhy&q=85&s=aef2370ad27ff0cefc0092b09fa840ab" alt="" width="1073" height="523" data-path="images/9491944d3cfa03049c637eaf455ebd04a04c08ed.png" />

    Copy the Secret Value and record it as `client_app_secret` for later. You'll need it to pass as parameter to get token endpoint.

    **Set API permissions**

    Click Add a permission button and select APIs my organization uses tab. Type `lobyco_app_name` or `lobyco_app_id` value received from Lobyco.

    Example: a test app ('LoopAppTst' in the screenshots) and its roles:

    <img src="https://mintcdn.com/lobyco-4c9fb3ad/wpdZgjvGMgh4Gbhy/images/75a2966557b890f61355d18e05fda5cc1142426c.png?fit=max&auto=format&n=wpdZgjvGMgh4Gbhy&q=85&s=55ede6e0ebb180c09ddd30b637cf3bde" alt="" width="1074" height="551" data-path="images/75a2966557b890f61355d18e05fda5cc1142426c.png" />

    <img src="https://mintcdn.com/lobyco-4c9fb3ad/wpdZgjvGMgh4Gbhy/images/f1c34543f8b80f766e71c6b7b87d9f6dac985d5d.png?fit=max&auto=format&n=wpdZgjvGMgh4Gbhy&q=85&s=2f02dedd0761f67ce6cee0d4865b21d5" alt="" width="1072" height="569" data-path="images/f1c34543f8b80f766e71c6b7b87d9f6dac985d5d.png" />

    Select required Application permissions for the client app and grant admin consent.

    Application permissions require Admin consent. Granting tenant-wide admin consent requires you to sign in as [Global Administrator](https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/directory-assign-admin-roles#global-administrator--company-administrator), an [Application Administrator](https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/directory-assign-admin-roles#application-administrator), or a [Cloud Application Administrator](https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/directory-assign-admin-roles#cloud-application-administrator).

    <img src="https://mintcdn.com/lobyco-4c9fb3ad/wpdZgjvGMgh4Gbhy/images/63a569f46647fa9619204f7357066932bc855d99.png?fit=max&auto=format&n=wpdZgjvGMgh4Gbhy&q=85&s=c8aa7a768aa2808ca31f6ecbc7588296" alt="" width="1073" height="570" data-path="images/63a569f46647fa9619204f7357066932bc855d99.png" />

    **Edit Manifest**

    Set accessTokenAcceptedVersion property to '2' in application manifest.

    <img src="https://mintcdn.com/lobyco-4c9fb3ad/wpdZgjvGMgh4Gbhy/images/29f128ef4965a63ebaee7e23fad2dc381a48fec0.png?fit=max&auto=format&n=wpdZgjvGMgh4Gbhy&q=85&s=e219d276b7e8881cbc29ad6358578605" alt="" width="1068" height="728" data-path="images/29f128ef4965a63ebaee7e23fad2dc381a48fec0.png" />
  </Step>

  <Step>
    ### Get token using secret

    To get a token by using the client credentials flow, send a POST request to the /token endpoint of Microsoft identity platform:

    ```http theme={"system"}
    POST /{client_tenant_id}/oauth2/v2.0/token HTTP/1.1
    Host: login.microsoftonline.com
    Content-Type: application/x-www-form-urlencoded
    client_id={client_app_id}
    &scope={lobyco_app_uri}.default
    &client_secret={client_app_secret}
    &grant_type=client_credentials
    ```

    Scope should always be equal to `{lobyco_app_uri}.default` regardless which scopes have been configured.

    Successful response example:

    ```json theme={"system"}
    {
    "token_type": "Bearer",
    "expires_in": 3599,
    "ext_expires_in": 3599,
    "access_token": "{token}"
    }
    ```

    [Access token](https://jwt.ms/) can be safely decoded using [https://jwt.ms/ .](https://jwt.ms/) Verify that token roles claim contains all selected application roles.

    ```json theme={"system"}
    {
    "typ": "JWT",
    "alg": "RS256",
    "kid": "HlC0R12skxNZ1WQwmjOF_6t_tDE"
    }.{
    "aud": "ffc6f3ee-f78d-4fd8-9080-8a2e09b7a680",
    "iss": "https://login.microsoftonline.com/4c867488-2d0b-43ff-9829-1aa2d7ae43b5/v2.0",
    "iat": 1580306045,
    "nbf": 1580306045,
    "exp": 1580309945,
    "aio": "42NgYNiR5fpp9pfwV2zf2a0aBHvnAwA=",
    "azp": "b76cd85d-060c-4616-a8fd-6d1bb7d28c39",
    "azpacr": "1",
    "oid": "42604102-34e1-487d-8e10-5f58c6d92243",
    "roles": [
    "Games.MobileAPI"
    ],
    "sub": "42604102-34e1-487d-8e10-5f58c6d92243",
    "tid": "4c867488-2d0b-43ff-9829-1aa2d7ae43b5",
    "uti": "0qhRSY_TLEyV10Iw7VUgAA",
    "ver": "2.0"
    ```

    If possible, it's recommended to use the supported [Microsoft Authentication Libraries (MSAL)](https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-daemon-acquire-token?tabs=dotnet#acquiretokenforclient-api) to get the token instead of protocol directly.
  </Step>

  <Step>
    ### Use a token

    Now that you've acquired a token, use the token to make requests to the Lobyco endpoints. Lobyco endpoints expect to receive token in Authorization header:

    ```http theme={"system"}
    Authorization: Bearer {token}
    ```

    When the token expires, repeat the request to the /token endpoint to acquire a fresh access token.

    You are now done.
  </Step>
</Steps>
