Search in sources :

Example 1 with DefaultUser

use of io.gravitee.am.identityprovider.api.DefaultUser in project gravitee-access-management by gravitee-io.

the class UserAuthenticationManagerTest method shouldAuthenticateUser_singleIdentityProvider.

@Test
public void shouldAuthenticateUser_singleIdentityProvider() {
    Client client = new Client();
    client.setClientId("client-id");
    client.setIdentityProviders(getApplicationIdentityProviders(true, "idp-1"));
    IdentityProvider identityProvider = new IdentityProvider();
    identityProvider.setId("idp-1");
    when(identityProviderManager.getIdentityProvider("idp-1")).thenReturn(identityProvider);
    when(passwordService.checkAccountPasswordExpiry(any(), any(), any())).thenReturn(false);
    when(userAuthenticationService.connect(any(), eq(true))).then(invocation -> {
        io.gravitee.am.identityprovider.api.User idpUser = invocation.getArgument(0);
        User user = new User();
        user.setUsername(idpUser.getUsername());
        return Single.just(user);
    });
    when(identityProviderManager.get("idp-1")).thenReturn(Maybe.just(new AuthenticationProvider() {

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(Authentication authentication) {
            return Maybe.just(new DefaultUser("username"));
        }

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(String username) {
            return Maybe.empty();
        }
    }));
    TestObserver<User> observer = userAuthenticationManager.authenticate(client, new Authentication() {

        @Override
        public Object getCredentials() {
            return null;
        }

        @Override
        public Object getPrincipal() {
            return "username";
        }

        @Override
        public AuthenticationContext getContext() {
            return null;
        }
    }).test();
    observer.assertNoErrors();
    observer.assertComplete();
    observer.assertValue(user -> user.getUsername().equals("username"));
    verify(eventManager, times(1)).publishEvent(eq(AuthenticationEvent.SUCCESS), any());
}
Also used : DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) User(io.gravitee.am.model.User) AuthenticationProvider(io.gravitee.am.identityprovider.api.AuthenticationProvider) IdentityProvider(io.gravitee.am.model.IdentityProvider) ApplicationIdentityProvider(io.gravitee.am.model.idp.ApplicationIdentityProvider) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) Authentication(io.gravitee.am.identityprovider.api.Authentication) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 2 with DefaultUser

use of io.gravitee.am.identityprovider.api.DefaultUser in project gravitee-access-management by gravitee-io.

the class UserAuthenticationManagerTest method shouldAuthenticateUser_multipleIdentityProvider.

@Test
public void shouldAuthenticateUser_multipleIdentityProvider() {
    Client client = new Client();
    client.setClientId("client-id");
    client.setIdentityProviders(getApplicationIdentityProviders(true, "idp-1", "idp-2"));
    IdentityProvider identityProvider = new IdentityProvider();
    identityProvider.setId("idp-1");
    IdentityProvider identityProvider2 = new IdentityProvider();
    identityProvider2.setId("idp-2");
    when(passwordService.checkAccountPasswordExpiry(any(), any(), any())).thenReturn(false);
    when(userAuthenticationService.connect(any(), eq(true))).then(invocation -> {
        io.gravitee.am.identityprovider.api.User idpUser = invocation.getArgument(0);
        User user = new User();
        user.setUsername(idpUser.getUsername());
        return Single.just(user);
    });
    when(identityProviderManager.getIdentityProvider("idp-1")).thenReturn(identityProvider);
    when(identityProviderManager.get("idp-1")).thenReturn(Maybe.just(new AuthenticationProvider() {

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(Authentication authentication) {
            throw new BadCredentialsException();
        }

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(String username) {
            return Maybe.empty();
        }
    }));
    when(identityProviderManager.getIdentityProvider("idp-2")).thenReturn(identityProvider2);
    when(identityProviderManager.get("idp-2")).thenReturn(Maybe.just(new AuthenticationProvider() {

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(Authentication authentication) {
            return Maybe.just(new DefaultUser("username"));
        }

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(String username) {
            return Maybe.empty();
        }
    }));
    TestObserver<User> observer = userAuthenticationManager.authenticate(client, new Authentication() {

        @Override
        public Object getCredentials() {
            return null;
        }

        @Override
        public Object getPrincipal() {
            return "username";
        }

        @Override
        public AuthenticationContext getContext() {
            return null;
        }
    }).test();
    observer.assertNoErrors();
    observer.assertComplete();
    observer.assertValue(user -> user.getUsername().equals("username"));
    verify(eventManager, times(1)).publishEvent(eq(AuthenticationEvent.SUCCESS), any());
}
Also used : DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) User(io.gravitee.am.model.User) AuthenticationProvider(io.gravitee.am.identityprovider.api.AuthenticationProvider) IdentityProvider(io.gravitee.am.model.IdentityProvider) ApplicationIdentityProvider(io.gravitee.am.model.idp.ApplicationIdentityProvider) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) Authentication(io.gravitee.am.identityprovider.api.Authentication) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 3 with DefaultUser

