use of org.springframework.security.oauth2.core.OAuth2AuthenticationException in project spring-security by spring-projects.
the class DefaultBearerTokenResolver method resolveFromRequestParameters.
private static String resolveFromRequestParameters(HttpServletRequest request) {
String[] values = request.getParameterValues("access_token");
if (values == null || values.length == 0) {
return null;
}
if (values.length == 1) {
return values[0];
}
BearerTokenError error = BearerTokenErrors.invalidRequest("Found multiple bearer tokens in the request");
throw new OAuth2AuthenticationException(error);
}
use of org.springframework.security.oauth2.core.OAuth2AuthenticationException in project OsmAnd-tools by osmandapp.
the class WebSecurityConfiguration method oauthGithubUserService.
private DefaultOAuth2UserService oauthGithubUserService() {
// authorize with admin for specific group
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
}
});
DefaultOAuth2UserService service = new DefaultOAuth2UserService() {
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2User user = super.loadUser(userRequest);
if (user == null) {
return null;
}
Set<GrantedAuthority> authorities = new LinkedHashSet<>();
if (!Algorithms.isEmpty(adminOauth2Url) && user.getAttribute("url") != null && user.getAttribute("url").toString().contains("github.com")) {
Map<String, Object> orgs = checkPermissionAccess(adminOauth2Url, userRequest, user);
// orgs.get("privacy").equals("closed");
if (orgs != null) {
authorities.add(new SimpleGrantedAuthority(ROLE_ADMIN));
}
}
String userNameAttributeName = userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUserNameAttributeName();
return new DefaultOAuth2User(authorities, user.getAttributes(), userNameAttributeName);
}
private Map<String, Object> checkPermissionAccess(Object orgUrl, OAuth2UserRequest userRequest, OAuth2User user) {
String organizationUrl = String.valueOf(orgUrl);
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(userRequest.getAccessToken().getTokenValue());
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
URI uri = UriComponentsBuilder.fromUriString(organizationUrl).build().toUri();
RequestEntity<?> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
ResponseEntity<Map<String, Object>> res = restTemplate.exchange(request, new ParameterizedTypeReference<Map<String, Object>>() {
});
if (!res.getStatusCode().is2xxSuccessful()) {
return null;
}
return res.getBody();
}
};
return service;
}
Aggregations