Java Code Examples for org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken#getPrincipal()
The following examples show how to use
org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken#getPrincipal() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractFlowController.java From oauth2-protocol-patterns with Apache License 2.0 | 6 votes |
protected ServiceCallResponse fromUiApp(OAuth2AuthenticationToken oauth2Authentication, HttpServletRequest request, ServiceCallResponse... serviceCallResponses) { OidcUser oidcUser = (OidcUser) oauth2Authentication.getPrincipal(); ServiceCallResponse serviceCallResponse = new ServiceCallResponse(); serviceCallResponse.setServiceName(ServicesConfig.UI_APP); serviceCallResponse.setServiceUri(request.getRequestURL().toString()); serviceCallResponse.setJti("(opaque to client)"); serviceCallResponse.setSub(oidcUser.getSubject()); serviceCallResponse.setAud(oidcUser.getAudience()); serviceCallResponse.setAuthorities(oauth2Authentication.getAuthorities().stream() .map(GrantedAuthority::getAuthority).sorted().collect(Collectors.toList())); if (serviceCallResponses != null) { serviceCallResponse.setServiceCallResponses(Arrays.asList(serviceCallResponses)); } return serviceCallResponse; }
Example 2
Source File: FacebookTokenStore.java From OAuth-2.0-Cookbook with MIT License | 6 votes |
@Override public AccessToken loadSecurityToken(OAuth2AuthenticationToken authentication) { DefaultOAuth2User user = (DefaultOAuth2User) authentication.getPrincipal(); String id = (String) user.getAttributes().get("id"); Optional<FacebookAuth> facebookAuth = repository.findById(id); if (facebookAuth.isPresent()) { FacebookAuth auth = facebookAuth.get(); return new AccessToken(AccessToken.TokenType.BEARER, id, Instant.ofEpochSecond(auth.getIssuedAt()), Instant.ofEpochSecond(auth.getExpirationTime())); } return null; }
Example 3
Source File: DefaultControllerAdvice.java From oauth2-protocol-patterns with Apache License 2.0 | 5 votes |
@ModelAttribute("idTokenClaims") Map<String, Object> idTokenClaims(OAuth2AuthenticationToken oauth2Authentication) { if (oauth2Authentication == null) { return Collections.emptyMap(); } OidcUser oidcUser = (OidcUser) oauth2Authentication.getPrincipal(); final List<String> claimNames = Arrays.asList("iss", "sub", "aud", "azp", "given_name", "family_name", "email"); return oidcUser.getClaims().entrySet().stream() .filter(e -> claimNames.contains(e.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); }
Example 4
Source File: FacebookTokenStore.java From OAuth-2.0-Cookbook with MIT License | 5 votes |
@Override public void saveSecurityToken(AccessToken securityToken, OAuth2AuthenticationToken authentication) { DefaultOAuth2User oAuth2User = (DefaultOAuth2User) authentication.getPrincipal(); FacebookAuth facebookAuth = new FacebookAuth(); facebookAuth.setUser(new User()); facebookAuth.setId((String) oAuth2User.getAttributes().get("id")); facebookAuth.setExpirationTime(securityToken.getExpiresAt().getEpochSecond()); facebookAuth.setIssuedAt(securityToken.getIssuedAt().getEpochSecond()); repository.save(facebookAuth); }
Example 5
Source File: FacebookTokenStore.java From OAuth-2.0-Cookbook with MIT License | 5 votes |
@Override public void removeSecurityToken(OAuth2AuthenticationToken authentication) { DefaultOAuth2User oAuth2User = (DefaultOAuth2User) authentication.getPrincipal(); String id = (String) oAuth2User.getAttributes().get("id"); Optional<FacebookAuth> facebookAuth = repository.findById(id); if (facebookAuth.isPresent()) { repository.delete(facebookAuth.get()); } }