|
| 1 | +/* |
| 2 | + * Copyright 2025 GoodData Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.gooddata.oauth2.server |
| 17 | + |
| 18 | +import io.github.oshai.kotlinlogging.KotlinLogging |
| 19 | +import org.springframework.web.server.ServerWebExchange |
| 20 | +import org.springframework.web.server.WebFilterChain |
| 21 | +import reactor.core.publisher.Mono |
| 22 | + |
| 23 | +private val logger = KotlinLogging.logger {} |
| 24 | + |
| 25 | +/** |
| 26 | + * Context data needed to create a user context. |
| 27 | + */ |
| 28 | +private data class UserContextData( |
| 29 | + val organizationId: String, |
| 30 | + val userId: String, |
| 31 | + val userName: String?, |
| 32 | + val tokenId: String?, |
| 33 | + val authMethod: AuthMethod?, |
| 34 | + val accessToken: String? |
| 35 | +) |
| 36 | + |
| 37 | +/** |
| 38 | + * Processes [CallContextAuthenticationToken] and creates user context from call context data. |
| 39 | + * |
| 40 | + * Unlike other authentication processors, this does NOT fetch organization/user from the authentication store |
| 41 | + * because call context authentication represents requests that have already been authenticated by an upstream |
| 42 | + * service. The upstream service has already validated credentials, checked for global logout, and verified |
| 43 | + * organization/user existence. |
| 44 | + * |
| 45 | + * This processor delegates header parsing to [CallContextHeaderProcessor] implementation. |
| 46 | + */ |
| 47 | +class CallContextAuthenticationProcessor( |
| 48 | + private val headerProcessor: CallContextHeaderProcessor, |
| 49 | + private val userContextProvider: ReactorUserContextProvider |
| 50 | +) : AuthenticationProcessor<CallContextAuthenticationToken>(userContextProvider) { |
| 51 | + |
| 52 | + override fun authenticate( |
| 53 | + authenticationToken: CallContextAuthenticationToken, |
| 54 | + exchange: ServerWebExchange, |
| 55 | + chain: WebFilterChain |
| 56 | + ): Mono<Void> { |
| 57 | + return try { |
| 58 | + val authDetails = headerProcessor.parseCallContextHeader(authenticationToken.callContextHeaderValue) |
| 59 | + ?: throw CallContextAuthenticationException("Call context header contains no user information") |
| 60 | + |
| 61 | + val authMethod = try { |
| 62 | + AuthMethod.valueOf(authDetails.authMethod) |
| 63 | + } catch (e: IllegalArgumentException) { |
| 64 | + logger.logError(e) { |
| 65 | + withAction("callContextAuth") |
| 66 | + withState("failed") |
| 67 | + withMessage { |
| 68 | + "Invalid authMethod '${authDetails.authMethod}' in CallContext header. " + |
| 69 | + "Valid values: ${AuthMethod.entries.joinToString { it.name }}" |
| 70 | + } |
| 71 | + } |
| 72 | + throw CallContextAuthenticationException( |
| 73 | + "Invalid authentication method in call context" |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + logger.logInfo { |
| 78 | + withAction("callContextAuth") |
| 79 | + withState("authenticated") |
| 80 | + withOrganizationId(authDetails.organizationId) |
| 81 | + withUserId(authDetails.userId) |
| 82 | + withAuthenticationMethod(authMethod.name) |
| 83 | + authDetails.tokenId?.let { withTokenId(it) } |
| 84 | + withMessage { "Processed authenticated call context" } |
| 85 | + } |
| 86 | + |
| 87 | + val userContextData = UserContextData( |
| 88 | + organizationId = authDetails.organizationId, |
| 89 | + userId = authDetails.userId, |
| 90 | + userName = null, |
| 91 | + tokenId = authDetails.tokenId, |
| 92 | + authMethod = authMethod, |
| 93 | + accessToken = null |
| 94 | + ) |
| 95 | + withUserContext(userContextData) { |
| 96 | + chain.filter(exchange) |
| 97 | + } |
| 98 | + } catch (e: CallContextAuthenticationException) { |
| 99 | + val remoteAddress = exchange.request.remoteAddress?.address?.hostAddress |
| 100 | + logger.logError(e) { |
| 101 | + withAction("callContextAuth") |
| 102 | + withState("failed") |
| 103 | + withMessage { "Call context authentication failed from $remoteAddress" } |
| 104 | + } |
| 105 | + Mono.error(e) |
| 106 | + } catch (@Suppress("TooGenericExceptionCaught") e: Exception) { |
| 107 | + // Must catch all exceptions to prevent auth chain disruption |
| 108 | + logger.logError(e) { |
| 109 | + withAction("callContextAuth") |
| 110 | + withState("error") |
| 111 | + withMessage { "Unexpected error during call context authentication" } |
| 112 | + } |
| 113 | + Mono.error(CallContextAuthenticationException("Authentication failed", e)) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private fun <T> withUserContext( |
| 118 | + userContextData: UserContextData, |
| 119 | + monoProvider: () -> Mono<T> |
| 120 | + ): Mono<T> { |
| 121 | + val contextView = userContextProvider.getContextView( |
| 122 | + organizationId = userContextData.organizationId, |
| 123 | + userId = userContextData.userId, |
| 124 | + userName = userContextData.userName, |
| 125 | + tokenId = userContextData.tokenId, |
| 126 | + authMethod = userContextData.authMethod, |
| 127 | + accessToken = userContextData.accessToken |
| 128 | + ) |
| 129 | + |
| 130 | + return monoProvider().contextWrite(contextView) |
| 131 | + } |
| 132 | +} |
0 commit comments