Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 5c68379

Browse files
committed
Added changes suggested in PR. Specially, the getAccessToken() and getRefreshToken() will now return an empty object with an error message instead of throwing an exception
1 parent 00a1719 commit 5c68379

6 files changed

Lines changed: 136 additions & 23 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.venafi.vcert.sdk.example;
2+
3+
import java.security.KeyManagementException;
4+
import java.security.NoSuchAlgorithmException;
5+
import java.security.cert.CertificateEncodingException;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
9+
import com.venafi.vcert.sdk.Config;
10+
import com.venafi.vcert.sdk.VCertClient;
11+
import com.venafi.vcert.sdk.VCertException;
12+
import com.venafi.vcert.sdk.VCertTknClient;
13+
import com.venafi.vcert.sdk.certificate.CertificateRequest;
14+
import com.venafi.vcert.sdk.certificate.KeyType;
15+
import com.venafi.vcert.sdk.certificate.PEMCollection;
16+
import com.venafi.vcert.sdk.connectors.ZoneConfiguration;
17+
import com.venafi.vcert.sdk.connectors.tpp.TokenInfo;
18+
import com.venafi.vcert.sdk.endpoint.Authentication;
19+
import com.venafi.vcert.sdk.endpoint.ConnectorType;
20+
21+
public class TppTokenClient {
22+
23+
public static void main(String[] args) throws VCertException, CertificateEncodingException,
24+
NoSuchAlgorithmException, KeyManagementException {
25+
26+
String url = System.getenv("TPP_TOKEN_URL");
27+
String zone = System.getenv("TPPZONE");
28+
String appInfo = System.getenv("PRODUCT");
29+
String tpp_user = System.getenv("TPPUSER");
30+
String tpp_passwd = System.getenv("TPPPASSWORD");
31+
32+
if (tpp_user == null)
33+
tpp_user = "local:admin";
34+
if (tpp_passwd == null)
35+
tpp_passwd = "password";
36+
if (url == null)
37+
url = "https://tpp.venafi.example/vedsdk";
38+
if (zone == null)
39+
zone = "Certificates\\vcert\\";
40+
if (appInfo == null)
41+
appInfo = "CompanyName AppName";
42+
43+
// Configuration
44+
Config config = Config.builder().connectorType(ConnectorType.TPP_TOKEN).baseUrl(url).appInfo(appInfo)
45+
// To use proxy uncomment the lines below
46+
// .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)))
47+
// .proxyUser("myUser")
48+
// .proxyPassword("myPasscode")
49+
.build();
50+
51+
Authentication auth = Authentication.builder().user(tpp_user).password(tpp_passwd).build();
52+
53+
VCertTknClient client = new VCertTknClient(config);
54+
TokenInfo tknInfo = client.getAccessToken(auth);
55+
56+
ZoneConfiguration zoneConfiguration = client.readZoneConfiguration(zone);
57+
58+
// Generate a certificate
59+
CertificateRequest certificateRequest = new CertificateRequest()
60+
.subject(new CertificateRequest.PKIXName().commonName("vcert-java.venafi.example")
61+
.organization(Collections.singletonList("Venafi, Inc."))
62+
.organizationalUnit(Arrays.asList("Product Management"))
63+
.country(Collections.singletonList("US"))
64+
.locality(Collections.singletonList("Salt Lake City"))
65+
.province(Collections.singletonList("Utah")))
66+
.keyType(KeyType.RSA).keyLength(2048);
67+
68+
certificateRequest = client.generateRequest(zoneConfiguration, certificateRequest);
69+
70+
// Submit the certificate request
71+
client.requestCertificate(certificateRequest, zoneConfiguration);
72+
73+
// Retrieve PEM collection from Venafi
74+
PEMCollection pemCollection = client.retrieveCertificate(certificateRequest);
75+
System.out.println(pemCollection.certificate());
76+
}
77+
}

src/main/java/com/venafi/vcert/sdk/connectors/tpp/TokenInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public class TokenInfo {
1414
private String scope;
1515
private String identity;
1616
private long refreshUntil;
17-
17+
private boolean authorized;
18+
private String errorMessage;
1819
}

