use of org.cloudfoundry.identity.uaa.oauth.UaaOauth2Authentication in project uaa by cloudfoundry.
the class MultitenantJdbcClientDetailsServiceTests method authenticateAsClient.
private static void authenticateAsClient(final String currentZoneId) {
UaaOauth2Authentication authentication = mock(UaaOauth2Authentication.class);
when(authentication.getZoneId()).thenReturn(currentZoneId);
when(authentication.getPrincipal()).thenReturn("client1");
SecurityContextHolder.getContext().setAuthentication(authentication);
}
use of org.cloudfoundry.identity.uaa.oauth.UaaOauth2Authentication in project uaa by cloudfoundry.
the class UserTokenGranter method validateRequest.
protected Authentication validateRequest(TokenRequest request) {
// things to validate
// 1. Authentication must exist and be authenticated
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated() || !(authentication instanceof UaaOauth2Authentication)) {
throw new InsufficientAuthenticationException("Invalid authentication object:" + authentication);
}
UaaOauth2Authentication oauth2Authentication = (UaaOauth2Authentication) authentication;
// 2. authentication must be a user, and authenticated
if (oauth2Authentication.getUserAuthentication() == null || !oauth2Authentication.getUserAuthentication().isAuthenticated()) {
throw new InsufficientAuthenticationException("Authentication containing a user is required");
}
// 3. parameter requesting_client_id must be present
if (request.getRequestParameters() == null || request.getRequestParameters().get(USER_TOKEN_REQUESTING_CLIENT_ID) == null) {
throw new InvalidGrantException("Parameter " + USER_TOKEN_REQUESTING_CLIENT_ID + " is required.");
}
// 4. grant_type must be user_token
if (!TokenConstants.GRANT_TYPE_USER_TOKEN.equals(request.getGrantType())) {
throw new InvalidGrantException("Invalid grant type");
}
// 5. requesting client must have user_token grant type
ClientDetails requesting = clientDetailsService.loadClientByClientId(request.getRequestParameters().get(USER_TOKEN_REQUESTING_CLIENT_ID), IdentityZoneHolder.get().getId());
super.validateGrantType(GRANT_TYPE_USER_TOKEN, requesting);
// 6. receiving client must have refresh_token grant type
ClientDetails receiving = clientDetailsService.loadClientByClientId(request.getRequestParameters().get(CLIENT_ID), IdentityZoneHolder.get().getId());
super.validateGrantType(GRANT_TYPE_REFRESH_TOKEN, receiving);
return oauth2Authentication.getUserAuthentication();
}
use of org.cloudfoundry.identity.uaa.oauth.UaaOauth2Authentication in project uaa by cloudfoundry.
the class AbstractUaaEvent method appendTokenDetails.
protected void appendTokenDetails(Authentication caller, StringBuilder builder) {
String tokenValue = null;
if (caller instanceof UaaOauth2Authentication) {
tokenValue = ((UaaOauth2Authentication) caller).getTokenValue();
} else if (caller.getDetails() instanceof OAuth2AuthenticationDetails) {
tokenValue = ((OAuth2AuthenticationDetails) authentication.getDetails()).getTokenValue();
}
if (hasText(tokenValue)) {
if (isJwtToken(tokenValue)) {
try {
Jwt token = JwtHelper.decode(tokenValue);
Map<String, Object> claims = JsonUtils.readValue(token.getClaims(), new TypeReference<Map<String, Object>>() {
});
String issuer = claims.get(ClaimConstants.ISS).toString();
String subject = claims.get(ClaimConstants.SUB).toString();
builder.append(", sub=").append(subject).append(", ").append("iss=").append(issuer);
} catch (Exception e) {
builder.append(", <token extraction failed>");
}
} else {
builder.append(", opaque-token=present");
}
}
}
use of org.cloudfoundry.identity.uaa.oauth.UaaOauth2Authentication in project uaa by cloudfoundry.
the class IdentityZoneSwitchingFilter method getAuthenticationForZone.
protected OAuth2Authentication getAuthenticationForZone(String identityZoneId, HttpServletRequest servletRequest) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof OAuth2Authentication)) {
return null;
}
OAuth2Authentication oa = (OAuth2Authentication) authentication;
Object oaDetails = oa.getDetails();
// strip client scopes
OAuth2Request request = oa.getOAuth2Request();
Collection<String> requestAuthorities = UaaStringUtils.getStringsFromAuthorities(request.getAuthorities());
Set<String> clientScopes = new HashSet<>();
Set<String> clientAuthorities = new HashSet<>();
for (String s : getZoneSwitchingScopes(identityZoneId)) {
String scope = stripPrefix(s, identityZoneId);
if (request.getScope().contains(s)) {
clientScopes.add(scope);
}
if (requestAuthorities.contains(s)) {
clientAuthorities.add(scope);
}
}
request = new OAuth2Request(request.getRequestParameters(), request.getClientId(), UaaStringUtils.getAuthoritiesFromStrings(clientAuthorities), request.isApproved(), clientScopes, request.getResourceIds(), request.getRedirectUri(), request.getResponseTypes(), request.getExtensions());
UaaAuthentication userAuthentication = (UaaAuthentication) oa.getUserAuthentication();
if (userAuthentication != null) {
userAuthentication = new UaaAuthentication(userAuthentication.getPrincipal(), null, UaaStringUtils.getAuthoritiesFromStrings(clientScopes), new UaaAuthenticationDetails(servletRequest), true, userAuthentication.getAuthenticatedTime());
}
oa = new UaaOauth2Authentication(((UaaOauth2Authentication) oa).getTokenValue(), IdentityZoneHolder.get().getId(), request, userAuthentication);
oa.setDetails(oaDetails);
return oa;
}
Aggregations