Remove internal Optional usage in favor of null checks
Issue gh-7155
This commit is contained in:
committed by
Eleftheria Stein-Kousathana
parent
2c2d3b5d85
commit
2c2e8e5f24
+24
-16
@@ -18,7 +18,6 @@ package org.springframework.security.oauth2.client.oidc.web.logout;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Optional;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@@ -52,25 +51,34 @@ public final class OidcClientInitiatedLogoutSuccessHandler extends SimpleUrlLogo
|
||||
@Override
|
||||
protected String determineTargetUrl(HttpServletRequest request,
|
||||
HttpServletResponse response, Authentication authentication) {
|
||||
String targetUrl = null;
|
||||
URI endSessionEndpoint;
|
||||
if (authentication instanceof OAuth2AuthenticationToken && authentication.getPrincipal() instanceof OidcUser) {
|
||||
endSessionEndpoint = this.endSessionEndpoint((OAuth2AuthenticationToken) authentication);
|
||||
if (endSessionEndpoint != null) {
|
||||
targetUrl = endpointUri(endSessionEndpoint, authentication);
|
||||
}
|
||||
}
|
||||
if (targetUrl == null) {
|
||||
targetUrl = super.determineTargetUrl(request, response);
|
||||
}
|
||||
|
||||
return Optional.of(authentication)
|
||||
.filter(OAuth2AuthenticationToken.class::isInstance)
|
||||
.filter(token -> authentication.getPrincipal() instanceof OidcUser)
|
||||
.map(OAuth2AuthenticationToken.class::cast)
|
||||
.flatMap(this::endSessionEndpoint)
|
||||
.map(endSessionEndpoint -> endpointUri(endSessionEndpoint, authentication))
|
||||
.orElseGet(() -> super.determineTargetUrl(request, response));
|
||||
return targetUrl;
|
||||
}
|
||||
|
||||
private Optional<URI> endSessionEndpoint(OAuth2AuthenticationToken token) {
|
||||
private URI endSessionEndpoint(OAuth2AuthenticationToken token) {
|
||||
String registrationId = token.getAuthorizedClientRegistrationId();
|
||||
return Optional.of(
|
||||
this.clientRegistrationRepository.findByRegistrationId(registrationId))
|
||||
.map(ClientRegistration::getProviderDetails)
|
||||
.map(ClientRegistration.ProviderDetails::getConfigurationMetadata)
|
||||
.map(configurationMetadata -> configurationMetadata.get("end_session_endpoint"))
|
||||
.map(Object::toString)
|
||||
.map(URI::create);
|
||||
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId(registrationId);
|
||||
|
||||
URI result = null;
|
||||
if (clientRegistration != null) {
|
||||
Object endSessionEndpoint = clientRegistration.getProviderDetails().getConfigurationMetadata().get("end_session_endpoint");
|
||||
if (endSessionEndpoint != null) {
|
||||
result = URI.create(endSessionEndpoint.toString());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private String endpointUri(URI endSessionEndpoint, Authentication authentication) {
|
||||
|
||||
+5
-3
@@ -21,7 +21,6 @@ import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.nimbusds.oauth2.sdk.GrantType;
|
||||
import com.nimbusds.oauth2.sdk.ParseException;
|
||||
@@ -141,8 +140,11 @@ public final class ClientRegistrations {
|
||||
Map<String, Object> configuration = getConfiguration(issuer, oidc(uri), oidcRfc8414(uri), oauth(uri));
|
||||
AuthorizationServerMetadata metadata = parse(configuration, AuthorizationServerMetadata::parse);
|
||||
ClientRegistration.Builder builder = withProviderConfiguration(metadata, issuer);
|
||||
return Optional.ofNullable((String) configuration.get("userinfo_endpoint"))
|
||||
.map(builder::userInfoUri).orElse(builder);
|
||||
String userinfoEndpoint = (String) configuration.get("userinfo_endpoint");
|
||||
if (userinfoEndpoint != null) {
|
||||
builder.userInfoUri(userinfoEndpoint);
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static URI oidc(URI issuer) {
|
||||
|
||||
+8
-7
@@ -16,11 +16,11 @@
|
||||
package org.springframework.security.oauth2.server.resource.authentication;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.authentication.AbstractAuthenticationToken;
|
||||
@@ -128,11 +128,12 @@ public final class OAuth2IntrospectionAuthenticationProvider implements Authenti
|
||||
}
|
||||
|
||||
private Collection<GrantedAuthority> extractAuthorities(Map<String, Object> claims) {
|
||||
Collection<String> scopes = (Collection<String>) claims.get(SCOPE);
|
||||
return Optional.ofNullable(scopes).orElse(Collections.emptyList())
|
||||
.stream()
|
||||
.map(authority -> new SimpleGrantedAuthority("SCOPE_" + authority))
|
||||
.collect(Collectors.toList());
|
||||
Collection<String> scopes = (Collection<String>) claims.getOrDefault(SCOPE, Collections.emptyList());
|
||||
List<GrantedAuthority> authorities = new ArrayList<>();
|
||||
for (String scope : scopes) {
|
||||
authorities.add(new SimpleGrantedAuthority("SCOPE_" + scope));
|
||||
}
|
||||
return authorities;
|
||||
}
|
||||
|
||||
private static BearerTokenError invalidToken(String message) {
|
||||
|
||||
+8
-7
@@ -17,11 +17,11 @@
|
||||
package org.springframework.security.oauth2.server.resource.authentication;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.security.oauth2.core.OAuth2TokenAttributes;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -108,11 +108,12 @@ public class OAuth2IntrospectionReactiveAuthenticationManager implements Reactiv
|
||||
}
|
||||
|
||||
private Collection<GrantedAuthority> extractAuthorities(Map<String, Object> claims) {
|
||||
Collection<String> scopes = (Collection<String>) claims.get(SCOPE);
|
||||
return Optional.ofNullable(scopes).orElse(Collections.emptyList())
|
||||
.stream()
|
||||
.map(authority -> new SimpleGrantedAuthority("SCOPE_" + authority))
|
||||
.collect(Collectors.toList());
|
||||
Collection<String> scopes = (Collection<String>) claims.getOrDefault(SCOPE, Collections.emptyList());
|
||||
List<GrantedAuthority> authorities = new ArrayList<>();
|
||||
for (String scope : scopes) {
|
||||
authorities.add(new SimpleGrantedAuthority("SCOPE_" + scope));
|
||||
}
|
||||
return authorities;
|
||||
}
|
||||
|
||||
private static BearerTokenError invalidToken(String message) {
|
||||
|
||||
+16
-11
@@ -22,7 +22,6 @@ import java.time.Instant;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.nimbusds.oauth2.sdk.TokenIntrospectionResponse;
|
||||
@@ -124,16 +123,22 @@ public class NimbusOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> introspect(String token) {
|
||||
TokenIntrospectionSuccessResponse response = Optional.of(token)
|
||||
.map(this.requestEntityConverter::convert)
|
||||
.map(this::makeRequest)
|
||||
.map(this::adaptToNimbusResponse)
|
||||
.map(this::parseNimbusResponse)
|
||||
.map(this::castToNimbusSuccess)
|
||||
// relying solely on the authorization server to validate this token (not checking 'exp', for example)
|
||||
.filter(TokenIntrospectionSuccessResponse::isActive)
|
||||
.orElseThrow(() -> new OAuth2IntrospectionException("Provided token [" + token + "] isn't active"));
|
||||
return convertClaimsSet(response);
|
||||
RequestEntity<?> requestEntity = this.requestEntityConverter.convert(token);
|
||||
if (requestEntity == null) {
|
||||
throw new OAuth2IntrospectionException("Provided token [" + token + "] isn't active");
|
||||
}
|
||||
|
||||
ResponseEntity<String> responseEntity = makeRequest(requestEntity);
|
||||
HTTPResponse httpResponse = adaptToNimbusResponse(responseEntity);
|
||||
TokenIntrospectionResponse introspectionResponse = parseNimbusResponse(httpResponse);
|
||||
TokenIntrospectionSuccessResponse introspectionSuccessResponse = castToNimbusSuccess(introspectionResponse);
|
||||
|
||||
// relying solely on the authorization server to validate this token (not checking 'exp', for example)
|
||||
if (!introspectionSuccessResponse.isActive()) {
|
||||
throw new OAuth2IntrospectionException("Provided token [" + token + "] isn't active");
|
||||
}
|
||||
|
||||
return convertClaimsSet(introspectionSuccessResponse);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user