java.lang.Object
com.irurueta.authserver.authorization.infrastructure.persistence.dev.DevDataInitializer
All Implemented Interfaces:
org.springframework.boot.ApplicationRunner

@Component @Profile("dev") public class DevDataInitializer extends Object implements org.springframework.boot.ApplicationRunner
Seeds a demo user, a confidential BFF client and a public device-flow client when the dev profile is active, so a freshly started authorization server has someone to sign in, a client to sign in through and a client for the OAuth 2.0 Device Authorization Grant (device-client, which the polling command line tool of the service-client uses).

The device-client is dev-only on purpose: it is a public client that anybody who can reach the server can use to start a device flow, and its user-facing name and scopes are those of a demo. A deployment registers its own device clients through the admin API with the urn:ietf:params:oauth:grant-type:device_code grant type.

Purpose: local development and the end-to-end tests. Started with spring-boot:run -Dspring-boot.run.profiles=dev, it lets a developer sign in through the BFF by hand. The e2e-tests module (SignInSmokeIT) starts the packaged jar with the dev profile and reads the generated credentials from the log lines below.

Why it lives in src/main although it only seeds demo data: the end-to-end tests run the real jar, and a class under src/test never reaches it. It is inert everywhere else: without the dev profile the bean is not even created. Never activate that profile outside local development.

It lives in the persistence adapter, next to the Flyway migrations, because it writes the tables those migrations create: a schema change that breaks it is then caught by this module's own tests.

Secrets are generated at first start and logged once as DEV ONLY warnings; nothing is committed. Running it again does not change existing data.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final String
    Audience of the authorization server's own API (authserver.api.audience).
    private final List<String>
    Post-logout redirect URIs of the BFF client (authserver.clients.bff.post-logout-redirect-uris); empty when not configured.
    private final List<String>
    Redirect URIs of the BFF client (authserver.clients.bff.redirect-uris, which the dev profile sets to the local BFF); empty when not configured.
    Repository where the BFF client is stored.
    (package private) static final String
    Username of the demo user.
    private static final Duration
    Lifetime of the access tokens of the device client.
    (package private) static final String
    Client id of the public client of the device authorization grant.
    (package private) static final String
    Grant type of the device authorization grant.
    private static final Duration
    Lifetime of the refresh tokens of the device client.
    private static final List<String>
    Scopes of the device client.
    private final org.springframework.security.crypto.password.PasswordEncoder
    Encoder of generated secrets.
    private final org.springframework.jdbc.core.simple.JdbcClient
    JDBC client used to insert the demo user.
    private final SecureRandom
    Source of randomness for generated secrets.
    private static final int
    Number of random bytes of generated secrets.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    Creates the BFF client from its default definition and the configured redirect URIs, with a generated secret, unless it already exists or no redirect URI is configured.
    private void
    Creates the demo user, with a generated password, unless it already exists.
    private void
    Creates the public device client, which has no secret and needs no redirect URI, unless it already exists.
    private String
    Generates a random URL-safe secret.
    void
    run(org.springframework.boot.ApplicationArguments args)
    Creates the demo user, the BFF client and the device client when they do not exist yet.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEMO_USERNAME

      static final String DEMO_USERNAME
      Username of the demo user.
      See Also:
    • DEVICE_CLIENT_ID

      static final String DEVICE_CLIENT_ID
      Client id of the public client of the device authorization grant.
      See Also:
    • DEVICE_CODE_GRANT

      static final String DEVICE_CODE_GRANT
      Grant type of the device authorization grant.
      See Also:
    • DEVICE_SCOPES

      private static final List<String> DEVICE_SCOPES
      Scopes of the device client. openid is left out on purpose: Spring Authorization Server refuses it in a device authorization request, because the device flow issues no ID token.
    • DEVICE_ACCESS_TOKEN_TTL

      private static final Duration DEVICE_ACCESS_TOKEN_TTL
      Lifetime of the access tokens of the device client.
    • DEVICE_REFRESH_TOKEN_TTL

      private static final Duration DEVICE_REFRESH_TOKEN_TTL
      Lifetime of the refresh tokens of the device client.
    • SECRET_BYTES

      private static final int SECRET_BYTES
      Number of random bytes of generated secrets.
      See Also:
    • random

      private final SecureRandom random
      Source of randomness for generated secrets.
    • jdbc

      private final org.springframework.jdbc.core.simple.JdbcClient jdbc
      JDBC client used to insert the demo user.
    • encoder

      private final org.springframework.security.crypto.password.PasswordEncoder encoder
      Encoder of generated secrets.
    • clients

      private final ClientRegistrationRepository clients
      Repository where the BFF client is stored.
    • bffRedirectUris

      @Value("${authserver.clients.bff.redirect-uris:}") private final List<String> bffRedirectUris
      Redirect URIs of the BFF client (authserver.clients.bff.redirect-uris, which the dev profile sets to the local BFF); empty when not configured.
    • bffPostLogoutRedirectUris

      @Value("${authserver.clients.bff.post-logout-redirect-uris:}") private final List<String> bffPostLogoutRedirectUris
      Post-logout redirect URIs of the BFF client (authserver.clients.bff.post-logout-redirect-uris); empty when not configured.
    • apiAudience

      @Value("${authserver.api.audience:auth-server}") private final String apiAudience
      Audience of the authorization server's own API (authserver.api.audience).
  • Constructor Details

    • DevDataInitializer

      public DevDataInitializer()
  • Method Details

    • run

      @Transactional public void run(org.springframework.boot.ApplicationArguments args)
      Creates the demo user, the BFF client and the device client when they do not exist yet.
      Specified by:
      run in interface org.springframework.boot.ApplicationRunner
      Parameters:
      args - application arguments (unused).
    • createDemoUser

      private void createDemoUser()
      Creates the demo user, with a generated password, unless it already exists.
    • createBffClient

      private void createBffClient()
      Creates the BFF client from its default definition and the configured redirect URIs, with a generated secret, unless it already exists or no redirect URI is configured.
    • createDeviceClient

      private void createDeviceClient()
      Creates the public device client, which has no secret and needs no redirect URI, unless it already exists. It requires the user's consent to the requested scopes, as the verification page asks for it.
    • generateSecret

      private String generateSecret()
      Generates a random URL-safe secret.
      Returns:
      the secret.