Search in sources :

Example 11 with QueryParam

use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.

the class ClientConsentControllerTest method getClientConsentLogPage.

@WithGw2AuthLogin
public void getClientConsentLogPage(MockHttpSession session) throws Exception {
    final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
    final ClientRegistrationEntity clientRegistration = this.testHelper.createClientRegistration(accountId, "Name");
    final ClientConsentEntity clientAuthorization = this.testHelper.createClientConsent(accountId, clientRegistration.id(), Set.of(Gw2ApiPermission.ACCOUNT.oauth2()));
    final Queue<ClientConsentLogEntity> insertedLogs = new PriorityQueue<>(Comparator.comparing(ClientConsentLogEntity::timestamp).reversed());
    for (int i = 0; i < 143; i++) {
        final int generateMessageCount = ThreadLocalRandom.current().nextInt(20);
        final List<String> messages = new ArrayList<>(generateMessageCount);
        for (int j = 0; j < generateMessageCount; j++) {
            messages.add(UUID.randomUUID().toString());
        }
        insertedLogs.offer(this.testHelper.createClientLog(accountId, clientAuthorization.clientRegistrationId(), UUID.randomUUID().toString(), messages));
    }
    final ObjectMapper mapper = new ObjectMapper();
    int page = 0;
    do {
        final String responseJson = this.mockMvc.perform(get("/api/client/consent/{clientId}/logs", clientRegistration.clientId()).session(session).queryParam("page", Integer.toString(page))).andExpect(status().isOk()).andExpect(jsonPath("$.page").exists()).andExpect(jsonPath("$.nextPage").exists()).andExpect(jsonPath("$.logs").exists()).andReturn().getResponse().getContentAsString();
        final JsonNode responseNode = mapper.readTree(responseJson);
        final int nextPage = responseNode.get("nextPage").intValue();
        assertEquals(page, responseNode.get("page").intValue());
        assertTrue(nextPage == page + 1 || nextPage == -1);
        final JsonNode logsNode = responseNode.get("logs");
        assertTrue(logsNode.isArray());
        for (int i = 0; i < logsNode.size(); i++) {
            final ClientConsentLogEntity expectedLog = insertedLogs.poll();
            assertNotNull(expectedLog);
            final JsonNode logNode = logsNode.get(i);
            assertInstantEquals(expectedLog.timestamp(), logNode.get("timestamp").textValue());
            assertEquals(expectedLog.type(), logNode.get("type").textValue());
            final JsonNode messagesNode = logNode.get("messages");
            assertTrue(messagesNode.isArray());
            for (int j = 0; j < messagesNode.size(); j++) {
                assertEquals(expectedLog.messages().get(j), messagesNode.get(j).textValue());
            }
        }
        page = nextPage;
    } while (page != -1);
    assertTrue(insertedLogs.isEmpty());
}
Also used : ClientConsentLogEntity(com.gw2auth.oauth2.server.repository.client.consent.ClientConsentLogEntity) ClientRegistrationEntity(com.gw2auth.oauth2.server.repository.client.registration.ClientRegistrationEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ClientConsentEntity(com.gw2auth.oauth2.server.repository.client.consent.ClientConsentEntity)

Example 12 with QueryParam

use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.

the class VerificationControllerTest method startChallengeWithLongEnoughBetween.

@WithGw2AuthLogin
public void startChallengeWithLongEnoughBetween(MockHttpSession session) throws Exception {
    final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
    // prepare the testing clock
    Clock testingClock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
    this.verificationService.setClock(testingClock);
    this.mockMvc.perform(post("/api/verification").session(session).with(csrf()).queryParam("challengeId", "1")).andExpect(status().isOk()).andExpect(jsonPath("$.challengeId").value("1")).andExpect(jsonPath("$.message.apiTokenName").isString()).andExpect(jsonPath("$.nextAllowedStartTime").isString());
    final Gw2AccountVerificationChallengeEntity startedChallenge = this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").orElse(null);
    assertNotNull(startedChallenge);
    // wait 29min (not enough)
    testingClock = Clock.offset(testingClock, Duration.ofMinutes(31L));
    this.verificationService.setClock(testingClock);
    // try to start a new challenge
    this.mockMvc.perform(post("/api/verification").session(session).with(csrf()).queryParam("challengeId", "2")).andExpect(status().isOk()).andExpect(jsonPath("$.challengeId").value("2")).andExpect(jsonPath("$.message.gw2ItemId").isNumber()).andExpect(jsonPath("$.message.buyOrderCoins").isNumber()).andExpect(jsonPath("$.nextAllowedStartTime").isString());
    // started challenge should be modified
    final Gw2AccountVerificationChallengeEntity updatedStartedChallenge = this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").orElse(null);
    assertNotNull(updatedStartedChallenge);
    assertNotEquals(startedChallenge, updatedStartedChallenge);
}
Also used : Clock(java.time.Clock) Gw2AccountVerificationChallengeEntity(com.gw2auth.oauth2.server.repository.verification.Gw2AccountVerificationChallengeEntity)

Example 13 with QueryParam

use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.

the class VerificationControllerTest method startAndSubmitChallengeForGw2AccountHavingAPendingVerification.

@WithGw2AuthLogin
public void startAndSubmitChallengeForGw2AccountHavingAPendingVerification(MockHttpSession session) throws Exception {
    final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
    // prepare the testing clock
    Clock testingClock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
    this.verificationService.setClock(testingClock);
    final UUID gw2AccountId = UUID.randomUUID();
    final String gw2ApiToken = TestHelper.randomRootToken();
    final String gw2ApiSubtoken = TestHelper.createSubtokenJWT(UUID.randomUUID(), Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant(), Duration.ofMinutes(90L));
    // prepare the gw2 api
    this.gw2RestServer.reset();
    preparedGw2RestServerForCreateSubtoken(gw2ApiToken, gw2ApiSubtoken, Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant().plus(Duration.ofMinutes(90L)));
    preparedGw2RestServerForAccountRequest(gw2AccountId, gw2ApiSubtoken);
    prepareGw2RestServerForTokenInfoRequest(gw2ApiSubtoken, "Not the name that was requested", Set.of(Gw2ApiPermission.ACCOUNT));
    // start the challenge
    this.verificationService.startChallenge(accountId, 1L);
    // submit the challenge
    this.mockMvc.perform(post("/api/verification/pending").session(session).with(csrf()).queryParam("token", gw2ApiToken)).andExpect(status().isOk()).andExpect(jsonPath("$.isSuccess").value("false")).andExpect(jsonPath("$.pending").isMap());
    // started challenge should be removed
    assertTrue(this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").isEmpty());
    // pending challenge should be inserted
    final Gw2AccountVerificationChallengeEntity startedChallenge = this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, gw2AccountId.toString()).orElse(null);
    assertNotNull(startedChallenge);
    // start a new challenge
    this.verificationService.startChallenge(accountId, 1L);
    // prepare the gw2 api again
    this.gw2RestServer.reset();
    preparedGw2RestServerForCreateSubtoken(gw2ApiToken, gw2ApiSubtoken, Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant().plus(Duration.ofMinutes(90L)));
    preparedGw2RestServerForAccountRequest(gw2AccountId, gw2ApiSubtoken);
    prepareGw2RestServerForTokenInfoRequest(gw2ApiSubtoken, "Not the name that was requested", Set.of(Gw2ApiPermission.ACCOUNT));
    // submit the challenge again (for the same gw2 account)
    this.mockMvc.perform(post("/api/verification/pending").session(session).with(csrf()).queryParam("token", gw2ApiToken)).andExpect(status().isBadRequest());
    // pending challenge should not be modified
    assertEquals(startedChallenge, this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, gw2AccountId.toString()).orElse(null));
}
Also used : Clock(java.time.Clock) UUID(java.util.UUID) Gw2AccountVerificationChallengeEntity(com.gw2auth.oauth2.server.repository.verification.Gw2AccountVerificationChallengeEntity)

