Skip to content

Commit

Permalink
fix: Add create event to ShibbolethController and Add missing member …
Browse files Browse the repository at this point in the history
…name
  • Loading branch information
DanielPalafox committed Nov 8, 2023
1 parent 2526b67 commit c13418b
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public interface EventManager {

boolean removeEvents(String orcid);

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import org.orcid.core.common.manager.EventManager;
import org.orcid.core.constants.OrcidOauth2Constants;
import org.orcid.core.manager.ClientDetailsEntityCacheManager;
import org.orcid.core.manager.v3.read_only.RecordNameManagerReadOnly;
import org.orcid.core.utils.EventType;
import org.orcid.jaxb.model.clientgroup.ClientType;
import org.orcid.jaxb.model.v3.release.record.Name;
import org.orcid.persistence.dao.EventDao;
import org.orcid.persistence.jpa.entities.ClientDetailsEntity;
import org.orcid.persistence.jpa.entities.EventEntity;
import org.orcid.pojo.ajaxForm.PojoUtil;
import org.orcid.pojo.ajaxForm.RequestInfoForm;

import java.io.UnsupportedEncodingException;
Expand All @@ -30,13 +34,16 @@ public class EventManagerImpl implements EventManager {
@Resource
private ClientDetailsEntityCacheManager clientDetailsEntityCacheManager;

@Resource(name = "recordNameManagerReadOnlyV3")
private RecordNameManagerReadOnly recordNameManagerReadOnly;

@Override
public boolean removeEvents(String orcid) {
return eventDao.removeEvents(orcid);
}

@Override
public void createEvent(String orcid, EventType eventType, HttpServletRequest request, RequestInfoForm requestInfoForm) {
public void createEvent(String orcid, EventType eventType, HttpServletRequest request) {
String label = "Website";
String clientId = null;
String redirectUrl = null;
Expand All @@ -48,17 +55,33 @@ public void createEvent(String orcid, EventType eventType, HttpServletRequest re
} else {
if (request != null) {
Boolean isOauth2ScreensRequest = (Boolean) request.getSession().getAttribute(OrcidOauth2Constants.OAUTH_2SCREENS);
if (isOauth2ScreensRequest != null && isOauth2ScreensRequest) {
RequestInfoForm requestInfoForm = (RequestInfoForm) request.getSession().getAttribute("requestInfoForm");
if (requestInfoForm != null) {
clientId = requestInfoForm.getClientId();
redirectUrl = removeAttributesFromUrl(requestInfoForm.getRedirectUrl());
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");
redirectUrl = getParameterValue(queryString, "redirect_uri");
ClientDetailsEntity clientDetailsEntity = clientDetailsEntityCacheManager.retrieve(clientId);
label = "OAuth " + clientDetailsEntity.getClientName();
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 (StringUtils.isBlank(memberName)) {
memberName = clientName;
}
label = "OAuth " + memberName + " " + clientName;
}
} else if (requestInfoForm != null) {
clientId = requestInfoForm.getClientId();
redirectUrl = removeAttributesFromUrl(requestInfoForm.getRedirectUrl());
label = "OAuth " + requestInfoForm.getClientName();
}
}

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, null, requestInfoForm);
eventManager.createEvent(requestInfoForm.getUserOrcid(), 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, null);
eventManager.createEvent(authentication.getPrincipal().toString(), 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 @@ -326,9 +326,6 @@ private ModelAndView processSocialLogin(HttpServletRequest request, HttpServletR
userConnectionId = userConnection.getId().getUserid();
// Store relevant data in the session
socialSignInUtils.setSignedInData(request, userData);
if (Features.EVENTS.isActive()) {
eventManager.createEvent(userConnection.getOrcid(), EventType.SIGN_IN, request, null);
}

if(userConnection.isLinked()) {
// If user exists and is linked update user connection info
Expand All @@ -351,6 +348,9 @@ private ModelAndView processSocialLogin(HttpServletRequest request, HttpServletR
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);
}
userCookieGenerator.addCookie(userConnectionId, response);

if ("social_2FA".equals(view.getViewName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public ModelAndView loginGetHandler(HttpServletRequest request, HttpServletRespo
} else {
orcid = auth.getPrincipal().toString();
}
eventManager.createEvent(orcid, eventType, null, requestInfoForm);
eventManager.createEvent(orcid, 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, null);
eventManager.createEvent(orcid, 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 @@ -281,6 +281,9 @@ public void validateGrcaptcha(HttpServletRequest request, @RequestBody Registrat
Locale locale = RequestContextUtils.getLocale(request);
// Ip
String ip = OrcidRequestUtil.getIpAddress(request);
if (Features.EVENTS.isActive()) {
eventManager.createEvent(getCurrentUserOrcid(), EventType.NEW_REGISTRATION, request);
}
createMinimalRegistrationAndLogUserIn(request, response, reg, usedCaptcha, locale, ip);
} catch (Exception e) {
LOGGER.error("Error registering a new user", e);
Expand All @@ -304,9 +307,6 @@ public void validateGrcaptcha(HttpServletRequest request, @RequestBody Registrat
redirectUrl = calculateRedirectUrl(request, response, true, true);
}
r.setUrl(redirectUrl);
if (Features.EVENTS.isActive()) {
eventManager.createEvent(getCurrentUserOrcid(), EventType.NEW_REGISTRATION, request, null);
}
return r;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.orcid.core.common.manager.EventManager;
import org.orcid.core.constants.OrcidOauth2Constants;
import org.orcid.core.manager.BackupCodeManager;
import org.orcid.core.manager.IdentityProviderManager;
Expand All @@ -20,6 +21,8 @@
import org.orcid.core.manager.v3.read_only.EmailManagerReadOnly;
import org.orcid.core.oauth.OrcidProfileUserDetails;
import org.orcid.core.security.OrcidUserDetailsService;
import org.orcid.core.togglz.Features;
import org.orcid.core.utils.EventType;
import org.orcid.core.utils.JsonUtils;
import org.orcid.frontend.web.exception.FeatureDisabledException;
import org.orcid.persistence.jpa.entities.ProfileEntity;
Expand Down Expand Up @@ -81,6 +84,9 @@ public class ShibbolethController extends BaseController {

@Resource
private OrcidUserDetailsService orcidUserDetailsService;

@Resource
private EventManager eventManager;

@RequestMapping(value = { "/2FA/authenticationCode.json" }, method = RequestMethod.GET)
public @ResponseBody TwoFactorAuthenticationCodes getTwoFactorCodeWrapper() {
Expand Down Expand Up @@ -165,6 +171,10 @@ public ModelAndView signinHandler(HttpServletRequest request, HttpServletRespons
try {
notifyUser(shibIdentityProvider, userConnectionEntity);
processAuthentication(remoteUser, userConnectionEntity);
if (Features.EVENTS.isActive()) {
OrcidProfileUserDetails orcidProfileUserDetails = getOrcidProfileUserDetails(userConnectionEntity.getOrcid());
eventManager.createEvent(orcidProfileUserDetails.getOrcid(), EventType.SIGN_IN, request);
}
} catch (AuthenticationException e) {
// this should never happen
SecurityContextHolder.getContext().setAuthentication(null);
Expand Down

0 comments on commit c13418b

Please sign in to comment.