Search in sources :

Example 41 with OAuth2Authentication

use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project geonetwork-microservices by geonetwork.

the class SearchControllerSecurityConfigurerTest method createToken.

private String createToken(String userName, List<GrantedAuthority> authorities) {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userName, null, authorities);
    OAuth2Request request = new OAuth2Request(null, "clientId", null, true, null, null, null, null, null);
    OAuth2Authentication auth = new OAuth2Authentication(request, token);
    return jwtAccessTokenConverter.enhance(new DefaultOAuth2AccessToken(""), auth).getValue();
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)

Example 42 with OAuth2Authentication

use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project uaa by cloudfoundry.

the class CheckTokenEndpointTests method testClientWildcard.

@Test
public void testClientWildcard() throws Exception {
    BaseClientDetails client = new BaseClientDetails("client", "zones", "zones.*.admin", "authorization_code, password", "scim.read, scim.write", "http://localhost:8080/uaa");
    client.setAutoApproveScopes(Collections.singletonList("zones.*.admin"));
    Map<String, BaseClientDetails> clientDetailsStore = Collections.singletonMap("client", client);
    clientDetailsService.setClientDetailsStore(IdentityZoneHolder.get().getId(), clientDetailsStore);
    tokenServices.setClientDetailsService(clientDetailsService);
    authorizationRequest = new AuthorizationRequest("client", Collections.singleton("zones.myzone.admin"));
    authorizationRequest.setResourceIds(new HashSet<>(Arrays.asList("client", "zones")));
    authentication = new OAuth2Authentication(authorizationRequest.createOAuth2Request(), UaaAuthenticationTestFactory.getAuthentication(userId, userName, "olds@vmware.com"));
    endpoint.checkToken(tokenServices.createAccessToken(authentication).getValue(), Collections.emptyList(), request);
}
Also used : BaseClientDetails(org.springframework.security.oauth2.provider.client.BaseClientDetails) AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 43 with OAuth2Authentication

use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project uaa by cloudfoundry.

the class LocalUaaRestTemplateTests method acquireAccessToken.

@Test
void acquireAccessToken() {
    OAuth2ClientContext mockOAuth2ClientContext = mock(OAuth2ClientContext.class);
    OAuth2AccessToken mockOAuth2AccessToken = mock(OAuth2AccessToken.class);
    when(mockAuthorizationServerTokenServices.createAccessToken(any())).thenReturn(mockOAuth2AccessToken);
    OAuth2AccessToken actualResult = localUaaRestTemplate.acquireAccessToken(mockOAuth2ClientContext);
    assertThat(actualResult, is(mockOAuth2AccessToken));
    ImmutableMap<String, String> requestParameters = ImmutableMap.<String, String>builder().put(OAuth2Utils.CLIENT_ID, "login").put(OAuth2Utils.GRANT_TYPE, GRANT_TYPE_CLIENT_CREDENTIALS).build();
    OAuth2Request request = new OAuth2Request(requestParameters, "login", new HashSet<>(), true, Sets.newHashSet("something", "else"), Sets.newHashSet(OriginKeys.UAA), null, new HashSet<>(), ImmutableMap.<String, Serializable>builder().build());
    OAuth2Authentication authentication = new OAuth2Authentication(request, null);
    verify(mockIdentityZoneManager).getCurrentIdentityZoneId();
    verify(mockMultitenantClientServices).loadClientByClientId("login", "currentIdentityZoneId");
    verify(mockOAuth2ClientContext).setAccessToken(mockOAuth2AccessToken);
    verify(mockAuthorizationServerTokenServices).createAccessToken(authentication);
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) Serializable(java.io.Serializable) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OAuth2ClientContext(org.springframework.security.oauth2.client.OAuth2ClientContext) Test(org.junit.jupiter.api.Test)

Example 44 with OAuth2Authentication

use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project uaa by cloudfoundry.

the class AbstractUaaEvent method getOrigin.

// Ideally we want to get to the point where details is never null, but this
// isn't currently possible
// due to some OAuth authentication scenarios which don't set it.
protected String getOrigin(Principal principal) {
    if (principal instanceof Authentication) {
        Authentication caller = (Authentication) principal;
        StringBuilder builder = new StringBuilder();
        if (caller instanceof OAuth2Authentication) {
            OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) caller;
            builder.append("client=").append(oAuth2Authentication.getOAuth2Request().getClientId());
            if (!oAuth2Authentication.isClientOnly()) {
                builder.append(", ").append("user=").append(oAuth2Authentication.getName());
            }
        } else {
            builder.append("caller=").append(caller.getName());
        }
        if (caller.getDetails() != null) {
            builder.append(", details=(");
            try {
                @SuppressWarnings("unchecked") Map<String, Object> map = JsonUtils.readValue((String) caller.getDetails(), new TypeReference<Map<String, Object>>() {
                });
                if (map.containsKey("remoteAddress")) {
                    builder.append("remoteAddress=").append(map.get("remoteAddress")).append(", ");
                }
                builder.append("type=").append(caller.getDetails().getClass().getSimpleName());
            } catch (Exception e) {
                // ignore
                builder.append(caller.getDetails());
            }
            appendTokenDetails(caller, builder);
            builder.append(")");
        }
        return builder.toString();
    }
    return principal == null ? null : principal.getName();
}
Also used : UaaOauth2Authentication(org.cloudfoundry.identity.uaa.oauth.UaaOauth2Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Map(java.util.Map)

Example 45 with OAuth2Authentication

use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project uaa by cloudfoundry.

the class LoginServerTokenEndpointFilter method onSuccessfulAuthentication.

@Override
protected void onSuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) throws IOException {
    super.onSuccessfulAuthentication(request, response, authResult);
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth instanceof OAuth2Authentication) {
        ((OAuth2Authentication) auth).setAuthenticated(true);
    }
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication)

Aggregations

OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)538 Authentication (org.springframework.security.core.Authentication)211 Test (org.junit.Test)192 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)177 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)159 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)107 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)91 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)68 HashMap (java.util.HashMap)67 Date (java.util.Date)47 IsEmptyString.isEmptyString (org.hamcrest.text.IsEmptyString.isEmptyString)42 GrantedAuthority (org.springframework.security.core.GrantedAuthority)35 Map (java.util.Map)32 OAuth2RefreshToken (org.springframework.security.oauth2.common.OAuth2RefreshToken)30 BaseClientDetails (org.springframework.security.oauth2.provider.client.BaseClientDetails)30 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)29 InvalidTokenException (org.springframework.security.oauth2.common.exceptions.InvalidTokenException)28 OAuth2Authentication (org.maxkey.authz.oauth2.provider.OAuth2Authentication)27 UaaAuthentication (org.cloudfoundry.identity.uaa.authentication.UaaAuthentication)26 HashSet (java.util.HashSet)23