3333import org .springframework .util .Assert ;
3434
3535/**
36- * An {@link OAuth2AuthorizationService} that stores {@link OAuth2Authorization}'s in-memory.
36+ * An {@link OAuth2AuthorizationService} that stores {@link OAuth2Authorization}'s
37+ * in-memory.
3738 *
3839 * <p>
3940 * <b>NOTE:</b> This implementation should ONLY be used during development/testing.
4445 * @see OAuth2AuthorizationService
4546 */
4647public final class InMemoryOAuth2AuthorizationService implements OAuth2AuthorizationService {
48+
4749 private int maxInitializedAuthorizations = 100 ;
4850
4951 /*
50- * Stores "initialized" (uncompleted) authorizations, where an access token has not yet been granted.
51- * This state occurs with the authorization_code grant flow during the user consent step OR
52- * when the code is returned in the authorization response but the access token request is not yet initiated.
52+ * Stores "initialized" (uncompleted) authorizations, where an access token has not
53+ * yet been granted. This state occurs with the authorization_code grant flow during
54+ * the user consent step OR when the code is returned in the authorization response
55+ * but the access token request is not yet initiated.
5356 */
54- private Map <String , OAuth2Authorization > initializedAuthorizations =
55- Collections .synchronizedMap (new MaxSizeHashMap <>(this .maxInitializedAuthorizations ));
57+ private Map <String , OAuth2Authorization > initializedAuthorizations = Collections
58+ .synchronizedMap (new MaxSizeHashMap <>(this .maxInitializedAuthorizations ));
5659
5760 /*
5861 * Stores "completed" authorizations, where an access token has been granted.
@@ -64,7 +67,8 @@ public final class InMemoryOAuth2AuthorizationService implements OAuth2Authoriza
6467 */
6568 InMemoryOAuth2AuthorizationService (int maxInitializedAuthorizations ) {
6669 this .maxInitializedAuthorizations = maxInitializedAuthorizations ;
67- this .initializedAuthorizations = Collections .synchronizedMap (new MaxSizeHashMap <>(this .maxInitializedAuthorizations ));
70+ this .initializedAuthorizations = Collections
71+ .synchronizedMap (new MaxSizeHashMap <>(this .maxInitializedAuthorizations ));
6872 }
6973
7074 /**
@@ -75,17 +79,17 @@ public InMemoryOAuth2AuthorizationService() {
7579 }
7680
7781 /**
78- * Constructs an {@code InMemoryOAuth2AuthorizationService} using the provided parameters.
79- *
82+ * Constructs an {@code InMemoryOAuth2AuthorizationService} using the provided
83+ * parameters.
8084 * @param authorizations the authorization(s)
8185 */
8286 public InMemoryOAuth2AuthorizationService (OAuth2Authorization ... authorizations ) {
8387 this (Arrays .asList (authorizations ));
8488 }
8589
8690 /**
87- * Constructs an {@code InMemoryOAuth2AuthorizationService} using the provided parameters.
88- *
91+ * Constructs an {@code InMemoryOAuth2AuthorizationService} using the provided
92+ * parameters.
8993 * @param authorizations the authorization(s)
9094 */
9195 public InMemoryOAuth2AuthorizationService (List <OAuth2Authorization > authorizations ) {
@@ -103,7 +107,8 @@ public void save(OAuth2Authorization authorization) {
103107 Assert .notNull (authorization , "authorization cannot be null" );
104108 if (isComplete (authorization )) {
105109 this .authorizations .put (authorization .getId (), authorization );
106- } else {
110+ }
111+ else {
107112 this .initializedAuthorizations .put (authorization .getId (), authorization );
108113 }
109114 }
@@ -113,7 +118,8 @@ public void remove(OAuth2Authorization authorization) {
113118 Assert .notNull (authorization , "authorization cannot be null" );
114119 if (isComplete (authorization )) {
115120 this .authorizations .remove (authorization .getId (), authorization );
116- } else {
121+ }
122+ else {
117123 this .initializedAuthorizations .remove (authorization .getId (), authorization );
118124 }
119125 }
@@ -123,9 +129,7 @@ public void remove(OAuth2Authorization authorization) {
123129 public OAuth2Authorization findById (String id ) {
124130 Assert .hasText (id , "id cannot be empty" );
125131 OAuth2Authorization authorization = this .authorizations .get (id );
126- return authorization != null ?
127- authorization :
128- this .initializedAuthorizations .get (id );
132+ return authorization != null ? authorization : this .initializedAuthorizations .get (id );
129133 }
130134
131135 @ Nullable
@@ -149,7 +153,9 @@ private static boolean isComplete(OAuth2Authorization authorization) {
149153 return authorization .getAccessToken () != null ;
150154 }
151155
152- private static boolean hasToken (OAuth2Authorization authorization , String token , @ Nullable OAuth2TokenType tokenType ) {
156+ private static boolean hasToken (OAuth2Authorization authorization , String token ,
157+ @ Nullable OAuth2TokenType tokenType ) {
158+ // @formatter:off
153159 if (tokenType == null ) {
154160 return matchesState (authorization , token ) ||
155161 matchesAuthorizationCode (authorization , token ) ||
@@ -173,6 +179,7 @@ private static boolean hasToken(OAuth2Authorization authorization, String token,
173179 } else if (OAuth2ParameterNames .USER_CODE .equals (tokenType .getValue ())) {
174180 return matchesUserCode (authorization , token );
175181 }
182+ // @formatter:on
176183 return false ;
177184 }
178185
@@ -181,42 +188,38 @@ private static boolean matchesState(OAuth2Authorization authorization, String to
181188 }
182189
183190 private static boolean matchesAuthorizationCode (OAuth2Authorization authorization , String token ) {
184- OAuth2Authorization .Token <OAuth2AuthorizationCode > authorizationCode =
185- authorization .getToken (OAuth2AuthorizationCode .class );
191+ OAuth2Authorization .Token <OAuth2AuthorizationCode > authorizationCode = authorization
192+ .getToken (OAuth2AuthorizationCode .class );
186193 return authorizationCode != null && authorizationCode .getToken ().getTokenValue ().equals (token );
187194 }
188195
189196 private static boolean matchesAccessToken (OAuth2Authorization authorization , String token ) {
190- OAuth2Authorization .Token <OAuth2AccessToken > accessToken =
191- authorization .getToken (OAuth2AccessToken .class );
197+ OAuth2Authorization .Token <OAuth2AccessToken > accessToken = authorization .getToken (OAuth2AccessToken .class );
192198 return accessToken != null && accessToken .getToken ().getTokenValue ().equals (token );
193199 }
194200
195201 private static boolean matchesRefreshToken (OAuth2Authorization authorization , String token ) {
196- OAuth2Authorization .Token <OAuth2RefreshToken > refreshToken =
197- authorization .getToken (OAuth2RefreshToken .class );
202+ OAuth2Authorization .Token <OAuth2RefreshToken > refreshToken = authorization .getToken (OAuth2RefreshToken .class );
198203 return refreshToken != null && refreshToken .getToken ().getTokenValue ().equals (token );
199204 }
200205
201206 private static boolean matchesIdToken (OAuth2Authorization authorization , String token ) {
202- OAuth2Authorization .Token <OidcIdToken > idToken =
203- authorization .getToken (OidcIdToken .class );
207+ OAuth2Authorization .Token <OidcIdToken > idToken = authorization .getToken (OidcIdToken .class );
204208 return idToken != null && idToken .getToken ().getTokenValue ().equals (token );
205209 }
206210
207211 private static boolean matchesDeviceCode (OAuth2Authorization authorization , String token ) {
208- OAuth2Authorization .Token <OAuth2DeviceCode > deviceCode =
209- authorization .getToken (OAuth2DeviceCode .class );
212+ OAuth2Authorization .Token <OAuth2DeviceCode > deviceCode = authorization .getToken (OAuth2DeviceCode .class );
210213 return deviceCode != null && deviceCode .getToken ().getTokenValue ().equals (token );
211214 }
212215
213216 private static boolean matchesUserCode (OAuth2Authorization authorization , String token ) {
214- OAuth2Authorization .Token <OAuth2UserCode > userCode =
215- authorization .getToken (OAuth2UserCode .class );
217+ OAuth2Authorization .Token <OAuth2UserCode > userCode = authorization .getToken (OAuth2UserCode .class );
216218 return userCode != null && userCode .getToken ().getTokenValue ().equals (token );
217219 }
218220
219221 private static final class MaxSizeHashMap <K , V > extends LinkedHashMap <K , V > {
222+
220223 private final int maxSize ;
221224
222225 private MaxSizeHashMap (int maxSize ) {
0 commit comments