use of com.gw2auth.oauth2.server.service.user.Gw2AuthUser in project oauth2-server by gw2auth.
the class OAuth2ConsentController method oauth2ConsentInformation.
@GetMapping(value = "/api/oauth2/consent", produces = MediaType.APPLICATION_JSON_VALUE)
public OAuth2ConsentInfoResponse oauth2ConsentInformation(@AuthenticationPrincipal Gw2AuthUser user, @RequestParam(OAuth2ParameterNames.CLIENT_ID) UUID clientId, @RequestParam(OAuth2ParameterNames.STATE) String state, @RequestParam(OAuth2ParameterNames.SCOPE) String scopes) {
final ClientRegistration clientRegistration = this.clientRegistrationService.getClientRegistration(clientId).orElseThrow();
final Set<String> requestedScopes = Utils.split(scopes, " ").collect(Collectors.toSet());
final Set<Gw2ApiPermission> requestedGw2ApiPermissions = requestedScopes.stream().flatMap((scope) -> Gw2ApiPermission.fromOAuth2(scope).stream()).collect(Collectors.toSet());
final boolean requestedVerifiedInformation = requestedScopes.contains(ClientConsentService.GW2AUTH_VERIFIED_SCOPE);
final List<ApiToken> apiTokens = this.apiTokenService.getApiTokens(user.getAccountId());
final List<OAuth2ConsentInfoResponse.MinimalApiToken> apiTokensWithSufficientPermissionResponses = new ArrayList<>();
final List<OAuth2ConsentInfoResponse.MinimalApiToken> apiTokensWithInsufficientPermissionResponses = new ArrayList<>();
final Set<UUID> verifiedGw2AccountIds;
if (apiTokens.isEmpty() || !requestedVerifiedInformation) {
verifiedGw2AccountIds = Set.of();
} else {
verifiedGw2AccountIds = this.verificationService.getVerifiedGw2AccountIds(user.getAccountId());
}
for (ApiToken apiToken : apiTokens) {
final OAuth2ConsentInfoResponse.MinimalApiToken resultApiToken = OAuth2ConsentInfoResponse.MinimalApiToken.create(apiToken, verifiedGw2AccountIds.contains(apiToken.gw2AccountId()));
if (apiToken.gw2ApiPermissions().containsAll(requestedGw2ApiPermissions)) {
apiTokensWithSufficientPermissionResponses.add(resultApiToken);
} else {
apiTokensWithInsufficientPermissionResponses.add(resultApiToken);
}
}
final Set<UUID> previouslyConsentedGw2AccountIds = this.clientAuthorizationService.getLatestClientAuthorization(user.getAccountId(), clientRegistration.id(), requestedScopes).map(ClientAuthorization::gw2AccountIds).orElseGet(Set::of);
final MultiValueMap<String, String> submitFormParameters = new LinkedMultiValueMap<>();
submitFormParameters.set(OAuth2ParameterNames.CLIENT_ID, clientId.toString());
submitFormParameters.set(OAuth2ParameterNames.STATE, state);
requestedScopes.forEach((scope) -> submitFormParameters.add(OAuth2ParameterNames.SCOPE, scope));
final String cancelUri = UriComponentsBuilder.fromPath("/api/oauth2/consent-deny").replaceQueryParam(OAuth2ParameterNames.CLIENT_ID, clientId).replaceQueryParam(OAuth2ParameterNames.STATE, state).toUriString();
return new OAuth2ConsentInfoResponse(ClientRegistrationPublicResponse.create(clientRegistration), requestedGw2ApiPermissions, requestedVerifiedInformation, "/oauth2/authorize", submitFormParameters, cancelUri, apiTokensWithSufficientPermissionResponses, apiTokensWithInsufficientPermissionResponses, previouslyConsentedGw2AccountIds);
}
use of com.gw2auth.oauth2.server.service.user.Gw2AuthUser in project oauth2-server by gw2auth.
the class ApiTokenController method updateApiToken.
@PatchMapping(value = "/api/token/{gw2AccountId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ApiTokenResponse updateApiToken(@AuthenticationPrincipal Gw2AuthUser user, @PathVariable("gw2AccountId") UUID gw2AccountId, @RequestParam(value = "displayName", required = false) String displayName, @RequestParam(value = "gw2ApiToken", required = false) String gw2ApiToken) {
final ApiToken apiToken = this.apiTokenService.updateApiToken(user.getAccountId(), gw2AccountId, gw2ApiToken, displayName);
final List<ClientAuthorization> clientAuthorizations = this.clientAuthorizationService.getClientAuthorizations(user.getAccountId(), Set.of(apiToken.gw2AccountId()));
final List<ApiTokenResponse.Authorization> authorizations;
if (!clientAuthorizations.isEmpty()) {
final Set<Long> clientRegistrationIds = clientAuthorizations.stream().map(ClientAuthorization::clientRegistrationId).collect(Collectors.toSet());
authorizations = this.clientRegistrationService.getClientRegistrations(clientRegistrationIds).stream().map(ApiTokenResponse.Authorization::create).collect(Collectors.toList());
} else {
authorizations = List.of();
}
final boolean isVerified = this.verificationService.getVerifiedAccountId(apiToken.gw2AccountId()).orElse(-1L) == user.getAccountId();
return ApiTokenResponse.create(apiToken, isVerified, authorizations);
}
use of com.gw2auth.oauth2.server.service.user.Gw2AuthUser in project oauth2-server by gw2auth.
the class ApiTokenController method getApiTokens.
@GetMapping(value = "/api/token", produces = MediaType.APPLICATION_JSON_VALUE)
public List<ApiTokenResponse> getApiTokens(@AuthenticationPrincipal Gw2AuthUser user) {
final List<ApiToken> apiTokens = this.apiTokenService.getApiTokens(user.getAccountId());
// get all gw2 account ids for authorization batch lookup
final Set<UUID> gw2AccountIds = apiTokens.stream().map(ApiToken::gw2AccountId).collect(Collectors.toSet());
// aggregate authorizations for later lookup
final List<ClientAuthorization> clientAuthorizations = this.clientAuthorizationService.getClientAuthorizations(user.getAccountId(), gw2AccountIds);
final Set<Long> clientRegistrationIds = new HashSet<>(clientAuthorizations.size());
final Map<UUID, Set<Long>> clientRegistrationIdsByGw2AccountId = new HashMap<>(clientAuthorizations.size());
for (ClientAuthorization clientAuthorization : clientAuthorizations) {
clientRegistrationIds.add(clientAuthorization.clientRegistrationId());
for (UUID gw2AccountId : clientAuthorization.gw2AccountIds()) {
clientRegistrationIdsByGw2AccountId.computeIfAbsent(gw2AccountId, (k) -> new HashSet<>()).add(clientAuthorization.clientRegistrationId());
}
}
// find all client registrations for the registration ids and remember them by id
final Map<Long, ClientRegistration> clientRegistrationById = this.clientRegistrationService.getClientRegistrations(clientRegistrationIds).stream().collect(Collectors.toMap(ClientRegistration::id, Function.identity()));
// find all verified gw2 account ids for this account (better than querying for every single one)
final Set<UUID> verifiedGw2AccountIds = this.verificationService.getVerifiedGw2AccountIds(user.getAccountId());
final List<ApiTokenResponse> response = new ArrayList<>(apiTokens.size());
for (ApiToken apiToken : apiTokens) {
final Set<Long> clientRegistrationIdsForThisToken = clientRegistrationIdsByGw2AccountId.get(apiToken.gw2AccountId());
final List<ApiTokenResponse.Authorization> authorizations;
if (clientRegistrationIdsForThisToken != null && !clientRegistrationIdsForThisToken.isEmpty()) {
authorizations = new ArrayList<>(clientRegistrationIdsForThisToken.size());
for (long clientRegistrationId : clientRegistrationIdsForThisToken) {
final ClientRegistration clientRegistration = clientRegistrationById.get(clientRegistrationId);
if (clientRegistration != null) {
authorizations.add(ApiTokenResponse.Authorization.create(clientRegistration));
}
}
} else {
authorizations = List.of();
}
response.add(ApiTokenResponse.create(apiToken, verifiedGw2AccountIds.contains(apiToken.gw2AccountId()), authorizations));
}
return response;
}
Aggregations