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

Commit 9e0c07e

Browse files
Merge pull request #103 from Venafi/authentication-standardization
Standardize the authentication process for different VCert clients
2 parents a1f1284 + 14c4ccb commit 9e0c07e

17 files changed

Lines changed: 1191 additions & 300 deletions

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,43 @@ shows snippets for VCert-Java v0.6.2.
4444

4545
## Usage
4646

47+
4748
Instantiate a client for Trust Protection Platform using token authentication with an existing
4849
access token:
4950

5051
```java
52+
//Create an Authentication object with the access token
5153
final Authentication auth = Authentication.builder()
5254
.accessToken("9PQwQeiTLhcB8/W3/z2Lbw==")
5355
.build();
5456

57+
//Create a Config object setting the Authentication object
5558
final Config config = Config.builder()
5659
.connectorType(ConnectorType.TPP_TOKEN)
5760
.baseUrl("https://tpp.venafi.example")
5861
.credentials(auth)
5962
.build();
6063

64+
//Create the client with the Config object. The client will be authenticated
6165
final VCertTknClient client = new VCertTknClient(config);
6266
```
6367

6468
Or instantiate a client for Venafi Cloud:
6569

6670
```java
71+
//Create an Authentication object with the API Key
6772
final Authentication auth = Authentication.builder()
6873
.apiKey("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")
6974
.build();
7075

76+
//Create a Config object setting the Authentication object
7177
final Config config = Config.builder()
7278
.connectorType(ConnectorType.CLOUD)
79+
.credentials(auth)
7380
.build();
7481

82+
//Create the client with the Config object. The client will be authenticated
7583
final VCertClient client = new VCertClient(config);
76-
client.authenticate(auth);
7784
```
7885

7986
Then use your client to request certificates:
@@ -187,21 +194,54 @@ _without_ an existing token by providing a username/password. Such a token is g
187194
short-term or temporary use and as such should be revoked upon completion of your tasks:
188195

189196
```java
197+
//Create an Authentication object with the user and password
190198
final Authentication auth = Authentication.builder()
191199
.user("local:apiuser")
192200
.password("password")
193201
.build();
194202

203+
//Create a Config object
195204
final Config config = Config.builder()
196205
.connectorType(ConnectorType.TPP_TOKEN)
197206
.baseUrl("https://tpp.venafi.example")
198207
.build();
199-
208+
209+
//Create the client with the Config object. The client is not authenticated yet
200210
final VCertTknClient client = new VCertTknClient(config);
211+
212+
//Get the access token. It will cause the client's authentication
201213
client.getAccessToken(auth);
202214

203215
///// REQUEST, RENEW, AND/OR REVOKE CERTIFICATES...
204216

217+
//Revoke the access token
218+
client.revokeAccessToken();
219+
```
220+
221+
Or you can try the authentication in constructor way:
222+
223+
```java
224+
//Create an Authentication object with the user and password
225+
final Authentication auth = Authentication.builder()
226+
.user("local:apiuser")
227+
.password("password")
228+
.build();
229+
230+
//Create a Config object setting the Authentication object
231+
final Config config = Config.builder()
232+
.connectorType(ConnectorType.TPP_TOKEN)
233+
.baseUrl("https://tpp.venafi.example")
234+
.credentials(auth)
235+
.build();
236+
237+
//Create the client with the Config object. The client will be authenticated
238+
//Internally the access token will be gotten and accessible
239+
//via the getTokenInfo() method.
240+
final VCertTknClient client = new VCertTknClient(config);
241+
242+
///// REQUEST, RENEW, AND/OR REVOKE CERTIFICATES...
243+
244+
//Revoke the access token
205245
client.revokeAccessToken();
206246
```
207247

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
<junit.version>5.3.1</junit.version>
7070
<mockito.version>2.25.1</mockito.version>
7171
<wiremock.version>2.22.0</wiremock.version>
72-
<assertj.version>3.12.2</assertj.version>
72+
<assertj.version>3.22.0</assertj.version>
7373
<ini4j.version>0.5.4</ini4j.version>
7474
<commonslang3.version>3.11</commonslang3.version>
7575
<jarName>${project.artifactId}-${project.version}</jarName>

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public VCertClient(Config config) throws VCertException {
3535
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
3636

3737
this.connector = createConnector(config);
38+
39+
if(config.credentials() != null) {
40+
this.connector.authenticate(config.credentials());
41+
}
3842

3943
connector.setVendorAndProductName(isBlank(config.appInfo()) ? VCertConstants.DEFAULT_VENDOR_AND_PRODUCT_NAME :
4044
config.appInfo());
@@ -64,6 +68,11 @@ protected Connector createConnector(Config config) throws VCertException {
6468
VCertClient(Connector connector) {
6569
this.connector = connector;
6670
}
71+
72+
@Override
73+
public Authentication getCredentials() {
74+
return connector.getCredentials();
75+
}
6776

6877
/**
6978
* {@inheritDoc}
@@ -124,13 +133,33 @@ public void ping() throws VCertException {
124133
* {@inheritDoc}
125134
*/
126135
@Override
127-
public void authenticate(Authentication auth) throws VCertException {
136+
public void authenticate(Authentication credentials) throws VCertException {
128137
try {
129-
connector.authenticate(auth);
138+
connector.authenticate(credentials);
130139
} catch (FeignException e) {
131140
throw VCertException.fromFeignException(e);
132141
}
133142
}
143+
144+
/**
145+
* {@inheritDoc}
146+
*/
147+
@Override
148+
public boolean isEmptyCredentials(Authentication credentials) {
149+
return connector.isEmptyCredentials(credentials);
150+
}
151+
152+
/**
153+
* {@inheritDoc}
154+
*/
155+
@Override
156+
public void authorize(Authentication credentials) throws VCertException {
157+
try {
158+
connector.authorize(credentials);
159+
} catch (FeignException e) {
160+
throw VCertException.fromFeignException(e);
161+
}
162+
}
134163

135164
/**
136165
* {@inheritDoc}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ protected Connector createConnector(Config config) throws VCertException {
2323
switch (config.connectorType()) {
2424
case TPP_TOKEN:{
2525
connector = new TppTokenConnector(Tpp.connect(config));
26-
((TppTokenConnector) connector).credentials(config.credentials());
2726
break;
2827
}
2928
default:

0 commit comments

Comments
 (0)