|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2002-2017 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2002-2018 the original author or authors.
|
|
*
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -20,7 +20,6 @@ import org.springframework.security.oauth2.client.registration.ClientRegistratio
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
-import java.util.Base64;
|
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
@@ -29,6 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
* {@link OAuth2AuthorizedClient Authorized Client(s)} in-memory.
|
|
* {@link OAuth2AuthorizedClient Authorized Client(s)} in-memory.
|
|
*
|
|
*
|
|
* @author Joe Grandja
|
|
* @author Joe Grandja
|
|
|
|
+ * @author Vedran Pavic
|
|
* @since 5.0
|
|
* @since 5.0
|
|
* @see OAuth2AuthorizedClientService
|
|
* @see OAuth2AuthorizedClientService
|
|
* @see OAuth2AuthorizedClient
|
|
* @see OAuth2AuthorizedClient
|
|
@@ -36,8 +36,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
* @see Authentication
|
|
* @see Authentication
|
|
*/
|
|
*/
|
|
public final class InMemoryOAuth2AuthorizedClientService implements OAuth2AuthorizedClientService {
|
|
public final class InMemoryOAuth2AuthorizedClientService implements OAuth2AuthorizedClientService {
|
|
- private final Map<String, OAuth2AuthorizedClient> authorizedClients = new ConcurrentHashMap<>();
|
|
|
|
private final ClientRegistrationRepository clientRegistrationRepository;
|
|
private final ClientRegistrationRepository clientRegistrationRepository;
|
|
|
|
+ private Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients = new ConcurrentHashMap<>();
|
|
|
|
|
|
/**
|
|
/**
|
|
* Constructs an {@code InMemoryOAuth2AuthorizedClientService} using the provided parameters.
|
|
* Constructs an {@code InMemoryOAuth2AuthorizedClientService} using the provided parameters.
|
|
@@ -49,7 +49,17 @@ public final class InMemoryOAuth2AuthorizedClientService implements OAuth2Author
|
|
this.clientRegistrationRepository = clientRegistrationRepository;
|
|
this.clientRegistrationRepository = clientRegistrationRepository;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the map of authorized clients to use.
|
|
|
|
+ * @param authorizedClients the map of authorized clients
|
|
|
|
+ */
|
|
|
|
+ public void setAuthorizedClients(Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients) {
|
|
|
|
+ Assert.notNull(authorizedClients, "authorizedClients cannot be null");
|
|
|
|
+ this.authorizedClients = authorizedClients;
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
public <T extends OAuth2AuthorizedClient> T loadAuthorizedClient(String clientRegistrationId, String principalName) {
|
|
public <T extends OAuth2AuthorizedClient> T loadAuthorizedClient(String clientRegistrationId, String principalName) {
|
|
Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty");
|
|
Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty");
|
|
Assert.hasText(principalName, "principalName cannot be empty");
|
|
Assert.hasText(principalName, "principalName cannot be empty");
|
|
@@ -57,15 +67,15 @@ public final class InMemoryOAuth2AuthorizedClientService implements OAuth2Author
|
|
if (registration == null) {
|
|
if (registration == null) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
- return (T) this.authorizedClients.get(this.getIdentifier(registration, principalName));
|
|
|
|
|
|
+ return (T) this.authorizedClients.get(OAuth2AuthorizedClientId.create(registration, principalName));
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal) {
|
|
public void saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal) {
|
|
Assert.notNull(authorizedClient, "authorizedClient cannot be null");
|
|
Assert.notNull(authorizedClient, "authorizedClient cannot be null");
|
|
Assert.notNull(principal, "principal cannot be null");
|
|
Assert.notNull(principal, "principal cannot be null");
|
|
- this.authorizedClients.put(this.getIdentifier(
|
|
|
|
- authorizedClient.getClientRegistration(), principal.getName()), authorizedClient);
|
|
|
|
|
|
+ this.authorizedClients.put(OAuth2AuthorizedClientId.create(authorizedClient.getClientRegistration(),
|
|
|
|
+ principal.getName()), authorizedClient);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -74,12 +84,8 @@ public final class InMemoryOAuth2AuthorizedClientService implements OAuth2Author
|
|
Assert.hasText(principalName, "principalName cannot be empty");
|
|
Assert.hasText(principalName, "principalName cannot be empty");
|
|
ClientRegistration registration = this.clientRegistrationRepository.findByRegistrationId(clientRegistrationId);
|
|
ClientRegistration registration = this.clientRegistrationRepository.findByRegistrationId(clientRegistrationId);
|
|
if (registration != null) {
|
|
if (registration != null) {
|
|
- this.authorizedClients.remove(this.getIdentifier(registration, principalName));
|
|
|
|
|
|
+ this.authorizedClients.remove(OAuth2AuthorizedClientId.create(registration, principalName));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private String getIdentifier(ClientRegistration registration, String principalName) {
|
|
|
|
- String identifier = "[" + registration.getRegistrationId() + "][" + principalName + "]";
|
|
|
|
- return Base64.getEncoder().encodeToString(identifier.getBytes());
|
|
|
|
- }
|
|
|
|
}
|
|
}
|