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 NPE on social sign in #6934

Merged
merged 2 commits into from
Nov 16, 2023
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 @@ -11,6 +11,6 @@
*/
public interface EventManager {

void createEvent(String orcid, EventType eventType, HttpServletRequest request);
void createEvent(EventType eventType, HttpServletRequest request);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class OauthController {
if (responseParam != null && !responseParam.isEmpty() && !PojoUtil.isEmpty(responseParam.get(0))) {
isResponseSet = true;
if (Features.EVENTS.isActive()) {
eventManager.createEvent(requestInfoForm.getUserOrcid(), EventType.REAUTHORIZE, request);
eventManager.createEvent(EventType.REAUTHORIZE, request);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AjaxAuthenticationSuccessHandler extends AjaxAuthenticationSuccessH
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
String targetUrl = getTargetUrl(request, response, authentication);
if (Features.EVENTS.isActive()) {
eventManager.createEvent(authentication.getPrincipal().toString(), EventType.SIGN_IN, request);
eventManager.createEvent(EventType.SIGN_IN, request);
}
response.setContentType("application/json");
response.getWriter().println("{\"success\": true, \"url\": \"" + targetUrl.replaceAll("^/", "") + "\"}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -335,24 +334,25 @@ 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);
// Store user info
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"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public ModelAndView loginGetHandler(HttpServletRequest request, HttpServletRespo
} else {
orcid = auth.getPrincipal().toString();
}
eventManager.createEvent(orcid, eventType, request);
eventManager.createEvent(eventType, request);
}
if(new HttpSessionRequestCache().getRequest(request, response) != null)
new HttpSessionRequestCache().removeRequest(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ PublicRecord getPublicRecord(@PathVariable("orcid") String orcid) {

try {
if (Features.EVENTS.isActive()) {
eventManager.createEvent(orcid, EventType.PUBLIC_PAGE, null);
eventManager.createEvent(EventType.PUBLIC_PAGE, null);
}
// Check if the profile is deprecated or locked
orcidSecurityManager.checkProfile(orcid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public void validateGrcaptcha(HttpServletRequest request, @RequestBody Registrat
// Ip
String ip = OrcidRequestUtil.getIpAddress(request);
if (Features.EVENTS.isActive()) {
eventManager.createEvent(getCurrentUserOrcid(), EventType.NEW_REGISTRATION, request);
eventManager.createEvent(EventType.NEW_REGISTRATION, request);
}
createMinimalRegistrationAndLogUserIn(request, response, reg, usedCaptcha, locale, ip);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public ModelAndView signinHandler(HttpServletRequest request, HttpServletRespons
processAuthentication(remoteUser, userConnectionEntity);
if (Features.EVENTS.isActive()) {
OrcidProfileUserDetails orcidProfileUserDetails = getOrcidProfileUserDetails(userConnectionEntity.getOrcid());
eventManager.createEvent(orcidProfileUserDetails.getOrcid(), EventType.SIGN_IN, request);
eventManager.createEvent(EventType.SIGN_IN, request);
}
} catch (AuthenticationException e) {
// this should never happen
Expand Down