Skip to content

Commit

Permalink
feat(oxauth): add ability to return error out of introspection and up…
Browse files Browse the repository at this point in the history
…date_token custom scripts #1760
  • Loading branch information
yuriyz committed Dec 16, 2022
1 parent 6ae16bb commit e66221e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import java.util.List;

/**
Expand Down Expand Up @@ -86,7 +87,11 @@ private boolean executeExternalModifyResponse(CustomScriptConfiguration scriptCo
final boolean result = script.modifyResponse(responseAsJsonObject, context);
log.trace("Finished external 'executeExternalModifyResponse' method, script name: {}, responseAsJsonObject: {} , context: {}, result: {}",
scriptConf.getName(), responseAsJsonObject, context, result);

context.throwWebApplicationExceptionIfSet();
return result;
} catch (WebApplicationException e) {
throw e;
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
saveScriptError(scriptConf.getCustomScript(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

package org.gluu.oxauth.service.external;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import com.google.common.base.Function;
import org.gluu.model.custom.script.CustomScriptType;
import org.gluu.model.custom.script.conf.CustomScriptConfiguration;
import org.gluu.model.custom.script.type.token.UpdateTokenType;
Expand All @@ -17,7 +15,9 @@
import org.gluu.service.custom.script.ExternalScriptService;
import org.slf4j.Logger;

import com.google.common.base.Function;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;

/**
* @author Yuriy Movchan
Expand All @@ -43,7 +43,10 @@ public boolean modifyIdTokenMethod(CustomScriptConfiguration script, JsonWebResp
final boolean result = updateTokenType.modifyIdToken(jsonWebResponse, context);
log.trace("Finished 'updateToken' method, script name: {}, jsonWebResponse: {}, context: {}, result: {}", script.getName(), jsonWebResponse, context, result);

context.throwWebApplicationExceptionIfSet();
return result;
} catch (WebApplicationException e) {
throw e;
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
saveScriptError(script.getCustomScript(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

package org.gluu.oxauth.service.external.context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.net.util.SubnetUtils;
import org.gluu.oxauth.model.util.Util;
import org.gluu.oxauth.util.ServerUtil;
Expand All @@ -18,6 +15,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
* Holds object required in custom scripts
*
Expand All @@ -30,6 +33,8 @@ public class ExternalScriptContext extends org.gluu.service.external.context.Ext

private final PersistenceEntryManager ldapEntryManager;

private WebApplicationException webApplicationException;

public ExternalScriptContext(HttpServletRequest httpRequest) {
this(httpRequest, null);
}
Expand Down Expand Up @@ -71,4 +76,26 @@ protected String getEntryAttributeValue(String dn, String attributeName) {

return "";
}

public WebApplicationException getWebApplicationException() {
return webApplicationException;
}

public void setWebApplicationException(WebApplicationException webApplicationException) {
this.webApplicationException = webApplicationException;
}

public WebApplicationException createWebApplicationException(int status, String entity) {
this.webApplicationException = new WebApplicationException(Response
.status(status)
.entity(entity)
.type(MediaType.APPLICATION_JSON_TYPE)
.build());
return this.webApplicationException;
}

public void throwWebApplicationExceptionIfSet() {
if (webApplicationException != null)
throw webApplicationException;
}
}

0 comments on commit e66221e

Please sign in to comment.