From 60faa2784e2baf47254eef58f757bdd728fd3535 Mon Sep 17 00:00:00 2001 From: amontenegro Date: Thu, 16 Nov 2023 09:58:27 -0600 Subject: [PATCH] Fix NPE on social sign in --- .../core/common/manager/EventManager.java | 2 +- .../common/manager/impl/EventManagerImpl.java | 52 ++++++++----------- .../web/controllers/LoginController.java | 12 ++--- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java b/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java index 04593fce26c..51439b40d4b 100644 --- a/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java +++ b/orcid-core/src/main/java/org/orcid/core/common/manager/EventManager.java @@ -11,6 +11,6 @@ */ public interface EventManager { - void createEvent(String orcid, EventType eventType, HttpServletRequest request); + void createEvent(EventType eventType, HttpServletRequest request); } diff --git a/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java b/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java index 4fbc9291f1e..82cd09d7501 100644 --- a/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java +++ b/orcid-core/src/main/java/org/orcid/core/common/manager/impl/EventManagerImpl.java @@ -39,42 +39,36 @@ public class EventManagerImpl implements EventManager { private RecordNameManagerReadOnly recordNameManagerReadOnly; @Override - public void createEvent(String orcid, EventType eventType, HttpServletRequest request) { + public void createEvent(EventType eventType, HttpServletRequest request) { String label = "Website"; String clientId = null; - String publicPage = null; - if (eventType == EventType.PUBLIC_PAGE) { - publicPage = orcid; - orcid = null; - } else { - if (request != null) { - Boolean isOauth2ScreensRequest = (Boolean) request.getSession().getAttribute(OrcidOauth2Constants.OAUTH_2SCREENS); - RequestInfoForm requestInfoForm = (RequestInfoForm) request.getSession().getAttribute("requestInfoForm"); - if (requestInfoForm != null) { - clientId = requestInfoForm.getClientId(); - label = "OAuth " + requestInfoForm.getMemberName() + " " + requestInfoForm.getClientName(); - } else if (isOauth2ScreensRequest != null && isOauth2ScreensRequest) { - String queryString = (String) request.getSession().getAttribute(OrcidOauth2Constants.OAUTH_QUERY_STRING); - clientId = getParameterValue(queryString, "client_id"); - ClientDetailsEntity clientDetailsEntity = clientDetailsEntityCacheManager.retrieve(clientId); - String memberName = ""; - String clientName = clientDetailsEntity.getClientName(); + if (request != null) { + Boolean isOauth2ScreensRequest = (Boolean) request.getSession().getAttribute(OrcidOauth2Constants.OAUTH_2SCREENS); + RequestInfoForm requestInfoForm = (RequestInfoForm) request.getSession().getAttribute("requestInfoForm"); + if (requestInfoForm != null) { + clientId = requestInfoForm.getClientId(); + label = "OAuth " + requestInfoForm.getMemberName() + " " + requestInfoForm.getClientName(); + } else if (isOauth2ScreensRequest != null && isOauth2ScreensRequest) { + String queryString = (String) request.getSession().getAttribute(OrcidOauth2Constants.OAUTH_QUERY_STRING); + clientId = getParameterValue(queryString, "client_id"); + ClientDetailsEntity clientDetailsEntity = clientDetailsEntityCacheManager.retrieve(clientId); + String memberName = ""; + String clientName = clientDetailsEntity.getClientName(); - if (ClientType.PUBLIC_CLIENT.equals(clientDetailsEntity.getClientType())) { - memberName = "PubApp"; - } else if (!PojoUtil.isEmpty(clientDetailsEntity.getGroupProfileId())) { - Name name = recordNameManagerReadOnly.getRecordName(clientDetailsEntity.getGroupProfileId()); - if (name != null) { - memberName = name.getCreditName() != null ? name.getCreditName().getContent() : ""; - } + if (ClientType.PUBLIC_CLIENT.equals(clientDetailsEntity.getClientType())) { + memberName = "PubApp"; + } else if (!PojoUtil.isEmpty(clientDetailsEntity.getGroupProfileId())) { + Name name = recordNameManagerReadOnly.getRecordName(clientDetailsEntity.getGroupProfileId()); + if (name != null) { + memberName = name.getCreditName() != null ? name.getCreditName().getContent() : ""; } + } - if (StringUtils.isBlank(memberName)) { - memberName = clientName; - } - label = "OAuth " + memberName + " " + clientName; + if (StringUtils.isBlank(memberName)) { + memberName = clientName; } + label = "OAuth " + memberName + " " + clientName; } } diff --git a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/LoginController.java b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/LoginController.java index 98a34a18d04..3184290f6f7 100644 --- a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/LoginController.java +++ b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/LoginController.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; -import java.util.Iterator; import java.util.UUID; import javax.annotation.Resource; @@ -335,7 +334,7 @@ private ModelAndView processSocialLogin(HttpServletRequest request, HttpServletR } else { // Forward to account link page view = socialLinking(request); - } + } } else { // Store relevant data in the session socialSignInUtils.setSignedInData(request, userData); @@ -343,16 +342,17 @@ private ModelAndView processSocialLogin(HttpServletRequest request, HttpServletR userConnectionId = createUserConnection(socialType, providerUserId, userData.getString(OrcidOauth2Constants.EMAIL), userData.getString(OrcidOauth2Constants.DISPLAY_NAME), accessToken, expiresIn); // Forward to account link page - view = socialLinking(request); + view = socialLinking(request); } if (userConnectionId == null) { throw new IllegalArgumentException("Unable to find userConnectionId for providerUserId = " + providerUserId); } + if (Features.EVENTS.isActive()) { - eventManager.createEvent(userConnection.getOrcid(), EventType.SIGN_IN, request); + eventManager.createEvent(EventType.SIGN_IN, request); } - userCookieGenerator.addCookie(userConnectionId, response); - + userCookieGenerator.addCookie(userConnectionId, response); + if ("social_2FA".equals(view.getViewName())) { return new ModelAndView("redirect:" + calculateRedirectUrl("/2fa-signin?social=true")); }