Class DeviceFlowCli

java.lang.Object
com.irurueta.authserver.serviceclient.device.DeviceFlowCli
All Implemented Interfaces:
org.springframework.boot.CommandLineRunner

@Component @Profile("device-flow") public class DeviceFlowCli extends Object implements org.springframework.boot.CommandLineRunner
Command-line client of the OAuth 2.0 device authorization grant (RFC 8628): requests a device code, prints the user code and the verification URI, then polls the token endpoint honouring interval, slow_down, authorization_pending and expired_token.

Running it from the packaged jar: the class is a runner of the device-flow Spring profile, so

 java -jar service-client-boot.jar --spring.profiles.active=device-flow --device-flow.issuer=http://localhost:9000
 
runs it instead of the regular application (which is unaffected without the profile). Optional settings are --device-flow.client-id, --device-flow.scope and --device-flow.print-access-token=true.

Output, one line each and in this order, so a test can parse them:

 User code: ABCD-EFGH
 Verification URI: http://localhost:9000/oauth2/device_verification
 Verification URI (complete): http://localhost:9000/oauth2/device_verification?user_code=ABCD-EFGH
 Device flow completed: token_type=Bearer expires_in=300 scope=profile documents.read refresh_token=false
 
The tokens themselves are never printed unless print-access-token is set, which adds an Access token: ... line. On any failure the process ends with a non-zero exit code.

Why it lives in src/main: the end-to-end test runs the packaged service-client jar, so the CLI has to be in it; it is a genuine, reusable tool of the application and not a test double.

  • Field Details

    • SLOW_DOWN_INCREMENT_SECONDS

      public static final long SLOW_DOWN_INCREMENT_SECONDS
      Seconds added to the polling interval each time the server answers slow_down (RFC 8628 section 3.5).
      See Also:
    • EXPIRED_TOKEN

      private static final String EXPIRED_TOKEN
      The expired token.
      See Also:
    • client

      private final DeviceAuthorizationClient client
      The client.
    • sleeper

      private final Sleeper sleeper
      The sleeper.
    • clock

      private final Clock clock
      The clock.
    • output

      private final DeviceFlowOutput output
      The output.
    • properties

      private final DeviceFlowProperties properties
      The properties.
  • Constructor Details

    • DeviceFlowCli

      public DeviceFlowCli()
  • Method Details

    • run

      public void run(String... args)
      Runs the whole flow.
      Specified by:
      run in interface org.springframework.boot.CommandLineRunner
      Parameters:
      args - ignored: settings arrive as Spring properties.
      Throws:
      DeviceFlowException - if the user denies the request, the device code expires, a request fails or the thread is interrupted.
    • poll

      private TokenResponse poll(DeviceAuthorization authorization)
      Polls until the user approves the request, the code expires or the server refuses.
      Parameters:
      authorization - the pending authorization.
      Returns:
      the issued tokens.
    • pause

      private void pause(Duration duration)
      Waits, turning an interruption into a DeviceFlowException.
      Parameters:
      duration - how long to wait.
    • failure

      private DeviceFlowException failure(PollOutcome.Failed failed)
      Maps a final error of the token endpoint to an exception.
      Parameters:
      failed - the error.
      Returns:
      the exception to throw.