Search in sources :

Example 46 with OAuth2Authentication

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

the class LoginAuthenticationManagerTests method setUp.

@BeforeEach
void setUp() {
    publisher = TestApplicationEventPublisher.forEventClass(IdentityProviderAuthenticationSuccessEvent.class);
    mockIdentityZoneManager = mock(IdentityZoneManager.class);
    manager = new LoginAuthenticationManager(mockIdentityZoneManager);
    manager.setApplicationEventPublisher(publisher);
    userDatabase = mock(UaaUserDatabase.class);
    manager.setUserDatabase(userDatabase);
    OAuth2Authentication oauth2Authentication = new OAuth2Authentication(new AuthorizationRequest("client", Arrays.asList("read", "write")).createOAuth2Request(), null);
    SecurityContextImpl context = new SecurityContextImpl();
    context.setAuthentication(oauth2Authentication);
    SecurityContextHolder.setContext(context);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) IdentityProviderAuthenticationSuccessEvent(org.cloudfoundry.identity.uaa.authentication.event.IdentityProviderAuthenticationSuccessEvent) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UaaUserDatabase(org.cloudfoundry.identity.uaa.user.UaaUserDatabase) IdentityZoneManager(org.cloudfoundry.identity.uaa.zone.beans.IdentityZoneManager) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 47 with OAuth2Authentication

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

the class PasswordChangeEventPublisherTests method setUp.

@BeforeEach
void setUp() {
    mockScimUserProvisioning = mock(ScimUserProvisioning.class);
    mockApplicationEventPublisher = mock(ApplicationEventPublisher.class);
    mockIdentityZoneManager = mock(IdentityZoneManager.class);
    currentZoneId = "currentZoneId-" + RandomStringUtils.random(8);
    subject = new PasswordChangeEventPublisher(mockScimUserProvisioning, mockIdentityZoneManager);
    subject.setApplicationEventPublisher(mockApplicationEventPublisher);
    authentication = new OAuth2Authentication(new AuthorizationRequest("client", Collections.singletonList("read")).createOAuth2Request(), UaaPasswordTestFactory.getAuthentication("ID", "joe", "joe@test.org"));
    SecurityContextHolder.getContext().setAuthentication(authentication);
}
Also used : AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) ScimUserProvisioning(org.cloudfoundry.identity.uaa.scim.ScimUserProvisioning) IdentityZoneManager(org.cloudfoundry.identity.uaa.zone.beans.IdentityZoneManager) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 48 with OAuth2Authentication

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

the class DefaultSecurityContextAccessor method getAuthenticationInfo.

@Override
public String getAuthenticationInfo() {
    Authentication a = SecurityContextHolder.getContext().getAuthentication();
    if (a instanceof OAuth2Authentication) {
        OAuth2Authentication oauth = ((OAuth2Authentication) a);
        String info = getClientId();
        if (!oauth.isClientOnly()) {
            info = info + "; " + a.getName() + "; " + getUserId();
        }
        return info;
    } else {
        return a.getName();
    }
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication)

Example 49 with OAuth2Authentication

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

the class DefaultSecurityContextAccessor method isAdmin.

@Override
public boolean isAdmin() {
    Authentication a = SecurityContextHolder.getContext().getAuthentication();
    String[] adminRoles = new String[] { "uaa.admin" };
    if (a == null) {
        return false;
    }
    boolean result = false;
    if (a instanceof OAuth2Authentication) {
        OAuth2Authentication oa = (OAuth2Authentication) a;
        result = OAuth2ExpressionUtils.hasAnyScope(oa, adminRoles);
    } else {
        result = hasAnyAdminScope(a, adminRoles);
    }
    String zoneAdminRole = "zones." + IdentityZoneHolder.get().getId() + ".admin";
    if (!result) {
        ContextSensitiveOAuth2SecurityExpressionMethods eval = new ContextSensitiveOAuth2SecurityExpressionMethods(a, IdentityZone.getUaa());
        result = eval.hasScopeInAuthZone(zoneAdminRole);
    }
    return result;
}
Also used : ContextSensitiveOAuth2SecurityExpressionMethods(org.cloudfoundry.identity.uaa.security.ContextSensitiveOAuth2SecurityExpressionMethods) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication)

Example 50 with OAuth2Authentication

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

the class DefaultSecurityContextAccessorTests method zoneAdminUserIsNotAdmin_BecauseOriginIsNotUaa.

@Test
void zoneAdminUserIsNotAdmin_BecauseOriginIsNotUaa() {
    BaseClientDetails client = new BaseClientDetails();
    List<SimpleGrantedAuthority> authorities = new LinkedList<>();
    authorities.add(new SimpleGrantedAuthority("zones." + IdentityZoneHolder.get().getId() + ".admin"));
    client.setAuthorities(authorities);
    UaaPrincipal principal = new UaaPrincipal("id", "username", "email", OriginKeys.UAA, null, MultitenancyFixture.identityZone("test", "test").getId());
    UaaAuthentication userAuthentication = new UaaAuthentication(principal, authorities, new UaaAuthenticationDetails(new MockHttpServletRequest()));
    AuthorizationRequest authorizationRequest = new AuthorizationRequest("admin", UaaStringUtils.getStringsFromAuthorities(authorities));
    authorizationRequest.setResourceIdsAndAuthoritiesFromClientDetails(client);
    SecurityContextHolder.getContext().setAuthentication(new OAuth2Authentication(authorizationRequest.createOAuth2Request(), userAuthentication));
    assertFalse(defaultSecurityContextAccessor.isAdmin());
}
Also used : BaseClientDetails(org.springframework.security.oauth2.provider.client.BaseClientDetails) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UaaPrincipal(org.cloudfoundry.identity.uaa.authentication.UaaPrincipal) AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) UaaAuthenticationDetails(org.cloudfoundry.identity.uaa.authentication.UaaAuthenticationDetails) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

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