Search in sources :

Example 1 with AccountFederationEntity

use of com.gw2auth.oauth2.server.repository.account.AccountFederationEntity in project oauth2-server by gw2auth.

the class AccountServiceImpl method getOrCreateAccountInternal.

@Transactional
protected AccountEntity getOrCreateAccountInternal(String issuer, String idAtIssuer) {
    final Optional<AccountEntity> optionalAccount = this.accountRepository.findByFederation(issuer, idAtIssuer);
    AccountEntity accountEntity;
    if (optionalAccount.isEmpty()) {
        accountEntity = this.accountRepository.save(new AccountEntity(null, Instant.now()));
        AccountFederationEntity accountFederationEntity = new AccountFederationEntity(issuer, idAtIssuer, accountEntity.id());
        accountFederationEntity = this.accountFederationRepository.save(accountFederationEntity);
    } else {
        accountEntity = optionalAccount.get();
    }
    return accountEntity;
}
Also used : AccountFederationEntity(com.gw2auth.oauth2.server.repository.account.AccountFederationEntity) AccountEntity(com.gw2auth.oauth2.server.repository.account.AccountEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with AccountFederationEntity

use of com.gw2auth.oauth2.server.repository.account.AccountFederationEntity in project oauth2-server by gw2auth.

the class AccountServiceImpl method addAccountFederationOrReturnExisting.

@Override
@Transactional
public Account addAccountFederationOrReturnExisting(long accountId, String issuer, String idAtIssuer) {
    final Optional<AccountEntity> optionalAccountEntity = this.accountRepository.findByFederation(issuer, idAtIssuer);
    AccountEntity accountEntity;
    if (optionalAccountEntity.isEmpty()) {
        accountEntity = this.accountRepository.findById(accountId).orElseThrow(IllegalArgumentException::new);
        AccountFederationEntity accountFederationEntity = new AccountFederationEntity(issuer, idAtIssuer, accountId);
        accountFederationEntity = this.accountFederationRepository.save(accountFederationEntity);
    } else {
        accountEntity = optionalAccountEntity.get();
    }
    return Account.fromEntity(accountEntity);
}
Also used : AccountFederationEntity(com.gw2auth.oauth2.server.repository.account.AccountFederationEntity) AccountEntity(com.gw2auth.oauth2.server.repository.account.AccountEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with AccountFederationEntity

use of com.gw2auth.oauth2.server.repository.account.AccountFederationEntity in project oauth2-server by gw2auth.

the class AccountControllerTest method addAccountFederation.

@WithGw2AuthLogin(issuer = "dummyIssuer", idAtIssuer = "A")
public void addAccountFederation(MockHttpSession session) throws Exception {
    final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
    final String loginURL = this.mockMvc.perform(get("/api/account/federation/{provider}", "dummyIssuer").session(session)).andExpect(status().is3xxRedirection()).andReturn().getResponse().getRedirectedUrl();
    this.gw2AuthLoginExtension.login(loginURL, "dummyIssuer", "B").andExpectAll(this.gw2AuthLoginExtension.expectSuccess());
    final List<AccountFederationEntity> result = this.accountFederationRepository.findAllByAccountId(accountId);
    assertEquals(2, result.size());
    assertTrue(result.containsAll(List.of(new AccountFederationEntity("dummyIssuer", "A", accountId), new AccountFederationEntity("dummyIssuer", "B", accountId))));
}
Also used : AccountFederationEntity(com.gw2auth.oauth2.server.repository.account.AccountFederationEntity)

Example 4 with AccountFederationEntity

use of com.gw2auth.oauth2.server.repository.account.AccountFederationEntity in project oauth2-server by gw2auth.

the class AccountControllerTest method addAccountFederationAlreadyLinkedToOtherAccount.

@WithGw2AuthLogin(issuer = "dummyIssuer", idAtIssuer = "A")
public void addAccountFederationAlreadyLinkedToOtherAccount(MockHttpSession session) throws Exception {
    final long otherUserAccountId = this.accountRepository.save(new AccountEntity(null, Instant.now())).id();
    this.accountFederationRepository.save(new AccountFederationEntity("dummyIssuer", "B", otherUserAccountId));
    final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
    final String loginURL = this.mockMvc.perform(get("/api/account/federation/{provider}", "dummyIssuer").session(session)).andExpect(status().is3xxRedirection()).andReturn().getResponse().getRedirectedUrl();
    this.gw2AuthLoginExtension.login(loginURL, "dummyIssuer", "B").andExpect(status().is3xxRedirection()).andExpect(header().string("Location", new StringEndsWith("?error")));
    // only the initial federation should be present
    final List<AccountFederationEntity> result = this.accountFederationRepository.findAllByAccountId(accountId);
    assertEquals(1, result.size());
}
Also used : StringEndsWith(org.hamcrest.core.StringEndsWith) AccountFederationEntity(com.gw2auth.oauth2.server.repository.account.AccountFederationEntity) AccountEntity(com.gw2auth.oauth2.server.repository.account.AccountEntity)

Example 5 with AccountFederationEntity

use of com.gw2auth.oauth2.server.repository.account.AccountFederationEntity in project oauth2-server by gw2auth.

the class AccountControllerTest method getAccountSummary.

@WithGw2AuthLogin
public void getAccountSummary(MockHttpSession session) throws Exception {
    final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
    final int apiTokens = 3;
    final int verifiedGw2Accounts = 5;
    final int clientRegistrations = 12;
    // this must be less than clientRegistrations! (only to keep the testcase simple)
    final int clientAuthorizations = 10;
    final int accountFederations = 2;
    for (int i = 0; i < apiTokens; i++) {
        this.testHelper.createApiToken(accountId, UUID.randomUUID(), "", Set.of(), "Name");
    }
    for (int i = 0; i < verifiedGw2Accounts; i++) {
        this.gw2AccountVerificationRepository.save(new Gw2AccountVerificationEntity(UUID.randomUUID(), accountId));
    }
    final Queue<ClientRegistrationEntity> clientRegistrationEntities = new LinkedList<>();
    for (int i = 0; i < clientRegistrations; i++) {
        clientRegistrationEntities.add(this.clientRegistrationRepository.save(new ClientRegistrationEntity(null, accountId, Instant.now(), "Name", UUID.randomUUID(), "", Set.of(), Set.of("http://127.0.0.1/"))));
    }
    for (int i = 0; i < clientAuthorizations; i++) {
        this.clientConsentRepository.save(new ClientConsentEntity(accountId, clientRegistrationEntities.poll().id(), UUID.randomUUID(), Set.of("dummy")));
    }
    // add one client authorization without scopes (that should not be counted)
    this.clientConsentRepository.save(new ClientConsentEntity(accountId, clientRegistrationEntities.poll().id(), UUID.randomUUID(), Set.of()));
    for (int i = 0; i < accountFederations; i++) {
        this.accountFederationRepository.save(new AccountFederationEntity(UUID.randomUUID().toString(), UUID.randomUUID().toString(), accountId));
    }
    this.mockMvc.perform(get("/api/account/summary").session(session)).andExpect(status().isOk()).andExpect(jsonPath("$.apiTokens").value(Integer.toString(apiTokens))).andExpect(jsonPath("$.verifiedGw2Accounts").value(Integer.toString(verifiedGw2Accounts))).andExpect(jsonPath("$.clientRegistrations").value(Integer.toString(clientRegistrations))).andExpect(jsonPath("$.clientAuthorizations").value(Integer.toString(clientAuthorizations))).andExpect(// one more because WithGw2AuthLogin adds one
    jsonPath("$.accountFederations").value(Integer.toString(accountFederations + 1)));
}
Also used : ClientRegistrationEntity(com.gw2auth.oauth2.server.repository.client.registration.ClientRegistrationEntity) AccountFederationEntity(com.gw2auth.oauth2.server.repository.account.AccountFederationEntity) Gw2AccountVerificationEntity(com.gw2auth.oauth2.server.repository.verification.Gw2AccountVerificationEntity) ClientConsentEntity(com.gw2auth.oauth2.server.repository.client.consent.ClientConsentEntity)

Aggregations

AccountFederationEntity (com.gw2auth.oauth2.server.repository.account.AccountFederationEntity)6 AccountEntity (com.gw2auth.oauth2.server.repository.account.AccountEntity)3 Transactional (org.springframework.transaction.annotation.Transactional)2 ClientConsentEntity (com.gw2auth.oauth2.server.repository.client.consent.ClientConsentEntity)1 ClientRegistrationEntity (com.gw2auth.oauth2.server.repository.client.registration.ClientRegistrationEntity)1 Gw2AccountVerificationEntity (com.gw2auth.oauth2.server.repository.verification.Gw2AccountVerificationEntity)1 StringEndsWith (org.hamcrest.core.StringEndsWith)1