InMemoryOAuth2AuthorizationService.save() supports insert and update

Related gh-220

Closes gh-222
This commit is contained in:
Joe Grandja 2021-02-10 09:46:13 -05:00
parent c9afc3e061
commit 09846eebeb
2 changed files with 79 additions and 11 deletions

View File

@ -15,6 +15,9 @@
*/ */
package org.springframework.security.oauth2.server.authorization; package org.springframework.security.oauth2.server.authorization;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -40,11 +43,40 @@ import org.springframework.util.Assert;
public final class InMemoryOAuth2AuthorizationService implements OAuth2AuthorizationService { public final class InMemoryOAuth2AuthorizationService implements OAuth2AuthorizationService {
private final Map<String, OAuth2Authorization> authorizations = new ConcurrentHashMap<>(); private final Map<String, OAuth2Authorization> authorizations = new ConcurrentHashMap<>();
/**
* Constructs an {@code InMemoryOAuth2AuthorizationService}.
*/
public InMemoryOAuth2AuthorizationService() {
this(Collections.emptyList());
}
/**
* Constructs an {@code InMemoryOAuth2AuthorizationService} using the provided parameters.
*
* @param authorizations the authorization(s)
*/
public InMemoryOAuth2AuthorizationService(OAuth2Authorization... authorizations) {
this(Arrays.asList(authorizations));
}
/**
* Constructs an {@code InMemoryOAuth2AuthorizationService} using the provided parameters.
*
* @param authorizations the authorization(s)
*/
public InMemoryOAuth2AuthorizationService(List<OAuth2Authorization> authorizations) {
Assert.notNull(authorizations, "authorizations cannot be null");
authorizations.forEach(authorization -> {
Assert.notNull(authorization, "authorization cannot be null");
Assert.isTrue(!this.authorizations.containsKey(authorization.getId()),
"The authorization must be unique. Found duplicate identifier: " + authorization.getId());
this.authorizations.put(authorization.getId(), authorization);
});
}
@Override @Override
public void save(OAuth2Authorization authorization) { public void save(OAuth2Authorization authorization) {
Assert.notNull(authorization, "authorization cannot be null"); Assert.notNull(authorization, "authorization cannot be null");
Assert.isTrue(!this.authorizations.containsKey(authorization.getId()),
"The authorization must be unique. Found duplicate identifier: " + authorization.getId());
this.authorizations.put(authorization.getId(), authorization); this.authorizations.put(authorization.getId(), authorization);
} }

View File

@ -17,6 +17,7 @@ package org.springframework.security.oauth2.server.authorization;
import java.time.Instant; import java.time.Instant;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -55,6 +56,34 @@ public class InMemoryOAuth2AuthorizationServiceTests {
this.authorizationService = new InMemoryOAuth2AuthorizationService(); this.authorizationService = new InMemoryOAuth2AuthorizationService();
} }
@Test
public void constructorVarargsWhenAuthorizationNullThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> new InMemoryOAuth2AuthorizationService((OAuth2Authorization) null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("authorization cannot be null");
}
@Test
public void constructorListWhenAuthorizationsNullThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> new InMemoryOAuth2AuthorizationService((List<OAuth2Authorization>) null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("authorizations cannot be null");
}
@Test
public void constructorWhenDuplicateAuthorizationsThenThrowIllegalArgumentException() {
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
.id(ID)
.principalName(PRINCIPAL_NAME)
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
.token(AUTHORIZATION_CODE)
.build();
assertThatThrownBy(() -> new InMemoryOAuth2AuthorizationService(authorization, authorization))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The authorization must be unique. Found duplicate identifier: id");
}
@Test @Test
public void saveWhenAuthorizationNullThenThrowIllegalArgumentException() { public void saveWhenAuthorizationNullThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> this.authorizationService.save(null)) assertThatThrownBy(() -> this.authorizationService.save(null))
@ -63,7 +92,7 @@ public class InMemoryOAuth2AuthorizationServiceTests {
} }
@Test @Test
public void saveWhenAuthorizationProvidedThenSaved() { public void saveWhenAuthorizationNewThenSaved() {
OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT) OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
.id(ID) .id(ID)
.principalName(PRINCIPAL_NAME) .principalName(PRINCIPAL_NAME)
@ -77,23 +106,30 @@ public class InMemoryOAuth2AuthorizationServiceTests {
assertThat(authorization).isEqualTo(expectedAuthorization); assertThat(authorization).isEqualTo(expectedAuthorization);
} }
// gh-222
@Test @Test
public void saveWhenAuthorizationNotUniqueThenThrowIllegalArgumentException() { public void saveWhenAuthorizationExistsThenUpdated() {
OAuth2Authorization expectedAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT) OAuth2Authorization originalAuthorization = OAuth2Authorization.withRegisteredClient(REGISTERED_CLIENT)
.id(ID) .id(ID)
.principalName(PRINCIPAL_NAME) .principalName(PRINCIPAL_NAME)
.authorizationGrantType(AUTHORIZATION_GRANT_TYPE) .authorizationGrantType(AUTHORIZATION_GRANT_TYPE)
.token(AUTHORIZATION_CODE) .token(AUTHORIZATION_CODE)
.build(); .build();
this.authorizationService.save(expectedAuthorization); this.authorizationService.save(originalAuthorization);
OAuth2Authorization authorization = this.authorizationService.findById( OAuth2Authorization authorization = this.authorizationService.findById(
expectedAuthorization.getId()); originalAuthorization.getId());
assertThat(authorization).isEqualTo(expectedAuthorization); assertThat(authorization).isEqualTo(originalAuthorization);
assertThatThrownBy(() -> this.authorizationService.save(authorization)) OAuth2Authorization updatedAuthorization = OAuth2Authorization.from(authorization)
.isInstanceOf(IllegalArgumentException.class) .attribute("custom-name-1", "custom-value-1")
.hasMessage("The authorization must be unique. Found duplicate identifier: " + ID); .build();
this.authorizationService.save(updatedAuthorization);
authorization = this.authorizationService.findById(
updatedAuthorization.getId());
assertThat(authorization).isEqualTo(updatedAuthorization);
assertThat(authorization).isNotEqualTo(originalAuthorization);
} }
@Test @Test