Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup Authentication Context from HttpServlet Request and Response #1418

Merged
merged 1 commit into from
Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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