-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding changes for the grant type client credentials mechanism #1
base: develop
Are you sure you want to change the base?
Changes from 1 commit
ae08a88
6217375
5caf556
d9e26b9
54eea3b
c26398d
b36b7fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,7 @@ public abstract class BaseHttpSourceConfig extends ReferencePluginConfig { | |
|
||
public static final String PAGINATION_INDEX_PLACEHOLDER_REGEX = "\\{pagination.index\\}"; | ||
public static final String PAGINATION_INDEX_PLACEHOLDER = "{pagination.index}"; | ||
public static final String PROPERTY_GRANT_TYPE = "grantType"; | ||
|
||
@Name(PROPERTY_URL) | ||
@Description("Url to fetch to the first page. The url must start with a protocol (e.g. http://).") | ||
|
@@ -442,6 +443,11 @@ public abstract class BaseHttpSourceConfig extends ReferencePluginConfig { | |
@Description("Output schema. Is required to be set.") | ||
protected String schema; | ||
|
||
@Nullable | ||
@Name(PROPERTY_GRANT_TYPE) | ||
@Description("Value of grant type to determine the OAuth mechanism") | ||
protected String grantType; | ||
|
||
protected BaseHttpSourceConfig(String referenceName) { | ||
super(referenceName); | ||
} | ||
|
@@ -719,6 +725,10 @@ public Schema getSchema() { | |
schema, e, PROPERTY_SCHEMA); | ||
} | ||
} | ||
@Nullable | ||
public String getGrantType() { | ||
return grantType; | ||
} | ||
|
||
@Nullable | ||
public Map<String, String> getHeadersMap() { | ||
|
@@ -798,7 +808,6 @@ public void validate(FailureCollector failureCollector) { | |
String.format("URL value is not valid: '%s'", getUrl()), e, PROPERTY_URL); | ||
} | ||
} | ||
|
||
// Validate Linear Retry Interval | ||
if (!containsMacro(PROPERTY_RETRY_POLICY) && getRetryPolicy() == RetryPolicy.LINEAR) { | ||
assertIsSet(getLinearRetryInterval(), PROPERTY_LINEAR_RETRY_INTERVAL, "retry policy is linear"); | ||
|
@@ -883,20 +892,22 @@ PAGINATION_INDEX_PLACEHOLDER, getPaginationType()), | |
// Validate OAuth2 properties | ||
if (!containsMacro(PROPERTY_OAUTH2_ENABLED) && this.getOauth2Enabled()) { | ||
String reasonOauth2 = "OAuth2 is enabled"; | ||
assertIsSet(getAuthUrl(), PROPERTY_AUTH_URL, reasonOauth2); | ||
assertIsSet(getTokenUrl(), PROPERTY_TOKEN_URL, reasonOauth2); | ||
assertIsSet(getClientId(), PROPERTY_CLIENT_ID, reasonOauth2); | ||
assertIsSet(getClientSecret(), PROPERTY_CLIENT_SECRET, reasonOauth2); | ||
assertIsSet(getRefreshToken(), PROPERTY_REFRESH_TOKEN, reasonOauth2); | ||
assertIsSet(getGrantType(), PROPERTY_GRANT_TYPE, reasonOauth2); | ||
|
||
// refresh token validate | ||
if (refreshTokenGrantType()) { | ||
assertIsSet(getAuthUrl(), PROPERTY_AUTH_URL, reasonOauth2); | ||
assertIsSet(getRefreshToken(), PROPERTY_REFRESH_TOKEN, reasonOauth2); | ||
} | ||
} | ||
// Validate Authentication properties | ||
AuthType authType = getAuthType(); | ||
switch (authType) { | ||
case OAUTH2: | ||
String reasonOauth2 = "OAuth2 is enabled"; | ||
if (!containsMacro(PROPERTY_AUTH_URL)) { | ||
assertIsSet(getAuthUrl(), PROPERTY_AUTH_URL, reasonOauth2); | ||
} | ||
if (!containsMacro(PROPERTY_TOKEN_URL)) { | ||
assertIsSet(getTokenUrl(), PROPERTY_TOKEN_URL, reasonOauth2); | ||
} | ||
|
@@ -906,8 +917,16 @@ PAGINATION_INDEX_PLACEHOLDER, getPaginationType()), | |
if (!containsMacro((PROPERTY_CLIENT_SECRET))) { | ||
assertIsSet(getClientSecret(), PROPERTY_CLIENT_SECRET, reasonOauth2); | ||
} | ||
if (!containsMacro(PROPERTY_REFRESH_TOKEN)) { | ||
assertIsSet(getRefreshToken(), PROPERTY_REFRESH_TOKEN, reasonOauth2); | ||
if (!containsMacro(PROPERTY_GRANT_TYPE)) { | ||
assertIsSet(getGrantType(), PROPERTY_GRANT_TYPE, reasonOauth2); | ||
if (refreshTokenGrantType()) { | ||
if (!containsMacro(PROPERTY_REFRESH_TOKEN)) { | ||
assertIsSet(getRefreshToken(), PROPERTY_REFRESH_TOKEN, reasonOauth2); | ||
} | ||
if (!containsMacro(PROPERTY_AUTH_URL)) { | ||
assertIsSet(getAuthUrl(), PROPERTY_AUTH_URL, reasonOauth2); | ||
} | ||
} | ||
} | ||
break; | ||
case SERVICE_ACCOUNT: | ||
|
@@ -941,6 +960,13 @@ PAGINATION_INDEX_PLACEHOLDER, getPaginationType()), | |
} | ||
} | ||
|
||
private boolean refreshTokenGrantType() { | ||
if (getGrantType() == "refresh_token") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create an enum for grant types and change the above code also accordingly. and write grant type specific if conditions. e.g.(if grant == enum.refreshtoken) else if grant=client_credentials There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enum for grant type created and made changes to the conditions accordingly. |
||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private boolean validateServiceAccount(FailureCollector collector) { | ||
if (containsMacro(PROPERTY_NAME_SERVICE_ACCOUNT_FILE_PATH) || containsMacro(PROPERTY_NAME_SERVICE_ACCOUNT_JSON)) { | ||
return false; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.client.utils.URIBuilder; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.message.BasicHeader; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
import java.io.ByteArrayInputStream; | ||
|
@@ -34,13 +35,14 @@ | |
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
/** | ||
* A class which contains utilities to make OAuth2 specific calls. | ||
*/ | ||
public class OAuthUtil { | ||
public static String getAccessTokenByRefreshToken(CloseableHttpClient httpclient, String tokenUrl, String clientId, | ||
String clientSecret, String refreshToken) | ||
String clientSecret, String refreshToken, String grantType) | ||
throws IOException { | ||
|
||
URI uri; | ||
|
@@ -49,7 +51,7 @@ public static String getAccessTokenByRefreshToken(CloseableHttpClient httpclient | |
.setParameter("client_id", clientId) | ||
.setParameter("client_secret", clientSecret) | ||
.setParameter("refresh_token", refreshToken) | ||
.setParameter("grant_type", "refresh_token") | ||
.setParameter("grant_type", grantType) | ||
.build(); | ||
} catch (URISyntaxException e) { | ||
throw new IllegalArgumentException("Failed to build token URI for OAuth2", e); | ||
|
@@ -92,5 +94,29 @@ public static String getAccessTokenByServiceAccount(BaseHttpSourceConfig config) | |
} | ||
return accessToken; | ||
} | ||
|
||
public static String getAccessTokenByClientCredentials(CloseableHttpClient httpclient, String tokenUrl, | ||
String clientId, String clientSecret, String grantType) | ||
throws IOException { | ||
URI uri; | ||
try { | ||
uri = new URIBuilder(tokenUrl).setParameter("grant_type", grantType).build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. define constants where ever any keyword getting used multiple times or has scope of getting used in future There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constants are defined for both client credentials method and refresh token method. |
||
} catch (URISyntaxException e) { | ||
throw new IllegalArgumentException("Failed to build token URI for OAuth2", e); | ||
} | ||
|
||
HttpPost httppost = new HttpPost(uri); | ||
httppost.addHeader(new BasicHeader("Authorization", "Basic " + getBase64EncodeValue(clientId, clientSecret))); | ||
httppost.addHeader(new BasicHeader("Content-Type", "application/json")); | ||
CloseableHttpResponse response = httpclient.execute(httppost); | ||
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8"); | ||
|
||
JsonElement jsonElement = JSONUtil.toJsonObject(responseString).get("access_token"); | ||
return jsonElement.getAsString(); | ||
} | ||
|
||
private static String getBase64EncodeValue(String clientId, String clientSecret) { | ||
return Base64.getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,18 @@ | |
] | ||
} | ||
}, | ||
{ | ||
"widget-type": "select", | ||
"label": "Grant Type", | ||
"name": "grantType", | ||
"widget-attributes" : { | ||
"values": [ | ||
"refresh_token", | ||
"client_credentials" | ||
], | ||
"default" : "refresh_token" | ||
} | ||
}, | ||
{ | ||
"widget-type": "textbox", | ||
"label": "Auth URL", | ||
|
@@ -684,27 +696,26 @@ | |
}, | ||
"show": [ | ||
{ | ||
"name": "authUrl", | ||
"type": "property" | ||
}, | ||
{ | ||
"name": "tokenUrl", | ||
"type": "property" | ||
"widget-type": "textbox", | ||
"label": "Token URL", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. label is not required here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed the labels attribute according to the filters conditions. |
||
"name": "tokenUrl" | ||
}, | ||
{ | ||
"name": "clientId", | ||
"type": "property" | ||
"widget-type": "textbox", | ||
"label": "Client ID", | ||
"name": "clientId" | ||
}, | ||
{ | ||
"name": "clientSecret", | ||
"type": "property" | ||
"widget-type": "password", | ||
"label": "Client Secret", | ||
"name": "clientSecret" | ||
}, | ||
{ | ||
"name": "scopes", | ||
"type": "property" | ||
}, | ||
{ | ||
"name": "refreshToken", | ||
"name": "grantType", | ||
"type": "property" | ||
} | ||
] | ||
|
@@ -780,6 +791,23 @@ | |
"type": "property" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "Grant Type Refresh Token", | ||
"condition": { | ||
"expression": "grantType == 'refresh_token' && authType == 'oAuth2'" | ||
}, | ||
"show": [ | ||
{ | ||
"widget-type": "textbox", | ||
"label": "Auth URL", | ||
"name": "authUrl" | ||
}, | ||
{ | ||
"name": "refreshToken", | ||
"type": "property" | ||
} | ||
] | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need this method, I can directly check for the condition where ever we are using this. In case we will add multiple grant types in future, it will create issues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checking the condition directly now.