Skip to content

Commit 8c17b97

Browse files
author
Steve Riesenberg
committed
Add support for device authorization response
Closes gh-12852
1 parent ac1d269 commit 8c17b97

File tree

7 files changed

+829
-2
lines changed

7 files changed

+829
-2
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/AuthorizationGrantType.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
* extensibility mechanism for defining additional grant types.
3333
*
3434
* @author Joe Grandja
35+
* @author Steve Riesenberg
3536
* @since 5.0
3637
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-1.3">Section
3738
* 1.3 Authorization Grant</a>
@@ -62,6 +63,12 @@ public final class AuthorizationGrantType implements Serializable {
6263
public static final AuthorizationGrantType JWT_BEARER = new AuthorizationGrantType(
6364
"urn:ietf:params:oauth:grant-type:jwt-bearer");
6465

66+
/**
67+
* @since 6.1
68+
*/
69+
public static final AuthorizationGrantType DEVICE_CODE = new AuthorizationGrantType(
70+
"urn:ietf:params:oauth:grant-type:device_code");
71+
6572
private final String value;
6673

6774
/**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.oauth2.core;
18+
19+
import java.time.Instant;
20+
21+
/**
22+
* An implementation of an {@link AbstractOAuth2Token} representing a device code as part
23+
* of the OAuth 2.0 Device Authorization Grant.
24+
*
25+
* @author Steve Riesenberg
26+
* @since 6.1
27+
* @see OAuth2UserCode
28+
* @see <a target="_blank" href= "https://tools.ietf.org/html/rfc8628#section-3.2">Section
29+
* 3.2 Device Authorization Response</a>
30+
*/
31+
public final class OAuth2DeviceCode extends AbstractOAuth2Token {
32+
33+
/**
34+
* Constructs an {@code OAuth2DeviceCode} using the provided parameters.
35+
* @param tokenValue the token value
36+
* @param issuedAt the time at which the token was issued
37+
* @param expiresAt the time at which the token expires
38+
*/
39+
public OAuth2DeviceCode(String tokenValue, Instant issuedAt, Instant expiresAt) {
40+
super(tokenValue, issuedAt, expiresAt);
41+
}
42+
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.oauth2.core;
18+
19+
import java.time.Instant;
20+
21+
/**
22+
* An implementation of an {@link AbstractOAuth2Token} representing a user code as part of
23+
* the OAuth 2.0 Device Authorization Grant.
24+
*
25+
* @author Steve Riesenberg
26+
* @since 6.1
27+
* @see OAuth2DeviceCode
28+
* @see <a target="_blank" href= "https://tools.ietf.org/html/rfc8628#section-3.2">Section
29+
* 3.2 Device Authorization Response</a>
30+
*/
31+
public final class OAuth2UserCode extends AbstractOAuth2Token {
32+
33+
/**
34+
* Constructs an {@code OAuth2UserCode} using the provided parameters.
35+
* @param tokenValue the token value
36+
* @param issuedAt the time at which the token was issued
37+
* @param expiresAt the time at which the token expires
38+
*/
39+
public OAuth2UserCode(String tokenValue, Instant issuedAt, Instant expiresAt) {
40+
super(tokenValue, issuedAt, expiresAt);
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.oauth2.core.endpoint;
18+
19+
import java.time.Instant;
20+
import java.time.temporal.ChronoUnit;
21+
import java.util.Collections;
22+
import java.util.Map;
23+
24+
import org.springframework.security.oauth2.core.OAuth2DeviceCode;
25+
import org.springframework.security.oauth2.core.OAuth2UserCode;
26+
import org.springframework.util.Assert;
27+
import org.springframework.util.CollectionUtils;
28+
29+
/**
30+
* A representation of an OAuth 2.0 Device Authorization Response.
31+
*
32+
* @author Steve Riesenberg
33+
* @since 6.1
34+
* @see OAuth2DeviceCode
35+
* @see OAuth2UserCode
36+
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc8628#section-3.2">Section
37+
* 3.2 Device Authorization Response</a>
38+
*/
39+
public final class OAuth2DeviceAuthorizationResponse {
40+
41+
private OAuth2DeviceCode deviceCode;
42+
43+
private OAuth2UserCode userCode;
44+
45+
private String verificationUri;
46+
47+
private String verificationUriComplete;
48+
49+
private long interval;
50+
51+
private Map<String, Object> additionalParameters;
52+
53+
private OAuth2DeviceAuthorizationResponse() {
54+
}
55+
56+
/**
57+
* Returns the {@link OAuth2DeviceCode Device Code}.
58+
* @return the {@link OAuth2DeviceCode}
59+
*/
60+
public OAuth2DeviceCode getDeviceCode() {
61+
return this.deviceCode;
62+
}
63+
64+
/**
65+
* Returns the {@link OAuth2UserCode User Code}.
66+
* @return the {@link OAuth2UserCode}
67+
*/
68+
public OAuth2UserCode getUserCode() {
69+
return this.userCode;
70+
}
71+
72+
/**
73+
* Returns the end-user verification URI.
74+
* @return the end-user verification URI
75+
*/
76+
public String getVerificationUri() {
77+
return this.verificationUri;
78+
}
79+
80+
/**
81+
* Returns the end-user verification URI that includes the user code.
82+
* @return the end-user verification URI that includes the user code
83+
*/
84+
public String getVerificationUriComplete() {
85+
return this.verificationUriComplete;
86+
}
87+
88+
/**
89+
* Returns the minimum amount of time (in seconds) that the client should wait between
90+
* polling requests to the token endpoint.
91+
* @return the minimum amount of time between polling requests
92+
*/
93+
public long getInterval() {
94+
return this.interval;
95+
}
96+
97+
/**
98+
* Returns the additional parameters returned in the response.
99+
* @return a {@code Map} of the additional parameters returned in the response, may be
100+
* empty.
101+
*/
102+
public Map<String, Object> getAdditionalParameters() {
103+
return this.additionalParameters;
104+
}
105+
106+
/**
107+
* Returns a new {@link Builder}, initialized with the provided device code and user
108+
* code values.
109+
* @param deviceCode the value of the device code
110+
* @param userCode the value of the user code
111+
* @return the {@link Builder}
112+
*/
113+
public static Builder with(String deviceCode, String userCode) {
114+
Assert.hasText(deviceCode, "deviceCode cannot be empty");
115+
Assert.hasText(userCode, "userCode cannot be empty");
116+
return new Builder(deviceCode, userCode);
117+
}
118+
119+
/**
120+
* Returns a new {@link Builder}, initialized with the provided device code and user
121+
* code.
122+
* @param deviceCode the {@link OAuth2DeviceCode}
123+
* @param userCode the {@link OAuth2UserCode}
124+
* @return the {@link Builder}
125+
*/
126+
public static Builder with(OAuth2DeviceCode deviceCode, OAuth2UserCode userCode) {
127+
Assert.notNull(deviceCode, "deviceCode cannot be null");
128+
Assert.notNull(userCode, "userCode cannot be null");
129+
return new Builder(deviceCode, userCode);
130+
}
131+
132+
/**
133+
* Returns a new {@link Builder}, initialized with the provided response.
134+
* @param deviceAuthorizationResponse the response to initialize the builder with
135+
* @return the {@link Builder}
136+
*/
137+
public static Builder withResponse(OAuth2DeviceAuthorizationResponse deviceAuthorizationResponse) {
138+
Assert.notNull(deviceAuthorizationResponse, "deviceAuthorizationResponse cannot be null");
139+
return new Builder(deviceAuthorizationResponse);
140+
}
141+
142+
/**
143+
* A builder for {@link OAuth2DeviceAuthorizationResponse}.
144+
*/
145+
public static final class Builder {
146+
147+
private final String deviceCode;
148+
149+
private final String userCode;
150+
151+
private String verificationUri;
152+
153+
private String verificationUriComplete;
154+
155+
private long expiresIn;
156+
157+
private long interval;
158+
159+
private Map<String, Object> additionalParameters;
160+
161+
private Builder(OAuth2DeviceAuthorizationResponse response) {
162+
OAuth2DeviceCode deviceCode = response.getDeviceCode();
163+
OAuth2UserCode userCode = response.getUserCode();
164+
this.deviceCode = deviceCode.getTokenValue();
165+
this.userCode = userCode.getTokenValue();
166+
this.verificationUri = response.getVerificationUri();
167+
this.verificationUriComplete = response.getVerificationUriComplete();
168+
this.expiresIn = ChronoUnit.SECONDS.between(deviceCode.getIssuedAt(), deviceCode.getExpiresAt());
169+
this.interval = response.getInterval();
170+
}
171+
172+
private Builder(OAuth2DeviceCode deviceCode, OAuth2UserCode userCode) {
173+
this.deviceCode = deviceCode.getTokenValue();
174+
this.userCode = userCode.getTokenValue();
175+
this.expiresIn = ChronoUnit.SECONDS.between(deviceCode.getIssuedAt(), deviceCode.getExpiresAt());
176+
}
177+
178+
private Builder(String deviceCode, String userCode) {
179+
this.deviceCode = deviceCode;
180+
this.userCode = userCode;
181+
}
182+
183+
/**
184+
* Sets the end-user verification URI.
185+
* @param verificationUri the end-user verification URI
186+
* @return the {@link Builder}
187+
*/
188+
public Builder verificationUri(String verificationUri) {
189+
this.verificationUri = verificationUri;
190+
return this;
191+
}
192+
193+
/**
194+
* Sets the end-user verification URI that includes the user code.
195+
* @param verificationUriComplete the end-user verification URI that includes the
196+
* user code
197+
* @return the {@link Builder}
198+
*/
199+
public Builder verificationUriComplete(String verificationUriComplete) {
200+
this.verificationUriComplete = verificationUriComplete;
201+
return this;
202+
}
203+
204+
/**
205+
* Sets the lifetime (in seconds) of the device code and user code.
206+
* @param expiresIn the lifetime (in seconds) of the device code and user code
207+
* @return the {@link Builder}
208+
*/
209+
public Builder expiresIn(long expiresIn) {
210+
this.expiresIn = expiresIn;
211+
return this;
212+
}
213+
214+
/**
215+
* Sets the minimum amount of time (in seconds) that the client should wait
216+
* between polling requests to the token endpoint.
217+
* @param interval the minimum amount of time between polling requests
218+
* @return the {@link Builder}
219+
*/
220+
public Builder interval(long interval) {
221+
this.interval = interval;
222+
return this;
223+
}
224+
225+
/**
226+
* Sets the additional parameters returned in the response.
227+
* @param additionalParameters the additional parameters returned in the response
228+
* @return the {@link Builder}
229+
*/
230+
public Builder additionalParameters(Map<String, Object> additionalParameters) {
231+
this.additionalParameters = additionalParameters;
232+
return this;
233+
}
234+
235+
/**
236+
* Builds a new {@link OAuth2DeviceAuthorizationResponse}.
237+
* @return a {@link OAuth2DeviceAuthorizationResponse}
238+
*/
239+
public OAuth2DeviceAuthorizationResponse build() {
240+
Assert.hasText(this.verificationUri, "verificationUri cannot be empty");
241+
Assert.isTrue(this.expiresIn > 0, "expiresIn must be greater than zero");
242+
243+
Instant issuedAt = Instant.now();
244+
Instant expiresAt = issuedAt.plusSeconds(this.expiresIn);
245+
OAuth2DeviceCode deviceCode = new OAuth2DeviceCode(this.deviceCode, issuedAt, expiresAt);
246+
OAuth2UserCode userCode = new OAuth2UserCode(this.userCode, issuedAt, expiresAt);
247+
248+
OAuth2DeviceAuthorizationResponse deviceAuthorizationResponse = new OAuth2DeviceAuthorizationResponse();
249+
deviceAuthorizationResponse.deviceCode = deviceCode;
250+
deviceAuthorizationResponse.userCode = userCode;
251+
deviceAuthorizationResponse.verificationUri = this.verificationUri;
252+
deviceAuthorizationResponse.verificationUriComplete = this.verificationUriComplete;
253+
deviceAuthorizationResponse.interval = this.interval;
254+
deviceAuthorizationResponse.additionalParameters = Collections
255+
.unmodifiableMap(CollectionUtils.isEmpty(this.additionalParameters) ? Collections.emptyMap()
256+
: this.additionalParameters);
257+
258+
return deviceAuthorizationResponse;
259+
}
260+
261+
}
262+
263+
}

0 commit comments

Comments
 (0)