Skip to content
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

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
import io.cdap.plugin.http.source.common.error.ErrorHandling;
import io.cdap.plugin.http.source.common.error.HttpErrorHandlerEntity;
import io.cdap.plugin.http.source.common.error.RetryableErrorHandling;
import io.cdap.plugin.http.source.common.http.AuthType;
import io.cdap.plugin.http.source.common.http.HttpClient;
import io.cdap.plugin.http.source.common.http.KeyStoreType;
import io.cdap.plugin.http.source.common.http.OAuthUtil;
import io.cdap.plugin.http.source.common.http.*;
import io.cdap.plugin.http.source.common.pagination.PaginationIteratorFactory;
import io.cdap.plugin.http.source.common.pagination.PaginationType;
import io.cdap.plugin.http.source.common.pagination.page.PageFormat;
Expand Down Expand Up @@ -119,6 +116,13 @@ 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";
public static final String PROPERTY_GRANT_TYPE_LABEL = "Grant type";

public static final String PARAMETER_CLIENT_ID = "client_id";
public static final String PARAMETER_CLIENT_SECRET = "client_secret";
public static final String PARAMETER_REFRESH_TOKEN = "refresh_token";
public static final String PARAMETER_GRANT_TYPE = "grant_type";
public static final String PARAMETER_ACCESS_TOKEN = "access_token";

@Name(PROPERTY_URL)
@Description("Url to fetch to the first page. The url must start with a protocol (e.g. http://).")
Expand Down Expand Up @@ -961,8 +965,10 @@ PAGINATION_INDEX_PLACEHOLDER, getPaginationType()),
}

private boolean refreshTokenGrantType() {
Copy link
Collaborator

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

Copy link
Owner Author

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.

if (getGrantType() == "refresh_token") {
if (getGrantType() == GrantType.REFRESH_TOKEN.getValue()) {
return true;
} else if (getGrantType() == GrantType.CLIENT_CREDENTIALS.getValue()) {
return false;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright © 2022 Cask Data, Inc.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2023

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made the year to 2023

*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package io.cdap.plugin.http.source.common.http;

import io.cdap.plugin.http.source.common.BaseHttpSourceConfig;
import io.cdap.plugin.http.source.common.exceptions.InvalidPropertyTypeException;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Enum for different grant type methods.
*/
public enum GrantType {
REFRESH_TOKEN("refresh_token"),
CLIENT_CREDENTIALS("client_credentials");

private final String value;

GrantType(String value) {
this.value = value;
}

public String getValue() {
return value;
}

/**
* Returns the GrantType.
*
* @param value the value is string type.
* @return The GrantType
*/
public static GrantType fromValue(String value) {
return Arrays.stream(GrantType.values()).filter(grantType -> grantType.getValue().equals(value))
.findAny().orElseThrow(() -> new InvalidPropertyTypeException(BaseHttpSourceConfig.PROPERTY_GRANT_TYPE_LABEL,
value, getAllowedValues()));
}

public static List<String> getAllowedValues() {
return Arrays.stream(GrantType.values()).map(v -> v.getValue())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ private CloseableHttpClient createHttpClient() throws IOException {

switch (authType) {
case OAUTH2:
String accessToken;
if (config.getGrantType() == "refresh_token") {
String accessToken = null;
if (config.getGrantType() == GrantType.REFRESH_TOKEN.getValue()) {
accessToken = OAuthUtil.getAccessTokenByRefreshToken(HttpClients.createDefault(), config.getTokenUrl(),
config.getClientId(), config.getClientSecret(),
config.getRefreshToken(), config.getGrantType());
} else {
} else if (config.getGrantType() == GrantType.CLIENT_CREDENTIALS.getValue()) {
accessToken = OAuthUtil.getAccessTokenByClientCredentials(HttpClients.createDefault(), config.getTokenUrl(),
config.getClientId(), config.getClientSecret(), config.getGrantType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public static String getAccessTokenByRefreshToken(CloseableHttpClient httpclient
URI uri;
try {
uri = new URIBuilder(tokenUrl)
.setParameter("client_id", clientId)
.setParameter("client_secret", clientSecret)
.setParameter("refresh_token", refreshToken)
.setParameter("grant_type", grantType)
.setParameter(BaseHttpSourceConfig.PARAMETER_CLIENT_ID, clientId)
.setParameter(BaseHttpSourceConfig.PARAMETER_CLIENT_SECRET, clientSecret)
.setParameter(BaseHttpSourceConfig.PARAMETER_REFRESH_TOKEN, refreshToken)
.setParameter(BaseHttpSourceConfig.PARAMETER_GRANT_TYPE, grantType)
.build();
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Failed to build token URI for OAuth2", e);
Expand All @@ -61,7 +61,7 @@ public static String getAccessTokenByRefreshToken(CloseableHttpClient httpclient
CloseableHttpResponse response = httpclient.execute(httppost);
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");

JsonElement jsonElement = JSONUtil.toJsonObject(responseString).get("access_token");
JsonElement jsonElement = JSONUtil.toJsonObject(responseString).get(BaseHttpSourceConfig.PARAMETER_ACCESS_TOKEN);
if (jsonElement == null) {
throw new IllegalArgumentException("Access token not found");
}
Expand Down Expand Up @@ -103,7 +103,7 @@ public static String getAccessTokenByClientCredentials(CloseableHttpClient httpc
throws IOException {
URI uri;
try {
uri = new URIBuilder(tokenUrl).setParameter("grant_type", grantType).build();
uri = new URIBuilder(tokenUrl).setParameter(BaseHttpSourceConfig.PARAMETER_GRANT_TYPE, grantType).build();
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Failed to build token URI for OAuth2", e);
}
Expand All @@ -114,7 +114,7 @@ public static String getAccessTokenByClientCredentials(CloseableHttpClient httpc
CloseableHttpResponse response = httpclient.execute(httppost);
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");

JsonElement jsonElement = JSONUtil.toJsonObject(responseString).get("access_token");
JsonElement jsonElement = JSONUtil.toJsonObject(responseString).get(BaseHttpSourceConfig.PARAMETER_ACCESS_TOKEN);
return jsonElement.getAsString();
}

Expand Down
20 changes: 8 additions & 12 deletions widgets/HTTP-batchsource.json
Original file line number Diff line number Diff line change
Expand Up @@ -696,19 +696,16 @@
},
"show": [
{
"widget-type": "textbox",
"label": "Token URL",
"name": "tokenUrl"
"name": "tokenUrl",
"type": "property"
},
{
"widget-type": "textbox",
"label": "Client ID",
"name": "clientId"
"name": "clientId",
"type": "property"
},
{
"widget-type": "password",
"label": "Client Secret",
"name": "clientSecret"
"name": "clientSecret",
"type": "property"
},
{
"name": "scopes",
Expand Down Expand Up @@ -799,9 +796,8 @@
},
"show": [
{
"widget-type": "textbox",
"label": "Auth URL",
"name": "authUrl"
"name": "authUrl",
"type": "property"
},
{
"name": "refreshToken",
Expand Down