Search in sources :

Example 11 with AccessTokenConverter

use of org.springframework.security.oauth2.provider.token.AccessTokenConverter in project mots by motech-implementations.

the class TokenConfiguration method tokenServices.

/**
 * Set-up CustomTokenServices with tokenStore().
 * @return token services
 */
@Bean
@Primary
public CustomTokenServices tokenServices() {
    TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
    tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter()));
    CustomTokenServices customTokenServices = new CustomTokenServices();
    customTokenServices.setTokenStore(tokenStore());
    customTokenServices.setTokenEnhancer(tokenEnhancerChain);
    customTokenServices.setClientDetailsService(clientDetailsService);
    customTokenServices.setSupportRefreshToken(true);
    customTokenServices.setAccessTokenValiditySeconds(tokenValiditySeconds);
    customTokenServices.setRefreshTokenValiditySeconds(tokenValiditySeconds * 2);
    customTokenServices.setReuseRefreshToken(false);
    return customTokenServices;
}
Also used : TokenEnhancerChain(org.springframework.security.oauth2.provider.token.TokenEnhancerChain) CustomTokenServices(org.motechproject.mots.security.token.CustomTokenServices) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 12 with AccessTokenConverter

use of org.springframework.security.oauth2.provider.token.AccessTokenConverter in project spring-security-oauth2-google by skate056.

the class GoogleTokenServicesTest method shouldLoadAuthenticationAndTransformValuesToStandardValues.

@Test
public void shouldLoadAuthenticationAndTransformValuesToStandardValues() throws Exception {
    Map<String, String> body = new HashMap<>();
    body.put("issued_to", "blh");
    body.put("user_id", "user@domain.google.com");
    body.put("email", "user@domain.google.com");
    given(response.getBody()).willReturn(body);
    given(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), any(ParameterizedTypeReference.class))).willReturn(response);
    given(accessTokenConverter.extractAuthentication(body)).willReturn(mockAuthentication);
    googleTokenServices.setRestTemplate(restTemplate);
    googleTokenServices.setCheckTokenEndpointUrl("//");
    googleTokenServices.setAccessTokenConverter(accessTokenConverter);
    OAuth2Authentication authentication = googleTokenServices.loadAuthentication(null);
    assertThat(authentication, is(mockAuthentication));
    verify(accessTokenConverter).extractAuthentication(mapCapture.capture());
    Map<String, String> value = mapCapture.getValue();
    assertThat(value.get("user_name"), notNullValue());
    assertThat(value.get("user_name"), is("user@domain.google.com"));
    assertThat(value.get("user_id"), notNullValue());
    assertThat(value.get("user_id"), is("user@domain.google.com"));
    assertThat(value.get("client_id"), notNullValue());
    assertThat(value.get("client_id"), is("blh"));
    assertThat(value.get("issued_to"), notNullValue());
    assertThat(value.get("issued_to"), is("blh"));
}
Also used : HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Matchers.anyString(org.mockito.Matchers.anyString) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 13 with AccessTokenConverter

use of org.springframework.security.oauth2.provider.token.AccessTokenConverter in project new-cloud by xie-summer.

the class ResourceServerConfiguration method accessTokenConverter.

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    Resource resource = new ClassPathResource("public.txt");
    String publicKey = null;
    try {
        publicKey = inputStream2String(resource.getInputStream());
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    converter.setVerifierKey(publicKey);
    converter.setAccessTokenConverter(new CustomerAccessTokenConverter());
    return converter;
}
Also used : CustomerAccessTokenConverter(com.framework.auth.config.support.CustomerAccessTokenConverter) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) JwtAccessTokenConverter(org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource) Bean(org.springframework.context.annotation.Bean)

Example 14 with AccessTokenConverter

use of org.springframework.security.oauth2.provider.token.AccessTokenConverter in project openlmis-stockmanagement by OpenLMIS.

the class ResourceServerSecurityConfiguration method accessTokenConverter.

/**
 * AccessTokenConverter bean initializer.
 */
@Bean
public AccessTokenConverter accessTokenConverter() {
    DefaultAccessTokenConverter defaultAccessTokenConverter = new DefaultAccessTokenConverter();
    defaultAccessTokenConverter.setUserTokenConverter(new CustomUserAuthenticationConverter());
    return defaultAccessTokenConverter;
}
Also used : DefaultAccessTokenConverter(org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter) Bean(org.springframework.context.annotation.Bean)

Example 15 with AccessTokenConverter

use of org.springframework.security.oauth2.provider.token.AccessTokenConverter in project theskeleton by codenergic.

the class SecurityConfig method accessTokenConverter.

/**
 * Token converter and enhancer
 * @return
 */
@Bean
public JwtAccessTokenConverter accessTokenConverter(@Value("${security.jwt.signing-key:}") String signingKey, ResourceLoader resourceLoader) throws IOException {
    DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
    accessTokenConverter.setUserTokenConverter(new UserAccessTokenAuthenticationConverter());
    JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
    jwtAccessTokenConverter.setAccessTokenConverter(accessTokenConverter);
    if (StringUtils.isBlank(signingKey))
        return jwtAccessTokenConverter;
    if (ResourceUtils.isUrl(signingKey)) {
        Resource signingKeyResource = resourceLoader.getResource(signingKey);
        signingKey = IOUtils.toString(signingKeyResource.getURI(), StandardCharsets.UTF_8);
    }
    jwtAccessTokenConverter.setSigningKey(signingKey);
    jwtAccessTokenConverter.setVerifierKey(signingKey);
    return jwtAccessTokenConverter;
}
Also used : DefaultAccessTokenConverter(org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter) Resource(org.springframework.core.io.Resource) JwtAccessTokenConverter(org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter) Bean(org.springframework.context.annotation.Bean)

Aggregations

Bean (org.springframework.context.annotation.Bean)8 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)5 Test (org.junit.Test)4 Matchers.anyString (org.mockito.Matchers.anyString)3 AccessTokenConverter (org.springframework.security.oauth2.provider.token.AccessTokenConverter)3 DefaultAccessTokenConverter (org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter)3 TokenEnhancerChain (org.springframework.security.oauth2.provider.token.TokenEnhancerChain)3 JwtAccessTokenConverter (org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter)3 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Primary (org.springframework.context.annotation.Primary)2 Resource (org.springframework.core.io.Resource)2 JwtTokenStore (org.springframework.security.oauth2.provider.token.store.JwtTokenStore)2 CustomerAccessTokenConverter (com.framework.auth.config.support.CustomerAccessTokenConverter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1