---
title: "Authentication"
slug: "authentication-1523437"
abstract: "Overview of OAuth authentication, password authentication, client credentials authentication, and how to refresh token authentication."
---
# Authentication

Sitecore Content Hub uses the OAuth 2.0 authorization framework for security. `OAuth` is an open standard for access delegation, commonly used as a way for Internet users to grant websites or applications access to their information on other sites but without giving these websites or apps the passwords. OAuth 2.0 provides authorization flows for web applications, desktop applications, mobile phones, and smart devices.

> [!note]
> [External components](https://doc.sitecore.com/ch/en/users/content-hub/external.html) have access to a pre-authenticated Content Hub JavaScript SDK client for sending HTTP requests to Sitecore Content Hub. The SDK provides dedicated clients for common operations, and a [raw client](https://doc.sitecore.com/ch/en/developers/cloud-dev/raw-client-1523471.html "Raw client") for custom requests. For requests to third-party servers, you can use standard browser APIs such as [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).

## Set up OAuth in Content Hub

To set up a connection to your project, create an `M.OAuthClient` entity in Content Hub.

> [!note]
> The `RedirectUrl` property is mandatory, but it's not relevant in this case. Any value can be provided.

To set up OAuth in Content Hub:

1. On the menu bar, click **Manage** :mdiCogOutline:.
2. On the **Manage** page, click **OAuth clients**.
3. If the SDK's `M.OAuthClient` entity is not in the list, click :mdiPlus: **OAuth Client** to add a new client.
4. In the **Oauth Client** dialog, enter the required `M.OAuthClient` entity values.
    - **Name**
    - **Client ID**
    - **Client Secret**
    - **Redirect Url**
5. From the **Client Type** drop-down list, click **Client Credentials**.
6. In the **Users** field, click **Add** :mdiPlus: and, in the **User** dialog, [select a user](https://doc.sitecore.com/ch/en/developers/cloud-dev/authentication-1523437.html#UUID-696ca5f2-159c-d8f4-93c7-c2954dc50869_section-idm273425541647300 "Client credentials authentication"), and then click **Save**.
7. In the **OAuth Client** dialog, click **Save**.

> [!tip]
> To edit an OAuth client, next to the client you want to edit, click **Quick Edit** :mdiPencil:. To delete the client, click **Delete** :mdiDelete:.

## Password authentication

In addition to a `ClientId` and a `ClientSecret`, you specify a username and a password to authenticate with Content Hub from your project. The following is an example of a Content Hub authentication:

```javascript
import OAuthPasswordGrant from "@sitecore/sc-contenthub-webclient-sdk/dist/authentication/oauth-password-grant";
import { ContentHubClient } from "@sitecore/sc-contenthub-webclient-sdk/dist/clients/content-hub-client";

// Your Sitecore Content Hub endpoint to connect to
const endpoint = "https://your.m.endpoint.com";

// Enter your credentials here
const oauth = new OAuthPasswordGrant(
    "client_id",
    "client_secret",
    "username",
    "password"
);

// Create the JavaScript SDK client
const client = new ContentHubClient(endpoint, oauth);

// Authentication
// returns true when authentication succeeds.
// returns false or throws an error when authentication failed.

await client.internalClient.authenticateAsync();

```

The `await client.internalClient.authenticateAsync()` will return `TRUE` if the authentication is sucessful, or in the case of a failure, it will either return `FALSE` or throw an error.

The `await` expression can also be written as part of a function as follows:

```javascript
async function main() {
  await ...
}
main();

```

When using `await`, be sure to either include a declaration for the `Promise` constructor or include `ES2015` in your `--lib` option:

```javascript
// configuration using tsconfig.json
{
 "compilerOptions": {
  "lib": [
   "es2015"
  ]
 }
}

```

Alternatively, use the following code:

```shell
tsc main.ts --lib ES2015

```

## Client credentials authentication

> [!important]
> The client must be created with the **Client type** set to **Client credentials**.

When you set up OAuth in Content Hub, you can define multiple users in the **Users** field, however, for the purpose of authentication only the first one will be used, This means that all operations will be executed in the context of that user.

> [!tip]
> You can also create a dedicated service user account to use in the OAuth client, so that operations performed by a service are not linked to an individual user account.

The following code provides an example of the client credentials authentication:

```javascript
import OAuthClientCredentialsGrant from "@sitecore/sc-contenthub-webclient-sdk/dist/authentication/oauth-client-credentials-grant";
const oauth = new OAuthClientCredentialsGrant("client_id", "client_secret");

```

## Refresh token authentication

Refresh token authentication can be used instead of a username and password authentication. In the refresh token authentication, it is important to subscribe to the `IWebMClient.RefreshTokenReceived` event. Instead of creating an `OAuthPasswordGrant`, change this to `OAuthRefreshTokenGrant`:

```javascript
import OAuthRefreshTokenGrant from "@sitecore/sc-contenthub-webclient-sdk/dist/authentication/oauth-refreshtoken-grant";

// Enter your credentials here
const oauth = new OAuthRefreshTokenGrant(
  "client_id",
  "client_secret",
  "refresh_token",
);

```