use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.
the class Gw2AuthLoginExtension method loginInternal.
private ResultActions loginInternal(ExtensionContext context, String loginURL, String issuer, String idAtIssuer) throws Exception {
final MockHttpSession session = context.getStore(NAMESPACE).getOrComputeIfAbsent("session", (k) -> new MockHttpSession(), MockHttpSession.class);
this.testClientRegistrationRepository.prepareRegistrationId(issuer);
final MvcResult result = this.mockMvc.perform(get(loginURL).session(session)).andReturn();
final String location = Objects.requireNonNull(result.getResponse().getHeader("Location"));
final String state = Utils.parseQuery(new URL(location).getQuery()).filter(QueryParam::hasValue).filter((queryParam) -> queryParam.name().equals(OAuth2ParameterNames.STATE)).map(QueryParam::value).findFirst().orElseThrow();
return this.mockMvc.perform(get("/login/oauth2/code/{issuer}", issuer).session(session).queryParam("code", idAtIssuer).queryParam("state", state));
}
use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.
the class VerificationControllerTest method startAndSubmitApiTokenNameChallengeLaterFulfilled.
@WithGw2AuthLogin
public void startAndSubmitApiTokenNameChallengeLaterFulfilled(MockHttpSession session) throws Exception {
final UUID gw2AccountId = UUID.randomUUID();
// insert an api token for another account but for the same gw2 account id
final long otherUserAccountId = this.accountRepository.save(new AccountEntity(null, Instant.now())).id();
this.testHelper.createApiToken(otherUserAccountId, gw2AccountId, Set.of(), "Name");
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 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
final VerificationChallengeStart challengeStart = 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 15 minutes pass
testingClock = Clock.offset(testingClock, Duration.ofMinutes(15L));
this.verificationService.setClock(testingClock);
// prepare the api again and now set the name to the requested one
this.gw2RestServer.reset();
prepareGw2RestServerForTokenInfoRequest(gw2ApiSubtoken, challengeStart.message().get("apiTokenName").toString(), Set.of(Gw2ApiPermission.ACCOUNT));
// simulate scheduled check
this.verificationService.tryVerifyAllPending();
// pending challenge should be removed
assertTrue(this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, gw2AccountId.toString()).isEmpty());
// account should now be verified
final Gw2AccountVerificationEntity accountVerification = this.gw2AccountVerificationRepository.findById(gw2AccountId).orElse(null);
assertNotNull(accountVerification);
assertEquals(accountId, accountVerification.accountId());
// the other users api token should be removed
assertTrue(this.apiTokenRepository.findByAccountIdAndGw2AccountId(otherUserAccountId, gw2AccountId).isEmpty());
}
use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.
the class VerificationControllerTest method startAndSubmitApiTokenNameChallengeDirectlyFulfilled.
@WithGw2AuthLogin
public void startAndSubmitApiTokenNameChallengeDirectlyFulfilled(MockHttpSession session) throws Exception {
final UUID gw2AccountId = UUID.randomUUID();
// insert an api token for another account but for the same gw2 account id
final long otherUserAccountId = this.accountRepository.save(new AccountEntity(null, Instant.now())).id();
this.testHelper.createApiToken(otherUserAccountId, gw2AccountId, Set.of(), "Name");
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 String gw2ApiToken = TestHelper.randomRootToken();
final String gw2ApiSubtoken = TestHelper.createSubtokenJWT(UUID.randomUUID(), Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant(), Duration.ofMinutes(90L));
// start the challenge
final VerificationChallengeStart challengeStart = this.verificationService.startChallenge(accountId, 1L);
// 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, challengeStart.message().get("apiTokenName").toString(), Set.of(Gw2ApiPermission.ACCOUNT));
// submit the challenge
this.mockMvc.perform(post("/api/verification/pending").session(session).with(csrf()).queryParam("token", gw2ApiToken)).andExpect(status().isOk()).andExpect(jsonPath("$.isSuccess").value("true"));
// started challenge should be removed
assertTrue(this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").isEmpty());
// pending challenge should not be present (either removed or never inserted)
assertTrue(this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, gw2AccountId.toString()).isEmpty());
// account should now be verified
final Gw2AccountVerificationEntity accountVerification = this.gw2AccountVerificationRepository.findById(gw2AccountId).orElse(null);
assertNotNull(accountVerification);
assertEquals(accountId, accountVerification.accountId());
// the other users api token should be removed
assertTrue(this.apiTokenRepository.findByAccountIdAndGw2AccountId(otherUserAccountId, gw2AccountId).isEmpty());
}
use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.
the class VerificationControllerTest method startAndSubmitChallengeForGw2AccountAlreadyVerified.
@WithGw2AuthLogin
public void startAndSubmitChallengeForGw2AccountAlreadyVerified(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 = UUID.randomUUID().toString();
final String gw2ApiSubtoken = TestHelper.createSubtokenJWT(UUID.randomUUID(), Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant(), Duration.ofMinutes(90L));
// insert the verification
this.gw2AccountVerificationRepository.save(new Gw2AccountVerificationEntity(gw2AccountId, accountId));
// 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().isBadRequest());
}
use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.
the class AccountControllerTest method deleteAccountFederation.
@WithGw2AuthLogin(issuer = "issuer", idAtIssuer = "idAtIssuer")
public void deleteAccountFederation(MockHttpSession session) throws Exception {
final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
this.accountFederationRepository.save(new AccountFederationEntity("issuer2", "idAtIssuer2", accountId));
this.mockMvc.perform(delete("/api/account/federation").session(session).queryParam("issuer", "issuer2").queryParam("idAtIssuer", "idAtIssuer2").with(csrf())).andExpect(status().isOk());
final List<AccountFederationEntity> result = this.accountFederationRepository.findAllByAccountId(accountId);
assertEquals(1, result.size());
assertEquals(new AccountFederationEntity("issuer", "idAtIssuer", accountId), result.get(0));
}
Aggregations