Skip to content

Commit bfbac65

Browse files
committed
fix(auth): add comprehensive logging to OAuth2 token exchange
Add warn-level logs for all OAuth failure paths to aid debugging.
1 parent 205cd58 commit bfbac65

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/modules/authentication/implementations/OAuth2AuthenticationModule.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public QSession createSession(QInstance qInstance, Map<String, String> context)
140140
QRecord redirectStateRecord = GetAction.execute(oauth2MetaData.getRedirectStateTableName(), Map.of("state", context.get("state")));
141141
if(redirectStateRecord == null)
142142
{
143+
LOG.warn("OAuth callback state not found", logPair("state", context.get("state")));
143144
throw (new QAuthenticationException("State not found"));
144145
}
145146
redirectUri.set(redirectStateRecord.getValueString("redirectUri"));
@@ -241,6 +242,7 @@ else if(context.containsKey("sessionUUID") || context.containsKey("sessionId") |
241242
}
242243
catch(Exception e)
243244
{
245+
LOG.warn("Failed to create session", e, logPair("contextKeys", context.keySet()));
244246
throw (new QAuthenticationException("Failed to create session (token)", e));
245247
}
246248
}
@@ -252,10 +254,20 @@ else if(context.containsKey("sessionUUID") || context.containsKey("sessionId") |
252254
***************************************************************************/
253255
private QSession createSessionFromTokenRequest(TokenRequest tokenRequest) throws ParseException, IOException, QException
254256
{
257+
///////////////////////////////////////////////////////////////////////////
258+
// log token request details before sending to aid debugging auth issues //
259+
///////////////////////////////////////////////////////////////////////////
260+
LOG.debug("Sending token request",
261+
logPair("tokenEndpoint", tokenRequest.getEndpointURI()),
262+
logPair("clientId", tokenRequest.getClientAuthentication() != null ? tokenRequest.getClientAuthentication().getClientID() : null),
263+
logPair("grantType", tokenRequest.getAuthorizationGrant() != null ? tokenRequest.getAuthorizationGrant().getType() : null));
264+
255265
TokenResponse tokenResponse = TokenResponse.parse(tokenRequest.toHTTPRequest().send());
256266

257267
if(tokenResponse.indicatesSuccess())
258268
{
269+
LOG.debug("Token request succeeded", logPair("tokenEndpoint", tokenRequest.getEndpointURI()));
270+
259271
AccessToken accessToken = tokenResponse.toSuccessResponse().getTokens().getAccessToken();
260272

261273
////////////////////////////////////////////////////////////////////
@@ -308,7 +320,12 @@ private QSession createSessionFromTokenRequest(TokenRequest tokenRequest) throws
308320
else
309321
{
310322
ErrorObject errorObject = tokenResponse.toErrorResponse().getErrorObject();
311-
LOG.info("Token request failed", logPair("code", errorObject.getCode()), logPair("description", errorObject.getDescription()));
323+
LOG.warn("Token request failed",
324+
logPair("code", errorObject.getCode()),
325+
logPair("description", errorObject.getDescription()),
326+
logPair("httpStatus", errorObject.getHTTPStatusCode()),
327+
logPair("tokenEndpoint", tokenRequest.getEndpointURI()),
328+
logPair("clientId", tokenRequest.getClientAuthentication() != null ? tokenRequest.getClientAuthentication().getClientID() : null));
312329
throw (new QAuthenticationException(errorObject.getDescription()));
313330
}
314331
}
@@ -361,6 +378,7 @@ public String getLoginRedirectUrl(String originalUrl) throws QAuthenticationExce
361378
QTableMetaData stateTable = QContext.getQInstance().getTable(oauth2MetaData.getRedirectStateTableName());
362379
if(stateTable == null)
363380
{
381+
LOG.error("OAuth redirect state table not defined in QInstance", logPair("tableName", oauth2MetaData.getRedirectStateTableName()));
364382
throw (new QAuthenticationException("The table specified as the oauthRedirectStateTableName [" + oauth2MetaData.getRedirectStateTableName() + "] is not defined in the QInstance"));
365383
}
366384

@@ -382,6 +400,7 @@ public String getLoginRedirectUrl(String originalUrl) throws QAuthenticationExce
382400
.withValue("redirectUri", originalUrl))).getRecords().get(0);
383401
if(CollectionUtils.nullSafeHasContents(insertedState.getErrors()))
384402
{
403+
LOG.warn("Error storing OAuth redirect state", logPair("errors", insertedState.getErrorsAsString()));
385404
throw (new QAuthenticationException("Error storing redirect state: " + insertedState.getErrorsAsString()));
386405
}
387406
});
@@ -572,6 +591,7 @@ private String doGetAccessTokenFromSessionUUID(String sessionUUID) throws QAuthe
572591
DecodedJWT jwt = JWT.decode(accessToken);
573592
if(jwt.getExpiresAtAsInstant().isBefore(Instant.now()))
574593
{
594+
LOG.warn("Session accessToken is expired", logPair("sessionUUID", sessionUUID));
575595
throw (new QAuthenticationException("accessToken is expired"));
576596
}
577597

0 commit comments

Comments
 (0)