29
29
30
30
import java .io .File ;
31
31
import java .util .Optional ;
32
+ import java .util .stream .Stream ;
32
33
import javax .annotation .Nullable ;
33
34
34
-
35
35
/**
36
36
* Base configuration for HTTP Source and HTTP Sink
37
37
*/
@@ -48,6 +48,8 @@ public abstract class BaseHttpConfig extends ReferencePluginConfig {
48
48
public static final String PROPERTY_PROXY_URL = "proxyUrl" ;
49
49
public static final String PROPERTY_PROXY_USERNAME = "proxyUsername" ;
50
50
public static final String PROPERTY_PROXY_PASSWORD = "proxyPassword" ;
51
+ public static final String PROPERTY_OAUTH2_GRANT_TYPE = "oauth2GrantType" ;
52
+ public static final String PROPERTY_OAUTH2_CLIENT_AUTHENTICATION = "oauth2ClientAuthentication" ;
51
53
52
54
public static final String PROPERTY_AUTH_TYPE_LABEL = "Auth type" ;
53
55
@@ -93,6 +95,18 @@ public abstract class BaseHttpConfig extends ReferencePluginConfig {
93
95
@ Macro
94
96
protected String authUrl ;
95
97
98
+ @ Nullable
99
+ @ Name (PROPERTY_OAUTH2_GRANT_TYPE )
100
+ @ Description ("Which Oauth2 grant type flow is used." )
101
+ @ Macro
102
+ protected String oauth2GrantType ;
103
+
104
+ @ Nullable
105
+ @ Name (PROPERTY_OAUTH2_CLIENT_AUTHENTICATION )
106
+ @ Description ("Send auth credentials in the request body or as query param." )
107
+ @ Macro
108
+ protected String oauth2ClientAuthentication ;
109
+
96
110
@ Nullable
97
111
@ Name (PROPERTY_TOKEN_URL )
98
112
@ Description ("Endpoint for the resource server, which exchanges the authorization code for an access token." )
@@ -208,6 +222,19 @@ public String getOAuth2Enabled() {
208
222
return oauth2Enabled ;
209
223
}
210
224
225
+ public OAuth2GrantType getOauth2GrantType () {
226
+ OAuth2GrantType grantType = OAuth2GrantType .getGrantType (oauth2GrantType );
227
+ return getEnumValueByString (OAuth2GrantType .class , grantType .getValue (),
228
+ PROPERTY_OAUTH2_GRANT_TYPE );
229
+ }
230
+
231
+ public OAuth2ClientAuthentication getOauth2ClientAuthentication () {
232
+ OAuth2ClientAuthentication clientAuthentication = OAuth2ClientAuthentication .getClientAuthentication (
233
+ oauth2ClientAuthentication );
234
+ return getEnumValueByString (OAuth2ClientAuthentication .class ,
235
+ clientAuthentication .getValue (), PROPERTY_OAUTH2_CLIENT_AUTHENTICATION );
236
+ }
237
+
211
238
@ Nullable
212
239
public String getAuthUrl () {
213
240
return authUrl ;
@@ -365,21 +392,7 @@ public void validate(FailureCollector failureCollector) {
365
392
AuthType authType = getAuthType ();
366
393
switch (authType ) {
367
394
case OAUTH2 :
368
- String reasonOauth2 = "OAuth2 is enabled" ;
369
- if (!containsMacro (PROPERTY_TOKEN_URL )) {
370
- assertIsSetWithFailureCollector (getTokenUrl (), PROPERTY_TOKEN_URL , reasonOauth2 , failureCollector );
371
- }
372
- if (!containsMacro (PROPERTY_CLIENT_ID )) {
373
- assertIsSetWithFailureCollector (getClientId (), PROPERTY_CLIENT_ID , reasonOauth2 , failureCollector );
374
- }
375
- if (!containsMacro ((PROPERTY_CLIENT_SECRET ))) {
376
- assertIsSetWithFailureCollector (getClientSecret (), PROPERTY_CLIENT_SECRET , reasonOauth2 ,
377
- failureCollector );
378
- }
379
- if (!containsMacro (PROPERTY_REFRESH_TOKEN )) {
380
- assertIsSetWithFailureCollector (getRefreshToken (), PROPERTY_REFRESH_TOKEN , reasonOauth2 ,
381
- failureCollector );
382
- }
395
+ validateOAuth2Fields (failureCollector );
383
396
break ;
384
397
case SERVICE_ACCOUNT :
385
398
String reasonSA = "Service Account is enabled" ;
@@ -423,4 +436,57 @@ public static void assertIsSetWithFailureCollector(Object propertyValue, String
423
436
null ).withConfigProperty (propertyName );
424
437
}
425
438
}
439
+
440
+ private void validateOAuth2Fields (FailureCollector failureCollector ) {
441
+ String reasonOauth2GrantType = String .format ("OAuth2 is enabled and grant type is %s." ,
442
+ getOauth2GrantType ().getValue ());
443
+ if (!containsMacro (PROPERTY_TOKEN_URL )) {
444
+ assertIsSetWithFailureCollector (getTokenUrl (), PROPERTY_TOKEN_URL ,
445
+ reasonOauth2GrantType , failureCollector );
446
+ }
447
+ if (!containsMacro (PROPERTY_CLIENT_ID )) {
448
+ assertIsSetWithFailureCollector (getClientId (), PROPERTY_CLIENT_ID ,
449
+ reasonOauth2GrantType , failureCollector );
450
+ }
451
+ if (!containsMacro (PROPERTY_CLIENT_SECRET )) {
452
+ assertIsSetWithFailureCollector (getClientSecret (), PROPERTY_CLIENT_SECRET ,
453
+ reasonOauth2GrantType , failureCollector );
454
+ }
455
+ if (!containsMacro (PROPERTY_OAUTH2_CLIENT_AUTHENTICATION )) {
456
+ assertIsSetWithFailureCollector (getOauth2ClientAuthentication (),
457
+ PROPERTY_OAUTH2_CLIENT_AUTHENTICATION , reasonOauth2GrantType , failureCollector );
458
+ }
459
+ // in case of refresh token grant type, also check additional fields
460
+ if (OAuth2GrantType .REFRESH_TOKEN .equals (getOauth2GrantType ())) {
461
+ if (!containsMacro (PROPERTY_REFRESH_TOKEN )) {
462
+ assertIsSetWithFailureCollector (getRefreshToken (), PROPERTY_REFRESH_TOKEN ,
463
+ reasonOauth2GrantType , failureCollector );
464
+ }
465
+ }
466
+ failureCollector .getOrThrowException ();
467
+ }
468
+
469
+ /**
470
+ * Retrieves the corresponding enum constant of a given string value.
471
+ *
472
+ * <p>This method takes an enum class that implements {@code EnumWithValue} and searches for an
473
+ * enum constant that matches the provided string value. If no matching value is found, it throws
474
+ * an {@code InvalidConfigPropertyException}.</p>
475
+ *
476
+ * @param <T> the type of enum that implements {@code EnumWithValue}
477
+ * @param enumClass the class of the enum to search within
478
+ * @param stringValue the string representation of the enum value
479
+ * @param propertyName the name of the property (used for error messages)
480
+ * @return the corresponding enum constant if a match is found
481
+ * @throws InvalidConfigPropertyException if the string value does not match any enum constant
482
+ */
483
+ public static <T extends EnumWithValue > T
484
+ getEnumValueByString (Class <T > enumClass , String stringValue , String propertyName ) {
485
+ return Stream .of (enumClass .getEnumConstants ())
486
+ .filter (keyType -> keyType .getValue ().equalsIgnoreCase (stringValue ))
487
+ .findAny ()
488
+ .orElseThrow (() -> new InvalidConfigPropertyException (
489
+ String .format ("Unsupported value for '%s': '%s'" , propertyName , stringValue ),
490
+ propertyName ));
491
+ }
426
492
}
0 commit comments