use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.
the class WebClientReactivePasswordTokenResponseClientTests method getTokenResponseWhenAuthenticationPrivateKeyJwtThenFormParametersAreSent.
@Test
public void getTokenResponseWhenAuthenticationPrivateKeyJwtThenFormParametersAreSent() throws Exception {
// @formatter:off
String accessTokenSuccessResponse = "{\n" + " \"access_token\": \"access-token-1234\",\n" + " \"token_type\": \"bearer\",\n" + " \"expires_in\": \"3600\"\n" + "}\n";
// @formatter:on
this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
// @formatter:off
ClientRegistration clientRegistration = this.clientRegistrationBuilder.clientAuthenticationMethod(ClientAuthenticationMethod.PRIVATE_KEY_JWT).build();
// @formatter:on
// Configure Jwt client authentication converter
JWK jwk = TestJwks.DEFAULT_RSA_JWK;
Function<ClientRegistration, JWK> jwkResolver = (registration) -> jwk;
configureJwtClientAuthenticationConverter(jwkResolver);
OAuth2PasswordGrantRequest passwordGrantRequest = new OAuth2PasswordGrantRequest(clientRegistration, this.username, this.password);
this.tokenResponseClient.getTokenResponse(passwordGrantRequest).block();
RecordedRequest actualRequest = this.server.takeRequest();
assertThat(actualRequest.getHeader(HttpHeaders.AUTHORIZATION)).isNull();
assertThat(actualRequest.getBody().readUtf8()).contains("grant_type=password", "client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer", "client_assertion=");
}
use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.
the class Saml2AuthenticationTokenConverterTests method convertWhenGetRequestInvalidDeflatedThenSaml2AuthenticationException.
@Test
public void convertWhenGetRequestInvalidDeflatedThenSaml2AuthenticationException() {
Saml2AuthenticationTokenConverter converter = new Saml2AuthenticationTokenConverter(this.relyingPartyRegistrationResolver);
given(this.relyingPartyRegistrationResolver.convert(any(HttpServletRequest.class))).willReturn(this.relyingPartyRegistration);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
byte[] invalidDeflated = "invalid".getBytes();
String encoded = Saml2Utils.samlEncode(invalidDeflated);
request.setParameter(Saml2ParameterNames.SAML_RESPONSE, encoded);
assertThatExceptionOfType(Saml2AuthenticationException.class).isThrownBy(() -> converter.convert(request)).withCauseInstanceOf(IOException.class).satisfies((ex) -> assertThat(ex.getSaml2Error().getErrorCode()).isEqualTo(Saml2ErrorCodes.INVALID_RESPONSE)).satisfies((ex) -> assertThat(ex.getSaml2Error().getDescription()).isEqualTo("Unable to inflate string"));
}
use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.
the class Saml2AuthenticationTokenConverterTests method convertWhenSamlResponseInvalidBase64ThenSaml2AuthenticationException.
@Test
public void convertWhenSamlResponseInvalidBase64ThenSaml2AuthenticationException() {
Saml2AuthenticationTokenConverter converter = new Saml2AuthenticationTokenConverter(this.relyingPartyRegistrationResolver);
given(this.relyingPartyRegistrationResolver.convert(any(HttpServletRequest.class))).willReturn(this.relyingPartyRegistration);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setParameter(Saml2ParameterNames.SAML_RESPONSE, "invalid");
assertThatExceptionOfType(Saml2AuthenticationException.class).isThrownBy(() -> converter.convert(request)).withCauseInstanceOf(IllegalArgumentException.class).satisfies((ex) -> assertThat(ex.getSaml2Error().getErrorCode()).isEqualTo(Saml2ErrorCodes.INVALID_RESPONSE)).satisfies((ex) -> assertThat(ex.getSaml2Error().getDescription()).isEqualTo("Failed to decode SAMLResponse"));
}
use of org.springframework.core.convert.converter.Converter in project spring-data-mongodb by spring-projects.
the class MappingMongoConverterUnitTests method mappingConsidersCustomConvertersNotWritingTypeInformation.
// DATAMONGO-724
@Test
public void mappingConsidersCustomConvertersNotWritingTypeInformation() {
Person person = new Person();
person.firstname = "Dave";
ClassWithMapProperty entity = new ClassWithMapProperty();
entity.mapOfPersons = new HashMap<String, Person>();
entity.mapOfPersons.put("foo", person);
entity.mapOfObjects = new HashMap<String, Object>();
entity.mapOfObjects.put("foo", person);
CustomConversions conversions = new MongoCustomConversions(Arrays.asList(new Converter<Person, org.bson.Document>() {
@Override
public org.bson.Document convert(Person source) {
return //
new org.bson.Document().append("firstname", source.firstname).append("_class", Person.class.getName());
}
}, new Converter<org.bson.Document, Person>() {
@Override
public Person convert(org.bson.Document source) {
Person person = new Person();
person.firstname = source.get("firstname").toString();
person.lastname = "converter";
return person;
}
}));
MongoMappingContext context = new MongoMappingContext();
context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
context.afterPropertiesSet();
MappingMongoConverter mongoConverter = new MappingMongoConverter(resolver, context);
mongoConverter.setCustomConversions(conversions);
mongoConverter.afterPropertiesSet();
org.bson.Document document = new org.bson.Document();
mongoConverter.write(entity, document);
ClassWithMapProperty result = mongoConverter.read(ClassWithMapProperty.class, document);
assertThat(result.mapOfPersons, is(notNullValue()));
Person personCandidate = result.mapOfPersons.get("foo");
assertThat(personCandidate, is(notNullValue()));
assertThat(personCandidate.firstname, is("Dave"));
assertThat(result.mapOfObjects, is(notNullValue()));
Object value = result.mapOfObjects.get("foo");
assertThat(value, is(notNullValue()));
assertThat(value, is(instanceOf(Person.class)));
assertThat(((Person) value).firstname, is("Dave"));
assertThat(((Person) value).lastname, is("converter"));
}
use of org.springframework.core.convert.converter.Converter in project spring-integration by spring-projects.
the class DatatypeChannelTests method conversionServiceBeanUsedByDefault.
@Test
public void conversionServiceBeanUsedByDefault() {
GenericApplicationContext context = new GenericApplicationContext();
Converter<Boolean, Integer> converter = new Converter<Boolean, Integer>() {
@Override
public Integer convert(Boolean source) {
return source ? 1 : 0;
}
};
BeanDefinitionBuilder conversionServiceBuilder = BeanDefinitionBuilder.genericBeanDefinition(ConversionServiceFactoryBean.class);
conversionServiceBuilder.addPropertyValue("converters", Collections.singleton(converter));
context.registerBeanDefinition(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME, conversionServiceBuilder.getBeanDefinition());
BeanDefinition messageConverter = BeanDefinitionBuilder.genericBeanDefinition(DefaultDatatypeChannelMessageConverter.class).getBeanDefinition();
context.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_DATATYPE_CHANNEL_MESSAGE_CONVERTER_BEAN_NAME, messageConverter);
BeanDefinitionBuilder channelBuilder = BeanDefinitionBuilder.genericBeanDefinition(QueueChannel.class);
channelBuilder.addPropertyValue("datatypes", "java.lang.Integer, java.util.Date");
context.registerBeanDefinition("testChannel", channelBuilder.getBeanDefinition());
context.refresh();
QueueChannel channel = context.getBean("testChannel", QueueChannel.class);
assertSame(context.getBean(ConversionService.class), TestUtils.getPropertyValue(channel, "messageConverter.conversionService"));
assertTrue(channel.send(new GenericMessage<Boolean>(Boolean.TRUE)));
assertEquals(1, channel.receive().getPayload());
context.close();
}
Aggregations