Skip to content

Commit cd1ea48

Browse files
committed
Add getter & builder methods for OAuth2AuthorizationRequest and OAuth2AuthorizationConsent inside OAuth2AuthenticationContext
1 parent 8be811f commit cd1ea48

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AuthorizationCodeRequestAuthenticationContext.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.function.Consumer;
2222

2323
import org.springframework.lang.Nullable;
24+
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
25+
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsent;
2426
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
2527
import org.springframework.util.Assert;
2628

@@ -63,6 +65,27 @@ public RegisteredClient getRegisteredClient() {
6365
return get(RegisteredClient.class);
6466
}
6567

68+
/**
69+
* Returns the {@link OAuth2AuthorizationRequest oauth2 authorization request}.
70+
*
71+
* @return the {@link OAuth2AuthorizationRequest}
72+
*/
73+
@Nullable
74+
public OAuth2AuthorizationRequest getOAuth2AuthorizationRequest() {
75+
return get(OAuth2AuthorizationRequest.class);
76+
}
77+
78+
/**
79+
* Returns the {@link OAuth2AuthorizationConsent oauth2 authorization consent}.
80+
*
81+
* @return the {@link OAuth2AuthorizationConsent}
82+
*/
83+
@Nullable
84+
public OAuth2AuthorizationConsent getOAuth2AuthorizationConsent() {
85+
return get(OAuth2AuthorizationConsent.class);
86+
}
87+
88+
6689
/**
6790
* Constructs a new {@link Builder} with the provided {@link OAuth2AuthorizationCodeRequestAuthenticationToken}.
6891
*
@@ -78,6 +101,20 @@ public static Builder with(OAuth2AuthorizationCodeRequestAuthenticationToken aut
78101
*/
79102
public static final class Builder extends AbstractBuilder<OAuth2AuthorizationCodeRequestAuthenticationContext, Builder> {
80103

104+
/**
105+
* Associates an attribute.
106+
*
107+
* @param key the key for the attribute
108+
* @param value the value of the attribute
109+
* @return the {@link Builder} for further configuration
110+
*/
111+
@Override
112+
public Builder put(Object key, Object value) {
113+
Assert.notNull(key, "key cannot be null");
114+
getContext().put(key, value);
115+
return getThis();
116+
}
117+
81118
private Builder(OAuth2AuthorizationCodeRequestAuthenticationToken authentication) {
82119
super(authentication);
83120
}
@@ -92,6 +129,26 @@ public Builder registeredClient(RegisteredClient registeredClient) {
92129
return put(RegisteredClient.class, registeredClient);
93130
}
94131

132+
/**
133+
* Sets the {@link OAuth2AuthorizationRequest oauth2 authorization request}.
134+
*
135+
* @param oAuth2AuthorizationRequest the {@link OAuth2AuthorizationRequest}
136+
* @return the {@link Builder} for further configuration
137+
*/
138+
public Builder oAuth2AuthorizationRequest(OAuth2AuthorizationRequest oAuth2AuthorizationRequest) {
139+
return put(OAuth2AuthorizationRequest.class, oAuth2AuthorizationRequest);
140+
}
141+
142+
/**
143+
* Sets the {@link OAuth2AuthorizationConsent oauth2 authorization consent}.
144+
*
145+
* @param oAuth2AuthorizationConsent the {@link OAuth2AuthorizationConsent}
146+
* @return the {@link Builder} for further configuration
147+
*/
148+
public Builder oAuth2AuthorizationConsent(OAuth2AuthorizationConsent oAuth2AuthorizationConsent) {
149+
return put(OAuth2AuthorizationConsent.class, oAuth2AuthorizationConsent);
150+
}
151+
95152
/**
96153
* Builds a new {@link OAuth2AuthorizationCodeRequestAuthenticationContext}.
97154
*

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AuthorizationCodeRequestAuthenticationProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ public Authentication authenticate(Authentication authentication) throws Authent
177177
OAuth2AuthorizationCodeRequestAuthenticationContext contextWithAuthorizationRequestAndAuthorizationConsent =
178178
OAuth2AuthorizationCodeRequestAuthenticationContext.with(authorizationCodeRequestAuthentication)
179179
.registeredClient(registeredClient)
180-
.context(context -> context.put(OAuth2AuthorizationRequest.class, authorizationRequest))
181-
.context(context -> context.put(OAuth2AuthorizationConsent.class, currentAuthorizationConsent))
180+
.oAuth2AuthorizationConsent(currentAuthorizationConsent)
181+
.oAuth2AuthorizationRequest(authorizationRequest)
182182
.build();
183183

184184
if (requiresAuthorizationConsent.test(contextWithAuthorizationRequestAndAuthorizationConsent)) {
@@ -296,14 +296,14 @@ private boolean requireAuthorizationConsent(OAuth2AuthorizationCodeRequestAuthen
296296
return false;
297297
}
298298

299-
OAuth2AuthorizationRequest authorizationRequest = context.get(OAuth2AuthorizationRequest.class);
299+
OAuth2AuthorizationRequest authorizationRequest = context.getOAuth2AuthorizationRequest();
300300
// 'openid' scope does not require consent
301301
if (authorizationRequest.getScopes().contains(OidcScopes.OPENID) &&
302302
authorizationRequest.getScopes().size() == 1) {
303303
return false;
304304
}
305305

306-
OAuth2AuthorizationConsent authorizationConsent = context.get(OAuth2AuthorizationConsent.class);
306+
OAuth2AuthorizationConsent authorizationConsent = context.getOAuth2AuthorizationConsent();
307307
if (authorizationConsent != null &&
308308
authorizationConsent.getScopes().containsAll(authorizationRequest.getScopes())) {
309309
return false;

0 commit comments

Comments
 (0)