2020-04-27 16:52:09 +07:00
|
|
|
/*
|
|
|
|
* Copyright 2020 the original author or authors.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.springframework.security.oauth2.server.authorization;
|
|
|
|
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
|
|
|
|
/**
|
2020-05-16 15:27:22 +07:00
|
|
|
* An {@link OAuth2AuthorizationService} that stores {@link OAuth2Authorization}'s in-memory.
|
2020-04-27 16:52:09 +07:00
|
|
|
*
|
|
|
|
* @author Krisztian Toth
|
2020-05-16 15:27:22 +07:00
|
|
|
* @since 0.0.1
|
|
|
|
* @see OAuth2AuthorizationService
|
2020-04-27 16:52:09 +07:00
|
|
|
*/
|
|
|
|
public final class InMemoryOAuth2AuthorizationService implements OAuth2AuthorizationService {
|
|
|
|
private final List<OAuth2Authorization> authorizations;
|
|
|
|
|
|
|
|
/**
|
2020-05-16 15:27:22 +07:00
|
|
|
* Constructs an {@code InMemoryOAuth2AuthorizationService}.
|
2020-04-27 16:52:09 +07:00
|
|
|
*/
|
|
|
|
public InMemoryOAuth2AuthorizationService() {
|
2020-05-16 15:27:22 +07:00
|
|
|
this.authorizations = new CopyOnWriteArrayList<>();
|
2020-04-27 16:52:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-16 15:27:22 +07:00
|
|
|
* Constructs an {@code InMemoryOAuth2AuthorizationService} using the provided parameters.
|
2020-04-27 16:52:09 +07:00
|
|
|
*
|
2020-05-16 15:27:22 +07:00
|
|
|
* @param authorizations the initial {@code List} of {@link OAuth2Authorization}(s)
|
2020-04-27 16:52:09 +07:00
|
|
|
*/
|
|
|
|
public InMemoryOAuth2AuthorizationService(List<OAuth2Authorization> authorizations) {
|
2020-05-16 15:27:22 +07:00
|
|
|
Assert.notEmpty(authorizations, "authorizations cannot be empty");
|
2020-04-27 16:52:09 +07:00
|
|
|
this.authorizations = new CopyOnWriteArrayList<>(authorizations);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void save(OAuth2Authorization authorization) {
|
|
|
|
Assert.notNull(authorization, "authorization cannot be null");
|
|
|
|
this.authorizations.add(authorization);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public OAuth2Authorization findByTokenAndTokenType(String token, TokenType tokenType) {
|
|
|
|
Assert.hasText(token, "token cannot be empty");
|
|
|
|
Assert.notNull(tokenType, "tokenType cannot be null");
|
|
|
|
return this.authorizations.stream()
|
2020-05-16 15:27:22 +07:00
|
|
|
.filter(authorization -> hasToken(authorization, token, tokenType))
|
2020-04-27 16:52:09 +07:00
|
|
|
.findFirst()
|
|
|
|
.orElse(null);
|
|
|
|
}
|
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
private boolean hasToken(OAuth2Authorization authorization, String token, TokenType tokenType) {
|
|
|
|
if (TokenType.AUTHORIZATION_CODE.equals(tokenType)) {
|
|
|
|
return token.equals(authorization.getAttributes().get(TokenType.AUTHORIZATION_CODE.getValue()));
|
|
|
|
} else if (TokenType.ACCESS_TOKEN.equals(tokenType)) {
|
|
|
|
return authorization.getAccessToken() != null &&
|
|
|
|
authorization.getAccessToken().getTokenValue().equals(token);
|
2020-04-27 16:52:09 +07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|