@@ -56,23 +56,6 @@ public final class OAuth2TokenExchangeAuthenticationConverter implements Authent
56
56
57
57
private static final String TOKEN_TYPE_IDENTIFIERS_URI = "https://datatracker.ietf.org/doc/html/rfc8693#section-3" ;
58
58
59
- private static final AuthorizationGrantType TOKEN_EXCHANGE = new AuthorizationGrantType (
60
- "urn:ietf:params:oauth:grant-type:token-exchange" );
61
-
62
- private static final String AUDIENCE = "audience" ;
63
-
64
- private static final String RESOURCE = "resource" ;
65
-
66
- private static final String REQUESTED_TOKEN_TYPE = "requested_token_type" ;
67
-
68
- private static final String SUBJECT_TOKEN = "subject_token" ;
69
-
70
- private static final String SUBJECT_TOKEN_TYPE = "subject_token_type" ;
71
-
72
- private static final String ACTOR_TOKEN = "actor_token" ;
73
-
74
- private static final String ACTOR_TOKEN_TYPE = "actor_token_type" ;
75
-
76
59
private static final String ACCESS_TOKEN_TYPE_VALUE = "urn:ietf:params:oauth:token-type:access_token" ;
77
60
78
61
private static final String JWT_TOKEN_TYPE_VALUE = "urn:ietf:params:oauth:token-type:jwt" ;
@@ -86,27 +69,27 @@ public Authentication convert(HttpServletRequest request) {
86
69
87
70
// grant_type (REQUIRED)
88
71
String grantType = parameters .getFirst (OAuth2ParameterNames .GRANT_TYPE );
89
- if (!TOKEN_EXCHANGE .getValue ().equals (grantType )) {
72
+ if (!AuthorizationGrantType . TOKEN_EXCHANGE .getValue ().equals (grantType )) {
90
73
return null ;
91
74
}
92
75
93
76
Authentication clientPrincipal = SecurityContextHolder .getContext ().getAuthentication ();
94
77
95
78
// resource (OPTIONAL)
96
- List <String > resources = parameters .getOrDefault (RESOURCE , Collections .emptyList ());
79
+ List <String > resources = parameters .getOrDefault (OAuth2ParameterNames . RESOURCE , Collections .emptyList ());
97
80
if (!CollectionUtils .isEmpty (resources )) {
98
81
for (String resource : resources ) {
99
82
if (!isValidUri (resource )) {
100
83
OAuth2EndpointUtils .throwError (
101
84
OAuth2ErrorCodes .INVALID_REQUEST ,
102
- RESOURCE ,
85
+ OAuth2ParameterNames . RESOURCE ,
103
86
OAuth2EndpointUtils .ACCESS_TOKEN_REQUEST_ERROR_URI );
104
87
}
105
88
}
106
89
}
107
90
108
91
// audience (OPTIONAL)
109
- List <String > audiences = parameters .getOrDefault (AUDIENCE , Collections .emptyList ());
92
+ List <String > audiences = parameters .getOrDefault (OAuth2ParameterNames . AUDIENCE , Collections .emptyList ());
110
93
111
94
// scope (OPTIONAL)
112
95
String scope = parameters .getFirst (OAuth2ParameterNames .SCOPE );
@@ -125,87 +108,87 @@ public Authentication convert(HttpServletRequest request) {
125
108
}
126
109
127
110
// requested_token_type (OPTIONAL)
128
- String requestedTokenType = parameters .getFirst (REQUESTED_TOKEN_TYPE );
111
+ String requestedTokenType = parameters .getFirst (OAuth2ParameterNames . REQUESTED_TOKEN_TYPE );
129
112
if (StringUtils .hasText (requestedTokenType )) {
130
- if (parameters .get (REQUESTED_TOKEN_TYPE ).size () != 1 ) {
113
+ if (parameters .get (OAuth2ParameterNames . REQUESTED_TOKEN_TYPE ).size () != 1 ) {
131
114
OAuth2EndpointUtils .throwError (
132
115
OAuth2ErrorCodes .INVALID_REQUEST ,
133
- REQUESTED_TOKEN_TYPE ,
116
+ OAuth2ParameterNames . REQUESTED_TOKEN_TYPE ,
134
117
OAuth2EndpointUtils .ACCESS_TOKEN_REQUEST_ERROR_URI );
135
118
}
136
119
137
- validateTokenType (REQUESTED_TOKEN_TYPE , requestedTokenType );
120
+ validateTokenType (OAuth2ParameterNames . REQUESTED_TOKEN_TYPE , requestedTokenType );
138
121
} else {
139
122
requestedTokenType = ACCESS_TOKEN_TYPE_VALUE ;
140
123
}
141
124
142
125
// subject_token (REQUIRED)
143
- String subjectToken = parameters .getFirst (SUBJECT_TOKEN );
126
+ String subjectToken = parameters .getFirst (OAuth2ParameterNames . SUBJECT_TOKEN );
144
127
if (!StringUtils .hasText (subjectToken ) ||
145
- parameters .get (SUBJECT_TOKEN ).size () != 1 ) {
128
+ parameters .get (OAuth2ParameterNames . SUBJECT_TOKEN ).size () != 1 ) {
146
129
OAuth2EndpointUtils .throwError (
147
130
OAuth2ErrorCodes .INVALID_REQUEST ,
148
- SUBJECT_TOKEN ,
131
+ OAuth2ParameterNames . SUBJECT_TOKEN ,
149
132
OAuth2EndpointUtils .ACCESS_TOKEN_REQUEST_ERROR_URI );
150
133
}
151
134
152
135
// subject_token_type (REQUIRED)
153
- String subjectTokenType = parameters .getFirst (SUBJECT_TOKEN_TYPE );
136
+ String subjectTokenType = parameters .getFirst (OAuth2ParameterNames . SUBJECT_TOKEN_TYPE );
154
137
if (!StringUtils .hasText (subjectTokenType ) ||
155
- parameters .get (SUBJECT_TOKEN_TYPE ).size () != 1 ) {
138
+ parameters .get (OAuth2ParameterNames . SUBJECT_TOKEN_TYPE ).size () != 1 ) {
156
139
OAuth2EndpointUtils .throwError (
157
140
OAuth2ErrorCodes .INVALID_REQUEST ,
158
- SUBJECT_TOKEN_TYPE ,
141
+ OAuth2ParameterNames . SUBJECT_TOKEN_TYPE ,
159
142
OAuth2EndpointUtils .ACCESS_TOKEN_REQUEST_ERROR_URI );
160
143
} else {
161
- validateTokenType (SUBJECT_TOKEN_TYPE , subjectTokenType );
144
+ validateTokenType (OAuth2ParameterNames . SUBJECT_TOKEN_TYPE , subjectTokenType );
162
145
}
163
146
164
147
// actor_token (OPTIONAL, REQUIRED if actor_token_type is provided)
165
- String actorToken = parameters .getFirst (ACTOR_TOKEN );
148
+ String actorToken = parameters .getFirst (OAuth2ParameterNames . ACTOR_TOKEN );
166
149
if (StringUtils .hasText (actorToken ) &&
167
- parameters .get (ACTOR_TOKEN ).size () != 1 ) {
150
+ parameters .get (OAuth2ParameterNames . ACTOR_TOKEN ).size () != 1 ) {
168
151
OAuth2EndpointUtils .throwError (
169
152
OAuth2ErrorCodes .INVALID_REQUEST ,
170
- ACTOR_TOKEN ,
153
+ OAuth2ParameterNames . ACTOR_TOKEN ,
171
154
OAuth2EndpointUtils .ACCESS_TOKEN_REQUEST_ERROR_URI );
172
155
}
173
156
174
157
// actor_token_type (OPTIONAL, REQUIRED if actor_token is provided)
175
- String actorTokenType = parameters .getFirst (ACTOR_TOKEN_TYPE );
158
+ String actorTokenType = parameters .getFirst (OAuth2ParameterNames . ACTOR_TOKEN_TYPE );
176
159
if (StringUtils .hasText (actorTokenType )) {
177
- if (parameters .get (ACTOR_TOKEN_TYPE ).size () != 1 ) {
160
+ if (parameters .get (OAuth2ParameterNames . ACTOR_TOKEN_TYPE ).size () != 1 ) {
178
161
OAuth2EndpointUtils .throwError (
179
162
OAuth2ErrorCodes .INVALID_REQUEST ,
180
- ACTOR_TOKEN_TYPE ,
163
+ OAuth2ParameterNames . ACTOR_TOKEN_TYPE ,
181
164
OAuth2EndpointUtils .ACCESS_TOKEN_REQUEST_ERROR_URI );
182
165
}
183
166
184
- validateTokenType (ACTOR_TOKEN_TYPE , actorTokenType );
167
+ validateTokenType (OAuth2ParameterNames . ACTOR_TOKEN_TYPE , actorTokenType );
185
168
}
186
169
187
170
if (!StringUtils .hasText (actorToken ) && StringUtils .hasText (actorTokenType )) {
188
171
OAuth2EndpointUtils .throwError (
189
172
OAuth2ErrorCodes .INVALID_REQUEST ,
190
- ACTOR_TOKEN ,
173
+ OAuth2ParameterNames . ACTOR_TOKEN ,
191
174
OAuth2EndpointUtils .ACCESS_TOKEN_REQUEST_ERROR_URI );
192
175
} else if (StringUtils .hasText (actorToken ) && !StringUtils .hasText (actorTokenType )) {
193
176
OAuth2EndpointUtils .throwError (
194
177
OAuth2ErrorCodes .INVALID_REQUEST ,
195
- ACTOR_TOKEN_TYPE ,
178
+ OAuth2ParameterNames . ACTOR_TOKEN_TYPE ,
196
179
OAuth2EndpointUtils .ACCESS_TOKEN_REQUEST_ERROR_URI );
197
180
}
198
181
199
182
Map <String , Object > additionalParameters = new HashMap <>();
200
183
parameters .forEach ((key , value ) -> {
201
184
if (!key .equals (OAuth2ParameterNames .GRANT_TYPE ) &&
202
- !key .equals (RESOURCE ) &&
203
- !key .equals (AUDIENCE ) &&
204
- !key .equals (REQUESTED_TOKEN_TYPE ) &&
205
- !key .equals (SUBJECT_TOKEN ) &&
206
- !key .equals (SUBJECT_TOKEN_TYPE ) &&
207
- !key .equals (ACTOR_TOKEN ) &&
208
- !key .equals (ACTOR_TOKEN_TYPE ) &&
185
+ !key .equals (OAuth2ParameterNames . RESOURCE ) &&
186
+ !key .equals (OAuth2ParameterNames . AUDIENCE ) &&
187
+ !key .equals (OAuth2ParameterNames . REQUESTED_TOKEN_TYPE ) &&
188
+ !key .equals (OAuth2ParameterNames . SUBJECT_TOKEN ) &&
189
+ !key .equals (OAuth2ParameterNames . SUBJECT_TOKEN_TYPE ) &&
190
+ !key .equals (OAuth2ParameterNames . ACTOR_TOKEN ) &&
191
+ !key .equals (OAuth2ParameterNames . ACTOR_TOKEN_TYPE ) &&
209
192
!key .equals (OAuth2ParameterNames .SCOPE )) {
210
193
additionalParameters .put (key , (value .size () == 1 ) ? value .get (0 ) : value .toArray (new String [0 ]));
211
194
}
0 commit comments