Search in sources :

Example 1 with GA4GHPassport

use of bio.terra.externalcreds.models.GA4GHPassport in project terra-external-credentials-manager by DataBiosphere.

the class AuthorizationCodeExchangeTest method runTest.

private void runTest(LinkedAccount expectedLinkedAccount, GA4GHPassport expectedPassport, List<GA4GHVisa> expectedVisas) throws URISyntaxException {
    var state = new OAuth2State.Builder().provider(expectedLinkedAccount.getProviderName()).random(OAuth2State.generateRandomState(new SecureRandom())).build();
    String encodedState = state.encode(objectMapper);
    setupMocks(expectedLinkedAccount, expectedPassport, authorizationCode, redirectUri, scopes, encodedState);
    linkedAccountService.upsertOAuth2State(expectedLinkedAccount.getUserId(), state);
    var linkedAccountWithPassportAndVisas = providerService.createLink(expectedLinkedAccount.getProviderName(), expectedLinkedAccount.getUserId(), authorizationCode, redirectUri, scopes, encodedState);
    assertPresent(linkedAccountWithPassportAndVisas);
    assertEquals(expectedLinkedAccount, linkedAccountWithPassportAndVisas.get().getLinkedAccount().withExpires(passportExpiresTime).withId(Optional.empty()));
    var stablePassport = linkedAccountWithPassportAndVisas.get().getPassport().map(p -> p.withId(Optional.empty()).withLinkedAccountId(Optional.empty()));
    assertEquals(Optional.ofNullable(expectedPassport), stablePassport);
    var stableVisas = linkedAccountWithPassportAndVisas.get().getVisas().stream().map(visa -> visa.withLastValidated(Optional.empty()).withId(Optional.empty()).withPassportId(Optional.empty())).collect(Collectors.toList());
    assertEquals(expectedVisas, stableVisas);
    // state should have been removed from the db
    assertThrows(BadRequestException.class, () -> linkedAccountService.validateAndDeleteOAuth2State(expectedLinkedAccount.getUserId(), state));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) GA4GHVisa(bio.terra.externalcreds.models.GA4GHVisa) LinkedAccount(bio.terra.externalcreds.models.LinkedAccount) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) JOSEException(com.nimbusds.jose.JOSEException) OAuth2AuthorizationException(org.springframework.security.oauth2.core.OAuth2AuthorizationException) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) SecureRandom(java.security.SecureRandom) AfterAll(org.junit.jupiter.api.AfterAll) OAuth2State(bio.terra.externalcreds.models.OAuth2State) BaseTest(bio.terra.externalcreds.BaseTest) BeforeAll(org.junit.jupiter.api.BeforeAll) DefaultOAuth2User(org.springframework.security.oauth2.core.user.DefaultOAuth2User) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) URI(java.net.URI) TokenType(org.springframework.security.oauth2.core.OAuth2AccessToken.TokenType) JwtSigningTestUtils(bio.terra.externalcreds.JwtSigningTestUtils) MockBean(org.springframework.boot.test.mock.mockito.MockBean) TestUtils(bio.terra.externalcreds.TestUtils) Assertions.assertInstanceOf(org.junit.jupiter.api.Assertions.assertInstanceOf) Timestamp(java.sql.Timestamp) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) Collectors(java.util.stream.Collectors) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) ExternalCredsConfig(bio.terra.externalcreds.config.ExternalCredsConfig) Test(org.junit.jupiter.api.Test) List(java.util.List) BadRequestException(bio.terra.common.exception.BadRequestException) OAuth2User(org.springframework.security.oauth2.core.user.OAuth2User) GA4GHPassport(bio.terra.externalcreds.models.GA4GHPassport) Optional(java.util.Optional) OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error) TokenTypeEnum(bio.terra.externalcreds.models.TokenTypeEnum) Collections(java.util.Collections) AuthorizationGrantType(org.springframework.security.oauth2.core.AuthorizationGrantType) SecureRandom(java.security.SecureRandom) OAuth2State(bio.terra.externalcreds.models.OAuth2State)

Aggregations

BadRequestException (bio.terra.common.exception.BadRequestException)1 BaseTest (bio.terra.externalcreds.BaseTest)1 JwtSigningTestUtils (bio.terra.externalcreds.JwtSigningTestUtils)1 TestUtils (bio.terra.externalcreds.TestUtils)1 ExternalCredsConfig (bio.terra.externalcreds.config.ExternalCredsConfig)1 GA4GHPassport (bio.terra.externalcreds.models.GA4GHPassport)1 GA4GHVisa (bio.terra.externalcreds.models.GA4GHVisa)1 LinkedAccount (bio.terra.externalcreds.models.LinkedAccount)1 OAuth2State (bio.terra.externalcreds.models.OAuth2State)1 TokenTypeEnum (bio.terra.externalcreds.models.TokenTypeEnum)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JOSEException (com.nimbusds.jose.JOSEException)1 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 SecureRandom (java.security.SecureRandom)1 Timestamp (java.sql.Timestamp)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1