@@ -1147,30 +1147,27 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
1147
1147
* @since 5.3
1148
1148
*/
1149
1149
public final static class OpaqueTokenRequestPostProcessor implements RequestPostProcessor {
1150
- private final Map <String , Object > attributes = new HashMap <>();
1151
- private Converter <Map <String , Object >, Instant > expiresAtConverter =
1152
- attributes -> getInstant (attributes , "exp" );
1153
- private Converter <Map <String , Object >, Instant > issuedAtConverter =
1154
- attributes -> getInstant (attributes , "iat" );
1155
- private Converter <Map <String , Object >, Collection <GrantedAuthority >> authoritiesConverter =
1156
- attributes -> getAuthorities (attributes );
1150
+ private Supplier <Map <String , Object >> attributes = this ::defaultAttributes ;
1151
+ private Supplier <Collection <GrantedAuthority >> authorities = this ::defaultAuthorities ;
1157
1152
1158
- private OAuth2AuthenticatedPrincipal principal ;
1153
+ private Supplier < OAuth2AuthenticatedPrincipal > principal = this :: defaultPrincipal ;
1159
1154
1160
- private OpaqueTokenRequestPostProcessor () {
1161
- this .attributes .put (OAuth2IntrospectionClaimNames .SUBJECT , "user" );
1162
- this .attributes .put (OAuth2IntrospectionClaimNames .SCOPE , "read" );
1163
- }
1155
+ private OpaqueTokenRequestPostProcessor () { }
1164
1156
1165
1157
/**
1166
- * Add the provided attribute to the resulting principal
1167
- * @param name the attribute name
1168
- * @param value the attribute value
1158
+ * Mutate the attributes using the given {@link Consumer}
1159
+ *
1160
+ * @param attributesConsumer The {@link Consumer} for mutating the {@Map} of attributes
1169
1161
* @return the {@link OpaqueTokenRequestPostProcessor} for further configuration
1170
1162
*/
1171
- public OpaqueTokenRequestPostProcessor attribute (String name , Object value ) {
1172
- Assert .notNull (name , "name cannot be null" );
1173
- this .attributes .put (name , value );
1163
+ public OpaqueTokenRequestPostProcessor attributes (Consumer <Map <String , Object >> attributesConsumer ) {
1164
+ Assert .notNull (attributesConsumer , "attributesConsumer cannot be null" );
1165
+ this .attributes = () -> {
1166
+ Map <String , Object > attributes = defaultAttributes ();
1167
+ attributesConsumer .accept (attributes );
1168
+ return attributes ;
1169
+ };
1170
+ this .principal = this ::defaultPrincipal ;
1174
1171
return this ;
1175
1172
}
1176
1173
@@ -1181,7 +1178,8 @@ public OpaqueTokenRequestPostProcessor attribute(String name, Object value) {
1181
1178
*/
1182
1179
public OpaqueTokenRequestPostProcessor authorities (Collection <GrantedAuthority > authorities ) {
1183
1180
Assert .notNull (authorities , "authorities cannot be null" );
1184
- this .authoritiesConverter = attributes -> authorities ;
1181
+ this .authorities = () -> authorities ;
1182
+ this .principal = this ::defaultPrincipal ;
1185
1183
return this ;
1186
1184
}
1187
1185
@@ -1192,7 +1190,8 @@ public OpaqueTokenRequestPostProcessor authorities(Collection<GrantedAuthority>
1192
1190
*/
1193
1191
public OpaqueTokenRequestPostProcessor authorities (GrantedAuthority ... authorities ) {
1194
1192
Assert .notNull (authorities , "authorities cannot be null" );
1195
- this .authoritiesConverter = attributes -> Arrays .asList (authorities );
1193
+ this .authorities = () -> Arrays .asList (authorities );
1194
+ this .principal = this ::defaultPrincipal ;
1196
1195
return this ;
1197
1196
}
1198
1197
@@ -1203,46 +1202,41 @@ public OpaqueTokenRequestPostProcessor authorities(GrantedAuthority... authoriti
1203
1202
*/
1204
1203
public OpaqueTokenRequestPostProcessor scopes (String ... scopes ) {
1205
1204
Assert .notNull (scopes , "scopes cannot be null" );
1206
- this .authoritiesConverter = attributes -> getAuthorities (Arrays .asList (scopes ));
1205
+ this .authorities = () -> getAuthorities (Arrays .asList (scopes ));
1206
+ this .principal = this ::defaultPrincipal ;
1207
1207
return this ;
1208
1208
}
1209
1209
1210
1210
/**
1211
1211
* Use the provided principal
1212
- *
1213
- * Providing the principal takes precedence over
1214
- * any authorities or attributes provided via {@link #attribute(String, Object)},
1215
- * {@link #authorities} or {@link #scopes}.
1216
- *
1217
1212
* @param principal the principal to use
1218
1213
* @return the {@link OpaqueTokenRequestPostProcessor} for further configuration
1219
1214
*/
1220
1215
public OpaqueTokenRequestPostProcessor principal (OAuth2AuthenticatedPrincipal principal ) {
1221
1216
Assert .notNull (principal , "principal cannot be null" );
1222
- this .principal = principal ;
1217
+ this .principal = () -> principal ;
1223
1218
return this ;
1224
1219
}
1225
1220
1226
1221
@ Override
1227
1222
public MockHttpServletRequest postProcessRequest (MockHttpServletRequest request ) {
1228
1223
CsrfFilter .skipRequest (request );
1229
- OAuth2AuthenticatedPrincipal principal = getPrincipal ();
1224
+ OAuth2AuthenticatedPrincipal principal = this . principal . get ();
1230
1225
OAuth2AccessToken accessToken = getOAuth2AccessToken (principal );
1231
1226
BearerTokenAuthentication token = new BearerTokenAuthentication
1232
1227
(principal , accessToken , principal .getAuthorities ());
1233
1228
return new AuthenticationRequestPostProcessor (token ).postProcessRequest (request );
1234
1229
}
1235
1230
1236
- private OAuth2AuthenticatedPrincipal getPrincipal () {
1237
- if (this .principal != null ) {
1238
- return this .principal ;
1239
- }
1240
-
1241
- return new DefaultOAuth2AuthenticatedPrincipal
1242
- (this .attributes , this .authoritiesConverter .convert (this .attributes ));
1231
+ private Map <String , Object > defaultAttributes () {
1232
+ Map <String , Object > attributes = new HashMap <>();
1233
+ attributes .put (OAuth2IntrospectionClaimNames .SUBJECT , "user" );
1234
+ attributes .put (OAuth2IntrospectionClaimNames .SCOPE , "read" );
1235
+ return attributes ;
1243
1236
}
1244
1237
1245
- private Collection <GrantedAuthority > getAuthorities (Map <String , Object > attributes ) {
1238
+ private Collection <GrantedAuthority > defaultAuthorities () {
1239
+ Map <String , Object > attributes = this .attributes .get ();
1246
1240
Object scope = attributes .get (OAuth2IntrospectionClaimNames .SCOPE );
1247
1241
if (scope == null ) {
1248
1242
return Collections .emptyList ();
@@ -1257,12 +1251,24 @@ private Collection<GrantedAuthority> getAuthorities(Map<String, Object> attribut
1257
1251
return getAuthorities (Arrays .asList (scopes .split (" " )));
1258
1252
}
1259
1253
1254
+ private OAuth2AuthenticatedPrincipal defaultPrincipal () {
1255
+ return new DefaultOAuth2AuthenticatedPrincipal
1256
+ (this .attributes .get (), this .authorities .get ());
1257
+ }
1258
+
1260
1259
private Collection <GrantedAuthority > getAuthorities (Collection <?> scopes ) {
1261
1260
return scopes .stream ()
1262
1261
.map (scope -> new SimpleGrantedAuthority ("SCOPE_" + scope ))
1263
1262
.collect (Collectors .toList ());
1264
1263
}
1265
1264
1265
+ private OAuth2AccessToken getOAuth2AccessToken (OAuth2AuthenticatedPrincipal principal ) {
1266
+ Instant expiresAt = getInstant (principal .getAttributes (), "exp" );
1267
+ Instant issuedAt = getInstant (principal .getAttributes (), "iat" );
1268
+ return new OAuth2AccessToken (OAuth2AccessToken .TokenType .BEARER ,
1269
+ "token" , issuedAt , expiresAt );
1270
+ }
1271
+
1266
1272
private Instant getInstant (Map <String , Object > attributes , String name ) {
1267
1273
Object value = attributes .get (name );
1268
1274
if (value == null ) {
@@ -1273,13 +1279,6 @@ private Instant getInstant(Map<String, Object> attributes, String name) {
1273
1279
}
1274
1280
throw new IllegalArgumentException (name + " attribute must be of type Instant" );
1275
1281
}
1276
-
1277
- private OAuth2AccessToken getOAuth2AccessToken (OAuth2AuthenticatedPrincipal principal ) {
1278
- Instant expiresAt = this .expiresAtConverter .convert (principal .getAttributes ());
1279
- Instant issuedAt = this .issuedAtConverter .convert (principal .getAttributes ());
1280
- return new OAuth2AccessToken (OAuth2AccessToken .TokenType .BEARER ,
1281
- "token" , issuedAt , expiresAt );
1282
- }
1283
1282
}
1284
1283
1285
1284
/**
0 commit comments