The Client Credentials grant is used when applications request an access token to access their own resources, not on behalf of a user (for example, background services and daemons). It must be used only by confidential clients.
Aidbox OAuth module supports Client Credentials Grant flow in different formats. The first one is in strict adherence to specification for better compatibility. The second one uses JSON request as a more modern and simple way. Read the official OAuth 2.0 specification for more details.
Easy way
The easiest way to test Client Credentials Grant flow is to run through the Aidbox Sandbox UI (Auth -> Sandbox -> Client Credentials).
Configure Client
To start using this flow you have to create and configure Client. The only required parameters is secret and you also have to enable this flow for client by grant_types: ['client_credentials']
PUT /Client/api-clientAccept:text/yamlContent-Type:text/yamlsecret:verysecretgrant_types: - client_credentials
You can also configure token's format and expiration, as well refresh the token:
attribute
options
desc
auth.client_credentials.token_format
jwt
use access token in jwt format
auth.client_credentials.access_token_expiration
int (seconds)
token expiration time from issued at
auth.client_credentials.refresh_token
true/false
enable refresh_token
auth.client_credentials.refresh_token_expiration
int (seconds)
refresh token expiration time from issued or last usage
PUT /Client/api-clientAccept:text/yamlContent-Type:text/yamlsecret:verysecretgrant_types: - client_credentialsauth:client_credentials:access_token_expiration:600# 10 minutestoken_format:jwtrefresh_token:truerefresh_token_expiration:86400# 1 day
Since by default new client has no access to any resources, you probably want to configure AccessPolicy for this specific client:
PUT /AccessPolicy/api-clientAccept:text/yamlContent-Type:text/yamlengine:allowlink: - id:api-clientresourceType:Client
Get Access Token
The next step is to exchange client id and secret for Access Token.
Using Authorization header {base64(Client.id + ':' + Client.secret)} or by JSON request with client_id and client_secret in body:
POST /auth/tokenAuthorization:Basic YXBpLWNsaWVudDp2ZXJ5c2VjcmV0Content-Type:application/x-www-form-urlencodedgrant_type=client_credentials
POST /auth/tokenContent-Type:application/json{"grant_type":"client_credentials","client_id":"api-client","client_secret":"verysecret"}
For simple client configuration you will get JSON with access_token in response:
If you use JWT token format and provide in token request additional parameter audience, resulting token will set aud claim into value you've sent.
The "aud" (audience) claim identifies the recipients that the JWT is
intended for. Each principal intended to process the JWT MUST
identify itself with a value in the audience claim. If the principal
processing the claim does not identify itself with a value in the
"aud" claim when this claim is present, then the JWT MUST be
rejected. In the general case, the "aud" value is an array of case-
sensitive strings, each containing a StringOrURI value. In the
special case when the JWT has one audience, the "aud" value MAY be a
single case-sensitive string containing a StringOrURI value. The
interpretation of audience values is generally application specific.
Using Access Token
You can use access token in Authorization header for Aidbox API calls:
GET /PatientAuthorization:Bearer ZjQyNGFhY2EtNTY2MS00NjVjLWEzYmEtMjIwYjFkNDI5Yjhi
POST /auth/tokenContent-Type:application/json{"grant_type":"refresh_token","client_id":"webapp","refresh_token":"eyJhb..PloA"}
status:200{"access_token":"eyJh..D8YA",# new access_token"expores_id":600}
Revoke Access Token (Close Session)
Aidbox create Session (resource) for each Acce∂ss Token that can be closed with the special endpoint DELETE /Session with the token in the Authorization header: