Search in sources :

Example 1 with SessionsService

use of com.nexblocks.authguard.service.SessionsService in project AuthGuard by AuthGuard.

the class SessionProviderTest method generateForAccount.

@Test
void generateForAccount() {
    final SessionsService sessionsService = Mockito.mock(SessionsService.class);
    Mockito.when(sessionsService.create(any())).thenAnswer(invocation -> invocation.getArgument(0, SessionBO.class).withSessionToken("token"));
    final SessionProvider sessionProvider = new SessionProvider(sessionsService, sessionsConfig());
    final AccountBO account = AccountBO.builder().id("account-id").build();
    final AuthResponseBO expected = AuthResponseBO.builder().type("session_token").entityType(EntityType.ACCOUNT).entityId(account.getId()).validFor(Duration.ofMinutes(20).getSeconds()).build();
    final AuthResponseBO actual = sessionProvider.generateToken(account);
    assertThat(actual).isEqualToIgnoringGivenFields(expected, "token");
    assertThat(actual.getToken()).isNotNull().isInstanceOf(String.class);
}
Also used : SessionsService(com.nexblocks.authguard.service.SessionsService) Test(org.junit.jupiter.api.Test)

Example 2 with SessionsService

use of com.nexblocks.authguard.service.SessionsService in project AuthGuard by AuthGuard.

the class SessionProviderTest method delete.

@Test
void delete() {
    final SessionsService sessionsService = Mockito.mock(SessionsService.class);
    final SessionProvider sessionProvider = new SessionProvider(sessionsService, sessionsConfig());
    final String sessionToken = "session-token";
    final String accountId = "account";
    Mockito.when(sessionsService.deleteByToken(sessionToken)).thenReturn(Optional.of(SessionBO.builder().accountId(accountId).sessionToken(sessionToken).build()));
    final AuthResponseBO expected = AuthResponseBO.builder().type("session_token").entityType(EntityType.ACCOUNT).entityId(accountId).token(sessionToken).build();
    final AuthResponseBO actual = sessionProvider.delete(AuthRequestBO.builder().token(sessionToken).build());
    assertThat(actual).isEqualTo(expected);
}
Also used : SessionsService(com.nexblocks.authguard.service.SessionsService) Test(org.junit.jupiter.api.Test)

Example 3 with SessionsService

use of com.nexblocks.authguard.service.SessionsService in project AuthGuard by AuthGuard.

the class SessionProviderTest method generateForInactiveAccount.

@Test
void generateForInactiveAccount() {
    final SessionsService sessionsService = Mockito.mock(SessionsService.class);
    Mockito.when(sessionsService.create(any())).thenAnswer(invocation -> invocation.getArgument(0, SessionBO.class).withSessionToken("token"));
    final SessionProvider sessionProvider = new SessionProvider(sessionsService, sessionsConfig());
    final AccountBO account = AccountBO.builder().id("account-id").active(false).build();
    assertThatThrownBy(() -> sessionProvider.generateToken(account)).isInstanceOf(ServiceAuthorizationException.class);
}
Also used : SessionsService(com.nexblocks.authguard.service.SessionsService) Test(org.junit.jupiter.api.Test)

Example 4 with SessionsService

use of com.nexblocks.authguard.service.SessionsService in project AuthGuard by AuthGuard.

the class SessionVerifierTest method verifyNonExistingSession.

@Test
void verifyNonExistingSession() {
    final SessionsService sessionsService = Mockito.mock(SessionsService.class);
    final SessionVerifier sessionVerifier = new SessionVerifier(sessionsService);
    Mockito.when(sessionsService.getByToken(any())).thenReturn(Optional.empty());
    final Either<Exception, String> result = sessionVerifier.verifyAccountToken("invalid");
    assertThat(result.isLeft());
    assertThat(result.getLeft()).isInstanceOf(ServiceAuthorizationException.class);
}
Also used : SessionsService(com.nexblocks.authguard.service.SessionsService) ServiceAuthorizationException(com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 5 with SessionsService

use of com.nexblocks.authguard.service.SessionsService in project AuthGuard by AuthGuard.

the class OAuthServiceTest method setup.

@BeforeAll
void setup() {
    testIdentityServer = new TestIdentityServer();
    testIdentityServer.start();
    clientConfiguration = ImmutableOAuthClientConfiguration.builder().provider("test").authUrl("http://localhost:" + testIdentityServer.getPort() + "/auth").tokenUrl("http://localhost:" + testIdentityServer.getPort() + "/token").authRedirectUrl("http://localhost/redirect").tokenRedirectUrl("http://localhost/redirect").clientId("unit-tests").clientSecret("secret").addDefaultScopes("openid", "profile").build();
    accountProviderClientConfiguration = ImmutableOAuthClientConfiguration.builder().provider("account_test").authUrl("http://localhost:" + testIdentityServer.getPort() + "/auth").tokenUrl("http://localhost:" + testIdentityServer.getPort() + "/token").authRedirectUrl("http://localhost/redirect").tokenRedirectUrl("http://localhost/redirect").clientId("unit-tests").clientSecret("secret").addDefaultScopes("openid", "profile").accountProvider(true).emailField("email").build();
    final ImmutableOAuthConfiguration oAuthConfiguration = ImmutableOAuthConfiguration.builder().addClients(clientConfiguration).addClients(accountProviderClientConfiguration).build();
    sessionsService = Mockito.mock(SessionsService.class);
    accountsService = Mockito.mock(AccountsService.class);
    oAuthService = new OAuthService(oAuthConfiguration, sessionsService, accountsService);
}
Also used : TestIdentityServer(com.nexblocks.authguard.jwt.oauth.TestIdentityServer) SessionsService(com.nexblocks.authguard.service.SessionsService) AccountsService(com.nexblocks.authguard.service.AccountsService) ImmutableOAuthConfiguration(com.nexblocks.authguard.jwt.oauth.config.ImmutableOAuthConfiguration)

Aggregations

SessionsService (com.nexblocks.authguard.service.SessionsService)8 Test (org.junit.jupiter.api.Test)7 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)3 SessionBO (com.nexblocks.authguard.service.model.SessionBO)2 TestIdentityServer (com.nexblocks.authguard.jwt.oauth.TestIdentityServer)1 ImmutableOAuthConfiguration (com.nexblocks.authguard.jwt.oauth.config.ImmutableOAuthConfiguration)1 AccountsService (com.nexblocks.authguard.service.AccountsService)1