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);
}
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"));
}
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());
}
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);
}
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;
}
Aggregations