@@ -17,15 +17,19 @@ package com.gooddata.oauth2.server.oauth2.client
17
17
18
18
import com.gooddata.oauth2.server.ReactiveCookieService
19
19
import com.gooddata.oauth2.server.SPRING_EXTERNAL_IDP
20
+ import com.gooddata.oauth2.server.getOrganizationFromContext
20
21
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizationRequestResolver
21
22
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest
22
23
import org.springframework.web.server.ServerWebExchange
23
24
import reactor.core.publisher.Mono
24
25
import reactor.kotlin.core.publisher.switchIfEmpty
25
26
26
27
/* *
27
- * The implementation of [ServerOAuth2AuthorizationRequestResolver] that is able to append external identity provider
28
- * (OIDC federation) info to authorization requests based on [SPRING_EXTERNAL_IDP] cookie in the [ServerWebExchange].
28
+ * The implementation of [ServerOAuth2AuthorizationRequestResolver] that is able to append ad-hoc authentication attrs
29
+ * to authorization requests.
30
+ *
31
+ * Firstly it can add an external identity provider (OIDC federation) info to authorization requests
32
+ * based on [SPRING_EXTERNAL_IDP] cookie in the [ServerWebExchange].
29
33
*
30
34
* It wraps the default [ServerOAuth2AuthorizationRequestResolver] which is responsible for building of the original
31
35
* authorization request with standard parameters.
@@ -34,31 +38,35 @@ import reactor.kotlin.core.publisher.switchIfEmpty
34
38
*
35
39
* The new query parameter is Cognito-specific for now, so this does not ensure support for other identity providers.
36
40
*
41
+ * Secondly, it can add additional authentication attributes if present in the organization definition.
42
+ *
37
43
* @param defaultResolver the default [ServerOAuth2AuthorizationRequestResolver] to be wrapped
38
44
* @param cookieService the [ReactiveCookieService] to be used for cookie handling
39
45
*/
40
- class FederationAwareOauth2AuthorizationRequestResolver (
46
+ class CustomAttrsAwareOauth2AuthorizationRequestResolver (
41
47
private val defaultResolver : ServerOAuth2AuthorizationRequestResolver ,
42
48
private val cookieService : ReactiveCookieService ,
43
49
) : ServerOAuth2AuthorizationRequestResolver {
44
50
override fun resolve (exchange : ServerWebExchange ? ): Mono <OAuth2AuthorizationRequest > =
45
51
defaultResolver.resolve(exchange).flatMap { authorizationRequest ->
46
- enhanceRequestByExternalIdentityParam (authorizationRequest, exchange)
52
+ enhanceRequestByAdditionalParams (authorizationRequest, exchange)
47
53
}
48
54
49
55
override fun resolve (
50
56
exchange : ServerWebExchange ? ,
51
57
clientRegistrationId : String? ,
52
58
): Mono <OAuth2AuthorizationRequest > =
53
59
defaultResolver.resolve(exchange, clientRegistrationId).flatMap { authorizationRequest ->
54
- enhanceRequestByExternalIdentityParam (authorizationRequest, exchange)
60
+ enhanceRequestByAdditionalParams (authorizationRequest, exchange)
55
61
}
56
62
57
63
/* *
58
- * Enhances the provided [authorizationRequest] with external identity provider info based
59
- * on the [SPRING_EXTERNAL_IDP] cookie existence. If the cookie is present, it is cleared.
64
+ * Enhances the provided [authorizationRequest] with external additional authentication attributes.
65
+ * Adds additional authentication attributes from the organization definition if they are present.
66
+ * Adds identity provider info based on the [SPRING_EXTERNAL_IDP] cookie existence. If the cookie is present,
67
+ * it is cleared.
60
68
*/
61
- private fun enhanceRequestByExternalIdentityParam (
69
+ private fun enhanceRequestByAdditionalParams (
62
70
authorizationRequest : OAuth2AuthorizationRequest ,
63
71
exchange : ServerWebExchange ? ,
64
72
): Mono <OAuth2AuthorizationRequest > = Mono .justOrEmpty(exchange)
@@ -72,11 +80,23 @@ class FederationAwareOauth2AuthorizationRequestResolver(
72
80
additionalParams[COGNITO_EXTERNAL_PROVIDER_ID_PARAM_NAME ] = externalIdp
73
81
}
74
82
.build()
83
+ }.switchIfEmpty {
84
+ Mono .just(authorizationRequest)
85
+ }.flatMap { request ->
86
+ getOrganizationFromContext().flatMap { organization ->
87
+ Mono .just(OAuth2AuthorizationRequest .from(request)
88
+ .additionalParameters { additionalParams ->
89
+ // if organization contains additional authentication attributes, add them to the request
90
+ organization.oauthCustomAuthAttributes?.takeIf { it.isNotEmpty() }
91
+ ?.forEach { (key, value) ->
92
+ additionalParams[key] = value
93
+ }
94
+ }
95
+ .build()
96
+ )
97
+ }
75
98
}
76
99
}
77
- .switchIfEmpty {
78
- Mono .just(authorizationRequest)
79
- }
80
100
81
101
companion object {
82
102
/* *
0 commit comments