use of io.gravitee.am.identityprovider.api.DefaultUser in project gravitee-access-management by gravitee-io.

the class UserAuthenticationManagerTest method shouldAuthenticateUser_singleIdentityProvider_PasswordExipry.

@Test
public void shouldAuthenticateUser_singleIdentityProvider_PasswordExipry() {
    Client client = new Client();
    client.setClientId("client-id");
    client.setIdentityProviders(getApplicationIdentityProviders(true, "idp-1"));
    IdentityProvider identityProvider = new IdentityProvider();
    identityProvider.setId("idp-1");
    when(identityProviderManager.getIdentityProvider("idp-1")).thenReturn(identityProvider);
    when(passwordService.checkAccountPasswordExpiry(any(), any(), any())).thenReturn(true);
    when(userAuthenticationService.connect(any(), eq(true))).then(invocation -> {
        io.gravitee.am.identityprovider.api.User idpUser = invocation.getArgument(0);
        User user = new User();
        user.setUsername(idpUser.getUsername());
        return Single.just(user);
    });
    when(identityProviderManager.get("idp-1")).thenReturn(Maybe.just(new AuthenticationProvider() {

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(Authentication authentication) {
            return Maybe.just(new DefaultUser("username"));
        }

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(String username) {
            return Maybe.empty();
        }
    }));
    TestObserver<User> observer = userAuthenticationManager.authenticate(client, new Authentication() {

        @Override
        public Object getCredentials() {
            return null;
        }

        @Override
        public Object getPrincipal() {
            return "username";
        }

        @Override
        public AuthenticationContext getContext() {
            return null;
        }
    }).test();
    observer.awaitTerminalEvent();
    observer.assertError(AccountPasswordExpiredException.class);
    verify(eventManager, times(1)).publishEvent(eq(AuthenticationEvent.FAILURE), any());
}
Also used : DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) User(io.gravitee.am.model.User) AuthenticationProvider(io.gravitee.am.identityprovider.api.AuthenticationProvider) IdentityProvider(io.gravitee.am.model.IdentityProvider) ApplicationIdentityProvider(io.gravitee.am.model.idp.ApplicationIdentityProvider) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) Authentication(io.gravitee.am.identityprovider.api.Authentication) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 4 with DefaultUser

use of io.gravitee.am.identityprovider.api.DefaultUser in project gravitee-access-management by gravitee-io.

the class UserAuthenticationManagerTest method shouldAuthenticateUser_multipleIDPs_firstPriorityIdentityProvider.

