Search in sources :

Example 1 with OAuth2Authentication

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

the class GoogleAccessTokenConverter method extractAuthentication.

public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
    Map<String, String> parameters = new HashMap<>();
    Set<String> scope = parseScopes(map);
    Authentication user = userTokenConverter.extractAuthentication(map);
    String clientId = (String) map.get(CLIENT_ID);
    parameters.put(CLIENT_ID, clientId);
    Set<String> resourceIds = new LinkedHashSet<>(map.containsKey(AUD) ? (Collection<String>) map.get(AUD) : Collections.<String>emptySet());
    OAuth2Request request = new OAuth2Request(parameters, clientId, null, true, scope, resourceIds, null, null, null);
    return new OAuth2Authentication(request, user);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) HashMap(java.util.HashMap) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Collection(java.util.Collection)

Example 2 with OAuth2Authentication

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

the class GoogleAccessTokenConverterTest method shouldExtractAuthenticationAndScopesWhenScopeIsString.

//    private DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
@Test
public void shouldExtractAuthenticationAndScopesWhenScopeIsString() throws Exception {
    Map<String, Object> map = newHashMap();
    map.put(AccessTokenConverter.SCOPE, "a b");
    OAuth2Authentication authentication = accessTokenConverter.extractAuthentication(map);
    assertThat(authentication.getOAuth2Request().getScope(), containsInAnyOrder("a", "b"));
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Test(org.junit.Test)

Example 3 with OAuth2Authentication

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

the class GoogleTokenServicesTest method shouldLoadAuthenticationAndTransformValuesToStandardValuesAndAddDomainRole.

@Test
public void shouldLoadAuthenticationAndTransformValuesToStandardValuesAndAddDomainRole() 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);
    googleTokenServices.setRestTemplate(restTemplate);
    googleTokenServices.setCheckTokenEndpointUrl("//");
    DefaultUserAuthenticationConverter defaultUserAuthenticationConverter = new DefaultUserAuthenticationConverter();
    defaultUserAuthenticationConverter.setAuthorityGranter(authorityGranter);
    GoogleAccessTokenConverter realAccessTokenConverter = new GoogleAccessTokenConverter();
    realAccessTokenConverter.setUserTokenConverter(defaultUserAuthenticationConverter);
    googleTokenServices.setAccessTokenConverter(realAccessTokenConverter);
    OAuth2Authentication authentication = googleTokenServices.loadAuthentication(null);
    assertThat(authentication, notNullValue());
    verify(authorityGranter).getAuthorities(anyMap());
}
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 4 with OAuth2Authentication

use of org.springframework.security.oauth2.provider.OAuth2Authentication in project spring-boot by spring-projects.

the class UserInfoTokenServices method extractAuthentication.

private OAuth2Authentication extractAuthentication(Map<String, Object> map) {
    Object principal = getPrincipal(map);
    List<GrantedAuthority> authorities = this.authoritiesExtractor.extractAuthorities(map);
    OAuth2Request request = new OAuth2Request(null, this.clientId, null, true, null, null, null, null, null);
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(principal, "N/A", authorities);
    token.setDetails(map);
    return new OAuth2Authentication(request, token);
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) GrantedAuthority(org.springframework.security.core.GrantedAuthority) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 5 with OAuth2Authentication

use of org.springframework.security.oauth2.provider.OAuth2Authentication in project spring-security-oauth by spring-projects.

the class ScopeVoter method vote.

public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
    int result = ACCESS_ABSTAIN;
    if (!(authentication instanceof OAuth2Authentication)) {
        return result;
    }
    for (ConfigAttribute attribute : attributes) {
        if (denyAccess.equals(attribute.getAttribute())) {
            return ACCESS_DENIED;
        }
    }
    OAuth2Request clientAuthentication = ((OAuth2Authentication) authentication).getOAuth2Request();
    for (ConfigAttribute attribute : attributes) {
        if (this.supports(attribute)) {
            result = ACCESS_DENIED;
            Set<String> scopes = clientAuthentication.getScope();
            for (String scope : scopes) {
                if (attribute.getAttribute().toUpperCase().equals((scopePrefix + scope).toUpperCase())) {
                    return ACCESS_GRANTED;
                }
            }
            if (result == ACCESS_DENIED && throwException) {
                InsufficientScopeException failure = new InsufficientScopeException("Insufficient scope for this resource", Collections.singleton(attribute.getAttribute().substring(scopePrefix.length())));
                throw new AccessDeniedException(failure.getMessage(), failure);
            }
        }
    }
    return result;
}
Also used : InsufficientScopeException(org.springframework.security.oauth2.common.exceptions.InsufficientScopeException) OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ConfigAttribute(org.springframework.security.access.ConfigAttribute) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication)

Aggregations

OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)166 Test (org.junit.Test)116 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)84 Authentication (org.springframework.security.core.Authentication)68 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)57 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)49 Date (java.util.Date)34 HashMap (java.util.HashMap)22 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)21 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)20 DBUnitTest (org.orcid.test.DBUnitTest)17 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)15 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)15 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)15 HashSet (java.util.HashSet)13 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)13 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)13 OAuth2RefreshToken (org.springframework.security.oauth2.common.OAuth2RefreshToken)13 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)13 BaseClientDetails (org.springframework.security.oauth2.provider.client.BaseClientDetails)12