Skip to content

Instantly share code, notes, and snippets.

@aleixmorgadas
Last active July 27, 2023 13:26
Show Gist options
  • Select an option

  • Save aleixmorgadas/42c432d1529088eeb6262488b6b084e1 to your computer and use it in GitHub Desktop.

Select an option

Save aleixmorgadas/42c432d1529088eeb6262488b6b084e1 to your computer and use it in GitHub Desktop.

Revisions

  1. aleixmorgadas revised this gist Jul 27, 2023. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions Auth0Configuration.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,20 @@
    package dev.aleixmorgadas.example.configuration;

    import com.auth0.client.auth.AuthAPI;
    import com.auth0.client.mgmt.ManagementAPI;
    import com.auth0.exception.Auth0Exception;
    import jakarta.validation.constraints.NotBlank;
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.validation.annotation.Validated;

    @Slf4j
    @EnableScheduling
    @Configuration
  2. aleixmorgadas created this gist Jul 27, 2023.
    7 changes: 7 additions & 0 deletions 0_README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # Refreshing Auth0 ManagementAPI token before it expires.

    The expectation here is that the token expiration is 24h. You can check it in Applications > API > Settings > Token Settings.

    What I did is refreshing the token every 12 hours.

    In my case, I use Spring Boot 3.
    46 changes: 46 additions & 0 deletions Auth0Configuration.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    @Slf4j
    @EnableScheduling
    @Configuration
    @EnableConfigurationProperties(Auth0Configuration.Auth0Properties.class)
    @RequiredArgsConstructor
    @ConditionalOnProperty(value = "auth0.enabled", havingValue = "true")
    public class Auth0Configuration {
    final Auth0Properties properties;

    @Bean
    AuthAPI authAPI() {
    return AuthAPI.newBuilder(properties.domain, properties.clientId, properties.clientSecret).build();
    }

    @Bean
    ManagementAPI managementAPI(AuthAPI authAPI) throws Auth0Exception {
    var tokenHolder = authAPI.requestToken(properties.audience).execute().getBody();
    return ManagementAPI.newBuilder(properties.domain, tokenHolder.getAccessToken()).build();
    }

    @Configuration
    @RequiredArgsConstructor
    @EnableConfigurationProperties(Auth0Configuration.Auth0Properties.class)
    @ConditionalOnProperty(value = "auth0.enabled", havingValue = "true")
    static class RefreshToken {
    final Auth0Properties properties;
    final AuthAPI authAPI;
    final ManagementAPI managementAPI;

    @Scheduled(cron = "0 0 0/12 * * ?")
    void refreshManagementToken() throws Auth0Exception {
    var token = authAPI.requestToken(properties.audience).execute().getBody();
    managementAPI.setApiToken(token.getAccessToken());
    }
    }

    @Validated
    @ConfigurationProperties(prefix = "auth0")
    record Auth0Properties(
    @NotBlank String domain,
    @NotBlank String clientId,
    @NotBlank String clientSecret,
    @NotBlank String audience
    ) {
    }
    }