Search in sources :

Example 1 with Account

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);
}
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 Account

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;
}
Also used : Gw2AuthUser(com.gw2auth.oauth2.server.service.user.Gw2AuthUser) PathVariable(org.springframework.web.bind.annotation.PathVariable) AbstractRestController(com.gw2auth.oauth2.server.web.AbstractRestController) java.util(java.util) ApiTokenService(com.gw2auth.oauth2.server.service.apitoken.ApiTokenService) ClientAuthorizationService(com.gw2auth.oauth2.server.service.client.authorization.ClientAuthorizationService) MediaType(org.springframework.http.MediaType) Autowired(org.springframework.beans.factory.annotation.Autowired) RestController(org.springframework.web.bind.annotation.RestController) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) HttpStatus(org.springframework.http.HttpStatus) ApiToken(com.gw2auth.oauth2.server.service.apitoken.ApiToken) AuthenticationPrincipal(org.springframework.security.core.annotation.AuthenticationPrincipal) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseEntity(org.springframework.http.ResponseEntity) ClientAuthorization(com.gw2auth.oauth2.server.service.client.authorization.ClientAuthorization) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ClientAuthorization(com.gw2auth.oauth2.server.service.client.authorization.ClientAuthorization) ApiToken(com.gw2auth.oauth2.server.service.apitoken.ApiToken) ApiToken(com.gw2auth.oauth2.server.service.apitoken.ApiToken) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with Account

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));
}
Also used : Account(com.gw2auth.oauth2.server.service.account.Account) HttpSession(javax.servlet.http.HttpSession) OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error) OAuth2AuthenticationException(org.springframework.security.oauth2.core.OAuth2AuthenticationException)

Example 4 with Account

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))));
}
Also used : AccountFederationEntity(com.gw2auth.oauth2.server.repository.account.AccountFederationEntity)

Example 5 with Account

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)));
}
Also used : ClientRegistrationEntity(com.gw2auth.oauth2.server.repository.client.registration.ClientRegistrationEntity) Gw2AccountVerificationEntity(com.gw2auth.oauth2.server.repository.verification.Gw2AccountVerificationEntity) AccountEntity(com.gw2auth.oauth2.server.repository.account.AccountEntity) LinkedList(java.util.LinkedList) ClientConsentEntity(com.gw2auth.oauth2.server.repository.client.consent.ClientConsentEntity) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

AccountEntity (com.gw2auth.oauth2.server.repository.account.AccountEntity)7 Gw2AccountVerificationEntity (com.gw2auth.oauth2.server.repository.verification.Gw2AccountVerificationEntity)7 AccountFederationEntity (com.gw2auth.oauth2.server.repository.account.AccountFederationEntity)5 Clock (java.time.Clock)5 UUID (java.util.UUID)5 ClientConsentEntity (com.gw2auth.oauth2.server.repository.client.consent.ClientConsentEntity)4 VerificationChallengeStart (com.gw2auth.oauth2.server.service.verification.VerificationChallengeStart)3 Transactional (org.springframework.transaction.annotation.Transactional)3 ClientRegistrationEntity (com.gw2auth.oauth2.server.repository.client.registration.ClientRegistrationEntity)2 Gw2AccountVerificationChallengeEntity (com.gw2auth.oauth2.server.repository.verification.Gw2AccountVerificationChallengeEntity)2 Account (com.gw2auth.oauth2.server.service.account.Account)2 ApiToken (com.gw2auth.oauth2.server.service.apitoken.ApiToken)2 ApiTokenService (com.gw2auth.oauth2.server.service.apitoken.ApiTokenService)2 ClientAuthorization (com.gw2auth.oauth2.server.service.client.authorization.ClientAuthorization)2 ClientAuthorizationService (com.gw2auth.oauth2.server.service.client.authorization.ClientAuthorizationService)2 ClientRegistration (com.gw2auth.oauth2.server.service.client.registration.ClientRegistration)2 ClientRegistrationCreation (com.gw2auth.oauth2.server.service.client.registration.ClientRegistrationCreation)2 Gw2AuthUser (com.gw2auth.oauth2.server.service.user.Gw2AuthUser)2 AbstractRestController (com.gw2auth.oauth2.server.web.AbstractRestController)2 java.util (java.util)2