Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

No need to manually force port 80 when X-Forwarded-Port is not specif… #259

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -15,10 +15,15 @@
*/
package org.springframework.social.security.provider;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.social.connect.Connection;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* @author Stefan Fussennegger
Expand All @@ -30,6 +35,8 @@ public abstract class AbstractSocialAuthenticationService<S> implements SocialAu

private String connectionAddedRedirectUrl;

private Set<String> returnToUrlParameters;

public void afterPropertiesSet() throws Exception {
}

Expand All @@ -52,4 +59,59 @@ public void setConnectionAddedRedirectUrl(String connectionAddedRedirectUrl) {
this.connectionAddedRedirectUrl = connectionAddedRedirectUrl;
}

public void setReturnToUrlParameters(Set<String> returnToUrlParameters) {
Assert.notNull(returnToUrlParameters, "returnToUrlParameters cannot be null");
this.returnToUrlParameters = returnToUrlParameters;
}

public Set<String> getReturnToUrlParameters() {
if (returnToUrlParameters == null) {
returnToUrlParameters = new HashSet<String>();
}
return returnToUrlParameters;
}

protected String buildReturnToUrl(HttpServletRequest request) {
StringBuffer sb = getProxyHeaderAwareRequestURL(request);
sb.append("?");
for (String name : getReturnToUrlParameters()) {
// Assume for simplicity that there is only one value
String value = request.getParameter(name);

if (value == null) {
continue;
}
sb.append(name).append("=").append(value).append("&");
}
sb.setLength(sb.length() - 1); // strip trailing ? or &
return sb.toString();
}

protected StringBuffer getProxyHeaderAwareRequestURL(HttpServletRequest request) {
String host = request.getHeader("Host");
if (StringUtils.isEmpty(host)) {
return request.getRequestURL();
}
StringBuffer sb = new StringBuffer();
String schemeHeader = request.getHeader("X-Forwarded-Proto");
String portHeader = request.getHeader("X-Forwarded-Port");
String scheme = StringUtils.isEmpty(schemeHeader) ? request.getScheme() : schemeHeader;
String port = StringUtils.isEmpty(portHeader) ? "" : portHeader;
if (scheme.equals("http") && port.equals("80")) {
port = "";
}
if (scheme.equals("https") && port.equals("443")) {
port = "";
}
sb.append(scheme);
sb.append("://");
sb.append(host);
if (StringUtils.hasLength(port)) {
sb.append(":");
sb.append(port);
}
sb.append(request.getRequestURI());
return sb;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package org.springframework.social.security.provider;

import java.util.HashSet;
import java.util.Set;

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

Expand Down Expand Up @@ -45,8 +42,6 @@ public class OAuth1AuthenticationService<S> extends AbstractSocialAuthentication
private final Log logger = LogFactory.getLog(getClass());

private static final String OAUTH_TOKEN_ATTRIBUTE = "oauthToken";

private Set<String> returnToUrlParameters;

private OAuth1ConnectionFactory<S> connectionFactory;

Expand All @@ -62,18 +57,6 @@ public void setConnectionFactory(OAuth1ConnectionFactory<S> connectionFactory) {
this.connectionFactory = connectionFactory;
}

public void setReturnToUrlParameters(Set<String> returnToUrlParameters) {
Assert.notNull(returnToUrlParameters, "returnToUrlParameters cannot be null");
this.returnToUrlParameters = returnToUrlParameters;
}

public Set<String> getReturnToUrlParameters() {
if (returnToUrlParameters == null) {
returnToUrlParameters = new HashSet<String>();
}
return returnToUrlParameters;
}

public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
Assert.notNull(getConnectionFactory(), "connectionFactory");
Expand Down Expand Up @@ -114,26 +97,6 @@ public SocialAuthenticationToken getAuthToken(HttpServletRequest request, HttpSe
}
}

protected String buildReturnToUrl(HttpServletRequest request) {
StringBuffer sb = request.getRequestURL();
sb.append("?");

for (String name : getReturnToUrlParameters()) {
// Assume for simplicity that there is only one value
String value = request.getParameter(name);

if (value == null) {
continue;
}
sb.append(name).append("=").append(value).append("&");

}

sb.setLength(sb.length() - 1); // strip trailing ? or &

return sb.toString();
}

private OAuthToken extractCachedRequestToken(HttpServletRequest request) {
OAuthToken requestToken = (OAuthToken) request.getSession().getAttribute(OAUTH_TOKEN_ATTRIBUTE);
request.getSession().removeAttribute(OAUTH_TOKEN_ATTRIBUTE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package org.springframework.social.security.provider;

import java.util.HashSet;
import java.util.Set;

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

Expand All @@ -42,8 +39,6 @@ public class OAuth2AuthenticationService<S> extends AbstractSocialAuthentication
protected final Log logger = LogFactory.getLog(getClass());

private OAuth2ConnectionFactory<S> connectionFactory;

private Set<String> returnToUrlParameters;

private String defaultScope = "";

Expand All @@ -59,18 +54,6 @@ public void setConnectionFactory(OAuth2ConnectionFactory<S> connectionFactory) {
this.connectionFactory = connectionFactory;
}

public void setReturnToUrlParameters(Set<String> returnToUrlParameters) {
Assert.notNull(returnToUrlParameters, "returnToUrlParameters cannot be null");
this.returnToUrlParameters = returnToUrlParameters;
}

public Set<String> getReturnToUrlParameters() {
if (returnToUrlParameters == null) {
returnToUrlParameters = new HashSet<String>();
}
return returnToUrlParameters;
}

/**
* @param defaultScope OAuth scope to use, i.e. requested permissions
*/
Expand Down Expand Up @@ -113,48 +96,6 @@ private String generateState(OAuth2ConnectionFactory<?> connectionFactory, HttpS
return (state != null) ? state : connectionFactory.generateState();
}

protected String buildReturnToUrl(HttpServletRequest request) {
StringBuffer sb = getProxyHeaderAwareRequestURL(request);
sb.append("?");
for (String name : getReturnToUrlParameters()) {
// Assume for simplicity that there is only one value
String value = request.getParameter(name);

if (value == null) {
continue;
}
sb.append(name).append("=").append(value).append("&");
}
sb.setLength(sb.length() - 1); // strip trailing ? or &
return sb.toString();
}

protected StringBuffer getProxyHeaderAwareRequestURL(HttpServletRequest request) {
String host = request.getHeader("Host");
if (StringUtils.isEmpty(host)) {
return request.getRequestURL();
}
StringBuffer sb = new StringBuffer();
String schemeHeader = request.getHeader("X-Forwarded-Proto");
String portHeader = request.getHeader("X-Forwarded-Port");
String scheme = StringUtils.isEmpty(schemeHeader) ? "http" : schemeHeader;
String port = StringUtils.isEmpty(portHeader) ? "80" : portHeader;
if (scheme.equals("http") && port.equals("80")){
port = "";
}
if (scheme.equals("https") && port.equals("443")){
port = "";
}
sb.append(scheme);
sb.append("://");
sb.append(host);
if (StringUtils.hasLength(port)){
sb.append(":");
sb.append(port);
}
sb.append(request.getRequestURI());
return sb;
}
private void setScope(HttpServletRequest request, OAuth2Parameters params) {
String requestedScope = request.getParameter("scope");
if (StringUtils.hasLength(requestedScope)) {
Expand Down