Polish gh-71

This commit is contained in:
Joe Grandja 2020-05-16 04:27:22 -04:00
parent 7a6a316dbe
commit 3ab314c8aa
5 changed files with 206 additions and 235 deletions

View File

@ -17,33 +17,33 @@ package org.springframework.security.oauth2.server.authorization;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
/** /**
* In-memory implementation of {@link OAuth2AuthorizationService}. * An {@link OAuth2AuthorizationService} that stores {@link OAuth2Authorization}'s in-memory.
* *
* @author Krisztian Toth * @author Krisztian Toth
* @since 0.0.1
* @see OAuth2AuthorizationService
*/ */
public final class InMemoryOAuth2AuthorizationService implements OAuth2AuthorizationService { public final class InMemoryOAuth2AuthorizationService implements OAuth2AuthorizationService {
private final List<OAuth2Authorization> authorizations; private final List<OAuth2Authorization> authorizations;
/** /**
* Creates an {@link InMemoryOAuth2AuthorizationService}. * Constructs an {@code InMemoryOAuth2AuthorizationService}.
*/ */
public InMemoryOAuth2AuthorizationService() { public InMemoryOAuth2AuthorizationService() {
this(Collections.emptyList()); this.authorizations = new CopyOnWriteArrayList<>();
} }
/** /**
* Creates an {@link InMemoryOAuth2AuthorizationService} with the provided {@link List}<{@link OAuth2Authorization}> * Constructs an {@code InMemoryOAuth2AuthorizationService} using the provided parameters.
* as the in-memory store.
* *
* @param authorizations a {@link List}<{@link OAuth2Authorization}> object to use as the store * @param authorizations the initial {@code List} of {@link OAuth2Authorization}(s)
*/ */
public InMemoryOAuth2AuthorizationService(List<OAuth2Authorization> authorizations) { public InMemoryOAuth2AuthorizationService(List<OAuth2Authorization> authorizations) {
Assert.notNull(authorizations, "authorizations cannot be null"); Assert.notEmpty(authorizations, "authorizations cannot be empty");
this.authorizations = new CopyOnWriteArrayList<>(authorizations); this.authorizations = new CopyOnWriteArrayList<>(authorizations);
} }
@ -58,26 +58,18 @@ public final class InMemoryOAuth2AuthorizationService implements OAuth2Authoriza
Assert.hasText(token, "token cannot be empty"); Assert.hasText(token, "token cannot be empty");
Assert.notNull(tokenType, "tokenType cannot be null"); Assert.notNull(tokenType, "tokenType cannot be null");
return this.authorizations.stream() return this.authorizations.stream()
.filter(authorization -> doesMatch(authorization, token, tokenType)) .filter(authorization -> hasToken(authorization, token, tokenType))
.findFirst() .findFirst()
.orElse(null); .orElse(null);
} }
private boolean doesMatch(OAuth2Authorization authorization, String token, TokenType tokenType) { private boolean hasToken(OAuth2Authorization authorization, String token, TokenType tokenType) {
if (tokenType.equals(TokenType.ACCESS_TOKEN)) { if (TokenType.AUTHORIZATION_CODE.equals(tokenType)) {
return isAccessTokenEqual(token, authorization); return token.equals(authorization.getAttributes().get(TokenType.AUTHORIZATION_CODE.getValue()));
} else if (tokenType.equals(TokenType.AUTHORIZATION_CODE)) { } else if (TokenType.ACCESS_TOKEN.equals(tokenType)) {
return isAuthorizationCodeEqual(token, authorization); return authorization.getAccessToken() != null &&
authorization.getAccessToken().getTokenValue().equals(token);
} }
return false; return false;
} }
private boolean isAccessTokenEqual(String token, OAuth2Authorization authorization) {
return authorization.getAccessToken() != null && token.equals(authorization.getAccessToken().getTokenValue());
}
private boolean isAuthorizationCodeEqual(String token, OAuth2Authorization authorization) {
return token.equals(authorization.getAttributes().get(TokenType.AUTHORIZATION_CODE.getValue()));
}
} }

View File

