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

Commit ea9c2ea

Browse files
committed
Changed the way the token is passed and used by the client. Now the token will be stored and reused by the client instead of being required for each method call.
- Changed relevant test cases to properly test the new changes.
1 parent 77b4b69 commit ea9c2ea

5 files changed

Lines changed: 124 additions & 17 deletions

File tree

src/main/java/com/venafi/vcert/sdk/VCertTknClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ public TokenInfo getAccessToken(Authentication auth) throws VCertException{
103103
}
104104
}
105105

106+
@Override
107+
public TokenInfo getAccessToken() throws VCertException{
108+
try {
109+
return connector.getAccessToken();
110+
} catch (FeignException e) {
111+
throw VCertException.fromFeignException(e);
112+
} catch (Exception e) {
113+
throw new VCertException("Unexpected exception", e);
114+
}
115+
}
116+
106117
@Override
107118
public TokenInfo refreshAccessToken(String applicationId) throws VCertException{
108119
return connector.refreshAccessToken(applicationId);

src/main/java/com/venafi/vcert/sdk/connectors/TokenConnector.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.venafi.vcert.sdk.connectors;
22

3+
import com.venafi.vcert.sdk.Config;
34
import com.venafi.vcert.sdk.VCertException;
45
import com.venafi.vcert.sdk.certificate.*;
56
import com.venafi.vcert.sdk.connectors.tpp.TokenInfo;
@@ -52,6 +53,14 @@ public interface TokenConnector {
5253
*/
5354
TokenInfo getAccessToken (Authentication auth ) throws VCertException;
5455

56+
/**
57+
* returns a new access token. This method uses the {@link Authentication} object passed earlier
58+
* with the {@link Config} object.
59+
* @return the new token.
60+
* @throws VCertException throws this exception when authentication info is null.
61+
*/
62+
TokenInfo getAccessToken () throws VCertException;
63+
5564
/**
5665
* this is for refreshing a token.
5766
* @param applicationId the application id.

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.venafi.vcert.sdk.connectors.tpp;
22

3+
import com.google.common.annotations.VisibleForTesting;
34
import com.venafi.vcert.sdk.VCertException;
45
import com.venafi.vcert.sdk.certificate.*;
56
import com.venafi.vcert.sdk.connectors.*;
@@ -26,7 +27,9 @@
2627
public class TppTokenConnector extends AbstractTppConnector implements TokenConnector {
2728

2829
public TppTokenConnector(Tpp tpp){ super(tpp); }
30+
2931
@Setter
32+
@VisibleForTesting
3033
private Authentication credentials;
3134

3235
@Override
@@ -77,8 +80,9 @@ private Response doPing() throws VCertException{
7780

7881
@Override
7982
public TokenInfo getAccessToken(Authentication auth) throws VCertException {
80-
81-
VCertException.throwIfNull( auth, MISSING_CREDENTIALS_MESSAGE );
83+
if(isEmptyCredentials(auth)) {
84+
throw new VCertException(MISSING_CREDENTIALS_MESSAGE);
85+
}
8286

8387
AuthorizeTokenRequest info = new AuthorizeTokenRequest( auth.user(), auth.password(), auth.clientId(), auth.scope(), auth.state(), auth.redirectUri() );
8488
AuthorizeTokenResponse response = tpp.authorizeToken( info );
@@ -91,6 +95,11 @@ public TokenInfo getAccessToken(Authentication auth) throws VCertException {
9195
return accessTokenInfo;
9296
}
9397

98+
@Override
99+
public TokenInfo getAccessToken() throws VCertException {
100+
return getAccessToken(credentials);
101+
}
102+
94103
@Override
95104
public TokenInfo refreshAccessToken(String clientId ) throws VCertException{
96105
if(isBlank(credentials.refreshToken())){
@@ -116,6 +125,10 @@ public TokenInfo refreshAccessToken(String clientId ) throws VCertException{
116125
@Override
117126
public int revokeAccessToken() throws VCertException {
118127

128+
if(isEmptyToken()){
129+
throw new VCertException(MISSING_ACCESS_TOKEN_MESSAGE);
130+
}
131+
119132
String requestHeader = getAuthHeaderValue();//"Bearer "+accessToken;
120133

121134
Response response = tpp.revokeToken( requestHeader );
@@ -438,4 +451,28 @@ private ImportResponse doImportCertificate(ImportRequest request) throws VCertEx
438451
public Policy readPolicyConfiguration(String zone) throws VCertException {
439452
throw new UnsupportedOperationException("Method not yet implemented");
440453
}
454+
455+
private boolean isEmptyCredentials(Authentication credentials){
456+
if(credentials == null){
457+
return true;
458+
}
459+
460+
if(credentials.user() == null || credentials.user().isEmpty()){
461+
return true;
462+
}
463+
464+
if(credentials.password() == null || credentials.password().isEmpty()){
465+
return true;
466+
}
467+
468+
return false;
469+
}
470+
471+
private boolean isEmptyToken(){
472+
if(credentials == null || isBlank(credentials.accessToken())){
473+
return true;
474+
}
475+
476+
return false;
477+
}
441478
}

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

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.bouncycastle.openssl.PEMParser;
1212
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
1313
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.DisplayName;
1415
import org.junit.jupiter.api.Test;
1516

1617
import java.io.IOException;
@@ -39,19 +40,54 @@ class TppTokenConnectorAT {
3940
@BeforeEach
4041
void authenticate() throws VCertException {
4142
Security.addProvider(new BouncyCastleProvider());
43+
//Executes only once to ensure the same token is used across the tests
44+
if(TppTokenConnectorAT.info == null){
45+
Authentication authentication = Authentication.builder()
46+
.user(System.getenv("TPPUSER"))
47+
.password(System.getenv("TPPPASSWORD"))
48+
.scope("certificate:manage,revoke,discover")
49+
.build();
50+
51+
TokenInfo info = classUnderTest.getAccessToken(authentication);
52+
53+
assertThat(info).isNotNull();
54+
assertThat(info.accessToken()).isNotNull();
55+
assertThat(info.refreshToken()).isNotNull();
56+
57+
TppTokenConnectorAT.info = info;
58+
}
59+
}
60+
61+
@Test
62+
@DisplayName("Authenticate with credentials from Config object")
63+
void authenticateNoParameter() throws VCertException{
4264
Authentication authentication = Authentication.builder()
43-
.user(System.getenv("TPPUSER"))
44-
.password(System.getenv("TPPPASSWORD"))
45-
.scope("certificate:manage,revoke,discover")
46-
.build();
65+
.user(System.getenv("TPPUSER"))
66+
.password(System.getenv("TPPPASSWORD"))
67+
.scope("certificate:manage,revoke,discover")
68+
.build();
4769

48-
TokenInfo info = classUnderTest.getAccessToken(authentication);
70+
classUnderTest.credentials(authentication);
71+
72+
TokenInfo info = classUnderTest.getAccessToken();
4973

5074
assertThat(info).isNotNull();
5175
assertThat(info.accessToken()).isNotNull();
5276
assertThat(info.refreshToken()).isNotNull();
77+
}
5378

54-
TppTokenConnectorAT.info = info;
79+
@Test
80+
@DisplayName("Authenticate with invalid credentials")
81+
void authenticateInvalid(){
82+
Authentication authentication = Authentication.builder()
83+
.user("sample")
84+
.password("password")
85+
.scope("certificate:manage,revoke,discover")
86+
.build();
87+
88+
classUnderTest.credentials(authentication);
89+
90+
assertThrows(VCertException.class, () ->classUnderTest.getAccessToken());
5591
}
5692

5793
@Test
@@ -286,21 +322,34 @@ void refreshToken() throws VCertException{
286322

287323
assertThat(refreshInfo).isNotNull();
288324
assertThat(refreshInfo.accessToken()).isNotEqualTo(info.accessToken());
325+
assertThat(refreshInfo.refreshToken()).isNotEqualTo(info.refreshToken());
289326
}
290327

291-
// @Test
292-
// void refreshTokenInvalid() throws VCertException{
293-
// assertThrows(VCertException.class, () -> classUnderTest.refreshAccessToken("1234-1234-12345-123", "vcert-sdk"));
294-
// }
328+
@Test
329+
void refreshTokenInvalid() throws VCertException{
330+
Authentication invalidCredentials = Authentication.builder()
331+
.accessToken("abcde==")
332+
.refreshToken("1234-1234-12345-123")
333+
.build();
334+
classUnderTest.credentials(invalidCredentials);
335+
336+
assertThrows(VCertException.class, () -> classUnderTest.refreshAccessToken("vcert-sdk"));
337+
}
295338

296339
@Test
297340
void revokeToken() throws VCertException{
298341
int status = classUnderTest.revokeAccessToken();
299342
assertThat(status).isEqualTo(200);
300343
}
301344

302-
// @Test
303-
// void revokeTokenInvalid() throws VCertException{
304-
// assertThrows(VCertException.class, () ->classUnderTest.revokeAccessToken());
305-
// }
345+
@Test
346+
void revokeTokenInvalid() throws VCertException{
347+
Authentication invalidCredentials = Authentication.builder()
348+
.accessToken("abcde==")
349+
.refreshToken("1234-1234-12345-123")
350+
.build();
351+
classUnderTest.credentials(invalidCredentials);
352+
353+
assertThrows(VCertException.class, () ->classUnderTest.revokeAccessToken());
354+
}
306355
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ void setup() throws VCertException {
3434
.user("user")
3535
.password("pass")
3636
.build();
37-
info = classUnderTest.getAccessToken(auth);
37+
classUnderTest.credentials(auth);
38+
info = classUnderTest.getAccessToken();
3839
}
3940

4041
@Test

0 commit comments

Comments
 (0)