adamsaghy commented on code in PR #5883: URL: https://github.com/apache/fineract/pull/5883#discussion_r3298575386
########## fineract-security/src/main/java/org/apache/fineract/infrastructure/security/filter/OidcTenantAwareFilter.java: ########## @@ -0,0 +1,113 @@ +/** + * 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.fineract.infrastructure.security.filter; + +import com.nimbusds.jwt.JWTParser; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.fineract.infrastructure.core.config.FineractProperties; +import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil; +import org.apache.fineract.infrastructure.security.service.AuthTenantDetailsService; +import org.springframework.lang.NonNull; +import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver; +import org.springframework.web.filter.OncePerRequestFilter; + +/** + * Resolves the Fineract tenant from an incoming request and sets it in {@link ThreadLocalContextUtil} + * before Spring Security validates the JWT signature. + * + * <p>Resolution priority: + * <ol> + * <li>A configurable claim inside the Bearer JWT (parsed without signature verification)</li> + * <li>The {@code Fineract-Platform-TenantId} HTTP header</li> + * <li>The {@code tenantIdentifier} query parameter</li> + * </ol> + * + * <p>If no tenant can be resolved the filter does not block the request — downstream + * authentication will fail with an appropriate error if the tenant context is required. + */ +@Slf4j +@RequiredArgsConstructor +public class OidcTenantAwareFilter extends OncePerRequestFilter { + + private static final String TENANT_HEADER = "Fineract-Platform-TenantId"; + private static final String TENANT_PARAM = "tenantIdentifier"; + + private final BearerTokenResolver bearerTokenResolver; + private final AuthTenantDetailsService tenantDetailsService; + private final FineractProperties fineractProperties; + + @Override + protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, + @NonNull FilterChain filterChain) throws ServletException, IOException { + try { + String tenantId = resolveTenantId(request); + if (tenantId != null) { + ThreadLocalContextUtil.setTenant(tenantDetailsService.loadTenantById(tenantId, false)); + log.debug("OIDC tenant context set to '{}' for {}", tenantId, request.getRequestURI()); + } + filterChain.doFilter(request, response); + } catch (Exception e) { + // don't block; real auth will fail later if token or tenant is invalid + filterChain.doFilter(request, response); + } finally { + ThreadLocalContextUtil.reset(); + } + } + + private String resolveTenantId(HttpServletRequest request) { + // 1. JWT claim (no signature verification — same strategy as TenantAwareAuthenticationFilter) + String claimName = fineractProperties.getSecurity().getOidcFederation().getTenantClaimName(); + if (claimName != null) { + try { + String token = bearerTokenResolver.resolve(request); + if (token != null) { + var claims = JWTParser.parse(token).getJWTClaimsSet(); + String fromClaim = (String) claims.getClaim(claimName); + if (fromClaim != null) { + return fromClaim; + } + } + } catch (Exception ignored) { + // malformed token — fall through to header / query param Review Comment: I dont like this approach to ignore malformed token... -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
