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:9000runs 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=falseThe 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 Summary
FieldsModifier and TypeFieldDescriptionprivate final DeviceAuthorizationClientThe client.private final ClockThe clock.private static final StringThe expired token.private final DeviceFlowOutputThe output.private final DeviceFlowPropertiesThe properties.private final SleeperThe sleeper.static final longSeconds added to the polling interval each time the server answersslow_down(RFC 8628 section 3.5). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate DeviceFlowExceptionfailure(PollOutcome.Failed failed) Maps a final error of the token endpoint to an exception.private voidWaits, turning an interruption into aDeviceFlowException.private TokenResponsepoll(DeviceAuthorization authorization) Polls until the user approves the request, the code expires or the server refuses.voidRuns the whole flow.
-
Field Details
-
SLOW_DOWN_INCREMENT_SECONDS
public static final long SLOW_DOWN_INCREMENT_SECONDSSeconds added to the polling interval each time the server answersslow_down(RFC 8628 section 3.5).- See Also:
-
EXPIRED_TOKEN
The expired token.- See Also:
-
client
The client. -
sleeper
The sleeper. -
clock
The clock. -
output
The output. -
properties
The properties.
-
-
Constructor Details
-
DeviceFlowCli
public DeviceFlowCli()
-
-
Method Details
-
run
Runs the whole flow.- Specified by:
runin interfaceorg.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
Polls until the user approves the request, the code expires or the server refuses.- Parameters:
authorization- the pending authorization.- Returns:
- the issued tokens.
-
pause
Waits, turning an interruption into aDeviceFlowException.- Parameters:
duration- how long to wait.
-
failure
Maps a final error of the token endpoint to an exception.- Parameters:
failed- the error.- Returns:
- the exception to throw.
-