Search in sources :

Example 16 with AccountBO

use of com.nexblocks.authguard.service.model.AccountBO in project AuthGuard by AuthGuard.

the class ActionTokenServiceImpl method generateFromBasicAuth.

@Override
public Try<ActionTokenBO> generateFromBasicAuth(final AuthRequestBO authRequest, final String action) {
    final Either<Exception, AccountBO> authResult = basicAuthProvider.getAccount(authRequest);
    if (authResult.isLeft()) {
        return Try.failure(authResult.getLeft());
    }
    final AccountBO account = authResult.get();
    final AccountTokenDO token = generateToken(account, action);
    return Try.success(ActionTokenBO.builder().accountId(account.getId()).token(token.getToken()).validFor(TOKEN_LIFETIME.toSeconds()).build());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException)

Example 17 with AccountBO

use of com.nexblocks.authguard.service.model.AccountBO in project AuthGuard by AuthGuard.

the class ActionTokenServiceImpl method generateFromOtp.

@Override
public Try<ActionTokenBO> generateFromOtp(final String passwordId, final String otp, final String action) {
    final String otpToken = passwordId + ":" + otp;
    final Either<Exception, Optional<AccountBO>> otpResult = otpVerifier.verifyAccountToken(otpToken).map(accountsService::getById);
    if (otpResult.isLeft()) {
        return Try.failure(otpResult.getLeft());
    }
    final AccountBO account = otpResult.get().orElse(null);
    if (account == null) {
        return Try.failure(new ServiceException(ErrorCode.ACCOUNT_DOES_NOT_EXIST, "The account associated with that OTP no longer exists"));
    }
    final AccountTokenDO token = generateToken(account, action);
    return Try.success(ActionTokenBO.builder().accountId(account.getId()).token(token.getToken()).validFor(TOKEN_LIFETIME.toSeconds()).build());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) Optional(java.util.Optional) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) ServiceException(com.nexblocks.authguard.service.exceptions.ServiceException)

Example 18 with AccountBO

use of com.nexblocks.authguard.service.model.AccountBO in project AuthGuard by AuthGuard.

the class AccountsApiTest method create.

@Test
void create() {
    final CreateAccountRequestDTO requestDTO = CreateAccountRequestDTO.builder().externalId("external").email(AccountEmailDTO.builder().email("email@server.com").build()).domain("main").build();
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(UUID.randomUUID().toString()).build();
    final AccountBO accountBO = mapper().toBO(requestDTO);
    final AccountBO serviceResponse = accountBO.withId(UUID.randomUUID().toString());
    Mockito.when(accountsService.create(Mockito.eq(accountBO), Mockito.any())).thenReturn(serviceResponse);
    LOG.info("Request {}", requestDTO);
    final ValidatableResponse httpResponse = given().body(requestDTO).contentType(ContentType.JSON).header(IdempotencyHeader.HEADER_NAME, requestContext.getIdempotentKey()).post(url()).then().statusCode(201).contentType(ContentType.JSON);
    final AccountDTO response = httpResponse.extract().response().getBody().as(AccountDTO.class);
    assertThat(response).isEqualToIgnoringGivenFields(requestDTO, "id", "deleted", "createdAt", "lastModified", "social", "identityProvider");
    assertThat(response.getId()).isEqualTo(serviceResponse.getId());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) ValidatableResponse(io.restassured.response.ValidatableResponse) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) CreateAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO) AccountDTO(com.nexblocks.authguard.api.dto.entities.AccountDTO) Test(org.junit.jupiter.api.Test)

Example 19 with AccountBO

use of com.nexblocks.authguard.service.model.AccountBO in project AuthGuard by AuthGuard.

the class AccountsApiTest method createWithCredentials.

@Test
void createWithCredentials() {
    final CreateAccountRequestDTO accountRequest = CreateAccountRequestDTO.builder().externalId("external").email(AccountEmailDTO.builder().email("email@server.com").build()).domain("main").build();
    final CreateCredentialsRequestDTO credentialsRequest = CreateCredentialsRequestDTO.builder().plainPassword("password").addIdentifiers(UserIdentifierDTO.builder().identifier("username").type(UserIdentifier.Type.USERNAME).build()).build();
    final CreateCompleteAccountRequestDTO completeRequest = CreateCompleteAccountRequestDTO.builder().account(accountRequest).credentials(credentialsRequest).build();
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(UUID.randomUUID().toString()).build();
    final AccountBO accountBO = mapper().toBO(accountRequest);
    final AccountBO accountResponse = accountBO.withId(UUID.randomUUID().toString());
    final CredentialsBO credentialsBO = mapper().toBO(credentialsRequest).withAccountId(accountResponse.getId());
    final CredentialsBO credentialsResponse = credentialsBO.withId(UUID.randomUUID().toString());
    Mockito.when(accountsService.create(Mockito.eq(accountBO), Mockito.any())).thenReturn(accountResponse);
    Mockito.when(credentialsService.create(Mockito.eq(credentialsBO), Mockito.any())).thenReturn(credentialsResponse);
    LOG.info("Request {}", accountRequest);
    final ValidatableResponse httpResponse = given().body(completeRequest).contentType(ContentType.JSON).header(IdempotencyHeader.HEADER_NAME, requestContext.getIdempotentKey()).post(url("complete")).then().statusCode(201).contentType(ContentType.JSON);
    final CreateCompleteAccountResponseDTO response = httpResponse.extract().response().getBody().as(CreateCompleteAccountResponseDTO.class);
    assertThat(response.getAccountId()).isEqualTo(accountResponse.getId());
    assertThat(response.getCredentialsId()).isEqualTo(credentialsResponse.getId());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) CredentialsBO(com.nexblocks.authguard.service.model.CredentialsBO) ValidatableResponse(io.restassured.response.ValidatableResponse) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) CreateAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO) CreateCompleteAccountResponseDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountResponseDTO) CreateCompleteAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountRequestDTO) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO) Test(org.junit.jupiter.api.Test)