src/main/java/com/venafi/vcert/sdk/connectors/tpp/Tpp.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ CertificateRetrieveResponse certificateRetrieve(
6666
AuthorizeTokenResponse authorizeToken(AbstractTppConnector.AuthorizeTokenRequest authorizeRequest);
6767

6868
@RequestLine("POST /vedauth/authorize/token")
69-
@Headers("Content-Type: application/json") RefreshTokenResponse refreshToken(AbstractTppConnector.RefreshTokenRequest request);
69+
@Headers("Content-Type: application/json")
70+
RefreshTokenResponse refreshToken(AbstractTppConnector.RefreshTokenRequest request);
7071

7172
@RequestLine("GET /vedauth/revoke/token")
7273
@Headers("Authorization: {token}")

src/main/java/com/venafi/vcert/sdk/connectors/tpp/TppTokenConnector.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.venafi.vcert.sdk.endpoint.ConnectorType;
99
import com.venafi.vcert.sdk.utils.Is;
1010
import feign.FeignException;
11+
import feign.FeignException.Unauthorized;
1112
import feign.Response;
1213
import lombok.Setter;
1314

@@ -84,14 +85,22 @@ public TokenInfo getAccessToken(Authentication auth) throws VCertException {
8485
throw new VCertException(MISSING_CREDENTIALS_MESSAGE);
8586
}
8687

87-
AuthorizeTokenRequest info = new AuthorizeTokenRequest( auth.user(), auth.password(), auth.clientId(), auth.scope(), auth.state(), auth.redirectUri() );
88-
AuthorizeTokenResponse response = tpp.authorizeToken( info );
89-
TokenInfo accessTokenInfo = new TokenInfo(response.accessToken(), response.refreshToken(), response.expire(), response.tokenType(), response.scope(), response.identity(), response.refreshUntil());
90-
91-
this.credentials = auth;
92-
this.credentials.accessToken(accessTokenInfo.accessToken());
93-
this.credentials.refreshToken(accessTokenInfo.refreshToken());
94-
88+
TokenInfo accessTokenInfo;
89+
try {
90+
AuthorizeTokenRequest authRequest =
91+
new AuthorizeTokenRequest(auth.user(), auth.password(), auth.clientId(), auth.scope(), auth.state(),
92+
auth.redirectUri());
93+
AuthorizeTokenResponse response = tpp.authorizeToken(authRequest);
94+
accessTokenInfo = new TokenInfo(response.accessToken(), response.refreshToken(), response.expire(),
95+
response.tokenType(), response.scope(), response.identity(), response.refreshUntil(), true, null);
96+
97+
this.credentials = auth;
98+
this.credentials.accessToken(accessTokenInfo.accessToken());
99+
this.credentials.refreshToken(accessTokenInfo.refreshToken());
100+
} catch(Unauthorized e){
101+
accessTokenInfo = new TokenInfo(null, null, -1, null, null,
102+
null, -1, false, e.getMessage());
103+
}
95104
return accessTokenInfo;
96105
}
97106

@@ -105,21 +114,23 @@ public TokenInfo refreshAccessToken(String clientId ) throws VCertException{
105114
if(isBlank(credentials.refreshToken())){
106115
throw new VCertException(MISSING_REFRESH_TOKEN_MESSAGE);
107116
}
117+
TokenInfo tokenInfo;
108118
try {
109119
RefreshTokenRequest request = new RefreshTokenRequest(credentials.refreshToken(), clientId);
110120
RefreshTokenResponse response = tpp.refreshToken( request );
111121

112-
TokenInfo tokenInfo = new TokenInfo(response.accessToken(), response.refreshToken(), response.expire(),
113-
response.tokenType(), response.scope(), "",
114-
response.refreshUntil());
122+
tokenInfo = new TokenInfo(response.accessToken(), response.refreshToken(), response.expire(),
123+
response.tokenType(), response.scope(), "", response.refreshUntil(), true, null);
115124

116125
this.credentials.accessToken(tokenInfo.accessToken());
117126
this.credentials.refreshToken(tokenInfo.refreshToken());
118127

119128
return tokenInfo;
120129
}catch (FeignException.BadRequest e){
121-
throw new VCertException(e.getMessage());
130+
tokenInfo = new TokenInfo(null, null, -1, null, null,
131+
null, -1, false, e.getMessage());
122132
}
133+
return tokenInfo;
123134
}
124135

125136
@Override

src/test/java/com/venafi/vcert/sdk/connectors/tpp/TppTokenConnectorAT.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.venafi.vcert.sdk.connectors.ZoneConfiguration;
77
import com.venafi.vcert.sdk.endpoint.Authentication;
88
import feign.FeignException;
9-
import feign.FeignException.Unauthorized;
109

