Skip to content

Commit 9388002

Browse files
committed
Add javadoc for OAuth2TokenCustomizer
Issue gh-199
1 parent 1d4dcdd commit 9388002

File tree

7 files changed

+209
-1
lines changed

7 files changed

+209
-1
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/core/context/Context.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,36 @@
2626
*/
2727
public interface Context {
2828

29+
/**
30+
* Returns the value of the attribute associated to the key.
31+
*
32+
* @param key the key for the attribute
33+
* @param <V> the type of the value for the attribute
34+
* @return the value of the attribute associated to the key, or {@code null} if not available
35+
*/
2936
@Nullable
3037
<V> V get(Object key);
3138

39+
/**
40+
* Returns the value of the attribute associated to the key.
41+
*
42+
* @param key the key for the attribute
43+
* @param <V> the type of the value for the attribute
44+
* @return the value of the attribute associated to the key, or {@code null} if not available or not of the specified type
45+
*/
3246
@Nullable
3347
default <V> V get(Class<V> key) {
3448
Assert.notNull(key, "key cannot be null");
3549
V value = get((Object) key);
3650
return key.isInstance(value) ? value : null;
3751
}
3852

53+
/**
54+
* Returns {@code true} if an attribute associated to the key exists, {@code false} otherwise.
55+
*
56+
* @param key the key for the attribute
57+
* @return {@code true} if an attribute associated to the key exists, {@code false} otherwise
58+
*/
3959
boolean hasKey(Object key);
4060

4161
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@
2222

2323
import org.springframework.lang.Nullable;
2424
import org.springframework.security.oauth2.jwt.JoseHeader;
25+
import org.springframework.security.oauth2.jwt.Jwt;
2526
import org.springframework.security.oauth2.jwt.JwtClaimsSet;
27+
import org.springframework.security.oauth2.jwt.JwtEncoder;
2628
import org.springframework.util.Assert;
2729

2830
/**
31+
* An {@link OAuth2TokenContext} implementation used when encoding a {@link Jwt}.
32+
*
2933
* @author Joe Grandja
3034
* @since 0.1.0
3135
* @see OAuth2TokenContext
3236
* @see JoseHeader.Builder
3337
* @see JwtClaimsSet.Builder
38+
* @see JwtEncoder#encode(JoseHeader, JwtClaimsSet)
3439
*/
3540
public final class JwtEncodingContext implements OAuth2TokenContext {
3641
private final Map<Object, Object> context;
@@ -52,18 +57,38 @@ public boolean hasKey(Object key) {
5257
return this.context.containsKey(key);
5358
}
5459

60+
/**
61+
* Returns the {@link JoseHeader.Builder headers}.
62+
*
63+
* @return the {@link JoseHeader.Builder}
64+
*/
5565
public JoseHeader.Builder getHeaders() {
5666
return get(JoseHeader.Builder.class);
5767
}
5868

69+
/**
70+
* Returns the {@link JwtClaimsSet.Builder claims}.
71+
*
72+
* @return the {@link JwtClaimsSet.Builder}
73+
*/
5974
public JwtClaimsSet.Builder getClaims() {
6075
return get(JwtClaimsSet.Builder.class);
6176
}
6277

78+
/**
79+
* Constructs a new {@link Builder} with the provided headers and claims.
80+
*
81+
* @param headersBuilder the headers to initialize the builder
82+
* @param claimsBuilder the claims to initialize the builder
83+
* @return the {@link Builder}
84+
*/
6385
public static Builder with(JoseHeader.Builder headersBuilder, JwtClaimsSet.Builder claimsBuilder) {
6486
return new Builder(headersBuilder, claimsBuilder);
6587
}
6688

89+
/**
90+
* A builder for {@link JwtEncodingContext}.
91+
*/
6792
public static final class Builder extends AbstractBuilder<JwtEncodingContext, Builder> {
6893

6994
private Builder(JoseHeader.Builder headersBuilder, JwtClaimsSet.Builder claimsBuilder) {
@@ -73,18 +98,39 @@ private Builder(JoseHeader.Builder headersBuilder, JwtClaimsSet.Builder claimsBu
7398
put(JwtClaimsSet.Builder.class, claimsBuilder);
7499
}
75100

101+
/**
102+
* A {@code Consumer} of the {@link JoseHeader.Builder headers}
103+
* allowing the ability to add, replace, or remove.
104+
*
105+
* @param headersConsumer a {@code Consumer} of the {@link JoseHeader.Builder headers}
106+
* @return the {@link Builder} for further configuration
107+
*/
76108
public Builder headers(Consumer<JoseHeader.Builder> headersConsumer) {
77109
headersConsumer.accept(get(JoseHeader.Builder.class));
78110
return this;
79111
}
80112

113+
/**
114+
* A {@code Consumer} of the {@link JwtClaimsSet.Builder claims}
115+
* allowing the ability to add, replace, or remove.
116+
*
117+
* @param claimsConsumer a {@code Consumer} of the {@link JwtClaimsSet.Builder claims}
118+
* @return the {@link Builder} for further configuration
119+
*/
81120
public Builder claims(Consumer<JwtClaimsSet.Builder> claimsConsumer) {
82121
claimsConsumer.accept(get(JwtClaimsSet.Builder.class));
83122
return this;
84123
}
85124

125+
/**
126+
* Builds a new {@link JwtEncodingContext}.
127+
*
128+
* @return the {@link JwtEncodingContext}
129+
*/
86130
public JwtEncodingContext build() {
87131
return new JwtEncodingContext(getContext());
88132
}
133+
89134
}
135+
90136
}

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

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,85 +30,188 @@
3030
import org.springframework.util.Assert;
3131

3232
/**
33+
* A context that holds information associated to an OAuth 2.0 Token
34+
* and is used by an {@link OAuth2TokenCustomizer} for customizing the token attributes.
35+
*
3336
* @author Joe Grandja
3437
* @since 0.1.0
3538
* @see Context
39+
* @see OAuth2TokenCustomizer
3640
*/
3741
public interface OAuth2TokenContext extends Context {
3842

43+
/**
44+
* Returns the {@link RegisteredClient registered client}.
45+
*
46+
* @return the {@link RegisteredClient}
47+
*/
3948
default RegisteredClient getRegisteredClient() {
4049
return get(RegisteredClient.class);
4150
}
4251

52+
/**
53+
* Returns the {@link Authentication} representing the {@code Principal} resource owner (or client).
54+
*
55+
* @param <T> the type of the {@code Authentication}
56+
* @return the {@link Authentication} representing the {@code Principal} resource owner (or client)
57+
*/
4358
default <T extends Authentication> T getPrincipal() {
4459
return get(AbstractBuilder.PRINCIPAL_AUTHENTICATION_KEY);
4560
}
4661

62+
/**
63+
* Returns the {@link OAuth2Authorization authorization}.
64+
*
65+
* @return the {@link OAuth2Authorization}, or {@code null} if not available
66+
*/
4767
@Nullable
4868
default OAuth2Authorization getAuthorization() {
4969
return get(OAuth2Authorization.class);
5070
}
5171

72+
/**
73+
* Returns the authorized scope(s).
74+
*
75+
* @return the authorized scope(s)
76+
*/
5277
default Set<String> getAuthorizedScopes() {
5378
return hasKey(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME) ?
5479
get(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME) :
5580
Collections.emptySet();
5681
}
5782

83+
/**
84+
* Returns the {@link OAuth2TokenType token type}.
85+
*
86+
* @return the {@link OAuth2TokenType}
87+
*/
5888
default OAuth2TokenType getTokenType() {
5989
return get(OAuth2TokenType.class);
6090
}
6191

92+
/**
93+
* Returns the {@link AuthorizationGrantType authorization grant type}.
94+
*
95+
* @return the {@link AuthorizationGrantType}
96+
*/
6297
default AuthorizationGrantType getAuthorizationGrantType() {
6398
return get(AuthorizationGrantType.class);
6499
}
65100

101+
/**
102+
* Returns the {@link Authentication} representing the authorization grant.
103+
*
104+
* @param <T> the type of the {@code Authentication}
105+
* @return the {@link Authentication} representing the authorization grant
106+
*/
66107
default <T extends Authentication> T getAuthorizationGrant() {
67108
return get(AbstractBuilder.AUTHORIZATION_GRANT_AUTHENTICATION_KEY);
68109
}
69110

111+
/**
112+
* Base builder for implementations of {@link OAuth2TokenContext}.
113+
*
114+
* @param <T> the type of the context
115+
* @param <B> the type of the builder
116+
*/
70117
abstract class AbstractBuilder<T extends OAuth2TokenContext, B extends AbstractBuilder<T, B>> {
71118
private static final String PRINCIPAL_AUTHENTICATION_KEY =
72119
Authentication.class.getName().concat(".PRINCIPAL");
73120
private static final String AUTHORIZATION_GRANT_AUTHENTICATION_KEY =
74121
Authentication.class.getName().concat(".AUTHORIZATION_GRANT");
75122
private final Map<Object, Object> context = new HashMap<>();
76123

124+
/**
125+
* Sets the {@link RegisteredClient registered client}.
126+
*
127+
* @param registeredClient the {@link RegisteredClient}
128+
* @return the {@link AbstractBuilder} for further configuration
129+
*/
77130
public B registeredClient(RegisteredClient registeredClient) {
78131
return put(RegisteredClient.class, registeredClient);
79132
}
80133

134+
/**
135+
* Sets the {@link Authentication} representing the {@code Principal} resource owner (or client).
136+
*
137+
* @param principal the {@link Authentication} representing the {@code Principal} resource owner (or client)
138+
* @return the {@link AbstractBuilder} for further configuration
139+
*/
81140
public B principal(Authentication principal) {
82141
return put(PRINCIPAL_AUTHENTICATION_KEY, principal);
83142
}
84143

144+
/**
145+
* Sets the {@link OAuth2Authorization authorization}.
146+
*
147+
* @param authorization the {@link OAuth2Authorization}
148+
* @return the {@link AbstractBuilder} for further configuration
149+
*/
85150
public B authorization(OAuth2Authorization authorization) {
86151
return put(OAuth2Authorization.class, authorization);
87152
}
88153

154+
/**
155+
* Sets the authorized scope(s).
156+
*
157+
* @param authorizedScopes the authorized scope(s)
158+
* @return the {@link AbstractBuilder} for further configuration
159+
*/
89160
public B authorizedScopes(Set<String> authorizedScopes) {
90161
return put(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME, authorizedScopes);
91162
}
92163

164+
/**
165+
* Sets the {@link OAuth2TokenType token type}.
166+
*
167+
* @param tokenType the {@link OAuth2TokenType}
168+
* @return the {@link AbstractBuilder} for further configuration
169+
*/
93170
public B tokenType(OAuth2TokenType tokenType) {
94171
return put(OAuth2TokenType.class, tokenType);
95172
}
96173

174+
/**
175+
* Sets the {@link AuthorizationGrantType authorization grant type}.
176+
*
177+
* @param authorizationGrantType the {@link AuthorizationGrantType}
178+
* @return the {@link AbstractBuilder} for further configuration
179+
*/
97180
public B authorizationGrantType(AuthorizationGrantType authorizationGrantType) {
98181
return put(AuthorizationGrantType.class, authorizationGrantType);
99182
}
100183

184+
/**
185+
* Sets the {@link Authentication} representing the authorization grant.
186+
*
187+
* @param authorizationGrant the {@link Authentication} representing the authorization grant
188+
* @return the {@link AbstractBuilder} for further configuration
189+
*/
101190
public B authorizationGrant(Authentication authorizationGrant) {
102191
return put(AUTHORIZATION_GRANT_AUTHENTICATION_KEY, authorizationGrant);
103192
}
104193

194+
/**
195+
* Associates an attribute.
196+
*
197+
* @param key the key for the attribute
198+
* @param value the value of the attribute
199+
* @return the {@link AbstractBuilder} for further configuration
200+
*/
105201
public B put(Object key, Object value) {
106202
Assert.notNull(key, "key cannot be null");
107203
Assert.notNull(value, "value cannot be null");
108204
this.context.put(key, value);
109205
return getThis();
110206
}
111207

208+
/**
209+
* A {@code Consumer} of the attributes {@code Map}
210+
* allowing the ability to add, replace, or remove.
211+
*
212+
* @param contextConsumer a {@link Consumer} of the attributes {@code Map}
213+
* @return the {@link AbstractBuilder} for further configuration
214+
*/
112215
public B context(Consumer<Map<Object, Object>> contextConsumer) {
113216
contextConsumer.accept(this.context);
114217
return getThis();
@@ -128,7 +231,13 @@ protected final B getThis() {
128231
return (B) this;
129232
}
130233

234+
/**
235+
* Builds a new {@link OAuth2TokenContext}.
236+
*
237+
* @return the {@link OAuth2TokenContext}
238+
*/
131239
public abstract T build();
132240

133241
}
242+
134243
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@
1616
package org.springframework.security.oauth2.server.authorization;
1717

1818
/**
19+
* Implementations of this interface are responsible for customizing the
20+
* OAuth 2.0 Token attributes contained within the {@link OAuth2TokenContext}.
21+
*
1922
* @author Joe Grandja
2023
* @since 0.1.0
2124
* @see OAuth2TokenContext
25+
* @param <C> the type of the context containing the OAuth 2.0 Token attributes
2226
*/
2327
@FunctionalInterface
2428
public interface OAuth2TokenCustomizer<C extends OAuth2TokenContext> {
2529

30+
/**
31+
* Customize the OAuth 2.0 Token attributes.
32+
*
33+
* @param context the context containing the OAuth 2.0 Token attributes
34+
*/
2635
void customize(C context);
2736

2837
}

0 commit comments

Comments
 (0)