From f4145ca2fc9f514784cc6e6b637bbcede01ec0b6 Mon Sep 17 00:00:00 2001 From: jomu Date: Wed, 9 Jan 2019 20:08:44 +0100 Subject: [PATCH] improved logging --- .../account/boundary/ApiKeyService.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/ApiKeyService.java b/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/ApiKeyService.java index d8f041b..afa5b68 100644 --- a/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/ApiKeyService.java +++ b/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/ApiKeyService.java @@ -143,7 +143,7 @@ public class ApiKeyService implements Serializable { } catch (JWTException ex) { if (LOGGER.isTraceEnabled()) { LOGGER.trace(ex.toString(), ex); - } + } return false; } return validKey != null; @@ -151,20 +151,42 @@ public class ApiKeyService implements Serializable { private ApiKeyEntity getValidKey(String userName, String apiKey, String authorizationHeader) throws JWTException { AccountEntity userAccount = accountControl.getAccountEntity(userName, false); + if (userAccount == null) { + throw new JWTException("AccountControl exception"); + } List apiKeys = getUsersApiKeys(userAccount); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Found {} keys for user {}", apiKeys.size(), userName); + } Iterator it = apiKeys.iterator(); ApiKeyEntity keyToLogout = null; while (keyToLogout == null && it.hasNext()) { ApiKeyEntity key = it.next(); if (key.getApiKey().equals(apiKey)) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Found API key in database"); + } + ZonedDateTime issuedOn = ZonedDateTime.ofInstant(key.getIssuedOn().toInstant(), ZoneOffset.UTC); String testString = JWTEncoder.encode(password, issuer, issuedOn, key.getAccount().getUsername(), key.getApiKey(), key.getExpiration()); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Successfully created validation JWT for user {}", userName); + } + if (authorizationHeader.equals(testString)) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Found valid key for user {}", userName); + } + return key; } } } + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("No valid key for user {} found", userName); + } + return null; } @@ -213,9 +235,9 @@ public class ApiKeyService implements Serializable { keyToLogout = getValidKey(userName, jwtObject.getUnqiueId(), authorizationHeader); } catch (JWTException ex) { if (LOGGER.isTraceEnabled()) { - LOGGER.trace(ex.getMessage(), ex); - } - + LOGGER.trace(ex.getMessage(), ex); + } + keyToLogout = null; }