
org.apache.isis.security.keycloak.services.KeycloakOauth2UserService Maven / Gradle / Ivy
Show all versions of isis-security-keycloak Show documentation
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.isis.security.keycloak.services;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest;
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserService;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.OAuth2Error;
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.JwtException;
import org.springframework.util.CollectionUtils;
import org.apache.isis.core.config.IsisConfiguration;
import lombok.RequiredArgsConstructor;
import lombok.val;
@RequiredArgsConstructor
public class KeycloakOauth2UserService extends OidcUserService {
private final static OAuth2Error INVALID_REQUEST = new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST);
final JwtDecoder jwtDecoder;
final GrantedAuthoritiesMapper authoritiesMapper;
final IsisConfiguration isisConfiguration;
/**
* Augments {@link OidcUserService#loadUser(OidcUserRequest)} to add authorities
* provided by Keycloak.
*
* Needed because {@link OidcUserService#loadUser(OidcUserRequest)} (currently)
* does not provide a hook for adding custom authorities from a
* {@link OidcUserRequest}.
*/
@Override
public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException {
OidcUser user = super.loadUser(userRequest);
Set authorities = new LinkedHashSet<>();
authorities.addAll(user.getAuthorities());
authorities.addAll(extractKeycloakAuthorities(userRequest));
return new DefaultOidcUser(authorities, userRequest.getIdToken(), user.getUserInfo(), "preferred_username");
}
/**
* Extracts {@link GrantedAuthority GrantedAuthorities} from the AccessToken in
* the {@link OidcUserRequest}.
*
* @param userRequest
* @return
*/
private Collection extends GrantedAuthority> extractKeycloakAuthorities(OidcUserRequest userRequest) {
Jwt token = parseJwt(userRequest.getAccessToken().getTokenValue());
List combinedRoles = new ArrayList<>();
if(isisConfiguration.getSecurity().getKeycloak().isExtractClientRoles()) {
// attempt to parse out 'resource_access.${client_id}.roles'
val resourceObj = token.getClaims().get("resource_access");
if (resourceObj instanceof Map) {
@SuppressWarnings("rawtypes")
val resourceMap = (Map) resourceObj;
val clientId = userRequest.getClientRegistration().getClientId();
val clientResourceObj = resourceMap.get(clientId);
if(clientResourceObj instanceof Map) {
@SuppressWarnings("rawtypes")
val clientResource = (Map) clientResourceObj;
if (!CollectionUtils.isEmpty(clientResource)) {
val clientRolesObj = clientResource.get("roles");
if (clientResourceObj instanceof List) {
@SuppressWarnings("unchecked")
val clientRoles = (List