use of org.springframework.security.oauth2.core.ClientAuthenticationMethod in project midpoint by Evolveum.
the class OidcClientModuleWebSecurityConfiguration method buildInternal.
private static OidcClientModuleWebSecurityConfiguration buildInternal(OidcAuthenticationModuleType modelType, String prefixOfSequence, String publicHttpUrlPattern, ServletRequest request) {
OidcClientModuleWebSecurityConfiguration configuration = new OidcClientModuleWebSecurityConfiguration();
build(configuration, modelType, prefixOfSequence);
List<OidcClientAuthenticationModuleType> clients = modelType.getClient();
List<ClientRegistration> registrations = new ArrayList<>();
clients.forEach(client -> {
OidcOpenIdProviderType openIdProvider = client.getOpenIdProvider();
Assert.notNull(openIdProvider, "openIdProvider cannot be null");
ClientRegistration.Builder builder = null;
try {
builder = ClientRegistrations.fromOidcIssuerLocation(openIdProvider.getIssuerUri());
} catch (Exception e) {
LOGGER.debug("Couldn't create oidc client builder by issuer uri.");
}
Assert.hasText(client.getRegistrationId(), "registrationId cannot be empty");
if (builder == null) {
builder = ClientRegistration.withRegistrationId(client.getRegistrationId());
} else {
builder.registrationId(client.getRegistrationId());
}
builder.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE);
builder.userInfoAuthenticationMethod(AuthenticationMethod.HEADER);
UriComponentsBuilder redirectUri = UriComponentsBuilder.fromUriString(StringUtils.isNotBlank(publicHttpUrlPattern) ? publicHttpUrlPattern : getBasePath((HttpServletRequest) request));
redirectUri.pathSegment(DEFAULT_PREFIX_OF_MODULE, AuthUtil.stripSlashes(prefixOfSequence), AuthUtil.stripSlashes(modelType.getName()), AuthUtil.stripSlashes(RemoteModuleAuthenticationImpl.AUTHENTICATION_REQUEST_PROCESSING_URL_SUFFIX), client.getRegistrationId());
builder.redirectUri(redirectUri.toUriString());
Assert.hasText(client.getClientId(), "clientId cannot be empty");
builder.clientId(client.getClientId());
if (client.getNameOfUsernameAttribute() != null) {
builder.userNameAttributeName(client.getNameOfUsernameAttribute());
}
if (!Objects.isNull(client.getClientSecret())) {
try {
String clientSecret = protector.decryptString(client.getClientSecret());
builder.clientSecret(clientSecret);
} catch (EncryptionException e) {
LOGGER.error("Couldn't obtain clear string for client secret");
}
}
getOptionalIfNotEmpty(client.getClientName()).ifPresent(builder::clientName);
getOptionalIfNotEmpty(openIdProvider.getAuthorizationUri()).ifPresent(builder::authorizationUri);
getOptionalIfNotEmpty(openIdProvider.getTokenUri()).ifPresent(builder::tokenUri);
getOptionalIfNotEmpty(openIdProvider.getUserInfoUri()).ifPresent(builder::userInfoUri);
getOptionalIfNotEmpty(openIdProvider.getIssuerUri()).ifPresent(builder::issuerUri);
ClientRegistration clientRegistration = builder.build();
if (clientRegistration.getScopes() == null || !clientRegistration.getScopes().contains("openid")) {
List<String> scopes = new ArrayList<>();
if (clientRegistration.getScopes() != null) {
scopes.addAll(clientRegistration.getScopes());
}
scopes.add("openid");
builder.scope(scopes);
}
if (StringUtils.isNotEmpty(openIdProvider.getEndSessionUri())) {
Map<String, Object> configurationMetadata = new HashMap<>(clientRegistration.getProviderDetails().getConfigurationMetadata());
configurationMetadata.remove("end_session_endpoint");
configurationMetadata.put("end_session_endpoint", openIdProvider.getEndSessionUri());
builder.providerConfigurationMetadata(configurationMetadata);
}
if (client.getClientAuthenticationMethod() != null) {
builder.clientAuthenticationMethod(new ClientAuthenticationMethod(client.getClientAuthenticationMethod().name().toLowerCase()));
}
clientRegistration = builder.build();
Assert.hasText(clientRegistration.getProviderDetails().getUserInfoEndpoint().getUri(), "UserInfoUri cannot be empty");
registrations.add(clientRegistration);
OidcAdditionalConfiguration.Builder additionalConfBuilder = OidcAdditionalConfiguration.builder().singingAlg(client.getClientSigningAlgorithm());
if (client.getSimpleProofKey() != null) {
initializeProofKey(client.getSimpleProofKey(), additionalConfBuilder);
} else if (client.getKeyStoreProofKey() != null) {
initializeProofKey(client.getKeyStoreProofKey(), additionalConfBuilder);
}
configuration.additionalConfiguration.put(client.getRegistrationId(), additionalConfBuilder.build());
});
configuration.clientRegistrationRepository = new InMemoryClientRegistrationRepository(registrations);
return configuration;
}
use of org.springframework.security.oauth2.core.ClientAuthenticationMethod in project spring-boot by spring-projects.
the class OAuth2ClientPropertiesRegistrationAdapter method getClientRegistration.
private static ClientRegistration getClientRegistration(String registrationId, OAuth2ClientProperties.Registration properties, Map<String, Provider> providers) {
Builder builder = getBuilderFromIssuerIfPossible(registrationId, properties.getProvider(), providers);
if (builder == null) {
builder = getBuilder(registrationId, properties.getProvider(), providers);
}
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(properties::getClientId).to(builder::clientId);
map.from(properties::getClientSecret).to(builder::clientSecret);
map.from(properties::getClientAuthenticationMethod).as(ClientAuthenticationMethod::new).to(builder::clientAuthenticationMethod);
map.from(properties::getAuthorizationGrantType).as(AuthorizationGrantType::new).to(builder::authorizationGrantType);
map.from(properties::getRedirectUri).to(builder::redirectUri);
map.from(properties::getScope).as(StringUtils::toStringArray).to(builder::scope);
map.from(properties::getClientName).to(builder::clientName);
return builder.build();
}
use of org.springframework.security.oauth2.core.ClientAuthenticationMethod in project spring-security by spring-projects.
the class ClientRegistrationsBeanDefinitionParser method getClientRegistrations.
private List<ClientRegistration> getClientRegistrations(Element element, ParserContext parserContext, Map<String, Map<String, String>> providers) {
List<Element> clientRegistrationElts = DomUtils.getChildElementsByTagName(element, ELT_CLIENT_REGISTRATION);
List<ClientRegistration> clientRegistrations = new ArrayList<>();
for (Element clientRegistrationElt : clientRegistrationElts) {
String registrationId = clientRegistrationElt.getAttribute(ATT_REGISTRATION_ID);
String providerId = clientRegistrationElt.getAttribute(ATT_PROVIDER_ID);
ClientRegistration.Builder builder = getBuilderFromIssuerIfPossible(parserContext, registrationId, providerId, providers);
if (builder == null) {
builder = getBuilder(parserContext, registrationId, providerId, providers);
if (builder == null) {
Object source = parserContext.extractSource(element);
parserContext.getReaderContext().error(getErrorMessage(providerId, registrationId), source);
// error on the config skip to next element
continue;
}
}
getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_CLIENT_ID)).ifPresent(builder::clientId);
getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_CLIENT_SECRET)).ifPresent(builder::clientSecret);
getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_CLIENT_AUTHENTICATION_METHOD)).map(ClientAuthenticationMethod::new).ifPresent(builder::clientAuthenticationMethod);
getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_AUTHORIZATION_GRANT_TYPE)).map(AuthorizationGrantType::new).ifPresent(builder::authorizationGrantType);
getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_REDIRECT_URI)).ifPresent(builder::redirectUri);
getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_SCOPE)).map(StringUtils::commaDelimitedListToSet).ifPresent(builder::scope);
getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_CLIENT_NAME)).ifPresent(builder::clientName);
clientRegistrations.add(builder.build());
}
return clientRegistrations;
}
use of org.springframework.security.oauth2.core.ClientAuthenticationMethod in project spring-security by spring-projects.
the class NimbusJwtClientAuthenticationParametersConverterTests method convertWhenJwkNotResolvedThenThrowOAuth2AuthorizationException.
@Test
public void convertWhenJwkNotResolvedThenThrowOAuth2AuthorizationException() {
// @formatter:off
ClientRegistration clientRegistration = TestClientRegistrations.clientCredentials().clientAuthenticationMethod(ClientAuthenticationMethod.PRIVATE_KEY_JWT).build();
// @formatter:on
OAuth2ClientCredentialsGrantRequest clientCredentialsGrantRequest = new OAuth2ClientCredentialsGrantRequest(clientRegistration);
assertThatExceptionOfType(OAuth2AuthorizationException.class).isThrownBy(() -> this.converter.convert(clientCredentialsGrantRequest)).withMessage("[invalid_key] Failed to resolve JWK signing key for client registration '" + clientRegistration.getRegistrationId() + "'.");
}
use of org.springframework.security.oauth2.core.ClientAuthenticationMethod in project spring-security by spring-projects.
the class NimbusAuthorizationCodeTokenResponseClientTests method setUp.
@BeforeEach
public void setUp() {
this.clientRegistrationBuilder = TestClientRegistrations.clientRegistration().clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC);
this.authorizationRequest = TestOAuth2AuthorizationRequests.request().build();
this.authorizationResponse = TestOAuth2AuthorizationResponses.success().build();
this.authorizationExchange = new OAuth2AuthorizationExchange(this.authorizationRequest, this.authorizationResponse);
}
Aggregations