2727import io .cdap .plugin .http .common .http .AuthType ;
2828import io .cdap .plugin .http .common .http .OAuthUtil ;
2929
30+ import io .cdap .plugin .http .source .common .BaseHttpSourceConfig ;
3031import java .io .File ;
3132import java .util .Optional ;
3233import javax .annotation .Nullable ;
@@ -48,6 +49,8 @@ public abstract class BaseHttpConfig extends ReferencePluginConfig {
4849 public static final String PROPERTY_PROXY_URL = "proxyUrl" ;
4950 public static final String PROPERTY_PROXY_USERNAME = "proxyUsername" ;
5051 public static final String PROPERTY_PROXY_PASSWORD = "proxyPassword" ;
52+ public static final String PROPERTY_OAUTH2_GRANT_TYPE = "oauth2GrantType" ;
53+ public static final String PROPERTY_OAUTH2_CLIENT_AUTHENTICATION = "oauth2ClientAuthentication" ;
5154
5255 public static final String PROPERTY_AUTH_TYPE_LABEL = "Auth type" ;
5356
@@ -93,6 +96,18 @@ public abstract class BaseHttpConfig extends ReferencePluginConfig {
9396 @ Macro
9497 protected String authUrl ;
9598
99+ @ Nullable
100+ @ Name (PROPERTY_OAUTH2_GRANT_TYPE )
101+ @ Description ("Which Oauth2 grant type flow is used." )
102+ @ Macro
103+ protected String oauth2GrantType ;
104+
105+ @ Nullable
106+ @ Name (PROPERTY_OAUTH2_CLIENT_AUTHENTICATION )
107+ @ Description ("Which Oauth2 client authentication flow is used." )
108+ @ Macro
109+ protected String oauth2ClientAuthentication ;
110+
96111 @ Nullable
97112 @ Name (PROPERTY_TOKEN_URL )
98113 @ Description ("Endpoint for the resource server, which exchanges the authorization code for an access token." )
@@ -208,6 +223,19 @@ public String getOAuth2Enabled() {
208223 return oauth2Enabled ;
209224 }
210225
226+ public OAuthGrantType getOauth2GrantType () {
227+ OAuthGrantType grantType = OAuthGrantType .getGrantType (oauth2GrantType );
228+ return BaseHttpSourceConfig .getEnumValueByString (OAuthGrantType .class , grantType .getValue (),
229+ PROPERTY_OAUTH2_GRANT_TYPE );
230+ }
231+
232+ public OAuthClientAuthentication getOauth2ClientAuthentication () {
233+ OAuthClientAuthentication clientAuthentication = OAuthClientAuthentication .getClientAuthentication (
234+ oauth2ClientAuthentication );
235+ return BaseHttpSourceConfig .getEnumValueByString (OAuthClientAuthentication .class ,
236+ clientAuthentication .getValue (), PROPERTY_OAUTH2_CLIENT_AUTHENTICATION );
237+ }
238+
211239 @ Nullable
212240 public String getAuthUrl () {
213241 return authUrl ;
@@ -365,21 +393,7 @@ public void validate(FailureCollector failureCollector) {
365393 AuthType authType = getAuthType ();
366394 switch (authType ) {
367395 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- }
396+ validateOAuth2Fields (failureCollector );
383397 break ;
384398 case SERVICE_ACCOUNT :
385399 String reasonSA = "Service Account is enabled" ;
@@ -423,4 +437,33 @@ public static void assertIsSetWithFailureCollector(Object propertyValue, String
423437 null ).withConfigProperty (propertyName );
424438 }
425439 }
440+
441+ private void validateOAuth2Fields (FailureCollector failureCollector ) {
442+ String reasonOauth2GrantType = String .format ("OAuth2 is enabled and grant type is %s." ,
443+ getOauth2GrantType ().getValue ());
444+ if (!containsMacro (PROPERTY_TOKEN_URL )) {
445+ assertIsSetWithFailureCollector (getTokenUrl (), PROPERTY_TOKEN_URL ,
446+ reasonOauth2GrantType , failureCollector );
447+ }
448+ if (!containsMacro (PROPERTY_CLIENT_ID )) {
449+ assertIsSetWithFailureCollector (getClientId (), PROPERTY_CLIENT_ID ,
450+ reasonOauth2GrantType , failureCollector );
451+ }
452+ if (!containsMacro (PROPERTY_CLIENT_SECRET )) {
453+ assertIsSetWithFailureCollector (getClientSecret (), PROPERTY_CLIENT_SECRET ,
454+ reasonOauth2GrantType , failureCollector );
455+ }
456+ if (!containsMacro (PROPERTY_OAUTH2_CLIENT_AUTHENTICATION )) {
457+ assertIsSetWithFailureCollector (getOauth2ClientAuthentication (),
458+ PROPERTY_OAUTH2_CLIENT_AUTHENTICATION , reasonOauth2GrantType , failureCollector );
459+ }
460+ // in case of refresh token grant type, also check additional fields
461+ if (OAuthGrantType .REFRESH_TOKEN .equals (getOauth2GrantType ())) {
462+ if (!containsMacro (PROPERTY_REFRESH_TOKEN )) {
463+ assertIsSetWithFailureCollector (getRefreshToken (), PROPERTY_REFRESH_TOKEN ,
464+ reasonOauth2GrantType , failureCollector );
465+ }
466+ }
467+ failureCollector .getOrThrowException ();
468+ }
426469}
0 commit comments