Auth0 Home Blog Docs

Server-to-server auth with Refresh Token

auth

#1

Not sure which flow to use for this scenario: I have a Scala process running in my backend, that needs to talk to an API. This process is not exposed to the outside world and there is no user involved. I created a Non-Interactive-Client for making the calls and everything works fine. However, I would like to get a refresh token so I could automatically refresh the access-token, and this doesn’t work. I’m using the Java AuthAPI object to request a token, and if I do:
auth.requestToken(audience).setScope(“offline_access”).execute()
I get an error saying “Client has not been granted scopes: offline_access”.
The API is configured to “Allow Offline Access”, and the client is configured with grant-types Implicit, Authorization Code, Refresh Token and Client Credentials.

Am I doing it wrong? Is there something else to configure?


#2

Refresh tokens are only available when implementing an [Authorization Code Grant] (https://auth0.com/docs/api-auth/tutorials/authorization-code-grant), [Authorization Code Grant (PKCE)] (https://auth0.com/docs/api-auth/tutorials/authorization-code-grant-pkce) or [Resource Owner Password Grant] (https://auth0.com/docs/api-auth/tutorials/password-grant), as stated in the [refresh token documentation] (https://auth0.com/docs/tokens/refresh-token/current#restrictions).

As per the specification, a refresh token SHOULD NOT be included in the response for the client credentials grant. You can read more about it here: https://tools.ietf.org/html/rfc6749#section-4.4.3

Since you’re implementing [server-to-server auth] (https://auth0.com/docs/api-auth/tutorials/client-credentials), there is no real need for refresh tokens, since the credentials are not exposed to the end user and you can just retrieve new tokens once the previous ones expire.


#3

Hi Ricardo, thanks for the answer. It’s true that I can just ask for new tokens (this is how I ended up implementing it), but just to clear up my confusion around the different authentication flows: my code that requires authentication is not open to the world at all, it actually runs inside a Kubernetes pod with no exposed ports. So there are no security concerns regarding the refresh token. If I understand correctly, the right auth-flow for this scenario is Client Credentials Grant. But then, why not return a refresh-token as well…? I understand that it’s not necessary but I’m just curious.


#4

It is not returned specifically because the specification states that it should not be returned. In your case, the client credentials grant would have no benefit from the refresh token, as it does not add anything new that you don’t already have in your implementation, since storing the refresh token would have the same value as privately storing the credentials for the authentication request.