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;
|
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
import org.junit.Before;
|
2020-04-27 16:52:09 +07:00
|
|
|
import org.junit.Test;
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
2020-05-16 15:27:22 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
|
2020-04-27 16:52:09 +07:00
|
|
|
|
|
|
|
import java.time.Instant;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
|
|
|
|
|
/**
|
2020-05-16 15:27:22 +07:00
|
|
|
* Tests for {@link InMemoryOAuth2AuthorizationService}.
|
2020-04-27 16:52:09 +07:00
|
|
|
*
|
|
|
|
* @author Krisztian Toth
|
|
|
|
*/
|
|
|
|
public class InMemoryOAuth2AuthorizationServiceTests {
|
2020-05-16 15:27:22 +07:00
|
|
|
private static final RegisteredClient REGISTERED_CLIENT = TestRegisteredClients.registeredClient().build();
|
|
|
|
private static final String PRINCIPAL_NAME = "principal";
|
|
|
|
private static final String AUTHORIZATION_CODE = "code";
|
|
|
|
private InMemoryOAuth2AuthorizationService authorizationService;
|
2020-04-27 16:52:09 +07:00
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
@Before
|
|
|
|
public void setup() {
|
|
|
|
this.authorizationService = new InMemoryOAuth2AuthorizationService();
|
|
|
|
}
|
2020-04-27 16:52:09 +07:00
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
@Test
|
|
|
|
public void constructorWhenAuthorizationListNullThenThrowIllegalArgumentException() {
|
|
|
|
assertThatThrownBy(() -> new InMemoryOAuth2AuthorizationService(null))
|
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
|
.hasMessage("authorizations cannot be empty");
|
|
|
|
}
|
2020-04-27 16:52:09 +07:00
|
|
|
|
|
|
|
@Test
|
2020-05-16 15:27:22 +07:00
|
|
|
public void saveWhenAuthorizationNullThenThrowIllegalArgumentException() {
|
|
|
|
assertThatThrownBy(() -> this.authorizationService.save(null))
|
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
|
.hasMessage("authorization cannot be null");
|
|
|
|
}
|
2020-04-27 16:52:09 +07:00
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
@Test
|
|
|
|
public void saveWhenAuthorizationProvidedThenSaved() {
|
|
|
|
OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
.principalName(PRINCIPAL_NAME)
|
2020-05-21 16:46:59 +07:00
|
|
|
.attribute(OAuth2AuthorizationAttributeNames.CODE, AUTHORIZATION_CODE)
|
2020-04-27 16:52:09 +07:00
|
|
|
.build();
|
2020-05-16 15:27:22 +07:00
|
|
|
this.authorizationService.save(expectedAuthorization);
|
2020-04-27 16:52:09 +07:00
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
OAuth2Authorization authorization = this.authorizationService.findByTokenAndTokenType(
|
|
|
|
AUTHORIZATION_CODE, TokenType.AUTHORIZATION_CODE);
|
|
|
|
assertThat(authorization).isEqualTo(expectedAuthorization);
|
2020-04-27 16:52:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-05-16 15:27:22 +07:00
|
|
|
public void findByTokenAndTokenTypeWhenTokenNullThenThrowIllegalArgumentException() {
|
|
|
|
assertThatThrownBy(() -> this.authorizationService.findByTokenAndTokenType(null, TokenType.AUTHORIZATION_CODE))
|
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
|
.hasMessage("token cannot be empty");
|
|
|
|
}
|
2020-04-27 16:52:09 +07:00
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
@Test
|
|
|
|
public void findByTokenAndTokenTypeWhenTokenTypeNullThenThrowIllegalArgumentException() {
|
|
|
|
assertThatThrownBy(() -> this.authorizationService.findByTokenAndTokenType(AUTHORIZATION_CODE, null))
|
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
|
.hasMessage("tokenType cannot be null");
|
2020-04-27 16:52:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-05-16 15:27:22 +07:00
|
|
|
public void findByTokenAndTokenTypeWhenTokenTypeAuthorizationCodeThenFound() {
|
|
|
|
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
.principalName(PRINCIPAL_NAME)
|
2020-05-21 16:46:59 +07:00
|
|
|
.attribute(OAuth2AuthorizationAttributeNames.CODE, AUTHORIZATION_CODE)
|
2020-04-27 16:52:09 +07:00
|
|
|
.build();
|
2020-05-16 15:27:22 +07:00
|
|
|
this.authorizationService = new InMemoryOAuth2AuthorizationService(Collections.singletonList(authorization));
|
2020-04-27 16:52:09 +07:00
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
OAuth2Authorization result = this.authorizationService.findByTokenAndTokenType(
|
|
|
|
AUTHORIZATION_CODE, TokenType.AUTHORIZATION_CODE);
|
2020-04-27 16:52:09 +07:00
|
|
|
assertThat(authorization).isEqualTo(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-05-16 15:27:22 +07:00
|
|
|
public void findByTokenAndTokenTypeWhenTokenTypeAccessTokenThenFound() {
|
|
|
|
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
|
|
|
"access-token", Instant.now().minusSeconds(60), Instant.now());
|
|
|
|
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
|
|
|
|
.principalName(PRINCIPAL_NAME)
|
2020-05-21 16:46:59 +07:00
|
|
|
.attribute(OAuth2AuthorizationAttributeNames.CODE, AUTHORIZATION_CODE)
|
2020-04-27 16:52:09 +07:00
|
|
|
.accessToken(accessToken)
|
|
|
|
.build();
|
2020-05-16 15:27:22 +07:00
|
|
|
this.authorizationService.save(authorization);
|
2020-04-27 16:52:09 +07:00
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
OAuth2Authorization result = this.authorizationService.findByTokenAndTokenType(
|
|
|
|
"access-token", TokenType.ACCESS_TOKEN);
|
2020-04-27 16:52:09 +07:00
|
|
|
assertThat(authorization).isEqualTo(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-05-16 15:27:22 +07:00
|
|
|
public void findByTokenAndTokenTypeWhenTokenDoesNotExistThenNull() {
|
|
|
|
OAuth2Authorization result = this.authorizationService.findByTokenAndTokenType(
|
|
|
|
"access-token", TokenType.ACCESS_TOKEN);
|
2020-04-27 16:52:09 +07:00
|
|
|
assertThat(result).isNull();
|
|
|
|
}
|
|
|
|
}
|