Example 14 with QueryParam

use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.

the class VerificationControllerTest method startAndSubmitApiTokenNameChallengeUnfulfilled.

@WithGw2AuthLogin
public void startAndSubmitApiTokenNameChallengeUnfulfilled(MockHttpSession session) throws Exception {
    final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
    // prepare the testing clock
    Clock testingClock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
    this.verificationService.setClock(testingClock);
    final UUID gw2AccountId = UUID.randomUUID();
    final String gw2ApiToken = TestHelper.randomRootToken();
    final String gw2ApiSubtoken = TestHelper.createSubtokenJWT(UUID.randomUUID(), Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant(), Duration.ofMinutes(90L));
    // prepare the gw2 api
    this.gw2RestServer.reset();
    preparedGw2RestServerForCreateSubtoken(gw2ApiToken, gw2ApiSubtoken, Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant().plus(Duration.ofMinutes(90L)));
    preparedGw2RestServerForAccountRequest(gw2AccountId, gw2ApiSubtoken);
    prepareGw2RestServerForTokenInfoRequest(gw2ApiSubtoken, "Not the name that was requested", Set.of(Gw2ApiPermission.ACCOUNT));
    // start the challenge
    this.verificationService.startChallenge(accountId, 1L);
    // submit the challenge
    this.mockMvc.perform(post("/api/verification/pending").session(session).with(csrf()).queryParam("token", gw2ApiToken)).andExpect(status().isOk()).andExpect(jsonPath("$.isSuccess").value("false")).andExpect(jsonPath("$.pending").isMap());
    // started challenge should be removed
    assertTrue(this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").isEmpty());
    // pending challenge should be inserted
    assertTrue(this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, gw2AccountId.toString()).isPresent());
    // let 91 minutes pass
    testingClock = Clock.offset(testingClock, Duration.ofMinutes(91L));
    this.verificationService.setClock(testingClock);
    // prepare the api again
    this.gw2RestServer.reset();
    prepareGw2RestServerForTokenInfoRequest(gw2ApiSubtoken, "Not the name that was requested", Set.of(Gw2ApiPermission.ACCOUNT));
    // simulate scheduled check
    this.verificationService.tryVerifyAllPending();
    // pending challenge should be updated to verification failed entity
    final Gw2AccountVerificationChallengeEntity verificationFailedEntity = this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, gw2AccountId.toString()).orElse(null);
    assertNotNull(verificationFailedEntity);
    assertEquals(-1L, verificationFailedEntity.challengeId());
    assertInstantEquals(testingClock.instant().plus(Duration.ofHours(2L)), verificationFailedEntity.timeoutAt());
}
Also used : Clock(java.time.Clock) UUID(java.util.UUID) Gw2AccountVerificationChallengeEntity(com.gw2auth.oauth2.server.repository.verification.Gw2AccountVerificationChallengeEntity)