1110
import org.apache.commons.codec.digest.DigestUtils;
1211
import org.bouncycastle.jce.provider.BouncyCastleProvider;
@@ -53,6 +52,8 @@ void authenticate() throws VCertException {
5352
TokenInfo info = classUnderTest.getAccessToken(authentication);
5453

5554
assertThat(info).isNotNull();
55+
assertThat(info.authorized()).isTrue();
56+
assertThat(info.errorMessage()).isNull();
5657
assertThat(info.accessToken()).isNotNull();
5758
assertThat(info.refreshToken()).isNotNull();
5859

@@ -66,13 +67,15 @@ void authenticateNoParameter() throws VCertException{
6667
TokenInfo localInfo = classUnderTest.getAccessToken();
6768

6869
assertThat(localInfo).isNotNull();
70+
assertThat(localInfo.authorized()).isTrue();
71+
assertThat(localInfo.errorMessage()).isNull();
6972
assertThat(localInfo.accessToken()).isNotNull();
7073
assertThat(localInfo.refreshToken()).isNotNull();
7174
}
7275

7376
@Test
7477
@DisplayName("Authenticate with invalid credentials")
75-
void authenticateInvalid(){
78+
void authenticateInvalid() throws VCertException{
7679
Authentication authentication = Authentication.builder()
7780
.user("sample")
7881
.password("password")
@@ -81,7 +84,11 @@ void authenticateInvalid(){
8184

8285
classUnderTest.credentials(authentication);
8386

84-
assertThrows(Unauthorized.class, () ->classUnderTest.getAccessToken());
87+
TokenInfo info = classUnderTest.getAccessToken();
88+
assertThat(info).isNotNull();
89+
assertThat(info.authorized()).isFalse();
90+
assertThat(info.errorMessage()).isNotNull();
91+
8592

8693
// After setting invalid credentials to TPP, setting variable <info> to null
8794
// will allow for new token to be authorized
@@ -319,7 +326,11 @@ void refreshToken() throws VCertException{
319326
TokenInfo refreshInfo = classUnderTest.refreshAccessToken("vcert-sdk");
320327

321328
assertThat(refreshInfo).isNotNull();
329+
assertThat(refreshInfo.authorized()).isTrue();
330+
assertThat(refreshInfo.errorMessage()).isNull();
331+
assertThat(refreshInfo.accessToken()).isNotNull();
322332
assertThat(refreshInfo.accessToken()).isNotEqualTo(info.accessToken());
333+
assertThat(refreshInfo.refreshToken()).isNotNull();
323334
assertThat(refreshInfo.refreshToken()).isNotEqualTo(info.refreshToken());
324335
}
325336

@@ -331,7 +342,11 @@ void refreshTokenInvalid() throws VCertException{
331342
.build();
332343
classUnderTest.credentials(invalidCredentials);
333344

334-
assertThrows(VCertException.class, () -> classUnderTest.refreshAccessToken("vcert-sdk"));
345+
TokenInfo info = classUnderTest.refreshAccessToken("vcert-sdk");
346+
347+
assertThat(info).isNotNull();
348+
assertThat(info.authorized()).isFalse();
349+
assertThat(info.errorMessage()).isNotNull();
335350

336351
// After setting invalid credentials to TPP, setting variable <info> to null
337352
// will allow for new token to be authorized

src/test/java/com/venafi/vcert/sdk/connectors/tpp/TppTokenConnectorTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ void setUp() throws VCertException {
6464

6565
Authentication authentication = Authentication.builder().user("user").password("pass").build();
6666
info = classUnderTest.getAccessToken(authentication);
67+
assertThat(info).isNotNull();
68+
assertThat(info.authorized()).isTrue();
69+
assertThat(info.errorMessage()).isNull();
6770
}
6871

6972
@Test
@@ -212,6 +215,8 @@ void refreshAccessToken() throws VCertException{
212215

213216
TokenInfo newInfo = classUnderTest.refreshAccessToken("vcert-sdk");
214217
assertNotNull(newInfo);
218+
assertThat(newInfo.authorized()).isTrue();
219+
assertThat(newInfo.errorMessage()).isNull();
215220
assertNotNull(newInfo.accessToken());
216221
assertNotNull(newInfo.refreshToken());
217222

@@ -221,16 +226,19 @@ void refreshAccessToken() throws VCertException{
221226

222227
@Test
223228
@DisplayName("Refresh invalid access token")
224-
void refreshAccessTokenInvalid(){
229+
void refreshAccessTokenInvalid() throws VCertException{
225230
final Request request = Request.create(Request.HttpMethod.POST, "", new HashMap<String, Collection<String>>(), null);
226231

227232
when(tpp.refreshToken(any(AbstractTppConnector.RefreshTokenRequest.class))).thenThrow(new FeignException.BadRequest("400 Grant has been revoked, has expired, or the refresh token is invalid", request, null));
228233

229-
final Throwable throwable =
230-
assertThrows(VCertException.class, () -> classUnderTest.refreshAccessToken("vcert-sdk"));
231-
logger.info("VCertException = %s", throwable.getMessage());
234+
TokenInfo info = classUnderTest.refreshAccessToken("vcert-sdk");
235+
assertThat(info).isNotNull();
236+
assertThat(info.authorized()).isFalse();
237+
assertThat(info.errorMessage()).isNotNull();
238+
239+
logger.info("VCertException = %s", info.errorMessage());
232240

233-
assertThat(throwable.getMessage()).contains("Grant has been revoked, has expired, or the refresh token is invalid");
241+
assertThat(info.errorMessage()).contains("Grant has been revoked, has expired, or the refresh token is invalid");
234242
}
235243

236244
@Test

0 commit comments

Comments
 (0)