My questions are:
- Why do I receive a refresh token at all for
client_credentials
, which is a grant type for backend -> backend communication? The OAuth2
documentation link says explicitly that "A refresh token SHOULD
NOT be included" for client_credentials
grant type.
First, one needs to clarify the meaning of "SHOULD NOT" in that context. According to rfc2119:
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL
NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and
"OPTIONAL" in this document are to be interpreted as described in
RFC 2119.
Note that the force of these words is modified by the requirement
level of the document in which they are used.
MUST This word, or the terms "REQUIRED" or "SHALL", mean that the definition is an absolute requirement of the specification.
MUST NOT This phrase, or the phrase "SHALL NOT", mean that the definition is an absolute prohibition of the specification.
SHOULD This word, or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.
SHOULD NOT This phrase, or the phrase "NOT RECOMMENDED" mean that there may exist valid reasons in particular circumstances when the particular behavior is acceptable or even useful, but the full implications should be understood and the case carefully weighed before implementing any behavior described with this label.
As you can read, "SHOULD NOT" does not mean that the specification prohibits it but rather that it does not recommend it.
- Is it possible to configure keycloak that it won't send refresh tokens for clients with
client_credentials
grant type?
As first mentioned in the answer provided by @Clement Petit; Keycloak v12 introduced:
Support for OAuth2 Client Credentials grant without refresh token and without user session.
That option can be found in each client configuration under the section "OpenID Connect Compatibility Modes" as shown below:
- If the client scope includes
offline_access
- the refresh_expires_in
is 0
and, from what I understood, the refresh
token is an offline token. Here is a link to keycloak mailing
list where offline token usage is recommended by someone from
keycloak's team - isn't it in conflict with OIDC specification for
client_credentials
grant type?
As you have seen, the specification does not explicitly prohibits it. Notwithstanding, conceptually, it does not make sense to have a refresh token when using the client credentials flow since the backend already has the client secret stored locally anyway, and can simply acquire a new access token without the hassle of either having to store user credentials or request the user to enter them again.
From the original poster from the thread of the keycloak mailing list that you have posted:
Hi,
Currently, every time a confidential client tries to get a new access token
from the token endpoint a new session is created on the server. This can
lead to multiple active sessions for a single client/service account when
doing multiple requests to token endpoint.
To avoid that the client should store the access token/refresh token and
use a refresh token when appropriate in case the access token has expired.
That is fine.
one can infer that the reason why Keycloak had (at that time) the refresh token available for the client credential flows was to be used as a workaround to a technical issue with their client credential flow implementation (i.e., creating too many sessions).