@ -16,8 +16,10 @@
package org.springframework.security.oauth2.server.authorization; package org.springframework.security.oauth2.server.authorization;
import org.springframework.security.oauth2.core.OAuth2AccessToken; import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.io.Serializable;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -25,12 +27,18 @@ import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
* Represents a collection of attributes which describe an OAuth 2.0 authorization context. * A representation of an OAuth 2.0 Authorization,
* which holds state related to the authorization granted to the {@link #getRegisteredClientId() client}
* by the {@link #getPrincipalName() resource owner}.
* *
* @author Joe Grandja * @author Joe Grandja
* @author Krisztian Toth * @author Krisztian Toth
* @since 0.0.1
* @see RegisteredClient
* @see OAuth2AccessToken
*/ */
public class OAuth2Authorization { public class OAuth2Authorization implements Serializable {
private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
private String registeredClientId; private String registeredClientId;
private String principalName; private String principalName;
private OAuth2AccessToken accessToken; private OAuth2AccessToken accessToken;
@ -39,43 +47,64 @@ public class OAuth2Authorization {
protected OAuth2Authorization() { protected OAuth2Authorization() {
} }
/**
* Returns the identifier for the {@link RegisteredClient#getId() registered client}.
*
* @return the {@link RegisteredClient#getId()}
*/
public String getRegisteredClientId() { public String getRegisteredClientId() {
return this.registeredClientId; return this.registeredClientId;
} }
/**
* Returns the resource owner's {@code Principal} name.
*
* @return the resource owner's {@code Principal} name
*/
public String getPrincipalName() { public String getPrincipalName() {
return this.principalName; return this.principalName;
} }
/**
* Returns the {@link OAuth2AccessToken access token} credential.
*
* @return the {@link OAuth2AccessToken}
*/
public OAuth2AccessToken getAccessToken() { public OAuth2AccessToken getAccessToken() {
return this.accessToken; return this.accessToken;
} }
/**
* Returns the attribute(s) associated to the authorization.
*
* @return a {@code Map} of the attribute(s)
*/
public Map<String, Object> getAttributes() { public Map<String, Object> getAttributes() {
return this.attributes; return this.attributes;
} }
/** /**
* Returns an attribute with the provided name or {@code null} if not found. * Returns the value of an attribute associated to the authorization.
* *
* @param name the name of the attribute * @param name the name of the attribute
* @param <T> the type of the attribute * @param <T> the type of the attribute
* @return the found attribute or {@code null} * @return the value of the attribute associated to the authorization, or {@code null} if not available
*/ */
@SuppressWarnings("unchecked")
public <T> T getAttribute(String name) { public <T> T getAttribute(String name) {
Assert.hasText(name, "name cannot be empty"); Assert.hasText(name, "name cannot be empty");
return (T) this.attributes.get(name); return (T) this.attributes.get(name);
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object obj) {
if (this == o) { if (this == obj) {
return true; return true;
} }
if (o == null || getClass() != o.getClass()) { if (obj == null || getClass() != obj.getClass()) {
return false; return false;
} }
OAuth2Authorization that = (OAuth2Authorization) o; OAuth2Authorization that = (OAuth2Authorization) obj;
return Objects.equals(this.registeredClientId, that.registeredClientId) && return Objects.equals(this.registeredClientId, that.registeredClientId) &&
Objects.equals(this.principalName, that.principalName) && Objects.equals(this.principalName, that.principalName) &&
Objects.equals(this.accessToken, that.accessToken) && Objects.equals(this.accessToken, that.accessToken) &&
@ -88,59 +117,34 @@ public class OAuth2Authorization {
} }
/** /**
* Returns an empty {@link Builder}. * Returns a new {@link Builder}, initialized with the provided {@link RegisteredClient#getId()}.
* *
* @param registeredClient the {@link RegisteredClient}
* @return the {@link Builder} * @return the {@link Builder}
*/ */
public static Builder builder() { public static Builder withRegisteredClient(RegisteredClient registeredClient) {
return new Builder(); Assert.notNull(registeredClient, "registeredClient cannot be null");
return new Builder(registeredClient.getId());
} }
/** /**
* Returns a new {@link Builder}, initialized with the provided {@link OAuth2Authorization}. * A builder for {@link OAuth2Authorization}.
*
* @param authorization the {@link OAuth2Authorization} to copy from
* @return the {@link Builder}
*/ */
public static Builder withAuthorization(OAuth2Authorization authorization) { public static class Builder implements Serializable {
Assert.notNull(authorization, "authorization cannot be null"); private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
return new Builder(authorization);
}
/**
* Builder class for {@link OAuth2Authorization}.
*/
public static class Builder {
private String registeredClientId; private String registeredClientId;
private String principalName; private String principalName;
private OAuth2AccessToken accessToken; private OAuth2AccessToken accessToken;
private Map<String, Object> attributes = new HashMap<>(); private Map<String, Object> attributes = new HashMap<>();
protected Builder() { protected Builder(String registeredClientId) {
}
protected Builder(OAuth2Authorization authorization) {
this.registeredClientId = authorization.registeredClientId;
this.principalName = authorization.principalName;
this.accessToken = authorization.accessToken;
this.attributes = authorization.attributes;
}
/**
* Sets the registered client identifier.
*
* @param registeredClientId the client id
* @return the {@link Builder}
*/
public Builder registeredClientId(String registeredClientId) {
this.registeredClientId = registeredClientId; this.registeredClientId = registeredClientId;
return this;
} }
/** /**
* Sets the principal name. * Sets the resource owner's {@code Principal} name.
* *
* @param principalName the principal name * @param principalName the resource owner's {@code Principal} name
* @return the {@link Builder} * @return the {@link Builder}
*/ */
public Builder principalName(String principalName) { public Builder principalName(String principalName) {
@ -149,7 +153,7 @@ public class OAuth2Authorization {
} }
/** /**
* Sets the {@link OAuth2AccessToken}. * Sets the {@link OAuth2AccessToken access token} credential.
* *
* @param accessToken the {@link OAuth2AccessToken} * @param accessToken the {@link OAuth2AccessToken}
* @return the {@link Builder} * @return the {@link Builder}
@ -160,23 +164,24 @@ public class OAuth2Authorization {
} }
/** /**
* Adds the attribute with the specified name and {@link String} value to the attributes map. * Adds an attribute associated to the authorization.
* *
* @param name the name of the attribute * @param name the name of the attribute
* @param value the value of the attribute * @param value the value of the attribute
* @return the {@link Builder} * @return the {@link Builder}
*/ */
public Builder attribute(String name, String value) { public Builder attribute(String name, Object value) {
Assert.hasText(name, "name cannot be empty"); Assert.hasText(name, "name cannot be empty");
Assert.hasText(value, "value cannot be empty"); Assert.notNull(value, "value cannot be null");
this.attributes.put(name, value); this.attributes.put(name, value);
return this; return this;
} }
/** /**
* A {@code Consumer} of the attributes map allowing to access or modify its content. * A {@code Consumer} of the attributes {@code Map}
* allowing the ability to add, replace, or remove.
* *
* @param attributesConsumer a {@link Consumer} of the attributes map * @param attributesConsumer a {@link Consumer} of the attributes {@code Map}
* @return the {@link Builder} * @return the {@link Builder}
*/ */
public Builder attributes(Consumer<Map<String, Object>> attributesConsumer) { public Builder attributes(Consumer<Map<String, Object>> attributesConsumer) {
@ -190,22 +195,15 @@ public class OAuth2Authorization {
* @return the {@link OAuth2Authorization} * @return the {@link OAuth2Authorization}
*/ */
public OAuth2Authorization build() { public OAuth2Authorization build() {
Assert.hasText(this.registeredClientId, "registeredClientId cannot be empty");
Assert.hasText(this.principalName, "principalName cannot be empty"); Assert.hasText(this.principalName, "principalName cannot be empty");
if (this.accessToken == null && this.attributes.get(TokenType.AUTHORIZATION_CODE.getValue()) == null) { Assert.notNull(this.attributes.get(TokenType.AUTHORIZATION_CODE.getValue()), "authorization code cannot be null");
throw new IllegalArgumentException("either accessToken has to be set or the authorization code with key '"
+ TokenType.AUTHORIZATION_CODE.getValue() + "' must be provided in the attributes map");
}
return create();
}
private OAuth2Authorization create() { OAuth2Authorization authorization = new OAuth2Authorization();
OAuth2Authorization oAuth2Authorization = new OAuth2Authorization(); authorization.registeredClientId = this.registeredClientId;
oAuth2Authorization.registeredClientId = this.registeredClientId; authorization.principalName = this.principalName;
oAuth2Authorization.principalName = this.principalName; authorization.accessToken = this.accessToken;
oAuth2Authorization.accessToken = this.accessToken; authorization.attributes = Collections.unmodifiableMap(this.attributes);
oAuth2Authorization.attributes = Collections.unmodifiableMap(this.attributes); return authorization;
return oAuth2Authorization;
} }
} }
} }

View File

@ -16,12 +16,30 @@
package org.springframework.security.oauth2.server.authorization; package org.springframework.security.oauth2.server.authorization;
/** /**
* Implementations of this interface are responsible for the management
* of {@link OAuth2Authorization OAuth 2.0 Authorization(s)}.
*
* @author Joe Grandja * @author Joe Grandja
* @since 0.0.1
* @see OAuth2Authorization
*/ */
public interface OAuth2AuthorizationService { public interface OAuth2AuthorizationService {
/**
* Saves the {@link OAuth2Authorization}.
*
* @param authorization the {@link OAuth2Authorization}
*/
void save(OAuth2Authorization authorization); void save(OAuth2Authorization authorization);
/**
* Returns the {@link OAuth2Authorization} containing the provided {@code token},
* or {@code null} if not found.
*
* @param token the token credential
* @param tokenType the {@link TokenType token type}
* @return the {@link OAuth2Authorization} if found, otherwise {@code null}
*/
OAuth2Authorization findByTokenAndTokenType(String token, TokenType tokenType); OAuth2Authorization findByTokenAndTokenType(String token, TokenType tokenType);
} }

View File

@ -15,105 +15,108 @@
*/ */
package org.springframework.security.oauth2.server.authorization; package org.springframework.security.oauth2.server.authorization;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.security.oauth2.core.OAuth2AccessToken; import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* Unit tests for {@link InMemoryOAuth2AuthorizationService}. * Tests for {@link InMemoryOAuth2AuthorizationService}.
* *
* @author Krisztian Toth * @author Krisztian Toth
*/ */
public class InMemoryOAuth2AuthorizationServiceTests { public class InMemoryOAuth2AuthorizationServiceTests {
private static final RegisteredClient REGISTERED_CLIENT = TestRegisteredClients.registeredClient().build();
private static final String TOKEN = "token"; private static final String PRINCIPAL_NAME = "principal";
private static final TokenType AUTHORIZATION_CODE = TokenType.AUTHORIZATION_CODE; private static final String AUTHORIZATION_CODE = "code";
private static final TokenType ACCESS_TOKEN = TokenType.ACCESS_TOKEN;
private static final Instant ISSUED_AT = Instant.now().minusSeconds(60);
private static final Instant EXPIRES_AT = Instant.now();
private InMemoryOAuth2AuthorizationService authorizationService; private InMemoryOAuth2AuthorizationService authorizationService;
@Test @Before
public void saveWhenAuthorizationProvidedThenSavedInList() { public void setup() {
authorizationService = new InMemoryOAuth2AuthorizationService(new ArrayList<>()); this.authorizationService = new InMemoryOAuth2AuthorizationService();
OAuth2Authorization authorization = OAuth2Authorization.builder()
.registeredClientId("clientId")
.principalName("principalName")
.attribute(AUTHORIZATION_CODE.getValue(), TOKEN)
.build();
authorizationService.save(authorization);
assertThat(authorizationService.findByTokenAndTokenType(TOKEN, AUTHORIZATION_CODE)).isEqualTo(authorization);
} }
@Test @Test
public void saveWhenAuthorizationNotProvidedThenThrowIllegalArgumentException() { public void constructorWhenAuthorizationListNullThenThrowIllegalArgumentException() {
authorizationService = new InMemoryOAuth2AuthorizationService(new ArrayList<>()); assertThatThrownBy(() -> new InMemoryOAuth2AuthorizationService(null))
.isInstanceOf(IllegalArgumentException.class)
assertThatThrownBy(() -> authorizationService.save(null)) .hasMessage("authorizations cannot be empty");
.isInstanceOf(IllegalArgumentException.class);
} }
@Test @Test
public void findByTokenAndTokenTypeWhenTokenTypeIsAuthorizationCodeThenFound() { public void saveWhenAuthorizationNullThenThrowIllegalArgumentException() {
OAuth2Authorization authorization = OAuth2Authorization.builder() assertThatThrownBy(() -> this.authorizationService.save(null))
.registeredClientId("clientId") .isInstanceOf(IllegalArgumentException.class)
.principalName("principalName") .hasMessage("authorization cannot be null");
.attribute(AUTHORIZATION_CODE.getValue(), TOKEN)
.build();
authorizationService = new InMemoryOAuth2AuthorizationService(Collections.singletonList(authorization));
OAuth2Authorization result = authorizationService.findByTokenAndTokenType(TOKEN, TokenType.AUTHORIZATION_CODE);
assertThat(authorization).isEqualTo(result);
} }
@Test @Test
public void findByTokenAndTokenTypeWhenTokenTypeIsAccessTokenThenFound() { public void saveWhenAuthorizationProvidedThenSaved() {
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, TOKEN, ISSUED_AT, OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
EXPIRES_AT); .principalName(PRINCIPAL_NAME)
OAuth2Authorization authorization = OAuth2Authorization.builder() .attribute(TokenType.AUTHORIZATION_CODE.getValue(), AUTHORIZATION_CODE)
.registeredClientId("clientId")
.principalName("principalName")
.accessToken(accessToken)
.build(); .build();
authorizationService = new InMemoryOAuth2AuthorizationService(Collections.singletonList(authorization)); this.authorizationService.save(expectedAuthorization);
OAuth2Authorization result = authorizationService.findByTokenAndTokenType(TOKEN, ACCESS_TOKEN); OAuth2Authorization authorization = this.authorizationService.findByTokenAndTokenType(
assertThat(authorization).isEqualTo(result); AUTHORIZATION_CODE, TokenType.AUTHORIZATION_CODE);
} assertThat(authorization).isEqualTo(expectedAuthorization);
@Test
public void findByTokenAndTokenTypeWhenTokenWithTokenTypeDoesNotExistThenNull() {
OAuth2Authorization authorization = OAuth2Authorization.builder()
.registeredClientId("clientId")
.principalName("principalName")
.attribute(AUTHORIZATION_CODE.getValue(), TOKEN)
.build();
authorizationService = new InMemoryOAuth2AuthorizationService(Collections.singletonList(authorization));
OAuth2Authorization result = authorizationService.findByTokenAndTokenType(TOKEN, ACCESS_TOKEN);
assertThat(result).isNull();
} }
@Test @Test
public void findByTokenAndTokenTypeWhenTokenNullThenThrowIllegalArgumentException() { public void findByTokenAndTokenTypeWhenTokenNullThenThrowIllegalArgumentException() {
authorizationService = new InMemoryOAuth2AuthorizationService(); assertThatThrownBy(() -> this.authorizationService.findByTokenAndTokenType(null, TokenType.AUTHORIZATION_CODE))
assertThatThrownBy(() -> authorizationService.findByTokenAndTokenType(null, TokenType.AUTHORIZATION_CODE)) .isInstanceOf(IllegalArgumentException.class)
.isInstanceOf(IllegalArgumentException.class); .hasMessage("token cannot be empty");
} }
@Test @Test
public void findByTokenAndTokenTypeWhenTokenTypeNullThenThrowIllegalArgumentException() { public void findByTokenAndTokenTypeWhenTokenTypeNullThenThrowIllegalArgumentException() {
authorizationService = new InMemoryOAuth2AuthorizationService(); assertThatThrownBy(() -> this.authorizationService.findByTokenAndTokenType(AUTHORIZATION_CODE, null))
assertThatThrownBy(() -> authorizationService.findByTokenAndTokenType(TOKEN, null)) .isInstanceOf(IllegalArgumentException.class)
.isInstanceOf(IllegalArgumentException.class); .hasMessage("tokenType cannot be null");
}
@Test
public void findByTokenAndTokenTypeWhenTokenTypeAuthorizationCodeThenFound() {
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
.principalName(PRINCIPAL_NAME)
.attribute(TokenType.AUTHORIZATION_CODE.getValue(), AUTHORIZATION_CODE)
.build();
this.authorizationService = new InMemoryOAuth2AuthorizationService(Collections.singletonList(authorization));
OAuth2Authorization result = this.authorizationService.findByTokenAndTokenType(
AUTHORIZATION_CODE, TokenType.AUTHORIZATION_CODE);
assertThat(authorization).isEqualTo(result);
}
@Test
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)
.attribute(TokenType.AUTHORIZATION_CODE.getValue(), AUTHORIZATION_CODE)
.accessToken(accessToken)
.build();
this.authorizationService.save(authorization);
OAuth2Authorization result = this.authorizationService.findByTokenAndTokenType(
"access-token", TokenType.ACCESS_TOKEN);
assertThat(authorization).isEqualTo(result);
}
@Test
public void findByTokenAndTokenTypeWhenTokenDoesNotExistThenNull() {
OAuth2Authorization result = this.authorizationService.findByTokenAndTokenType(
"access-token", TokenType.ACCESS_TOKEN);
assertThat(result).isNull();
} }
} }

View File

@ -17,120 +17,80 @@ package org.springframework.security.oauth2.server.authorization;
import org.junit.Test; import org.junit.Test;
import org.springframework.security.oauth2.core.OAuth2AccessToken; import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
import java.time.Instant; import java.time.Instant;
import java.util.Collections;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.MapEntry.entry;
/** /**
* Unit tests For {@link OAuth2Authorization}. * Tests for {@link OAuth2Authorization}.
* *
* @author Krisztian Toth * @author Krisztian Toth
*/ */
public class OAuth2AuthorizationTests { public class OAuth2AuthorizationTests {
private static final RegisteredClient REGISTERED_CLIENT = TestRegisteredClients.registeredClient().build();
public static final String REGISTERED_CLIENT_ID = "clientId"; private static final String PRINCIPAL_NAME = "principal";
public static final String PRINCIPAL_NAME = "principal"; private static final OAuth2AccessToken ACCESS_TOKEN = new OAuth2AccessToken(
public static final OAuth2AccessToken ACCESS_TOKEN = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, OAuth2AccessToken.TokenType.BEARER, "access-token", Instant.now().minusSeconds(60), Instant.now());
"token", Instant.now().minusSeconds(60), Instant.now()); private static final String AUTHORIZATION_CODE = "code";
public static final String AUTHORIZATION_CODE_VALUE = TokenType.AUTHORIZATION_CODE.getValue();
public static final String CODE = "code";
public static final Map<String, Object> ATTRIBUTES = Collections.singletonMap(AUTHORIZATION_CODE_VALUE, CODE);
@Test @Test
public void buildWhenAllAttributesAreProvidedThenAllAttributesAreSet() { public void withRegisteredClientWhenRegisteredClientNullThenThrowIllegalArgumentException() {
OAuth2Authorization authorization = OAuth2Authorization.builder() assertThatThrownBy(() -> OAuth2Authorization.withRegisteredClient(null))
.registeredClientId(REGISTERED_CLIENT_ID) .isInstanceOf(IllegalArgumentException.class)
.principalName(PRINCIPAL_NAME) .hasMessage("registeredClient cannot be null");
.accessToken(ACCESS_TOKEN)
.attribute(AUTHORIZATION_CODE_VALUE, CODE)
.build();
assertThat(authorization.getRegisteredClientId()).isEqualTo(REGISTERED_CLIENT_ID);
assertThat(authorization.getPrincipalName()).isEqualTo(PRINCIPAL_NAME);
assertThat(authorization.getAccessToken()).isEqualTo(ACCESS_TOKEN);
assertThat(authorization.getAttributes()).isEqualTo(ATTRIBUTES);
}
@Test
public void buildWhenBuildThenImmutableMapIsCreated() {
OAuth2Authorization authorization = OAuth2Authorization.builder()
.registeredClientId(REGISTERED_CLIENT_ID)
.principalName(PRINCIPAL_NAME)
.accessToken(ACCESS_TOKEN)
.attribute("any", "value")
.build();
assertThatThrownBy(() -> authorization.getAttributes().put("any", "value"))
.isInstanceOf(UnsupportedOperationException.class);
}
@Test
public void buildWhenAccessTokenAndAuthorizationCodeNotProvidedThenThrowIllegalArgumentException() {
assertThatThrownBy(() ->
OAuth2Authorization.builder()
.registeredClientId(REGISTERED_CLIENT_ID)
.principalName(PRINCIPAL_NAME)
.build()
).isInstanceOf(IllegalArgumentException.class);
}
@Test
public void buildWhenRegisteredClientIdNotProvidedThenThrowIllegalArgumentException() {
assertThatThrownBy(() ->
OAuth2Authorization.builder()
.principalName(PRINCIPAL_NAME)
.accessToken(ACCESS_TOKEN)
.attribute(AUTHORIZATION_CODE_VALUE, CODE)
.build()
).isInstanceOf(IllegalArgumentException.class);
} }
@Test @Test
public void buildWhenPrincipalNameNotProvidedThenThrowIllegalArgumentException() { public void buildWhenPrincipalNameNotProvidedThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> assertThatThrownBy(() -> OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT).build())
OAuth2Authorization.builder() .isInstanceOf(IllegalArgumentException.class)
.registeredClientId(REGISTERED_CLIENT_ID) .hasMessage("principalName cannot be empty");
.accessToken(ACCESS_TOKEN)
.attribute(AUTHORIZATION_CODE_VALUE, CODE)
.build()
).isInstanceOf(IllegalArgumentException.class);
} }
@Test @Test
public void buildWhenAttributeSetWithNullNameThenThrowIllegalArgumentException() { public void buildWhenAuthorizationCodeNotProvidedThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> assertThatThrownBy(() ->
OAuth2Authorization.builder() OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
.attribute(null, CODE) .principalName(PRINCIPAL_NAME).build())
).isInstanceOf(IllegalArgumentException.class); .isInstanceOf(IllegalArgumentException.class)
.hasMessage("authorization code cannot be null");
} }
@Test @Test
public void buildWhenAttributeSetWithNullValueThenThrowIllegalArgumentException() { public void attributeWhenNameNullThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> assertThatThrownBy(() ->
OAuth2Authorization.builder() OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
.attribute(AUTHORIZATION_CODE_VALUE, null) .attribute(null, AUTHORIZATION_CODE))
).isInstanceOf(IllegalArgumentException.class); .isInstanceOf(IllegalArgumentException.class)
.hasMessage("name cannot be empty");
} }
@Test @Test
public void withOAuth2AuthorizationWhenAuthorizationProvidedThenAllAttributesAreCopied() { public void attributeWhenValueNullThenThrowIllegalArgumentException() {
OAuth2Authorization authorizationToCopy = OAuth2Authorization.builder() assertThatThrownBy(() ->
.registeredClientId(REGISTERED_CLIENT_ID) OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
.attribute(TokenType.AUTHORIZATION_CODE.getValue(), null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("value cannot be null");
}
@Test
public void buildWhenAllAttributesAreProvidedThenAllAttributesAreSet() {
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
.principalName(PRINCIPAL_NAME) .principalName(PRINCIPAL_NAME)
.attribute(AUTHORIZATION_CODE_VALUE, CODE)
.build();
OAuth2Authorization authorization = OAuth2Authorization.withAuthorization(authorizationToCopy)
.accessToken(ACCESS_TOKEN) .accessToken(ACCESS_TOKEN)
.attribute(TokenType.AUTHORIZATION_CODE.getValue(), AUTHORIZATION_CODE)
.build(); .build();
assertThat(authorization.getRegisteredClientId()).isEqualTo(REGISTERED_CLIENT_ID); assertThat(authorization.getRegisteredClientId()).isEqualTo(REGISTERED_CLIENT.getId());
assertThat(authorization.getPrincipalName()).isEqualTo(PRINCIPAL_NAME); assertThat(authorization.getPrincipalName()).isEqualTo(PRINCIPAL_NAME);
assertThat(authorization.getAccessToken()).isEqualTo(ACCESS_TOKEN); assertThat(authorization.getAccessToken()).isEqualTo(ACCESS_TOKEN);
assertThat(authorization.getAttributes()).isEqualTo(ATTRIBUTES); assertThat(authorization.getAttributes()).containsExactly(
entry(TokenType.AUTHORIZATION_CODE.getValue(), AUTHORIZATION_CODE));
} }
} }