added checks to avoid NPEs

This commit is contained in:
2019-04-29 08:13:27 +02:00
parent 4f3c3d4673
commit 59969ccef4

View File

@ -153,14 +153,17 @@ public class ApiKeyService implements Serializable {
@Transactional
@Lock(LockType.WRITE)
public ApiKeyObject createNewApiKey(String userName) throws ApiKeyException {
if (userName == null) {
throw new ApiKeyException("Username must not be null");
}
return createNewApiKey(userName, expirationInMinutes);
}
@Transactional
@Lock(LockType.WRITE)
public ApiKeyObject createNewApiKey(String userName, short expirationInMinutes) throws ApiKeyException {
if ((password == null || issuer == null)) {
LOGGER.error("password or issuer not set in, please validate configuration");
if ((password == null || issuer == null) || (userName == null)) {
LOGGER.error("password, issuer or username not set in, please validate configuration");
}
Date now = DateUtil.getCurrentTimeInUTC();
ZonedDateTime issuedOn = ZonedDateTime.ofInstant(now.toInstant(), ZoneId.of("UTC"));
@ -179,9 +182,16 @@ public class ApiKeyService implements Serializable {
}
public ApiKeyObject getApiKeyObject(ApiKeyEntity apiKey) throws ApiKeyException {
if (apiKey == null) {
throw new ApiKeyException("ApiKey must not be null");
}
ZonedDateTime issuedOn = ZonedDateTime.ofInstant(apiKey.getIssuedOn().toInstant(), ZoneId.of("UTC"));
ZonedDateTime expiresOn = issuedOn.plusMinutes(expirationInMinutes);
String userName = apiKey.getAccount().getUsername();
AccountEntity account = apiKey.getAccount();
if (account == null) {
throw new ApiKeyException("Account of apiKey must not be null");
}
String userName = account.getUsername();
try {
String jwtString = JWTEncoder.encode(password, issuer, issuedOn, userName, apiKey.getApiKey(), apiKey.getExpiration());
em.persist(apiKey);