|
| 1 | +# OAuth Authentication (Client Credentials Flow) |
| 2 | + |
| 3 | +Client Credentials |
| 4 | + |
| 5 | +Our APIs use standard OAuth 2.0 to allow authorization of personal data. Read on if you want to know how OAuth 2.0 works and how to integrate your application with our security components. You can find the detailed specification for the OAuth 2.0 Authorization Framework [here](https://tools.ietf.org/html/rfc6749). |
| 6 | + |
| 7 | +**Note:** This document describes the client credentials code flow. Some of our products use the authorization code flow, which is documented [here](https://developer.mercedes-benz.com/content-page/oauth-documentation). |
| 8 | + |
| 9 | +## Client Credentials |
| 10 | + |
| 11 | +Your client credentials carry many privileges, so be sure to keep them secure! Therefore, make sure that your application fulfills the following prerequisites: |
| 12 | + |
| 13 | +- It must keep the client credentials on a safe place in the backend (on the server). |
| 14 | + |
| 15 | +- It must not expose any of the client credentials (client id, client secret, access tokens) to the client. |
| 16 | + |
| 17 | + |
| 18 | +## Client Credentials Flow |
| 19 | + |
| 20 | +If an API supports the Client Credentials Flow, The client_credentials grant type is used by clients to obtain an access token outside of the context of a user. This is typically used by clients to access resources about themselves rather than to access a user’s resources. |
| 21 | + |
| 22 | +These are the steps that the flow executes: |
| 23 | + |
| 24 | +1. Request to get an access token |
| 25 | + |
| 26 | +2. Use the access token to call the API |
| 27 | + |
| 28 | + |
| 29 | +### From the developer’s perspective |
| 30 | + |
| 31 | +In the following, we demonstrate how the client credentials code flow looks from a developer’s perspective who is developing an application using our provided APIs. |
| 32 | + |
| 33 | +#### 1. Request to get an access token |
| 34 | + |
| 35 | +In order to initiate the end user’s authorization, you should call our access token endpoint. The client must authenticate using the HTTP Basic method and provide the clientId and the clientSecret (_<insert_your_client_id_here>:<insert_your_client_secret_here>_) encoded with BASE64 in the HTTP Authorization header. |
| 36 | + |
| 37 | +The following parameters are used in the **--data** value: |
| 38 | + |
| 39 | +- **grant_type=client_credentials:** This indicates for the token endpoint to use the OAuth 2.0 Client Credentials Flow for this request. |
| 40 | + |
| 41 | +- **scope=openid**: This scope is needed in order to receive a valid token. |
| 42 | + |
| 43 | + |
| 44 | +You will then receive the OAuth access token in the server response. Note that the **expires_in** field in the response represents the validity period of the access token in seconds. Per default it is 3600s. |
| 45 | + |
| 46 | +Example Request |
| 47 | + |
| 48 | +```http |
| 49 | +curl --request POST 'https://ssoalpha.dvb.corpinter.net/v1/token' \ |
| 50 | +--header 'Authorization: Basic <insert_your_base64_encoded_client_id_and_client_secret_here>' \ |
| 51 | +--header 'Content-Type: application/x-www-form-urlencoded' \ |
| 52 | +--data 'grant_type=client_credentials' \ |
| 53 | +--data 'scope=openid' |
| 54 | +``` |
| 55 | + |
| 56 | +Example Authorization Header |
| 57 | + |
| 58 | +```http |
| 59 | +Authorization: Basic VGhpc0lzWW91ckNsaWVudElkOlRoaXNJc1lvdXJDbGllbnRTZWNyZXQ= |
| 60 | +``` |
| 61 | + |
| 62 | +Example Response |
| 63 | + |
| 64 | +```json |
| 65 | +{ |
| 66 | + "access_token":"<your_access_token>", |
| 67 | + "token_type":"bearer", |
| 68 | + "expires_in":3599, |
| 69 | + "id_token":"<your_id_token>" |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +#### 2. Use the access token to call the API |
| 74 | + |
| 75 | +Now you can use the access token to call the API as long as it is not expired. Add the provided token to the authorization header to your API request. |
| 76 | + |
| 77 | +Typical error cases: |
| 78 | + |
| 79 | +| | | |
| 80 | +| ------------------- | ---------------------------------------------- | |
| 81 | +| **HTTP Error Code** | **Description** | |
| 82 | +| 401 | The given access token is not valid (anymore). | |
0 commit comments