@Test
public void shouldAuthenticateUser_multipleIDPs_firstPriorityIdentityProvider() {
    Client client = new Client();
    client.setClientId("client-id");
    client.setIdentityProviders(getApplicationIdentityProviders(true, "idp-1", "idp-2"));
    IdentityProvider identityProvider = new IdentityProvider();
    identityProvider.setId("idp-1");
    IdentityProvider identityProvider2 = new IdentityProvider();
    identityProvider2.setId("idp-2");
    when(userAuthenticationService.connect(any(), eq(true))).then(invocation -> {
        io.gravitee.am.identityprovider.api.User idpUser = invocation.getArgument(0);
        User user = new User();
        user.setUsername(idpUser.getUsername());
        return Single.just(user);
    });
    when(identityProviderManager.getIdentityProvider("idp-1")).thenReturn(identityProvider);
    when(identityProviderManager.get("idp-1")).thenReturn(Maybe.just(new AuthenticationProvider() {

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(Authentication authentication) {
            return Maybe.just(new DefaultUser("username1"));
        }

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(String username) {
            return Maybe.empty();
        }
    }));
    when(identityProviderManager.getIdentityProvider("idp-2")).thenReturn(identityProvider2);
    TestObserver<User> observer = userAuthenticationManager.authenticate(client, new Authentication() {

        @Override
        public Object getCredentials() {
            return null;
        }

        @Override
        public Object getPrincipal() {
            return "username";
        }

        @Override
        public AuthenticationContext getContext() {
            return null;
        }
    }).test();
    observer.assertNoErrors();
    observer.assertComplete();
    observer.assertValue(user -> user.getUsername().equals("username1"));
    verify(eventManager, times(1)).publishEvent(eq(AuthenticationEvent.SUCCESS), any());
    client.setIdentityProviders(getApplicationIdentityProviders(false, "idp-1", "idp-2"));
    when(identityProviderManager.get("idp-2")).thenReturn(Maybe.just(new AuthenticationProvider() {

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(Authentication authentication) {
            return Maybe.just(new DefaultUser("username2"));
        }

        @Override
        public Maybe<io.gravitee.am.identityprovider.api.User> loadUserByUsername(String username) {
            return Maybe.empty();
        }
    }));
    observer = userAuthenticationManager.authenticate(client, new Authentication() {

        @Override
        public Object getCredentials() {
            return null;
        }

        @Override
        public Object getPrincipal() {
            return "username";
        }

        @Override
        public AuthenticationContext getContext() {
            return null;
        }
    }).test();
    observer.assertNoErrors();
    observer.assertComplete();
    observer.assertValue(user -> user.getUsername().equals("username2"));
    verify(eventManager, times(2)).publishEvent(eq(AuthenticationEvent.SUCCESS), any());
}
Also used : DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) User(io.gravitee.am.model.User) AuthenticationContext(io.gravitee.am.identityprovider.api.AuthenticationContext) AuthenticationProvider(io.gravitee.am.identityprovider.api.AuthenticationProvider) IdentityProvider(io.gravitee.am.model.IdentityProvider) ApplicationIdentityProvider(io.gravitee.am.model.idp.ApplicationIdentityProvider) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) Authentication(io.gravitee.am.identityprovider.api.Authentication) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 5 with DefaultUser

use of io.gravitee.am.identityprovider.api.DefaultUser in project gravitee-access-management by gravitee-io.

the class UserServiceImpl method convert.

private io.gravitee.am.identityprovider.api.User convert(User user) {
    DefaultUser idpUser = new DefaultUser(user.getUsername());
    idpUser.setCredentials(user.getPassword());
    Map<String, Object> additionalInformation = new HashMap<>();
    if (user.getFirstName() != null) {
        idpUser.setFirstName(user.getFirstName());
        additionalInformation.put(StandardClaims.GIVEN_NAME, user.getFirstName());
    }
    if (user.getLastName() != null) {
        idpUser.setLastName(user.getLastName());
        additionalInformation.put(StandardClaims.FAMILY_NAME, user.getLastName());
    }
    if (user.getEmail() != null) {
        idpUser.setEmail(user.getEmail());
        additionalInformation.put(StandardClaims.EMAIL, user.getEmail());
    }
    if (user.getAdditionalInformation() != null) {
        user.getAdditionalInformation().forEach(additionalInformation::putIfAbsent);
    }
    idpUser.setAdditionalInformation(additionalInformation);
    return idpUser;
}
Also used : DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) RandomString(io.gravitee.am.common.utils.RandomString)

Aggregations

DefaultUser (io.gravitee.am.identityprovider.api.DefaultUser)113 Test (org.junit.Test)70 Maybe (io.reactivex.Maybe)27 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)21 TestObserver (io.reactivex.observers.TestObserver)20 User (io.gravitee.am.model.User)19 Single (io.reactivex.Single)18 Client (io.gravitee.am.model.oidc.Client)17 EventType (io.gravitee.am.common.audit.EventType)15 Organization (io.gravitee.am.model.Organization)15 ReferenceType (io.gravitee.am.model.ReferenceType)15 Completable (io.reactivex.Completable)15 HashMap (java.util.HashMap)15 Before (org.junit.Before)15 RunWith (org.junit.runner.RunWith)15 Mock (org.mockito.Mock)15 Mockito (org.mockito.Mockito)15 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)15 ConstantKeys (io.gravitee.am.common.utils.ConstantKeys)14 TechnicalException (io.gravitee.am.repository.exceptions.TechnicalException)14