Skip to content

Commit 13a6103

Browse files
author
Steve Riesenberg
committed
Add tests and update examples in docs
Closes gh-1156
1 parent 5c6879d commit 13a6103

File tree

13 files changed

+533
-10
lines changed

13 files changed

+533
-10
lines changed

docs/src/docs/asciidoc/examples/src/main/java/sample/gettingStarted/SecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public RegisteredClientRepository registeredClientRepository() {
116116
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
117117
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
118118
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
119+
.authorizationGrantType(AuthorizationGrantType.DEVICE_CODE)
119120
.redirectUri("http://127.0.0.1:8080/login/oauth2/code/messaging-client-oidc")
120121
.redirectUri("http://127.0.0.1:8080/authorized")
121122
.scope(OidcScopes.OPENID)

docs/src/docs/asciidoc/examples/src/main/java/sample/jpa/entity/authorization/Authorization.java

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-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.
@@ -70,6 +70,20 @@ public class Authorization {
7070
@Column(length = 2000)
7171
private String oidcIdTokenClaims;
7272

73+
@Column(length = 4000)
74+
private String userCodeValue;
75+
private Instant userCodeIssuedAt;
76+
private Instant userCodeExpiresAt;
77+
@Column(length = 2000)
78+
private String userCodeMetadata;
79+
80+
@Column(length = 4000)
81+
private String deviceCodeValue;
82+
private Instant deviceCodeIssuedAt;
83+
private Instant deviceCodeExpiresAt;
84+
@Column(length = 2000)
85+
private String deviceCodeMetadata;
86+
7387
// @fold:on
7488
public String getId() {
7589
return id;
@@ -278,5 +292,69 @@ public String getOidcIdTokenClaims() {
278292
public void setOidcIdTokenClaims(String idTokenClaims) {
279293
this.oidcIdTokenClaims = idTokenClaims;
280294
}
295+
296+
public String getUserCodeValue() {
297+
return this.userCodeValue;
298+
}
299+
300+
public void setUserCodeValue(String userCodeValue) {
301+
this.userCodeValue = userCodeValue;
302+
}
303+
304+
public Instant getUserCodeIssuedAt() {
305+
return this.userCodeIssuedAt;
306+
}
307+
308+
public void setUserCodeIssuedAt(Instant userCodeIssuedAt) {
309+
this.userCodeIssuedAt = userCodeIssuedAt;
310+
}
311+
312+
public Instant getUserCodeExpiresAt() {
313+
return this.userCodeExpiresAt;
314+
}
315+
316+
public void setUserCodeExpiresAt(Instant userCodeExpiresAt) {
317+
this.userCodeExpiresAt = userCodeExpiresAt;
318+
}
319+
320+
public String getUserCodeMetadata() {
321+
return this.userCodeMetadata;
322+
}
323+
324+
public void setUserCodeMetadata(String userCodeMetadata) {
325+
this.userCodeMetadata = userCodeMetadata;
326+
}
327+
328+
public String getDeviceCodeValue() {
329+
return this.deviceCodeValue;
330+
}
331+
332+
public void setDeviceCodeValue(String deviceCodeValue) {
333+
this.deviceCodeValue = deviceCodeValue;
334+
}
335+
336+
public Instant getDeviceCodeIssuedAt() {
337+
return this.deviceCodeIssuedAt;
338+
}
339+
340+
public void setDeviceCodeIssuedAt(Instant deviceCodeIssuedAt) {
341+
this.deviceCodeIssuedAt = deviceCodeIssuedAt;
342+
}
343+
344+
public Instant getDeviceCodeExpiresAt() {
345+
return this.deviceCodeExpiresAt;
346+
}
347+
348+
public void setDeviceCodeExpiresAt(Instant deviceCodeExpiresAt) {
349+
this.deviceCodeExpiresAt = deviceCodeExpiresAt;
350+
}
351+
352+
public String getDeviceCodeMetadata() {
353+
return this.deviceCodeMetadata;
354+
}
355+
356+
public void setDeviceCodeMetadata(String deviceCodeMetadata) {
357+
this.deviceCodeMetadata = deviceCodeMetadata;
358+
}
281359
// @fold:off
282360
}

docs/src/docs/asciidoc/examples/src/main/java/sample/jpa/repository/authorization/AuthorizationRepository.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ public interface AuthorizationRepository extends JpaRepository<Authorization, St
3131
Optional<Authorization> findByAccessTokenValue(String accessToken);
3232
Optional<Authorization> findByRefreshTokenValue(String refreshToken);
3333
Optional<Authorization> findByOidcIdTokenValue(String idToken);
34+
Optional<Authorization> findByUserCodeValue(String userCode);
35+
Optional<Authorization> findByDeviceCodeValue(String deviceCode);
3436
@Query("select a from Authorization a where a.state = :token" +
3537
" or a.authorizationCodeValue = :token" +
3638
" or a.accessTokenValue = :token" +
3739
" or a.refreshTokenValue = :token" +
38-
" or a.oidcIdTokenValue = :token"
40+
" or a.oidcIdTokenValue = :token" +
41+
" or a.userCodeValue = :token" +
42+
" or a.deviceCodeValue = :token"
3943
)
40-
Optional<Authorization> findByStateOrAuthorizationCodeValueOrAccessTokenValueOrRefreshTokenValueOrOidcIdTokenValue(@Param("token") String token);
44+
Optional<Authorization> findByStateOrAuthorizationCodeValueOrAccessTokenValueOrRefreshTokenValueOrOidcIdTokenValueOrUserCodeValueOrDeviceCodeValue(@Param("token") String token);
4145
}

docs/src/docs/asciidoc/examples/src/main/java/sample/jpa/service/authorization/JpaOAuth2AuthorizationService.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import org.springframework.security.jackson2.SecurityJackson2Modules;
3232
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3333
import org.springframework.security.oauth2.core.OAuth2AccessToken;
34+
import org.springframework.security.oauth2.core.OAuth2DeviceCode;
3435
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
3536
import org.springframework.security.oauth2.core.OAuth2Token;
37+
import org.springframework.security.oauth2.core.OAuth2UserCode;
3638
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
3739
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
3840
import org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames;
@@ -89,7 +91,7 @@ public OAuth2Authorization findByToken(String token, OAuth2TokenType tokenType)
8991

9092
Optional<Authorization> result;
9193
if (tokenType == null) {
92-
result = this.authorizationRepository.findByStateOrAuthorizationCodeValueOrAccessTokenValueOrRefreshTokenValueOrOidcIdTokenValue(token);
94+
result = this.authorizationRepository.findByStateOrAuthorizationCodeValueOrAccessTokenValueOrRefreshTokenValueOrOidcIdTokenValueOrUserCodeValueOrDeviceCodeValue(token);
9395
} else if (OAuth2ParameterNames.STATE.equals(tokenType.getValue())) {
9496
result = this.authorizationRepository.findByState(token);
9597
} else if (OAuth2ParameterNames.CODE.equals(tokenType.getValue())) {
@@ -100,6 +102,10 @@ public OAuth2Authorization findByToken(String token, OAuth2TokenType tokenType)
100102
result = this.authorizationRepository.findByRefreshTokenValue(token);
101103
} else if (OidcParameterNames.ID_TOKEN.equals(tokenType.getValue())) {
102104
result = this.authorizationRepository.findByOidcIdTokenValue(token);
105+
} else if (OAuth2ParameterNames.USER_CODE.equals(tokenType.getValue())) {
106+
result = this.authorizationRepository.findByUserCodeValue(token);
107+
} else if (OAuth2ParameterNames.DEVICE_CODE.equals(tokenType.getValue())) {
108+
result = this.authorizationRepository.findByDeviceCodeValue(token);
103109
} else {
104110
result = Optional.empty();
105111
}
@@ -159,6 +165,22 @@ private OAuth2Authorization toObject(Authorization entity) {
159165
builder.token(idToken, metadata -> metadata.putAll(parseMap(entity.getOidcIdTokenMetadata())));
160166
}
161167

168+
if (entity.getUserCodeValue() != null) {
169+
OAuth2UserCode userCode = new OAuth2UserCode(
170+
entity.getUserCodeValue(),
171+
entity.getUserCodeIssuedAt(),
172+
entity.getUserCodeExpiresAt());
173+
builder.token(userCode, metadata -> metadata.putAll(parseMap(entity.getUserCodeMetadata())));
174+
}
175+
176+
if (entity.getUserCodeValue() != null) {
177+
OAuth2DeviceCode deviceCode = new OAuth2DeviceCode(
178+
entity.getDeviceCodeValue(),
179+
entity.getDeviceCodeIssuedAt(),
180+
entity.getDeviceCodeExpiresAt());
181+
builder.token(deviceCode, metadata -> metadata.putAll(parseMap(entity.getDeviceCodeMetadata())));
182+
}
183+
162184
return builder.build();
163185
}
164186

@@ -218,6 +240,26 @@ private Authorization toEntity(OAuth2Authorization authorization) {
218240
entity.setOidcIdTokenClaims(writeMap(oidcIdToken.getClaims()));
219241
}
220242

243+
OAuth2Authorization.Token<OAuth2UserCode> userCode =
244+
authorization.getToken(OAuth2UserCode.class);
245+
setTokenValues(
246+
userCode,
247+
entity::setUserCodeValue,
248+
entity::setUserCodeIssuedAt,
249+
entity::setUserCodeExpiresAt,
250+
entity::setUserCodeMetadata
251+
);
252+
253+
OAuth2Authorization.Token<OAuth2DeviceCode> deviceCode =
254+
authorization.getToken(OAuth2DeviceCode.class);
255+
setTokenValues(
256+
deviceCode,
257+
entity::setDeviceCodeValue,
258+
entity::setDeviceCodeIssuedAt,
259+
entity::setDeviceCodeExpiresAt,
260+
entity::setDeviceCodeMetadata
261+
);
262+
221263
return entity;
222264
}
223265

