use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project credhub by cloudfoundry-incubator.
the class UserContextFactory method createUserContext.
public UserContext createUserContext(OAuth2Authentication authentication, String token) {
OAuth2Request oauth2Request = authentication.getOAuth2Request();
String clientId = oauth2Request.getClientId();
String grantType = oauth2Request.getGrantType();
String userId = null;
String userName = null;
String issuer = null;
long validFrom = 0;
long validUntil = 0;
String scope = null;
if (token == null) {
OAuth2AuthenticationDetails authDetails = (OAuth2AuthenticationDetails) authentication.getDetails();
token = authDetails.getTokenValue();
}
OAuth2AccessToken accessToken;
accessToken = resourceServerTokenServices.readAccessToken(token);
if (accessToken != null) {
Set<String> scopes = accessToken.getScope();
scope = scopes == null ? null : String.join(",", scopes);
Map<String, Object> additionalInformation = accessToken.getAdditionalInformation();
userName = (String) additionalInformation.get("user_name");
userId = (String) additionalInformation.get("user_id");
issuer = (String) additionalInformation.get("iss");
validFrom = claimValueAsLong(additionalInformation);
validUntil = accessToken.getExpiration().toInstant().getEpochSecond();
}
return new UserContext(userId, userName, issuer, validFrom, validUntil, clientId, scope, grantType, UserContext.AUTH_METHOD_UAA);
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-security-oauth by spring-projects.
the class OAuth2AuthenticationManagerTests method testDetailsEnhanced.
@Test
public void testDetailsEnhanced() throws Exception {
authentication.setDetails("DETAILS");
Mockito.when(tokenServices.loadAuthentication("FOO")).thenReturn(authentication);
PreAuthenticatedAuthenticationToken request = new PreAuthenticatedAuthenticationToken("FOO", "");
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
servletRequest.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE, "BAR");
OAuth2AuthenticationDetails details = new OAuth2AuthenticationDetails(servletRequest);
request.setDetails(details);
Authentication result = manager.authenticate(request);
assertEquals(authentication, result);
assertEquals("BAR", ((OAuth2AuthenticationDetails) result.getDetails()).getTokenValue());
assertEquals("DETAILS", ((OAuth2AuthenticationDetails) result.getDetails()).getDecodedDetails());
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-security-oauth by spring-projects.
the class OAuth2AuthenticationTests method testSerializationWithDetails.
@Test
public void testSerializationWithDetails() {
OAuth2Authentication holder = new OAuth2Authentication(new AuthorizationRequest("client", Arrays.asList("read")).createOAuth2Request(), new UsernamePasswordAuthenticationToken("user", "pwd"));
holder.setDetails(new OAuth2AuthenticationDetails(new MockHttpServletRequest()));
OAuth2Authentication other = (OAuth2Authentication) SerializationUtils.deserialize(SerializationUtils.serialize(holder));
assertEquals(holder, other);
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project cuba by cuba-platform.
the class IdpAuthLifecycleManager method handleBeforeRestInvocationEvent.
@Order(Events.HIGHEST_PLATFORM_PRECEDENCE + 100)
@EventListener
public void handleBeforeRestInvocationEvent(BeforeRestInvocationEvent event) {
if (idpConfig.getIdpEnabled()) {
if (idpConfig.getIdpPingSessionOnRequest() && event.getAuthentication() instanceof OAuth2Authentication) {
IdpSessionStatus status = pingIdpSession(event.getAuthentication());
if (status == IdpSessionStatus.EXPIRED) {
Object details = event.getAuthentication().getDetails();
String accessToken = ((OAuth2AuthenticationDetails) details).getTokenValue();
oAuthTokenRevoker.revokeAccessToken(accessToken);
log.info("IDP session is expired. REST token {} revoked", accessToken);
event.preventInvocation();
String idpLoginUrl = getIdpLoginUrl(idpConfig.getIdpDefaultRedirectUrl());
Gson gson = new Gson();
String body = gson.toJson(new IdpSessionExpiredResponse("idp_session_expired", idpLoginUrl));
HttpServletResponse response = (HttpServletResponse) event.getResponse();
try {
response.setHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
response.getWriter().write(body);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
} catch (IOException e) {
throw new RuntimeException("Unable to send status to client", e);
}
}
}
}
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project cuba by cuba-platform.
the class IdpAuthLifecycleManager method pingIdpSession.
protected IdpSessionStatus pingIdpSession(Authentication authentication) {
if (authentication instanceof OAuth2Authentication) {
Object details = authentication.getDetails();
String accessTokenId = ((OAuth2AuthenticationDetails) details).getTokenValue();
OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenId);
if (accessToken == null) {
return IdpSessionStatus.UNSUPPORTED;
}
String idpSessionId = getIdpSessionId(accessToken);
if (idpSessionId == null) {
return IdpSessionStatus.UNSUPPORTED;
}
return pingIdpSessionServer(idpSessionId);
}
return IdpSessionStatus.UNSUPPORTED;
}
Aggregations