use of org.apache.catalina.HttpRequest in project Payara by payara.
the class AuthenticatorBase method postInvoke.
/**
* A post-request processing implementation that does nothing.
*
* Very few Valves override this behaviour as most Valve logic is used for request processing.
*/
@Override
public void postInvoke(Request request, Response response) throws IOException, ServletException {
Realm realm = this.context.getRealm();
HttpRequest hrequest = (HttpRequest) request;
HttpResponse hresponse = (HttpResponse) response;
/*
* Check realm for null since app may have been undeployed by the time its pipeline is invoked on the way out, in which
* case its realm will have been set to null. See IT 6801
*/
if (realm != null) {
realm.invokePostAuthenticateDelegate(hrequest, hresponse, context);
}
}
use of org.apache.catalina.HttpRequest in project Payara by payara.
the class AuthenticatorBase method invoke.
// --------------------------------------------------------- Public Methods
/**
* Enforce the security restrictions in the web application deployment descriptor of our associated Context.
*
* @param request Request to be processed
* @param response Response to be processed
*
* @exception IOException if an input/output error occurs
* @exception ServletException if thrown by a processing element
*/
@Override
public int invoke(Request request, Response response) throws IOException, ServletException {
// START GlassFish 247
if (!context.getAvailable()) {
try {
((HttpServletResponse) response.getResponse()).sendError(SC_SERVICE_UNAVAILABLE);
} catch (IllegalStateException | IOException e) {
;
}
return END_PIPELINE;
}
// END GlassFish 247
HttpRequest hrequest = (HttpRequest) request;
HttpResponse hresponse = (HttpResponse) response;
if (log.isLoggable(FINE)) {
log.fine("Security checking request " + ((HttpServletRequest) request.getRequest()).getMethod() + " " + ((HttpServletRequest) request.getRequest()).getRequestURI());
}
LoginConfig config = this.context.getLoginConfig();
// Have we got a cached authenticated Principal to record?
if (cache) {
Principal principal = ((HttpServletRequest) request.getRequest()).getUserPrincipal();
if (principal == null) {
Session session = getSession(hrequest);
if (session != null) {
principal = session.getPrincipal();
if (principal != null) {
if (log.isLoggable(FINE)) {
log.fine("We have cached auth type " + session.getAuthType() + " for principal " + session.getPrincipal());
}
hrequest.setAuthType(session.getAuthType());
hrequest.setUserPrincipal(principal);
}
}
}
}
Realm realm = this.context.getRealm();
// Is this request URI subject to a security constraint?
SecurityConstraint[] constraints = realm.findSecurityConstraints(hrequest, this.context);
if (constraints == null) {
log.fine(" Not subject to any constraint");
return processSecurityCheck(hrequest, hresponse, config);
}
log.fine(" Calling hasUserDataPermission()");
if (!realm.hasUserDataPermission(hrequest, hresponse, constraints)) {
log.fine(" Failed hasUserDataPermission() test");
// HTTP status code, so we do not have to do anything special
return END_PIPELINE;
}
int preAuthenticateCheckResult = realm.preAuthenticateCheck(hrequest, hresponse, constraints, disableProxyCaching, securePagesWithPragma, (sso != null));
if (preAuthenticateCheckResult == AUTHENTICATE_NOT_NEEDED) {
return processSecurityCheck(hrequest, hresponse, config);
}
if (preAuthenticateCheckResult == AUTHENTICATE_NEEDED) {
log.fine(" Calling authenticate()");
boolean authenticateResult = realm.invokeAuthenticateDelegate(hrequest, hresponse, context, this, false);
if (!authenticateResult) {
log.fine(" Failed authenticate() test");
return END_PIPELINE;
}
} else if (preAuthenticateCheckResult == AUTHENTICATED_NOT_AUTHORIZED) {
return END_PIPELINE;
}
log.log(FINE, " Calling accessControl()");
if (!realm.hasResourcePermission(hrequest, hresponse, constraints, this.context)) {
log.log(Level.FINE, " Failed accessControl() test");
Auditor[] auditors = context.getAuditors();
if (auditors != null) {
for (int j = 0; j < auditors.length; j++) {
auditors[j].webInvocation(hrequest, false);
}
}
/*
* ASSERT: AccessControl method has already set the appropriate HTTP status code, so we do not have to do anything
* special
*/
return END_PIPELINE;
}
Auditor[] auditors = this.context.getAuditors();
if (auditors != null) {
boolean success = true;
for (int j = 0; j < auditors.length; j++) {
try {
auditors[j].webInvocation(hrequest, true);
} catch (Exception e) {
success = false;
}
}
if (!success) {
// fail authorization if auditor blew up
return END_PIPELINE;
}
}
// Any and all specified constraints have been satisfied
log.fine("Successfully passed all security constraints");
return INVOKE_NEXT;
}
use of org.apache.catalina.HttpRequest in project Payara by payara.
the class GlassFishSingleSignOn method invoke.
// ---------------------------------------------------------- Valve Methods
/**
* Perform single-sign-on support processing for this request.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @return the valve flag
*/
// START OF IASRI 4665318
@Override
public int invoke(final Request request, final Response response) {
// END OF IASRI 4665318
// If this is not an HTTP request and response, just pass them on
/*
* GlassFish 6386229 if (!(request instanceof HttpRequest) || !(response instanceof HttpResponse)) { // START OF IASRI
* 4665318 // context.invokeNext(request, response); // return; return INVOKE_NEXT; // END OF IASRI 4665318 }
*/
HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
HttpServletResponse hres = (HttpServletResponse) response.getResponse();
request.removeNote(Constants.REQ_SSOID_NOTE);
request.removeNote(Constants.REQ_SSO_VERSION_NOTE);
// S1AS8 6155481 START
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.REQUEST_PROCESSED, hreq.getRequestURI());
}
if (hreq.getUserPrincipal() != null) {
// S1AS8 6155481 START
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.PRINCIPAL_ALREADY_AUTHENTICATED, hreq.getUserPrincipal().getName());
}
// return;
return INVOKE_NEXT;
// END OF IASRI 4665318
}
// S1AS8 6155481 START
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.CHECKING_SSO_COOKIE);
}
final Cookie[] cookies = hreq.getCookies();
if (cookies == null) {
return INVOKE_NEXT;
}
Cookie cookie = null;
Cookie versionCookie = null;
for (Cookie c : cookies) {
if (Constants.SINGLE_SIGN_ON_COOKIE.equals(c.getName())) {
cookie = c;
} else if (Constants.SINGLE_SIGN_ON_VERSION_COOKIE.equals(c.getName())) {
versionCookie = c;
}
if (cookie != null && versionCookie != null) {
break;
}
}
if (cookie == null) {
// S1AS8 6155481 START
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.SSO_COOKIE_NOT_PRESENT);
}
// return;
return INVOKE_NEXT;
// END OF IASRI 4665318
}
// Get the realm associated with the app of this request.
// If there is no realm available, do not process SSO.
Realm realm = request.getContext().getRealm();
if (realm == null) {
// S1AS8 6155481 START
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.NO_REALM_CONFIGURED);
}
// return;
return INVOKE_NEXT;
// END OF IASRI 4665318
}
String realmName = realm.getRealmName();
if (realmName == null) {
// S1AS8 6155481 START
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.NO_REALM_CONFIGURED);
}
// return;
return INVOKE_NEXT;
// END OF IASRI 4665318
}
if (debug >= 1) {
// S1AS8 6155481 START
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.APP_REALM);
}
}
// S1AS8 6155481 START
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.CHECKING_CACHED_PRINCIPAL);
}
long version = 0;
if (isVersioningSupported() && versionCookie != null) {
version = Long.parseLong(versionCookie.getValue());
}
SingleSignOnEntry entry = lookup(cookie.getValue(), version);
if (entry != null) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.FOUND_CACHED_PRINCIPAL, new Object[] { entry.getPrincipal().getName(), entry.getAuthType(), entry.getRealmName() });
}
// only use this SSO identity if it was set in the same realm
if (entry.getRealmName().equals(realmName)) {
request.setNote(Constants.REQ_SSOID_NOTE, cookie.getValue());
((HttpRequest) request).setAuthType(entry.getAuthType());
((HttpRequest) request).setUserPrincipal(entry.getPrincipal());
// Touch the SSO entry access time
entry.setLastAccessTime(System.currentTimeMillis());
if (isVersioningSupported()) {
long ver = entry.incrementAndGetVersion();
request.setNote(Constants.REQ_SSO_VERSION_NOTE, Long.valueOf(ver));
}
// update hit atomic counter
hitCount.incrementAndGet();
} else {
// S1AS8 6155481 START
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.IGNORING_SSO, realmName);
}
// consider this a cache miss, update atomic counter
missCount.incrementAndGet();
}
} else {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.NO_CACHED_PRINCIPAL_FOUND);
}
cookie.setMaxAge(0);
hres.addCookie(cookie);
// update miss atomic counter
missCount.incrementAndGet();
}
// return;
return INVOKE_NEXT;
// END OF IASRI 4665318
}
use of org.apache.catalina.HttpRequest in project Payara by payara.
the class StandardWrapperValve method invoke.
// --------------------------------------------------------- Public Methods
/**
* Invoke the servlet we are managing, respecting the rules regarding
* servlet lifecycle and SingleThreadModel support.
*
* @param request Request to be processed
* @param response Response to be produced
*
* @exception IOException if an input/output error occurred
* @exception ServletException if a servlet error occurred
*/
@Override
public int invoke(Request request, Response response) throws IOException, ServletException {
boolean unavailable = false;
Throwable throwable = null;
Servlet servlet = null;
StandardWrapper wrapper = (StandardWrapper) getContainer();
Context context = (Context) wrapper.getParent();
HttpRequest hrequest = (HttpRequest) request;
/*
* Create a request facade such that if the request was received
* at the root context, and the root context is mapped to a
* default-web-module, the default-web-module mapping is masked from
* the application code to which the request facade is being passed.
* For example, the request.facade's getContextPath() method will
* return "/", rather than the context root of the default-web-module,
* in this case.
*/
RequestFacade hreq = (RequestFacade) request.getRequest(true);
HttpServletResponse hres = (HttpServletResponse) response.getResponse();
// Check for the application being marked unavailable
if (!context.getAvailable()) {
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
response.setDetailMessage(rb.getString(LogFacade.APP_UNAVAILABLE));
// END S1AS 4878272
unavailable = true;
}
// Check for the servlet being marked unavailable
if (!unavailable && wrapper.isUnavailable()) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_UNAVAILABLE), wrapper.getName());
log(msg);
if (hres == null) {
// NOTE - Not much we can do generically
;
} else {
long available = wrapper.getAvailable();
if ((available > 0L) && (available < Long.MAX_VALUE)) {
hres.setDateHeader("Retry-After", available);
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
response.setDetailMessage(msg);
// END S1AS 4878272
} else if (available == Long.MAX_VALUE) {
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_NOT_FOUND);
msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_NOT_FOUND), wrapper.getName());
response.setDetailMessage(msg);
// END S1AS 4878272
}
}
unavailable = true;
}
// Allocate a servlet instance to process this request
try {
if (!unavailable) {
servlet = wrapper.allocate();
}
} catch (UnavailableException e) {
if (e.isPermanent()) {
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_NOT_FOUND);
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_NOT_FOUND), wrapper.getName());
response.setDetailMessage(msg);
// END S1AS 4878272
} else {
hres.setDateHeader("Retry-After", e.getUnavailableSeconds());
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_UNAVAILABLE), wrapper.getName());
response.setDetailMessage(msg);
// END S1AS 4878272
}
} catch (ServletException e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_ALLOCATE_EXCEPTION), wrapper.getName());
log(msg, StandardWrapper.getRootCause(e));
throwable = e;
exception(request, response, e);
servlet = null;
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_ALLOCATE_EXCEPTION), wrapper.getName());
log(msg, e);
throwable = e;
exception(request, response, e);
servlet = null;
}
// Acknowlege the request
try {
response.sendAcknowledgement();
} catch (IOException e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SEND_ACKNOWLEDGEMENT_EXCEPTION), wrapper.getName());
log(msg, e);
throwable = e;
exception(request, response, e);
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SEND_ACKNOWLEDGEMENT_EXCEPTION), wrapper.getName());
log(msg, e);
throwable = e;
exception(request, response, e);
servlet = null;
}
DataChunk requestPathMB = hrequest.getRequestPathMB();
hreq.setAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR, requestPathMB);
// Create the filter chain for this request
ApplicationFilterFactory factory = ApplicationFilterFactory.getInstance();
ApplicationFilterChain filterChain = factory.createFilterChain((ServletRequest) request, wrapper, servlet);
// NOTE: This also calls the servlet's service() method
try {
String jspFile = wrapper.getJspFile();
if (jspFile != null) {
hreq.setAttribute(Globals.JSP_FILE_ATTR, jspFile);
}
// START IASRI 4665318
if (servlet != null) {
if (filterChain != null) {
filterChain.setWrapper(wrapper);
filterChain.doFilter(hreq, hres);
} else {
wrapper.service(hreq, hres, servlet);
}
}
// END IASRI 4665318
} catch (ClientAbortException e) {
throwable = e;
exception(request, response, e);
} catch (IOException e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_SERVICE_EXCEPTION), wrapper.getName());
log(msg, e);
throwable = e;
exception(request, response, e);
} catch (UnavailableException e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_SERVICE_EXCEPTION), wrapper.getName());
log(msg, e);
// throwable = e;
// exception(request, response, e);
wrapper.unavailable(e);
long available = wrapper.getAvailable();
if ((available > 0L) && (available < Long.MAX_VALUE)) {
hres.setDateHeader("Retry-After", available);
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
String msgServletUnavailable = MessageFormat.format(rb.getString(LogFacade.SERVLET_UNAVAILABLE), wrapper.getName());
response.setDetailMessage(msgServletUnavailable);
// END S1AS 4878272
} else if (available == Long.MAX_VALUE) {
// BEGIN S1AS 4878272
hres.sendError(HttpServletResponse.SC_NOT_FOUND);
String msgServletNotFound = MessageFormat.format(rb.getString(LogFacade.SERVLET_NOT_FOUND), wrapper.getName());
response.setDetailMessage(msgServletNotFound);
// END S1AS 4878272
}
// Do not save exception in 'throwable', because we
// do not want to do exception(request, response, e) processing
} catch (ServletException e) {
Throwable rootCause = StandardWrapper.getRootCause(e);
if (!(rootCause instanceof ClientAbortException)) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_SERVICE_EXCEPTION), wrapper.getName());
log(msg, rootCause);
}
throwable = e;
exception(request, response, e);
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_SERVICE_EXCEPTION), wrapper.getName());
log(msg, e);
throwable = e;
exception(request, response, e);
}
// Release the filter chain (if any) for this request
try {
if (filterChain != null)
filterChain.release();
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.RELEASE_FILTERS_EXCEPTION), wrapper.getName());
log(msg, e);
if (throwable == null) {
throwable = e;
exception(request, response, e);
}
}
// Deallocate the allocated servlet instance
try {
if (servlet != null) {
wrapper.deallocate(servlet);
}
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.DEALLOCATE_EXCEPTION), wrapper.getName());
log(msg, e);
if (throwable == null) {
throwable = e;
exception(request, response, e);
}
}
// unload it and release this instance
try {
if ((servlet != null) && (wrapper.getAvailable() == Long.MAX_VALUE)) {
wrapper.unload();
}
} catch (Throwable e) {
String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_UNLOAD_EXCEPTION), wrapper.getName());
log(msg, e);
if (throwable == null) {
exception(request, response, e);
}
}
return END_PIPELINE;
}
use of org.apache.catalina.HttpRequest in project Payara by payara.
the class SingleSignOn method invoke.
/**
* Perform single-sign-on support processing for this request.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
@Override
public int invoke(Request request, Response response) throws IOException, ServletException {
// If this is not an HTTP request and response, just pass them on
/*
* GlassFish 6386229 if (!(request instanceof HttpRequest) || !(response instanceof HttpResponse)) { return INVOKE_NEXT;
* }
*/
HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
HttpServletResponse hres = (HttpServletResponse) response.getResponse();
request.removeNote(Constants.REQ_SSOID_NOTE);
request.removeNote(Constants.REQ_SSO_VERSION_NOTE);
// Has a valid user already been authenticated?
if (debug >= 1) {
String msg = MessageFormat.format(rb.getString(LogFacade.PROCESS_REQUEST_INFO), hreq.getRequestURI());
log(msg);
}
if (hreq.getUserPrincipal() != null) {
if (debug >= 1) {
String msg = MessageFormat.format(rb.getString(LogFacade.PRINCIPAL_BEEN_AUTHENTICATED_INFO), hreq.getUserPrincipal());
log(msg);
}
return END_PIPELINE;
}
// Check for the single sign on cookie
if (debug >= 1)
log(rb.getString(LogFacade.CHECK_SSO_COOKIE_INFO));
Cookie cookie = null;
Cookie versionCookie = null;
Cookie[] cookies = hreq.getCookies();
if (cookies == null)
cookies = new Cookie[0];
for (Cookie cookie1 : cookies) {
if (Constants.SINGLE_SIGN_ON_COOKIE.equals(cookie1.getName())) {
cookie = cookie1;
} else if (Constants.SINGLE_SIGN_ON_VERSION_COOKIE.equals(cookie1.getName())) {
versionCookie = cookie1;
}
if (cookie != null && versionCookie != null) {
break;
}
}
if (cookie == null) {
if (debug >= 1)
log(rb.getString(LogFacade.SSO_COOKIE_NOT_PRESENT_INFO));
return INVOKE_NEXT;
}
// Look up the cached Principal associated with this cookie value
if (debug >= 1) {
String msg = MessageFormat.format(rb.getString(LogFacade.CHECK_CACHED_PRINCIPAL_INFO), cookie.getValue());
log(msg);
}
long version = 0;
if (isVersioningSupported() && versionCookie != null) {
version = Long.parseLong(versionCookie.getValue());
}
SingleSignOnEntry entry = lookup(cookie.getValue(), version);
if (entry != null) {
if (debug >= 1) {
String msg = MessageFormat.format(rb.getString(LogFacade.FOUND_CACHED_PRINCIPAL_AUTH_TYPE_INFO), new Object[] { entry.getPrincipal().getName(), entry.getAuthType() });
log(msg);
}
request.setNote(Constants.REQ_SSOID_NOTE, cookie.getValue());
if (isVersioningSupported()) {
long ver = entry.incrementAndGetVersion();
request.setNote(Constants.REQ_SSO_VERSION_NOTE, Long.valueOf(ver));
}
((HttpRequest) request).setAuthType(entry.getAuthType());
((HttpRequest) request).setUserPrincipal(entry.getPrincipal());
} else {
if (debug >= 1)
log(rb.getString(LogFacade.NO_CACHED_PRINCIPAL_FOUND_INFO));
cookie.setMaxAge(0);
hres.addCookie(cookie);
}
// Invoke the next Valve in our pipeline
return INVOKE_NEXT;
}
Aggregations