|
|
|
@ -16,48 +16,37 @@ |
|
|
|
package org.springframework.security.oauth2.server.authorization; |
|
|
|
package org.springframework.security.oauth2.server.authorization; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
|
|
|
|
import org.springframework.security.core.SpringSecurityCoreVersion2; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.io.Serializable; |
|
|
|
import java.util.concurrent.CopyOnWriteArrayList; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* An {@link OAuth2AuthorizationService} that stores {@link OAuth2Authorization}'s in-memory. |
|
|
|
* An {@link OAuth2AuthorizationService} that stores {@link OAuth2Authorization}'s in-memory. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Krisztian Toth |
|
|
|
* @author Krisztian Toth |
|
|
|
|
|
|
|
* @author Joe Grandja |
|
|
|
* @since 0.0.1 |
|
|
|
* @since 0.0.1 |
|
|
|
* @see OAuth2AuthorizationService |
|
|
|
* @see OAuth2AuthorizationService |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public final class InMemoryOAuth2AuthorizationService implements OAuth2AuthorizationService { |
|
|
|
public final class InMemoryOAuth2AuthorizationService implements OAuth2AuthorizationService { |
|
|
|
private final List<OAuth2Authorization> authorizations; |
|
|
|
private final Map<OAuth2AuthorizationId, OAuth2Authorization> authorizations = new ConcurrentHashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Constructs an {@code InMemoryOAuth2AuthorizationService}. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public InMemoryOAuth2AuthorizationService() { |
|
|
|
|
|
|
|
this.authorizations = new CopyOnWriteArrayList<>(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Constructs an {@code InMemoryOAuth2AuthorizationService} using the provided parameters. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param authorizations the initial {@code List} of {@link OAuth2Authorization}(s) |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public InMemoryOAuth2AuthorizationService(List<OAuth2Authorization> authorizations) { |
|
|
|
|
|
|
|
Assert.notEmpty(authorizations, "authorizations cannot be empty"); |
|
|
|
|
|
|
|
this.authorizations = new CopyOnWriteArrayList<>(authorizations); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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"); |
|
|
|
this.authorizations.add(authorization); |
|
|
|
OAuth2AuthorizationId authorizationId = new OAuth2AuthorizationId( |
|
|
|
|
|
|
|
authorization.getRegisteredClientId(), authorization.getPrincipalName()); |
|
|
|
|
|
|
|
this.authorizations.put(authorizationId, authorization); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public OAuth2Authorization findByToken(String token, @Nullable TokenType tokenType) { |
|
|
|
public OAuth2Authorization findByToken(String token, @Nullable TokenType tokenType) { |
|
|
|
Assert.hasText(token, "token cannot be empty"); |
|
|
|
Assert.hasText(token, "token cannot be empty"); |
|
|
|
return this.authorizations.stream() |
|
|
|
return this.authorizations.values().stream() |
|
|
|
.filter(authorization -> hasToken(authorization, token, tokenType)) |
|
|
|
.filter(authorization -> hasToken(authorization, token, tokenType)) |
|
|
|
.findFirst() |
|
|
|
.findFirst() |
|
|
|
.orElse(null); |
|
|
|
.orElse(null); |
|
|
|
@ -72,4 +61,33 @@ public final class InMemoryOAuth2AuthorizationService implements OAuth2Authoriza |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class OAuth2AuthorizationId implements Serializable { |
|
|
|
|
|
|
|
private static final long serialVersionUID = SpringSecurityCoreVersion2.SERIAL_VERSION_UID; |
|
|
|
|
|
|
|
private final String registeredClientId; |
|
|
|
|
|
|
|
private final String principalName; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private OAuth2AuthorizationId(String registeredClientId, String principalName) { |
|
|
|
|
|
|
|
this.registeredClientId = registeredClientId; |
|
|
|
|
|
|
|
this.principalName = principalName; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean equals(Object obj) { |
|
|
|
|
|
|
|
if (this == obj) { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (obj == null || getClass() != obj.getClass()) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
OAuth2AuthorizationId that = (OAuth2AuthorizationId) obj; |
|
|
|
|
|
|
|
return Objects.equals(this.registeredClientId, that.registeredClientId) && |
|
|
|
|
|
|
|
Objects.equals(this.principalName, that.principalName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public int hashCode() { |
|
|
|
|
|
|
|
return Objects.hash(this.registeredClientId, this.principalName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|