Skip to content

Commit d2a1fca

Browse files
committed
Merge pull request #34957 from sjohnr
* gh-34957: Polish "Add properties to support device grant" Add properties to support device grant Closes gh-34957
2 parents c3e739c + 4eb7558 commit d2a1fca

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/server/servlet/OAuth2AuthorizationServerProperties.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ public static class Endpoint {
9999
*/
100100
private String authorizationUri;
101101

102+
/**
103+
* Authorization Server's OAuth 2.0 Device Authorization Endpoint.
104+
*/
105+
private String deviceAuthorizationUri;
106+
107+
/**
108+
* Authorization Server's OAuth 2.0 Device Verification Endpoint.
109+
*/
110+
private String deviceVerificationUri;
111+
102112
/**
103113
* Authorization Server's OAuth 2.0 Token Endpoint.
104114
*/
@@ -133,6 +143,22 @@ public void setAuthorizationUri(String authorizationUri) {
133143
this.authorizationUri = authorizationUri;
134144
}
135145

146+
public String getDeviceAuthorizationUri() {
147+
return this.deviceAuthorizationUri;
148+
}
149+
150+
public void setDeviceAuthorizationUri(String deviceAuthorizationUri) {
151+
this.deviceAuthorizationUri = deviceAuthorizationUri;
152+
}
153+
154+
public String getDeviceVerificationUri() {
155+
return this.deviceVerificationUri;
156+
}
157+
158+
public void setDeviceVerificationUri(String deviceVerificationUri) {
159+
this.deviceVerificationUri = deviceVerificationUri;
160+
}
161+
136162
public String getTokenUri() {
137163
return this.tokenUri;
138164
}
@@ -430,6 +456,11 @@ public static class Token {
430456
*/
431457
private String accessTokenFormat;
432458

459+
/**
460+
* Time-to-live for a device code.
461+
*/
462+
private Duration deviceCodeTimeToLive = Duration.ofMinutes(5);
463+
433464
/**
434465
* Whether refresh tokens are reused or a new refresh token is issued when
435466
* returning the access token response.
@@ -470,6 +501,14 @@ public void setAccessTokenFormat(String accessTokenFormat) {
470501
this.accessTokenFormat = accessTokenFormat;
471502
}
472503

504+
public Duration getDeviceCodeTimeToLive() {
505+
return this.deviceCodeTimeToLive;
506+
}
507+
508+
public void setDeviceCodeTimeToLive(Duration deviceCodeTimeToLive) {
509+
this.deviceCodeTimeToLive = deviceCodeTimeToLive;
510+
}
511+
473512
public boolean isReuseRefreshTokens() {
474513
return this.reuseRefreshTokens;
475514
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/server/servlet/OAuth2AuthorizationServerPropertiesMapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ AuthorizationServerSettings asAuthorizationServerSettings() {
5353
AuthorizationServerSettings.Builder builder = AuthorizationServerSettings.builder();
5454
map.from(this.properties::getIssuer).to(builder::issuer);
5555
map.from(endpoint::getAuthorizationUri).to(builder::authorizationEndpoint);
56+
map.from(endpoint::getDeviceAuthorizationUri).to(builder::deviceAuthorizationEndpoint);
57+
map.from(endpoint::getDeviceVerificationUri).to(builder::deviceVerificationEndpoint);
5658
map.from(endpoint::getTokenUri).to(builder::tokenEndpoint);
5759
map.from(endpoint::getJwkSetUri).to(builder::jwkSetEndpoint);
5860
map.from(endpoint::getTokenRevocationUri).to(builder::tokenRevocationEndpoint);
@@ -111,6 +113,7 @@ private TokenSettings getTokenSettings(Client client, PropertyMapper map) {
111113
map.from(token::getAuthorizationCodeTimeToLive).to(builder::authorizationCodeTimeToLive);
112114
map.from(token::getAccessTokenTimeToLive).to(builder::accessTokenTimeToLive);
113115
map.from(token::getAccessTokenFormat).as(OAuth2TokenFormat::new).to(builder::accessTokenFormat);
116+
map.from(token::getDeviceCodeTimeToLive).to(builder::deviceCodeTimeToLive);
114117
map.from(token::isReuseRefreshTokens).to(builder::reuseRefreshTokens);
115118
map.from(token::getRefreshTokenTimeToLive).to(builder::refreshTokenTimeToLive);
116119
map.from(token::getIdTokenSignatureAlgorithm)

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/server/servlet/OAuth2AuthorizationServerAutoConfigurationTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ void authorizationServerSettingsBeanShouldBeCreatedWhenPropertiesPresent() {
124124
this.contextRunner
125125
.withPropertyValues(PROPERTIES_PREFIX + ".issuer=https://example.com",
126126
PROPERTIES_PREFIX + ".endpoint.authorization-uri=/authorize",
127+
PROPERTIES_PREFIX + ".endpoint.device-authorization-uri=/device_authorization",
128+
PROPERTIES_PREFIX + ".endpoint.device-verification-uri=/device_verification",
127129
PROPERTIES_PREFIX + ".endpoint.token-uri=/token", PROPERTIES_PREFIX + ".endpoint.jwk-set-uri=/jwks",
128130
PROPERTIES_PREFIX + ".endpoint.token-revocation-uri=/revoke",
129131
PROPERTIES_PREFIX + ".endpoint.token-introspection-uri=/introspect",
@@ -134,6 +136,8 @@ void authorizationServerSettingsBeanShouldBeCreatedWhenPropertiesPresent() {
134136
AuthorizationServerSettings settings = context.getBean(AuthorizationServerSettings.class);
135137
assertThat(settings.getIssuer()).isEqualTo("https://example.com");
136138
assertThat(settings.getAuthorizationEndpoint()).isEqualTo("/authorize");
139+
assertThat(settings.getDeviceAuthorizationEndpoint()).isEqualTo("/device_authorization");
140+
assertThat(settings.getDeviceVerificationEndpoint()).isEqualTo("/device_verification");
137141
assertThat(settings.getTokenEndpoint()).isEqualTo("/token");
138142
assertThat(settings.getJwkSetEndpoint()).isEqualTo("/jwks");
139143
assertThat(settings.getTokenRevocationEndpoint()).isEqualTo("/revoke");

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/server/servlet/OAuth2AuthorizationServerPropertiesMapperTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void getRegisteredClientsWhenValidParametersShouldAdapt() {
6666
assertThat(registeredClient.getTokenSettings().getAccessTokenFormat()).isEqualTo(OAuth2TokenFormat.REFERENCE);
6767
assertThat(registeredClient.getTokenSettings().getAccessTokenTimeToLive()).isEqualTo(Duration.ofSeconds(300));
6868
assertThat(registeredClient.getTokenSettings().getRefreshTokenTimeToLive()).isEqualTo(Duration.ofHours(24));
69+
assertThat(registeredClient.getTokenSettings().getDeviceCodeTimeToLive()).isEqualTo(Duration.ofMinutes(30));
6970
assertThat(registeredClient.getTokenSettings().isReuseRefreshTokens()).isEqualTo(true);
7071
assertThat(registeredClient.getTokenSettings().getIdTokenSignatureAlgorithm())
7172
.isEqualTo(SignatureAlgorithm.RS512);
@@ -89,6 +90,7 @@ private OAuth2AuthorizationServerProperties.Client createClient() {
8990
token.setAccessTokenFormat("reference");
9091
token.setAccessTokenTimeToLive(Duration.ofSeconds(300));
9192
token.setRefreshTokenTimeToLive(Duration.ofHours(24));
93+
token.setDeviceCodeTimeToLive(Duration.ofMinutes(30));
9294
token.setReuseRefreshTokens(true);
9395
token.setIdTokenSignatureAlgorithm("rs512");
9496
return client;
@@ -99,6 +101,8 @@ void getAuthorizationServerSettingsWhenValidParametersShouldAdapt() {
99101
this.properties.setIssuer("https://example.com");
100102
OAuth2AuthorizationServerProperties.Endpoint endpoints = this.properties.getEndpoint();
101103
endpoints.setAuthorizationUri("/authorize");
104+
endpoints.setDeviceAuthorizationUri("/device_authorization");
105+
endpoints.setDeviceVerificationUri("/device_verification");
102106
endpoints.setTokenUri("/token");
103107
endpoints.setJwkSetUri("/jwks");
104108
endpoints.setTokenRevocationUri("/revoke");
@@ -110,6 +114,8 @@ void getAuthorizationServerSettingsWhenValidParametersShouldAdapt() {
110114
AuthorizationServerSettings settings = this.mapper.asAuthorizationServerSettings();
111115
assertThat(settings.getIssuer()).isEqualTo("https://example.com");
112116
assertThat(settings.getAuthorizationEndpoint()).isEqualTo("/authorize");
117+
assertThat(settings.getDeviceAuthorizationEndpoint()).isEqualTo("/device_authorization");
118+
assertThat(settings.getDeviceVerificationEndpoint()).isEqualTo("/device_verification");
113119
assertThat(settings.getTokenEndpoint()).isEqualTo("/token");
114120
assertThat(settings.getJwkSetEndpoint()).isEqualTo("/jwks");
115121
assertThat(settings.getTokenRevocationEndpoint()).isEqualTo("/revoke");

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/server/servlet/OAuth2AuthorizationServerPropertiesTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21+
import org.springframework.security.oauth2.server.authorization.settings.TokenSettings;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
2124
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2225

2326
/**
@@ -69,4 +72,10 @@ void authorizationGrantTypesEmptyThrowsException() {
6972
.withMessage("Authorization grant types must not be empty.");
7073
}
7174

75+
@Test
76+
void defaultDeviceCodeTimeToLiveMatchesBuilderDefault() {
77+
assertThat(new OAuth2AuthorizationServerProperties.Client().getToken().getDeviceCodeTimeToLive())
78+
.isEqualTo(TokenSettings.builder().build().getDeviceCodeTimeToLive());
79+
}
80+
7281
}

0 commit comments

Comments
 (0)