Example 15 with QueryParam

use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.

the class VerificationControllerTest method startChallengeWithSameChallengeIdAsExisting.

@WithGw2AuthLogin
public void startChallengeWithSameChallengeIdAsExisting(MockHttpSession session) throws Exception {
    final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
    // prepare the testing clock
    Clock testingClock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
    this.verificationService.setClock(testingClock);
    this.mockMvc.perform(post("/api/verification").session(session).with(csrf()).queryParam("challengeId", "1")).andExpect(status().isOk()).andExpect(jsonPath("$.challengeId").value("1")).andExpect(jsonPath("$.message.apiTokenName").isString()).andExpect(jsonPath("$.nextAllowedStartTime").isString());
    final Gw2AccountVerificationChallengeEntity startedChallenge = this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").orElse(null);
    assertNotNull(startedChallenge);
    // wait 31min (enough)
    testingClock = Clock.offset(testingClock, Duration.ofMinutes(31L));
    this.verificationService.setClock(testingClock);
    // try to start a new challenge
    this.mockMvc.perform(post("/api/verification").session(session).with(csrf()).queryParam("challengeId", "1")).andExpect(status().isBadRequest());
    // started challenge should not be modified
    assertEquals(startedChallenge, this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").orElse(null));
}
Also used : Clock(java.time.Clock) Gw2AccountVerificationChallengeEntity(com.gw2auth.oauth2.server.repository.verification.Gw2AccountVerificationChallengeEntity)

Aggregations

Clock (java.time.Clock)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 MvcResult (org.springframework.test.web.servlet.MvcResult)7 Gw2AccountVerificationEntity (com.gw2auth.oauth2.server.repository.verification.Gw2AccountVerificationEntity)6 ClientRegistration (com.gw2auth.oauth2.server.service.client.registration.ClientRegistration)6 ClientRegistrationCreation (com.gw2auth.oauth2.server.service.client.registration.ClientRegistrationCreation)6 UUID (java.util.UUID)6 ClientAuthorizationEntity (com.gw2auth.oauth2.server.repository.client.authorization.ClientAuthorizationEntity)5 Gw2AccountVerificationChallengeEntity (com.gw2auth.oauth2.server.repository.verification.Gw2AccountVerificationChallengeEntity)5 JSONObject (org.json.JSONObject)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 AccountEntity (com.gw2auth.oauth2.server.repository.account.AccountEntity)4 ClientConsentEntity (com.gw2auth.oauth2.server.repository.client.consent.ClientConsentEntity)4 VerificationChallengeStart (com.gw2auth.oauth2.server.service.verification.VerificationChallengeStart)3 ApiTokenEntity (com.gw2auth.oauth2.server.repository.apitoken.ApiTokenEntity)2 ClientRegistrationEntity (com.gw2auth.oauth2.server.repository.client.registration.ClientRegistrationEntity)2 QueryParam (com.gw2auth.oauth2.server.util.QueryParam)2 Utils (com.gw2auth.oauth2.server.util.Utils)2 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)2 com.gw2auth.oauth2.server (com.gw2auth.oauth2.server)1