Skip to content

Commit

Permalink
feat(jans-auth-server): made not found exceptions logging level confi…
Browse files Browse the repository at this point in the history
…gurable #4973 (#4982)
  • Loading branch information
yuriyz authored May 12, 2023
1 parent c8dc113 commit 98be22b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ tags:
| logClientNameOnClientAuthentication | Choose if application should log the Client Name on client authentication | [Details](#logclientnameonclientauthentication) |
| loggingLayout | Logging layout used for Jans Authorization Server loggers | [Details](#logginglayout) |
| loggingLevel | Specify the logging level for oxAuth loggers | [Details](#logginglevel) |
| logNotFoundEntityAsError | Boolean value specifying whether to log not_found entity exception as error or as trace. Default value is false (trace). | [Details](#lognotfoundentityaserror) |
| metricReporterInterval | The interval for metric reporter in seconds | [Details](#metricreporterinterval) |
| metricReporterKeepDataDays | The days to keep metric reported data | [Details](#metricreporterkeepdatadays) |
| mtlsAuthorizationEndpoint | URL for Mutual TLS (mTLS) Client Authentication and Certificate-Bound Access Tokens (MTLS) Endpoint | [Details](#mtlsauthorizationendpoint) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,9 @@ public class AppConfiguration implements Configuration {
@DocProperty(description = "The interval for configuration update in seconds")
private int configurationUpdateInterval;

@DocProperty(description = "Boolean value specifying whether to log not_found entity exception as error or as trace. Default value is false (trace).")
private Boolean logNotFoundEntityAsError;

@DocProperty(description = "Choose if client can update Grant Type values")
private Boolean enableClientGrantTypeUpdate;

Expand Down Expand Up @@ -881,6 +884,15 @@ public void setRotateDeviceSecret(Boolean rotateDeviceSecret) {
this.rotateDeviceSecret = rotateDeviceSecret;
}

public Boolean getLogNotFoundEntityAsError() {
if (logNotFoundEntityAsError == null) logNotFoundEntityAsError = false;
return logNotFoundEntityAsError;
}

public void setLogNotFoundEntityAsError(Boolean logNotFoundEntityAsError) {
this.logNotFoundEntityAsError = logNotFoundEntityAsError;
}

public Boolean getRequirePkce() {
if (requirePkce == null) requirePkce = false;
return requirePkce;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import jakarta.ejb.Stateless;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

Expand All @@ -30,6 +29,8 @@
import java.util.List;
import java.util.UUID;

import static org.apache.commons.lang.BooleanUtils.isTrue;

/**
* @author Yuriy Zabrovarnyy
* @author Javier Rojas Blum
Expand Down Expand Up @@ -140,7 +141,7 @@ public List<TokenEntity> getGrantsOfClient(String clientId) {
final String baseDn = clientService.buildClientDn(clientId);
return persistenceEntryManager.findEntries(baseDn, TokenEntity.class, Filter.createPresenceFilter("tknCde"));
} catch (Exception e) {
log.error(e.getMessage(), e);
logException(e);
}
return Collections.emptyList();
}
Expand All @@ -154,11 +155,19 @@ public TokenEntity getGrantByCode(String code) {
}
}

private void logException(Exception e) {
if (isTrue(appConfiguration.getLogNotFoundEntityAsError())) {
log.error(e.getMessage(), e);
} else {
log.trace(e.getMessage(), e);
}
}

private TokenEntity load(String tokenDn) {
try {
return persistenceEntryManager.find(TokenEntity.class, tokenDn);
} catch (Exception e) {
log.error(e.getMessage(), e);
logException(e);
}
return null;
}
Expand All @@ -167,7 +176,7 @@ public List<TokenEntity> getGrantsByGrantId(String grantId) {
try {
return persistenceEntryManager.findEntries(tokenBaseDn(), TokenEntity.class, Filter.createEqualityFilter("grtId", grantId));
} catch (Exception e) {
log.error(e.getMessage(), e);
logException(e);
}
return Collections.emptyList();
}
Expand All @@ -176,7 +185,7 @@ public List<TokenEntity> getGrantsByAuthorizationCode(String authorizationCode)
try {
return persistenceEntryManager.findEntries(tokenBaseDn(), TokenEntity.class, Filter.createEqualityFilter("authzCode", TokenHashUtil.hash(authorizationCode)));
} catch (Exception e) {
log.error(e.getMessage(), e);
logException(e);
}
return Collections.emptyList();
}
Expand All @@ -189,7 +198,7 @@ public List<TokenEntity> getGrantsBySessionDn(String sessionDn) {
grants.addAll(ldapGrants);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
logException(e);
}
return grants;
}
Expand All @@ -201,7 +210,7 @@ public void logout(String sessionDn) {
}

public void filterOutRefreshTokenFromDeletion(List<TokenEntity> tokens) {
if (BooleanUtils.isTrue(appConfiguration.getRemoveRefreshTokensForClientOnLogout())) {
if (isTrue(appConfiguration.getRemoveRefreshTokensForClientOnLogout())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import jakarta.inject.Named;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
Expand Down Expand Up @@ -289,15 +290,15 @@ public SessionId resetToStep(SessionId session, int resetToStep) {
}

if (resetToStep <= currentStep) {
for (int i = resetToStep; i <= currentStep; i++) {
String key = String.format("auth_step_passed_%d", i);
sessionAttributes.remove(key);
}
for (int i = resetToStep; i <= currentStep; i++) {
String key = String.format("auth_step_passed_%d", i);
sessionAttributes.remove(key);
}
} else {
// Scenario when we sckip steps. In this case we need to mark all previous steps as passed
for (int i = currentStep + 1; i < resetToStep; i++) {
sessionAttributes.put(String.format("auth_step_passed_%d", i), Boolean.TRUE.toString());
}
// Scenario when we sckip steps. In this case we need to mark all previous steps as passed
for (int i = currentStep + 1; i < resetToStep; i++) {
sessionAttributes.put(String.format("auth_step_passed_%d", i), Boolean.TRUE.toString());
}
}

sessionAttributes.put(io.jans.as.model.config.Constants.AUTH_STEP, String.valueOf(resetToStep));
Expand Down Expand Up @@ -342,16 +343,16 @@ public SessionId getSessionId() {
sessionId = identity.getSessionId().getId();
}

SessionId result = null;
SessionId result = null;
if (StringHelper.isNotEmpty(sessionId)) {
result = getSessionId(sessionId);
if ((result == null) && identity.getSessionId() != null) {
// Here we cover scenario when user were redirected from /device-code to ACR method
// which call this method in prepareForStep for step 1. The cookie in this case is not updated yet.
// hence actual information about session_id only in identity.
result = getSessionId(sessionId);
if ((result == null) && identity.getSessionId() != null) {
// Here we cover scenario when user were redirected from /device-code to ACR method
// which call this method in prepareForStep for step 1. The cookie in this case is not updated yet.
// hence actual information about session_id only in identity.
sessionId = identity.getSessionId().getId();
result = getSessionId(sessionId);
}
result = getSessionId(sessionId);
}
} else {
log.trace("Session cookie not exists");
}
Expand Down Expand Up @@ -792,7 +793,11 @@ public SessionId getSessionByDn(@Nullable String dn, boolean silently) {
return sessionId;
} catch (Exception e) {
if (!silently) {
log.error("Failed to get session by dn: {}. {}", dn, e.getMessage());
if (BooleanUtils.isTrue(appConfiguration.getLogNotFoundEntityAsError())) {
log.error("Failed to get session by dn: {}. {}", dn, e.getMessage());
} else {
log.trace("Failed to get session by dn: {}. {}", dn, e.getMessage());
}
}
}
return null;
Expand Down
2 changes: 2 additions & 0 deletions jans-config-api/docs/jans-config-api-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8469,6 +8469,8 @@ components:
type: boolean
allowBlankValuesInDiscoveryResponse:
type: boolean
logNotFoundEntityAsError:
type: boolean
checkUserPresenceOnRefreshToken:
type: boolean
consentGatheringScriptBackwardCompatibility:
Expand Down

0 comments on commit 98be22b

Please sign in to comment.