use of com.gw2auth.oauth2.server.service.account.Account 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.service.account.Account in project oauth2-server by gw2auth.
the class ClientAuthorizationController method getClientAuthorizations.
@GetMapping(value = "/api/client/authorization/{clientId}", produces = MediaType.APPLICATION_JSON_VALUE)
public List<ClientAuthorizationResponse> getClientAuthorizations(@AuthenticationPrincipal Gw2AuthUser user, @PathVariable("clientId") UUID clientId) {
final List<ClientAuthorization> clientAuthorizations = this.clientAuthorizationService.getClientAuthorizations(user.getAccountId(), clientId);
// get all gw2-account ids for batch lookup
final Set<UUID> gw2AccountIds = clientAuthorizations.stream().flatMap((v) -> v.gw2AccountIds().stream()).collect(Collectors.toSet());
final Map<UUID, ApiToken> apiTokenByGw2AccountId = this.apiTokenService.getApiTokens(user.getAccountId(), gw2AccountIds).stream().collect(Collectors.toMap(ApiToken::gw2AccountId, Function.identity()));
final List<ClientAuthorizationResponse> result = new ArrayList<>(clientAuthorizations.size());
for (ClientAuthorization clientAuthorization : clientAuthorizations) {
final List<ClientAuthorizationResponse.Token> tokens = new ArrayList<>(clientAuthorization.gw2AccountIds().size());
for (UUID gw2AccountId : clientAuthorization.gw2AccountIds()) {
final ApiToken apiToken = apiTokenByGw2AccountId.get(gw2AccountId);
if (apiToken != null) {
tokens.add(new ClientAuthorizationResponse.Token(gw2AccountId, apiToken.displayName()));
}
}
result.add(ClientAuthorizationResponse.create(clientAuthorization, tokens));
}
return result;
}
use of com.gw2auth.oauth2.server.service.account.Account in project oauth2-server by gw2auth.
the class AbstractUserService method loadUser.
protected Gw2AuthUser loadUser(OAuth2UserRequest userRequest, OAuth2User user) throws OAuth2AuthenticationException {
final HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession(false);
final String issuer = userRequest.getClientRegistration().getRegistrationId();
final String idAtIssuer = user.getName();
final Gw2AuthUser currentlyLoggedInUser = AuthenticationHelper.getUser().orElse(null);
boolean addFederation = false;
// check if the user tried to add this federation
if (session != null) {
final Object addAuthProviderValue = session.getAttribute(ADD_FEDERATION_SESSION_KEY);
session.removeAttribute(ADD_FEDERATION_SESSION_KEY);
if (issuer.equals(addAuthProviderValue)) {
addFederation = true;
}
}
Account account = null;
if (addFederation) {
// if this federation should be added, only allow if the user is currently logged in
if (currentlyLoggedInUser != null) {
final Account resultAccount = this.accountService.addAccountFederationOrReturnExisting(currentlyLoggedInUser.getAccountId(), issuer, idAtIssuer);
// only allow if this federation was not yet linked to another account
if (resultAccount.id() == currentlyLoggedInUser.getAccountId()) {
account = resultAccount;
}
}
} else {
// if no federation should be added (normal login), only allow if the user is not currently logged in
if (currentlyLoggedInUser == null) {
account = this.accountService.getOrCreateAccount(issuer, idAtIssuer);
}
}
if (account == null) {
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.ACCESS_DENIED));
}
return new Gw2AuthUser(user, account.id(), new Pair<>(issuer, idAtIssuer));
}
use of com.gw2auth.oauth2.server.service.account.Account 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.service.account.Account in project oauth2-server by gw2auth.
the class ApplicationControllerTest method getApplicationSummary.
@Test
public void getApplicationSummary() throws Exception {
final long accountId = this.accountRepository.save(new AccountEntity(null, Instant.now())).id();
final int accounts = 102;
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;
for (int i = 0; i < accounts; i++) {
this.accountRepository.save(new AccountEntity(null, Instant.now()));
}
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()));
this.mockMvc.perform(get("/api/application/summary")).andExpect(status().isOk()).andExpect(// we create one dummy account who owns everything else
jsonPath("$.accounts").value(Integer.toString(accounts + 1))).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)));
}
Aggregations