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

LPS-67348 Avoid deprecated method #122

Open
wants to merge 5 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 @@ -14,10 +14,12 @@

package com.liferay.login.authentication.google.web.internal.portlet.action;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.portlet.LiferayWindowState;
import com.liferay.portal.kernel.portlet.PortletURLFactoryUtil;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.struts.BaseStrutsAction;
import com.liferay.portal.kernel.struts.StrutsAction;
import com.liferay.portal.kernel.theme.ThemeDisplay;
Expand All @@ -33,10 +35,12 @@
import java.util.Arrays;
import java.util.List;

import javax.portlet.MutableRenderParameters;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
Expand Down Expand Up @@ -87,14 +91,24 @@ else if (cmd.equals("token")) {
if (Validator.isNotNull(authorizationCode)) {
String returnRequestUri = getReturnRequestUri(request);

User user = _googleAuthorization.addOrUpdateUser(
session, themeDisplay.getCompanyId(), authorizationCode,
returnRequestUri, _scopesLogin);
try {
User user = _googleAuthorization.addOrUpdateUser(
session, themeDisplay.getCompanyId(), authorizationCode,
returnRequestUri, _scopesLogin);

if ((user != null) &&
(user.getStatus() == WorkflowConstants.STATUS_INCOMPLETE)) {
if ((user != null) &&
(user.getStatus() ==
WorkflowConstants.STATUS_INCOMPLETE)) {

sendUpdateAccountRedirect(request, response, user);
sendUpdateAccountRedirect(request, response, user);

return null;
}
}
catch (PortalException pe) {
SessionErrors.add(request, pe.getClass(), pe);

sendError(request, response);

return null;
}
Expand All @@ -121,6 +135,24 @@ protected String getReturnRequestUri(HttpServletRequest request) {
_REDIRECT_URI;
}

protected void sendError(
HttpServletRequest request, HttpServletResponse response)
throws Exception {

PortletURL portletURL = PortletURLFactoryUtil.create(
request, PortletKeys.LOGIN, PortletRequest.RENDER_PHASE);

MutableRenderParameters renderParameters =
portletURL.getRenderParameters();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting this exception...

Caused by: java.lang.UnsupportedOperationException: Requires 3.0 opt-in
	at com.liferay.portlet.internal.PortletURLImpl.getRenderParameters(PortletURLImpl.java:298)

But it looks like we only support Portlet 2 still?
https://github.com/liferay/liferay-portal/blob/master/portal-impl/src/com/liferay/portal/service/impl/PortletLocalServiceImpl.java#L2508

Copy link
Owner

@stian-sigvartsen stian-sigvartsen Nov 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@csierra If you agree I am going to leave this commit out because it is only available in the portlet 3.0 API. So we can backport easier. See @topolik 's comment on another use of portlet 3.0 in similar OpenId Connect code: brianchandotcom@24df85d

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure... go ahead


renderParameters.setValue(
"mvcRenderCommandName", "/login/google_login_error");

portletURL.setWindowState(LiferayWindowState.POP_UP);

response.sendRedirect(portletURL.toString());
}

protected void sendLoginRedirect(
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Expand Down Expand Up @@ -180,4 +212,9 @@ protected void sendUpdateAccountRedirect(
@Reference
private Portal _portal;

@Reference(
target = "(osgi.web.symbolicname=com.liferay.login.authentication.google.web)"
)
private ServletContext _servletContext;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.login.authentication.google.web.internal.portlet.action;

import com.liferay.portal.kernel.portlet.bridges.mvc.MVCRenderCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCRenderConstants;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.service.UserLocalService;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.PortletKeys;
import com.liferay.portal.kernel.util.PrefsPropsUtil;
import com.liferay.portal.kernel.util.WebKeys;

import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

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

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* @author Stian Sigvartsen
*/
@Component(
immediate = true,
property = {
"javax.portlet.name=" + PortletKeys.FAST_LOGIN,
"javax.portlet.name=" + PortletKeys.LOGIN,
"mvc.command.name=/login/google_login_error"
},
service = MVCRenderCommand.class
)
public class GoogleLoginErrorMVCRenderCommand implements MVCRenderCommand {

@Override
public String render(
RenderRequest renderRequest, RenderResponse renderResponse)
throws PortletException {

ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
WebKeys.THEME_DISPLAY);

boolean googleAuthEnabled = PrefsPropsUtil.getBoolean(
themeDisplay.getCompanyId(), "google-auth-enabled", true);

if (!googleAuthEnabled) {
throw new PortletException(
new PrincipalException.MustBeEnabled(
themeDisplay.getCompanyId(),
GoogleLoginAction.class.getName()));
}

HttpServletRequest httpServletRequest = _portal.getHttpServletRequest(
renderRequest);

HttpServletResponse httpServletResponse =
_portal.getHttpServletResponse(renderResponse);

try {
RequestDispatcher requestDispatcher =
_servletContext.getRequestDispatcher("/error.jsp");

requestDispatcher.forward(httpServletRequest, httpServletResponse);
}
catch (Exception e) {
throw new PortletException("Unable to include error.jsp", e);
}

return MVCRenderConstants.MVC_PATH_VALUE_SKIP_DISPATCH;
}

@Reference
private Portal _portal;

@Reference(
target = "(osgi.web.symbolicname=com.liferay.login.authentication.google.web)"
)
private ServletContext _servletContext;

@Reference
private UserLocalService _userLocalService;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<%--
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
--%>

<%@ include file="/init.jsp" %>

<div class="sheet sheet-lg">
<div class="sheet-header">
<div class="autofit-padded-no-gutters-x autofit-row">
<div class="autofit-col autofit-col-expand">
<h2 class="sheet-title">
<liferay-ui:message key="failed-to-sign-in-using-this-google-account" />
</h2>
</div>
</div>
</div>

<div class="sheet-text">
<c:if test="<%= SessionErrors.contains(request, UserEmailAddressException.MustNotUseCompanyMx.class) %>">
<div class="alert alert-danger">
<liferay-ui:message key="this-google-account-cannot-be-used-to-register-a-new-user-because-its-email-domain-is-reserved" />
</div>
</c:if>

<c:if test="<%= SessionErrors.contains(request, StrangersNotAllowedException.class) %>">
<div class="alert alert-danger">
<liferay-ui:message key="only-known-users-are-allowed-to-sign-in-using-google" />
</div>
</c:if>
</div>

<div class="sheet-footer">
<aui:button-row>
<aui:button onClick="window.close();" value="close" />
</aui:button-row>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%--
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
--%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %><%@
taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<%@ page import="com.liferay.portal.kernel.exception.UserEmailAddressException" %><%@
page import="com.liferay.portal.kernel.servlet.SessionErrors" %><%@
page import="com.liferay.portal.security.sso.google.exception.StrangersNotAllowedException" %>

<portlet:defineObjects />
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
google=Google
google=Google
failed-to-sign-in-using-this-google-account=Failed to sign in using this Google account
only-known-users-are-allowed-to-sign-in-using-google=Only known users are allowed to sign in using Google.
this-google-account-cannot-be-used-to-register-a-new-user-because-its-email-domain-is-reserved=This google account cannot be used to register a new user because its email domain is reserved.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Bundle-Version: 3.0.0
Export-Package:\
com.liferay.portal.security.sso.google,\
com.liferay.portal.security.sso.google.configuration,\
com.liferay.portal.security.sso.google.constants
com.liferay.portal.security.sso.google.constants,\
com.liferay.portal.security.sso.google.exception
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.portal.security.sso.google.exception;

import com.liferay.portal.kernel.exception.PortalException;

/**
* @author Stian Sigvartsen
*/
public class StrangersNotAllowedException extends PortalException {

public StrangersNotAllowedException(long companyId) {
super(String.format("Company %s does not allow strangers", companyId));

this.companyId = companyId;
}

public final long companyId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version 1.0.0
Loading