liveBook · Manning

Refresh token

As explained in Spring in Action, Sixth Edition

A refresh token is a credential that a client presents to an authorization server to obtain a new access token after the current one expires.

A refresh token lets a client ask an authorization server for a new access token when the current one expires. It is not the token sent to the API: requests to a protected endpoint use the access token as a Bearer token. section 8.3

In the Taco Cloud admin application, the authorization server registers a client with both authorization-code and refresh-token grants. The authorization-code exchange returns an access_token and a refresh_token; the example access token expires after about five minutes. section 8.2 section 8.3

@Bean
public RegisteredClientRepository registeredClientRepository(
        PasswordEncoder passwordEncoder) {
  RegisteredClient registeredClient = 
    RegisteredClient.withId(UUID.randomUUID().toString())
      .clientId("taco-admin-client")
      .clientSecret(passwordEncoder.encode("secret"))
      .clientAuthenticationMethod(
              ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
      .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
      .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
      .redirectUri(
          "http://127.0.0.1:9090/login/oauth2/code/taco-admin-client")
      .scope("writeIngredients")
      .scope("deleteIngredients")
      .scope(OidcScopes.OPENID)
      .clientSettings(
          clientSettings -> clientSettings.requireUserConsent(true))
      .build();
  return new InMemoryRegisteredClientRepository(registeredClient);
}

The registered client enables AuthorizationGrantType.REFRESH_TOKEN alongside AUTHORIZATION_CODE. Its client ID and encoded secret identify the client when it later requests a token. section 8.2

Renew access after expiry

Suppose an admin creates an ingredient through the protected API. Once the access token expires, further requests return HTTP 401. The client sends its refresh token to the authorization server’s /oauth2/token endpoint, receives a new access token, and uses that token for subsequent API requests. section 8.3

$ curl localhost:9000/oauth2/token \
    -H"Content-type: application/x-www-form-urlencoded" \
    -d"grant_type=refresh_token&refresh_token=$refreshToken" \
    -u taco-admin-client:secret

This curl request sets grant_type=refresh_token, supplies the previously received refresh token, and authenticates as the registered client. Substitute the actual token for $refreshToken. section 8.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Shows client registration, a token response, and a renewal request; does not develop refresh-token internals.
Examples
Uses a registered admin client, a five-minute access token, protected ingredient requests, and a curl renewal command.
What it assumes
Assumes a registered OAuth 2 client and an initial access-token exchange; shows the client settings and resulting tokens.
What you can do after
Operate/configure it by enabling the refresh-token grant and requesting a new access token with curl after expiry.
How it connects
Connects refresh tokens to OAuth 2 grant types, access tokens, authorization servers, and resource servers.

Also explained in

Compare how 3 books explain refresh token →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.