use of com.gw2auth.oauth2.server.service.user.Gw2AuthUser 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.user.Gw2AuthUser in project oauth2-server by gw2auth.
the class ClientConsentController method getClientConsents.
@GetMapping(value = "/api/client/consent", produces = MediaType.APPLICATION_JSON_VALUE)
public List<ClientConsentResponse> getClientConsents(@AuthenticationPrincipal Gw2AuthUser user) {
final List<ClientConsent> clientConsents = this.clientConsentService.getClientConsents(user.getAccountId());
// get all client registration ids for batch lookup
final Set<Long> clientRegistrationIds = clientConsents.stream().map(ClientConsent::clientRegistrationId).collect(Collectors.toSet());
final Map<Long, ClientRegistration> clientRegistrationById = this.clientRegistrationService.getClientRegistrations(clientRegistrationIds).stream().collect(Collectors.toMap(ClientRegistration::id, Function.identity()));
final List<ClientConsentResponse> result = new ArrayList<>(clientConsents.size());
for (ClientConsent clientConsent : clientConsents) {
final ClientRegistration clientRegistration = clientRegistrationById.get(clientConsent.clientRegistrationId());
// only happens if theres a race, but dont want to add locks here
if (clientRegistration != null) {
result.add(ClientConsentResponse.create(clientConsent, clientRegistration));
}
}
return result;
}
use of com.gw2auth.oauth2.server.service.user.Gw2AuthUser 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.user.Gw2AuthUser in project oauth2-server by gw2auth.
the class ApiTokenController method addApiToken.
@PostMapping(value = "/api/token", produces = MediaType.APPLICATION_JSON_VALUE)
public ApiTokenResponse addApiToken(@AuthenticationPrincipal Gw2AuthUser user, @RequestBody String token) {
final ApiToken apiToken = this.apiTokenService.addApiToken(user.getAccountId(), token);
final boolean isVerified = this.verificationService.getVerifiedAccountId(apiToken.gw2AccountId()).orElse(-1L) == user.getAccountId();
return ApiTokenResponse.create(apiToken, isVerified, List.of());
}
use of com.gw2auth.oauth2.server.service.user.Gw2AuthUser in project oauth2-server by gw2auth.
the class OAuth2TokenCustomizerService method customize.
@Override
@Transactional
public void customize(JwtEncodingContext ctx) {
if (ctx.getTokenType().equals(OAuth2TokenType.ACCESS_TOKEN)) {
final OAuth2Authorization authorization = ctx.getAuthorization();
// the client of the application the user wants to access
final RegisteredClient registeredClient = ctx.getRegisteredClient();
final OAuth2AuthenticationToken auth = ctx.getPrincipal();
// the user
final OAuth2User oAuth2User = auth.getPrincipal();
if (authorization != null && oAuth2User instanceof Gw2AuthUser) {
final long accountId = ((Gw2AuthUser) oAuth2User).getAccountId();
final long clientRegistrationId = Long.parseLong(registeredClient.getId());
customize(ctx, authorization.getId(), accountId, clientRegistrationId);
} else {
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR));
}
}
}
Aggregations