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

Commit 893f394

Browse files
Resolving potential nullpointer exceptions
1 parent dee891a commit 893f394

3 files changed

Lines changed: 43 additions & 25 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public TppPingException(int status, String reason) {
6464
}
6565
}
6666

67+
public static class NullAuthenticationException extends ConnectorException {
68+
69+
private static final long serialVersionUID = 1L;
70+
71+
public NullAuthenticationException() {
72+
super("Authentication object is null");
73+
}
74+
}
75+
6776
public static class MissingCredentialsException extends ConnectorException {
6877

6978
private static final long serialVersionUID = 1L;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ private static String[] retrieveUsernamesFromTPPContacts(String policyName, TppA
330330
if (contactResponse != null && contactResponse.error() != null){
331331
throw new ConnectorException.TppContactException(policyName, contactResponse.error());
332332
}
333-
if (contactResponse.values() != null) {
333+
if (contactResponse != null && contactResponse.values() != null) {
334334
Object[] contacts = contactResponse.values();
335335
for (Object prefixedUniversal : contacts) {
336336
try{

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

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.venafi.vcert.sdk.connectors.ConnectorException.FailedToRevokeTokenException;
77
import com.venafi.vcert.sdk.connectors.ConnectorException.MissingAccessTokenException;
88
import com.venafi.vcert.sdk.connectors.ConnectorException.MissingRefreshTokenException;
9+
import com.venafi.vcert.sdk.connectors.ConnectorException.NullAuthenticationException;
910
import com.venafi.vcert.sdk.connectors.TokenConnector;
1011
import com.venafi.vcert.sdk.endpoint.Authentication;
1112
import com.venafi.vcert.sdk.endpoint.ConnectorType;
@@ -72,13 +73,17 @@ public void authorize(Authentication credentials) throws VCertException {
7273
}
7374

7475
private boolean isEmptyTokens( Authentication credentials ){
75-
return isEmptyAccessToken(credentials) && isBlank(credentials.refreshToken());
76+
return isEmptyAccessToken(credentials) && isEmptyRefreshToken(credentials);
7677
}
7778

7879
private boolean isEmptyAccessToken(Authentication credentials){
7980
return credentials == null || isBlank(credentials.accessToken());
8081
}
8182

83+
private boolean isEmptyRefreshToken(Authentication credentials){
84+
return credentials == null || isBlank(credentials.refreshToken());
85+
}
86+
8287
private void verifyAccessToken(Authentication credentials) throws VCertException {
8388
if(!isBlank(credentials.accessToken())) {
8489

@@ -121,31 +126,35 @@ public TokenInfo getTokenInfo() throws VCertException {
121126
@Override
122127
public TokenInfo getAccessToken(Authentication auth) throws VCertException {
123128

124-
Authentication authTemp = null;
125-
126129
if (auth != null) {
127130

128-
//creating a temp Authentication object based on the one passed as argument
129-
// in order to avoid to modify that original given it's needed that
130-
// the Authentication object to be passed to the authenticate() method needs
131-
// that the accessToken and refreshToken doesn't set
132-
authTemp = Authentication.builder()
133-
.user(auth.user())
134-
.password(auth.password())
135-
.clientId(auth.clientId())
136-
.scope(auth.scope())
137-
.state(auth.state())
138-
.redirectUri(auth.redirectUri())
139-
.build();
140-
}
141-
142-
authenticate(authTemp);
143-
144-
//setting the auth object as the credentials and setting into it the accessToken
145-
//and refreshToken hold by TokenInfo
146-
setTokenCredentials(auth);
147-
148-
return getTokenInfo();
131+
Authentication authTemp = null;
132+
133+
if (auth != null) {
134+
135+
//creating a temp Authentication object based on the one passed as argument
136+
// in order to avoid to modify that original given it's needed that
137+
// the Authentication object to be passed to the authenticate() method needs
138+
// that the accessToken and refreshToken doesn't set
139+
authTemp = Authentication.builder()
140+
.user(auth.user())
141+
.password(auth.password())
142+
.clientId(auth.clientId())
143+
.scope(auth.scope())
144+
.state(auth.state())
145+
.redirectUri(auth.redirectUri())
146+
.build();
147+
}
148+
149+
authenticate(authTemp);
150+
151+
//setting the auth object as the credentials and setting into it the accessToken
152+
//and refreshToken hold by TokenInfo
153+
setTokenCredentials(auth);
154+
155+
return getTokenInfo();
156+
} else
157+
throw new NullAuthenticationException();
149158
}
150159

151160
@Override

0 commit comments

Comments
 (0)