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

Fix #18: Process all URLs instead of only /c/portal/login / ...logout. #20

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 @@ -24,8 +24,7 @@
"dispatcher=REQUEST",
"servlet-context-name=",
"servlet-filter-name=SSO OpenID Connect Filter",
"url-pattern=/c/portal/login",
"url-pattern=/c/portal/logout"
"url-pattern=/*"
},
service = Filter.class
)
Expand Down
3 changes: 1 addition & 2 deletions oidc-hook/src/main/webapp/WEB-INF/liferay-hook.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</servlet-filter>
<servlet-filter-mapping>
<servlet-filter-name>OpenID Connect SSO Filter</servlet-filter-name>
<url-pattern>/c/portal/login</url-pattern>
<url-pattern>/c/portal/logout</url-pattern>
<url-pattern>/*</url-pattern>
</servlet-filter-mapping>
</hook>
37 changes: 36 additions & 1 deletion oidc-lib/src/main/java/nl/finalist/liferay/oidc/LibFilter.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package nl.finalist.liferay.oidc;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

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

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.oltu.oauth2.client.OAuthClient;
Expand Down Expand Up @@ -245,13 +248,45 @@ protected void exchangeCodeForAccessToken(HttpServletRequest request) throws IOE
protected void redirectToLogin(HttpServletRequest request, HttpServletResponse response, String clientId) throws
IOException {
try {
String ui_locales = null;

Cookie[] cookies = request.getCookies(); // look for GUEST_LANGUAGE_ID
if (null != cookies) {
for (Cookie cookie : cookies) {
liferay.trace("redirectToLogin: cookie: " + cookie.getName() + " = " + cookie.getValue());
if ("GUEST_LANGUAGE_ID".equals(cookie.getName())) {
String guestLanguageId = cookie.getValue();
String[] guestLocale = guestLanguageId.split("_");
ui_locales = guestLanguageId; // full locale, just as-is: 3-zone OR 2-zone OR 1-zone locale
if (guestLocale.length > 2) { // we got 3-zone locale: language_COUNTRY_REGION: Add "langauge_COUNTRY"
ui_locales += " " + guestLocale[0] + "_" + guestLocale[1];
}
if (guestLocale.length > 1) { // we got (3- or) 2-zone locale: language_COUNTRY: Add "language"
ui_locales += " " + guestLocale[0];
}
liferay.trace("redirectToLogin: use for ui_locales: " + ui_locales);
}
}
}

if (null == ui_locales) { // no GUEST_LANGUAGE_ID cookie available:
ui_locales = request.getServletPath().substring(1); // may be /c (default locale, useless) or /en (requested locale, useful) or /xy (useful) ...
}

if (null == ui_locales || ui_locales.length() < 2) { // skip values being too short to meet https://tools.ietf.org/html/rfc5646
// TODO: Improve locale recognition according to syntax given in RFC-5646
ui_locales = request.getLocale().getLanguage();
}
liferay.trace("redirectToLogin: ui_locales: " + ui_locales);

OAuthClientRequest oAuthRequest = OAuthClientRequest
.authorizationLocation(AUTHORIZATION_LOCATION)
.setClientId(clientId)
.setRedirectURI(getRedirectUri(request))
.setResponseType("code")
.setScope(SCOPE)
.setState(generateStateParam(request))
.setParameter("ui_locales", ui_locales) // see http://openid.net/specs/openid-connect-core-1_0.html#AuthRequest
.buildQueryMessage();
liferay.debug("Redirecting to URL: " + oAuthRequest.getLocationUri());
response.sendRedirect(oAuthRequest.getLocationUri());
Expand Down