Skip to content

Commit

Permalink
Cleanup Authentication Context from HttpServlet Request and Response
Browse files Browse the repository at this point in the history
These were added for Conditional authentication.
#1417
  • Loading branch information
Ruwan Abeykoon committed Mar 3, 2018
1 parent fec89fa commit b6c633a
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js;

import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.context.TransientObjectWrapper;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;

/**
Expand All @@ -38,76 +39,81 @@
public class JsAuthenticationContext extends AbstractJSObjectWrapper<AuthenticationContext> {

public JsAuthenticationContext(AuthenticationContext wrapped) {

super(wrapped);
}

@Override
public Object getMember(String name) {

switch (name) {
case FrameworkConstants.JSAttributes.JS_REQUESTED_ACR:
return getWrapped().getRequestedAcr();
case FrameworkConstants.JSAttributes.JS_AUTHENTICATED_SUBJECT:
return new JsAuthenticatedUser(getWrapped().getSubject());
case FrameworkConstants.JSAttributes.JS_LAST_AUTHENTICATED_USER:
return new JsAuthenticatedUser(getWrapped().getLastAuthenticatedUser());
case FrameworkConstants.JSAttributes.JS_TENANT_DOMAIN:
return getWrapped().getTenantDomain();
case FrameworkConstants.JSAttributes.JS_INITIAL_REQUEST:
return new JsServletRequest(getWrapped().getInitialRequest());
case FrameworkConstants.JSAttributes.JS_REQUEST:
return new JsServletRequest(getWrapped().getRequest());
case FrameworkConstants.JSAttributes.JS_RESPONSE:
return new JsServletResponse(getWrapped().getResponse());
default:
return super.getMember(name);
case FrameworkConstants.JSAttributes.JS_REQUESTED_ACR:
return getWrapped().getRequestedAcr();
case FrameworkConstants.JSAttributes.JS_AUTHENTICATED_SUBJECT:
return new JsAuthenticatedUser(getWrapped().getSubject());
case FrameworkConstants.JSAttributes.JS_LAST_AUTHENTICATED_USER:
return new JsAuthenticatedUser(getWrapped().getLastAuthenticatedUser());
case FrameworkConstants.JSAttributes.JS_TENANT_DOMAIN:
return getWrapped().getTenantDomain();
case FrameworkConstants.JSAttributes.JS_REQUEST:
return new JsServletRequest((TransientObjectWrapper) getWrapped()
.getParameter(FrameworkConstants.RequestAttribute.HTTP_REQUEST));
case FrameworkConstants.JSAttributes.JS_RESPONSE:
return new JsServletResponse((TransientObjectWrapper) getWrapped()
.getParameter(FrameworkConstants.RequestAttribute.HTTP_REQUEST));
default:
return super.getMember(name);
}
}

@Override
public boolean hasMember(String name) {

switch (name) {
case FrameworkConstants.JSAttributes.JS_REQUESTED_ACR:
return getWrapped().getRequestedAcr() != null;
case FrameworkConstants.JSAttributes.JS_AUTHENTICATED_SUBJECT:
return getWrapped().getSubject() != null;
case FrameworkConstants.JSAttributes.JS_LAST_AUTHENTICATED_USER:
return getWrapped().getLastAuthenticatedUser() != null;
case FrameworkConstants.JSAttributes.JS_TENANT_DOMAIN:
return getWrapped().getTenantDomain() != null;
case FrameworkConstants.JSAttributes.JS_INITIAL_REQUEST:
return getWrapped().getInitialRequest() != null;
case FrameworkConstants.JSAttributes.JS_REQUEST:
return getWrapped().getRequest() != null;
case FrameworkConstants.JSAttributes.JS_RESPONSE:
return getWrapped().getResponse() != null;
default:
return super.hasMember(name);
case FrameworkConstants.JSAttributes.JS_REQUESTED_ACR:
return getWrapped().getRequestedAcr() != null;
case FrameworkConstants.JSAttributes.JS_AUTHENTICATED_SUBJECT:
return getWrapped().getSubject() != null;
case FrameworkConstants.JSAttributes.JS_LAST_AUTHENTICATED_USER:
return getWrapped().getLastAuthenticatedUser() != null;
case FrameworkConstants.JSAttributes.JS_TENANT_DOMAIN:
return getWrapped().getTenantDomain() != null;
case FrameworkConstants.JSAttributes.JS_REQUEST:
return hasTransientValueInParameters(FrameworkConstants.RequestAttribute.HTTP_REQUEST);
case FrameworkConstants.JSAttributes.JS_RESPONSE:
return hasTransientValueInParameters(FrameworkConstants.RequestAttribute.HTTP_RESPONSE);
default:
return super.hasMember(name);
}
}

@Override
public void removeMember(String name) {

switch (name) {
case FrameworkConstants.JSAttributes.JS_SELECTED_ACR:
getWrapped().setSelectedAcr(null);
break;
default:
super.removeMember(name);
case FrameworkConstants.JSAttributes.JS_SELECTED_ACR:
getWrapped().setSelectedAcr(null);
break;
default:
super.removeMember(name);
}
}

@Override
public void setMember(String name, Object value) {

switch (name) {
case FrameworkConstants.JSAttributes.JS_SELECTED_ACR:
getWrapped().setSelectedAcr(String.valueOf(value));
break;
default:
super.setMember(name, value);
case FrameworkConstants.JSAttributes.JS_SELECTED_ACR:
getWrapped().setSelectedAcr(String.valueOf(value));
break;
default:
super.setMember(name, value);
}
}

private boolean hasTransientValueInParameters(String key) {

TransientObjectWrapper transientObjectWrapper = (TransientObjectWrapper) getWrapped().getParameter(key);
return transientObjectWrapper != null && transientObjectWrapper.getWrapped() != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@

package org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js;

import org.wso2.carbon.identity.application.authentication.framework.context.TransientObjectWrapper;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
* Javascript wrapper for Java level HTTPServletRequest.
Expand All @@ -37,54 +38,67 @@
* <p>
* Also it prevents writing an arbitrary values to the respective fields, keeping consistency on runtime HTTPServletRequest.
*/
public class JsServletRequest extends AbstractJSObjectWrapper<HttpServletRequest> {
public class JsServletRequest extends AbstractJSObjectWrapper<TransientObjectWrapper<HttpServletRequest>> {

public JsServletRequest(TransientObjectWrapper<HttpServletRequest> wrapped) {

public JsServletRequest(HttpServletRequest wrapped) {
super(wrapped);
}

@Override
public Object getMember(String name) {

switch (name) {
case FrameworkConstants.JSAttributes.JS_HEADERS:
Map headers = new HashMap();
Enumeration<String> headerNames = getWrapped().getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
headers.put(headerName, getWrapped().getHeader(headerName));
}
case FrameworkConstants.JSAttributes.JS_HEADERS:
Map headers = new HashMap();
Enumeration<String> headerNames = getRequest().getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
headers.put(headerName, getRequest().getHeader(headerName));
}
return new JsParameters(headers);
case FrameworkConstants.JSAttributes.JS_PARAMS:
return new JsParameters(getWrapped().getParameterMap());
case FrameworkConstants.JSAttributes.JS_COOKIES:
Map cookies = new HashMap();
Cookie[] cookieArr = getWrapped().getCookies();
if(cookieArr != null) {
for (Cookie cookie : cookieArr) {
cookies.put(cookie.getName(), new JsCookie(cookie));
}
}
return new JsParameters(headers);
case FrameworkConstants.JSAttributes.JS_PARAMS:
return new JsParameters(getRequest().getParameterMap());
case FrameworkConstants.JSAttributes.JS_COOKIES:
Map cookies = new HashMap();
Cookie[] cookieArr = getRequest().getCookies();
if (cookieArr != null) {
for (Cookie cookie : cookieArr) {
cookies.put(cookie.getName(), new JsCookie(cookie));
}
return new JsParameters(cookies);
default:
return super.getMember(name);
}
return new JsParameters(cookies);
default:
return super.getMember(name);
}
}

@Override
public boolean hasMember(String name) {

if (getRequest() == null) {
//Transient Object is null, hence no member access is possible.
return false;
}

switch (name) {
case FrameworkConstants.JSAttributes.JS_HEADERS:
return getWrapped().getHeaderNames() != null;
case FrameworkConstants.JSAttributes.JS_PARAMS:
return getWrapped().getParameterMap() != null;
case FrameworkConstants.JSAttributes.JS_COOKIES:
return getWrapped().getCookies() != null;
default:
return super.hasMember(name);
case FrameworkConstants.JSAttributes.JS_HEADERS:
return getRequest().getHeaderNames() != null;
case FrameworkConstants.JSAttributes.JS_PARAMS:
return getRequest().getParameterMap() != null;
case FrameworkConstants.JSAttributes.JS_COOKIES:
return getRequest().getCookies() != null;
default:
return super.hasMember(name);
}
}

private HttpServletRequest getRequest() {

TransientObjectWrapper<HttpServletRequest> transientObjectWrapper = getWrapped();
return transientObjectWrapper.getWrapped();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

package org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js;

import org.wso2.carbon.identity.application.authentication.framework.context.TransientObjectWrapper;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;

import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* Javascript wrapper for Java level HttpServletResponse.
Expand All @@ -36,38 +37,50 @@
* <p>
* Also it prevents writing an arbitrary values to the respective fields, keeping consistency on runtime HttpServletResponse.
*/
public class JsServletResponse extends AbstractJSObjectWrapper<HttpServletResponse> {
public class JsServletResponse extends AbstractJSObjectWrapper<TransientObjectWrapper<HttpServletResponse>> {

public JsServletResponse(TransientObjectWrapper<HttpServletResponse> wrapped) {

public JsServletResponse(HttpServletResponse wrapped) {
super(wrapped);
}

@Override
public Object getMember(String name) {

switch (name) {
case FrameworkConstants.JSAttributes.JS_HEADERS:
Map headers = new HashMap();
Collection<String> headerNames = getWrapped().getHeaderNames();
if (headerNames != null) {
for (String element : headerNames) {
headers.put(element, getWrapped().getHeader(element));
}
case FrameworkConstants.JSAttributes.JS_HEADERS:
Map headers = new HashMap();
Collection<String> headerNames = getResponse().getHeaderNames();
if (headerNames != null) {
for (String element : headerNames) {
headers.put(element, getResponse().getHeader(element));
}
return new JsHeaders(headers, getWrapped());
default:
return super.getMember(name);
}
return new JsHeaders(headers, getResponse());
default:
return super.getMember(name);
}
}

@Override
public boolean hasMember(String name) {

if (getResponse() == null) {
//Transient Object is null, hence no member access is possible.
return false;
}

switch (name) {
case FrameworkConstants.JSAttributes.JS_HEADERS:
return getWrapped().getHeaderNames() != null;
default:
return super.hasMember(name);
case FrameworkConstants.JSAttributes.JS_HEADERS:
return getResponse().getHeaderNames() != null;
default:
return super.hasMember(name);
}
}

private HttpServletResponse getResponse() {

TransientObjectWrapper<HttpServletResponse> transientObjectWrapper = getWrapped();
return transientObjectWrapper.getWrapped();
}
}
Loading

0 comments on commit b6c633a

Please sign in to comment.