Skip to content

Commit 58bef9e

Browse files
update
1 parent e1a406e commit 58bef9e

4 files changed

Lines changed: 36 additions & 34 deletions

File tree

src/main/java/io/cdap/plugin/http/common/BaseHttpConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,5 +468,6 @@ private void validateOAuth2Fields(FailureCollector failureCollector) {
468468
reasonOauth2GrantType, failureCollector);
469469
}
470470
}
471+
failureCollector.getOrThrowException();
471472
}
472473
}

src/main/java/io/cdap/plugin/http/common/OAuthGrantType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public enum OAuthGrantType implements EnumWithValue {
3434
}
3535

3636
public static OAuthGrantType getGrantType(String oauth2GrantType) {
37-
if (Objects.equals(oauth2GrantType, REFRESH_TOKEN.getLabel())) {
37+
if (oauth2GrantType == null || Objects.equals(oauth2GrantType, REFRESH_TOKEN.getLabel())) {
3838
return REFRESH_TOKEN;
3939
} else {
4040
return CLIENT_CREDENTIALS;

src/main/java/io/cdap/plugin/http/common/http/HttpClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ public CloseableHttpClient createHttpClient(String pageUriStr) throws IOExceptio
136136
ArrayList<Header> clientHeaders = new ArrayList<>();
137137
// oAuth2
138138
if (config.getOauth2Enabled()) {
139+
AccessToken oauthAccessToken = OAuthUtil.getAccessToken(HttpClients.createDefault(), config);
139140
clientHeaders.add(new BasicHeader("Authorization",
140-
"Bearer " + OAuthUtil.getAccessToken(HttpClients.createDefault(), config)
141-
.getTokenValue()));
141+
String.format("Bearer %s", oauthAccessToken.getTokenValue())));
142142
}
143143
httpClientBuilder.setDefaultHeaders(clientHeaders);
144144
return httpClientBuilder.build();

src/main/java/io/cdap/plugin/http/common/http/OAuthUtil.java

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.collect.ImmutableSet;
2222
import com.google.gson.JsonElement;
2323
import io.cdap.plugin.http.common.BaseHttpConfig;
24+
import io.cdap.plugin.http.common.OAuthClientAuthentication;
2425
import io.cdap.plugin.http.common.OAuthGrantType;
2526
import io.cdap.plugin.http.common.pagination.page.JSONUtil;
2627
import io.cdap.plugin.http.source.common.BaseHttpSourceConfig;
@@ -30,7 +31,6 @@
3031
import org.apache.http.client.utils.URIBuilder;
3132
import org.apache.http.impl.client.CloseableHttpClient;
3233
import org.apache.http.impl.client.HttpClients;
33-
import org.apache.http.message.BasicHeader;
3434
import org.apache.http.message.BasicNameValuePair;
3535
import org.apache.http.util.EntityUtils;
3636

@@ -41,11 +41,12 @@
4141
import java.net.URI;
4242
import java.net.URISyntaxException;
4343
import java.nio.charset.StandardCharsets;
44+
import java.time.Duration;
4445
import java.time.Instant;
4546
import java.util.ArrayList;
46-
import java.util.Base64;
4747
import java.util.Date;
4848
import java.util.List;
49+
import java.util.Objects;
4950
import javax.annotation.Nullable;
5051

5152
/**
@@ -101,32 +102,30 @@ private static AccessToken getAccessTokenByClientCredentials(CloseableHttpClient
101102
String tokenUrl, String clientId, String clientSecret, String scope,
102103
String clientAuthentication) throws IOException {
103104
URI uri;
105+
HttpPost httppost;
104106
try {
105-
uri = new URIBuilder(tokenUrl).build();
107+
if (Objects.equals(clientAuthentication, OAuthClientAuthentication.BODY.getValue())) {
108+
uri = new URIBuilder(tokenUrl).build();
109+
List<BasicNameValuePair> nameValuePairs = new ArrayList<>();
110+
nameValuePairs.add(new BasicNameValuePair("scope", scope));
111+
nameValuePairs.add(
112+
new BasicNameValuePair("grant_type", OAuthGrantType.CLIENT_CREDENTIALS.getValue()));
113+
nameValuePairs.add(new BasicNameValuePair("client_id", clientId));
114+
nameValuePairs.add(new BasicNameValuePair("client_secret", clientSecret));
115+
httppost = new HttpPost(uri);
116+
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
117+
} else {
118+
uri = new URIBuilder(tokenUrl).setParameter("client_id", clientId)
119+
.setParameter("client_secret", clientSecret)
120+
.setParameter("grant_type", OAuthGrantType.CLIENT_CREDENTIALS.getValue()).build();
121+
httppost = new HttpPost(uri);
122+
}
106123
} catch (URISyntaxException e) {
107124
throw new IllegalArgumentException(
108125
"Failed to build access token URI for OAuth2 with grant type = "
109126
+ OAuthGrantType.CLIENT_CREDENTIALS.getValue(), e);
110-
} catch (NullPointerException e) {
111-
throw new IllegalArgumentException(
112-
"One or more required OAuth2 parameters (Client ID, Client Secret, "
113-
+ "or Token URL) are missing.", e);
114127
}
115128

116-
HttpPost httppost = new HttpPost(uri);
117-
List<BasicNameValuePair> nameValuePairs = new ArrayList<>();
118-
nameValuePairs.add(new BasicNameValuePair("scope", scope));
119-
nameValuePairs.add(
120-
new BasicNameValuePair("grant_type", OAuthGrantType.CLIENT_CREDENTIALS.getValue()));
121-
nameValuePairs.add(new BasicNameValuePair("client_authentication", clientAuthentication));
122-
123-
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
124-
125-
String authorizationKey =
126-
"Basic " + Base64.getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes());
127-
128-
httppost.addHeader(new BasicHeader("Authorization", authorizationKey));
129-
130129
CloseableHttpResponse response = httpclient.execute(httppost);
131130
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
132131

@@ -139,9 +138,12 @@ private static AccessToken getAccessTokenByClientCredentials(CloseableHttpClient
139138
JsonElement expiresInElement = JSONUtil.toJsonObject(responseString).get("expires_in");
140139
Date expiresInDate = null;
141140
if (expiresInElement != null) {
142-
long expiresAtMilliseconds =
143-
System.currentTimeMillis() + (long) (expiresInElement.getAsInt() * 1000) - 60000L;
144-
expiresInDate = new Date(expiresAtMilliseconds);
141+
Instant now = Instant.now();
142+
Duration expiresIn = Duration.ofSeconds(expiresInElement.getAsInt());
143+
Duration buffer = Duration.ofMinutes(1);
144+
145+
Instant expiresAt = now.plus(expiresIn).minus(buffer);
146+
expiresInDate = Date.from(expiresAt);
145147
}
146148

147149
return new AccessToken(accessTokenElement.getAsString(), expiresInDate);
@@ -188,10 +190,6 @@ public static AccessToken getAccessTokenByRefreshToken(CloseableHttpClient httpc
188190
.build();
189191
} catch (URISyntaxException e) {
190192
throw new IllegalArgumentException("Failed to build token URI for OAuth2", e);
191-
} catch (NullPointerException e) {
192-
throw new IllegalArgumentException(
193-
"One or more required OAuth2 parameters (Auth URL, Token URL, Client ID, Client Secret, "
194-
+ "or Refresh Token) are missing.", e);
195193
}
196194

197195
HttpPost httppost = new HttpPost(uri);
@@ -206,9 +204,12 @@ public static AccessToken getAccessTokenByRefreshToken(CloseableHttpClient httpc
206204
JsonElement expiresInElement = JSONUtil.toJsonObject(responseString).get("expires_in");
207205
Date expiresInDate = null;
208206
if (expiresInElement != null) {
209-
long expiresAtMilliseconds = System.currentTimeMillis()
210-
+ (long) (expiresInElement.getAsInt() * 1000) - 60000L;
211-
expiresInDate = new Date(expiresAtMilliseconds);
207+
Instant now = Instant.now();
208+
Duration expiresIn = Duration.ofSeconds(expiresInElement.getAsInt());
209+
Duration buffer = Duration.ofMinutes(1);
210+
211+
Instant expiresAt = now.plus(expiresIn).minus(buffer);
212+
expiresInDate = Date.from(expiresAt);
212213
}
213214

214215
return new AccessToken(accessTokenElement.getAsString(), expiresInDate);

0 commit comments

Comments
 (0)