Example 20 with AccountBO

use of com.nexblocks.authguard.service.model.AccountBO in project AuthGuard by AuthGuard.

the class AccountsApiTest method createWithCredentialsAccountExists.

@Test
void createWithCredentialsAccountExists() {
    final CreateAccountRequestDTO accountRequest = CreateAccountRequestDTO.builder().externalId("external").email(AccountEmailDTO.builder().email("email@server.com").build()).domain("main").build();
    final CreateCredentialsRequestDTO credentialsRequest = CreateCredentialsRequestDTO.builder().plainPassword("password").addIdentifiers(UserIdentifierDTO.builder().identifier("username").type(UserIdentifier.Type.USERNAME).build()).build();
    final CreateCompleteAccountRequestDTO completeRequest = CreateCompleteAccountRequestDTO.builder().account(accountRequest).credentials(credentialsRequest).build();
    final RequestContextBO requestContext = RequestContextBO.builder().idempotentKey(UUID.randomUUID().toString()).build();
    final AccountBO accountBO = mapper().toBO(accountRequest);
    final AccountBO accountResponse = accountBO.withId(UUID.randomUUID().toString());
    final CredentialsBO credentialsBO = mapper().toBO(credentialsRequest).withAccountId(accountResponse.getId());
    final CredentialsBO credentialsResponse = credentialsBO.withId(UUID.randomUUID().toString());
    Mockito.when(accountsService.create(Mockito.eq(accountBO), Mockito.any())).thenThrow(new CompletionException(new IdempotencyException(IdempotentRecordBO.builder().entityId(accountResponse.getId()).build())));
    Mockito.when(credentialsService.create(Mockito.eq(credentialsBO), Mockito.any())).thenReturn(credentialsResponse);
    LOG.info("Request {}", accountRequest);
    final ValidatableResponse httpResponse = given().body(completeRequest).contentType(ContentType.JSON).header(IdempotencyHeader.HEADER_NAME, requestContext.getIdempotentKey()).post(url("complete")).then().statusCode(201).contentType(ContentType.JSON);
    final CreateCompleteAccountResponseDTO response = httpResponse.extract().response().getBody().as(CreateCompleteAccountResponseDTO.class);
    assertThat(response.getAccountId()).isEqualTo(accountResponse.getId());
    assertThat(response.getCredentialsId()).isEqualTo(credentialsResponse.getId());
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) CredentialsBO(com.nexblocks.authguard.service.model.CredentialsBO) ValidatableResponse(io.restassured.response.ValidatableResponse) RequestContextBO(com.nexblocks.authguard.service.model.RequestContextBO) CreateAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO) CompletionException(java.util.concurrent.CompletionException) IdempotencyException(com.nexblocks.authguard.service.exceptions.IdempotencyException) CreateCompleteAccountResponseDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountResponseDTO) CreateCompleteAccountRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountRequestDTO) CreateCredentialsRequestDTO(com.nexblocks.authguard.api.dto.requests.CreateCredentialsRequestDTO) Test(org.junit.jupiter.api.Test)

Aggregations

AccountBO (com.nexblocks.authguard.service.model.AccountBO)55 Test (org.junit.jupiter.api.Test)43 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)21 Message (com.nexblocks.authguard.emb.model.Message)15 AuthResponseBO (com.nexblocks.authguard.service.model.AuthResponseBO)15 OtpMessageBody (com.nexblocks.authguard.basic.otp.OtpMessageBody)8 PasswordlessMessageBody (com.nexblocks.authguard.basic.passwordless.PasswordlessMessageBody)8 OneTimePasswordBO (com.nexblocks.authguard.service.model.OneTimePasswordBO)8 ServiceAuthorizationException (com.nexblocks.authguard.service.exceptions.ServiceAuthorizationException)7 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)6 ImmutableTextMessage (com.nexblocks.authguard.external.sms.ImmutableTextMessage)6 RequestContextBO (com.nexblocks.authguard.service.model.RequestContextBO)6 ImmutableEmail (com.nexblocks.authguard.external.email.ImmutableEmail)5 JwtConfig (com.nexblocks.authguard.service.config.JwtConfig)5 StrategyConfig (com.nexblocks.authguard.service.config.StrategyConfig)5 ServiceException (com.nexblocks.authguard.service.exceptions.ServiceException)5 AuthRequestBO (com.nexblocks.authguard.service.model.AuthRequestBO)5 CreateAccountRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateAccountRequestDTO)4 OtpConfig (com.nexblocks.authguard.basic.config.OtpConfig)4 CreateCompleteAccountRequestDTO (com.nexblocks.authguard.api.dto.requests.CreateCompleteAccountRequestDTO)3