added checks to avoid NPEs
This commit is contained in:
@ -153,14 +153,17 @@ public class ApiKeyService implements Serializable {
|
|||||||
@Transactional
|
@Transactional
|
||||||
@Lock(LockType.WRITE)
|
@Lock(LockType.WRITE)
|
||||||
public ApiKeyObject createNewApiKey(String userName) throws ApiKeyException {
|
public ApiKeyObject createNewApiKey(String userName) throws ApiKeyException {
|
||||||
|
if (userName == null) {
|
||||||
|
throw new ApiKeyException("Username must not be null");
|
||||||
|
}
|
||||||
return createNewApiKey(userName, expirationInMinutes);
|
return createNewApiKey(userName, expirationInMinutes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@Lock(LockType.WRITE)
|
@Lock(LockType.WRITE)
|
||||||
public ApiKeyObject createNewApiKey(String userName, short expirationInMinutes) throws ApiKeyException {
|
public ApiKeyObject createNewApiKey(String userName, short expirationInMinutes) throws ApiKeyException {
|
||||||
if ((password == null || issuer == null)) {
|
if ((password == null || issuer == null) || (userName == null)) {
|
||||||
LOGGER.error("password or issuer not set in, please validate configuration");
|
LOGGER.error("password, issuer or username not set in, please validate configuration");
|
||||||
}
|
}
|
||||||
Date now = DateUtil.getCurrentTimeInUTC();
|
Date now = DateUtil.getCurrentTimeInUTC();
|
||||||
ZonedDateTime issuedOn = ZonedDateTime.ofInstant(now.toInstant(), ZoneId.of("UTC"));
|
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 {
|
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 issuedOn = ZonedDateTime.ofInstant(apiKey.getIssuedOn().toInstant(), ZoneId.of("UTC"));
|
||||||
ZonedDateTime expiresOn = issuedOn.plusMinutes(expirationInMinutes);
|
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 {
|
try {
|
||||||
String jwtString = JWTEncoder.encode(password, issuer, issuedOn, userName, apiKey.getApiKey(), apiKey.getExpiration());
|
String jwtString = JWTEncoder.encode(password, issuer, issuedOn, userName, apiKey.getApiKey(), apiKey.getExpiration());
|
||||||
em.persist(apiKey);
|
em.persist(apiKey);
|
||||||
|
|||||||
Reference in New Issue
Block a user