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;
}
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);
}
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))));
}
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());
}
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)));
}
Aggregations