@@ -260,6 +302,8 @@ private static AuthorizationGrantType resolveAuthorizationGrantType(String autho
260302
return AuthorizationGrantType.CLIENT_CREDENTIALS;
261303
} else if (AuthorizationGrantType.REFRESH_TOKEN.getValue().equals(authorizationGrantType)) {
262304
return AuthorizationGrantType.REFRESH_TOKEN;
305+
} else if (AuthorizationGrantType.DEVICE_CODE.getValue().equals(authorizationGrantType)) {
306+
return AuthorizationGrantType.DEVICE_CODE;
263307
}
264308
return new AuthorizationGrantType(authorizationGrantType); // Custom authorization grant type
265309
}

docs/src/docs/asciidoc/examples/src/test/java/sample/AuthorizationCodeGrantFlow.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-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.
@@ -109,7 +109,7 @@ public String authorize(RegisteredClient registeredClient) throws Exception {
109109
* Submit consent for the authorization request and obtain an authorization code.
110110
*
111111
* @param registeredClient The registered client
112-
* @param state The state paramter from the authorization request
112+
* @param state The state parameter from the authorization request
113113
* @return An authorization code
114114
*/
115115
public String submitConsent(RegisteredClient registeredClient, String state) throws Exception {

0 commit comments

Comments
 (0)