Search in sources :

Example 6 with UserconnectionEntity

use of org.orcid.persistence.jpa.entities.UserconnectionEntity in project ORCID-Source by ORCID.

the class InstitutionalSignInManagerTest method testDontPersistAndDontNotify.

@Test
public void testDontPersistAndDontNotify() throws UnsupportedEncodingException {
    when(mock_userConnectionDao.findByProviderIdAndProviderUserIdAndIdType(anyString(), anyString(), anyString())).thenReturn(new UserconnectionEntity());
    when(mock_clientDetailsEntityCacheManager.retrieveByIdP(anyString())).thenThrow(new IllegalArgumentException());
    when(mock_orcidOauth2TokenDetailService.doesClientKnowUser(anyString(), anyString())).thenReturn(true);
    institutionalSignInManager.createUserConnectionAndNotify("idType", "remoteUserId", "displayName", "providerId", userOrcid, Collections.<String, String>emptyMap());
    verify(mock_userConnectionDao, never()).persist(any());
    verify(mock_notificationManager, never()).sendAcknowledgeMessage(userOrcid, clientId);
}
Also used : UserconnectionEntity(org.orcid.persistence.jpa.entities.UserconnectionEntity) Test(org.junit.Test)

Example 7 with UserconnectionEntity

use of org.orcid.persistence.jpa.entities.UserconnectionEntity in project ORCID-Source by ORCID.

the class UserConnectionDaoImpl method updateLoginInformation.

@Override
@Transactional
public void updateLoginInformation(UserconnectionPK pk) {
    UserconnectionEntity entity = find(pk);
    entity.setLastLogin(new Timestamp(new Date().getTime()));
    merge(entity);
}
Also used : UserconnectionEntity(org.orcid.persistence.jpa.entities.UserconnectionEntity) Timestamp(java.sql.Timestamp) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with UserconnectionEntity

use of org.orcid.persistence.jpa.entities.UserconnectionEntity in project ORCID-Source by ORCID.

the class InstitutionalSignInManagerImpl method createUserConnectionAndNotify.

@Override
@Transactional
public void createUserConnectionAndNotify(String idType, String remoteUserId, String displayName, String providerId, String userOrcid, Map<String, String> headers) throws UnsupportedEncodingException {
    UserconnectionEntity userConnectionEntity = userConnectionDao.findByProviderIdAndProviderUserIdAndIdType(remoteUserId, providerId, idType);
    if (userConnectionEntity == null) {
        LOGGER.info("No user connection found for idType={}, remoteUserId={}, displayName={}, providerId={}, userOrcid={}", new Object[] { idType, remoteUserId, displayName, providerId, userOrcid });
        userConnectionEntity = new UserconnectionEntity();
        String randomId = Long.toString(new Random(Calendar.getInstance().getTimeInMillis()).nextLong());
        UserconnectionPK pk = new UserconnectionPK(randomId, providerId, remoteUserId);
        userConnectionEntity.setOrcid(userOrcid);
        userConnectionEntity.setProfileurl(orcidUrlManager.getBaseUriHttp() + "/" + userOrcid);
        userConnectionEntity.setDisplayname(displayName);
        userConnectionEntity.setRank(1);
        userConnectionEntity.setId(pk);
        userConnectionEntity.setLinked(true);
        userConnectionEntity.setLastLogin(new Date());
        userConnectionEntity.setIdType(idType);
        userConnectionEntity.setConnectionSatus(UserConnectionStatus.NOTIFIED);
        userConnectionEntity.setHeadersJson(JsonUtils.convertToJsonString(headers));
        userConnectionDao.persist(userConnectionEntity);
    } else {
        LOGGER.info("Found existing user connection, {}", userConnectionEntity);
    }
    sendNotification(userOrcid, providerId);
}
Also used : Random(java.util.Random) UserconnectionEntity(org.orcid.persistence.jpa.entities.UserconnectionEntity) UserconnectionPK(org.orcid.persistence.jpa.entities.UserconnectionPK) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with UserconnectionEntity

use of org.orcid.persistence.jpa.entities.UserconnectionEntity in project ORCID-Source by ORCID.

the class ShibbolethController method signinHandler.

@RequestMapping(value = { "/signin" }, method = RequestMethod.GET)
public ModelAndView signinHandler(HttpServletRequest request, HttpServletResponse response, @RequestHeader Map<String, String> headers, ModelAndView mav) {
    LOGGER.info("Headers for shibboleth sign in: {}", headers);
    checkEnabled();
    mav.setViewName("social_link_signin");
    String shibIdentityProvider = headers.get(InstitutionalSignInManager.SHIB_IDENTITY_PROVIDER_HEADER);
    mav.addObject("providerId", shibIdentityProvider);
    String displayName = institutionalSignInManager.retrieveDisplayName(headers);
    mav.addObject("accountId", displayName);
    RemoteUser remoteUser = institutionalSignInManager.retrieveRemoteUser(headers);
    if (remoteUser == null) {
        LOGGER.info("Failed federated log in for {}", shibIdentityProvider);
        identityProviderManager.incrementFailedCount(shibIdentityProvider);
        mav.addObject("unsupportedInstitution", true);
        mav.addObject("institutionContactEmail", identityProviderManager.retrieveContactEmailByProviderid(shibIdentityProvider));
        return mav;
    }
    // Check if the Shibboleth user is already linked to an ORCID account.
    // If so sign them in automatically.
    UserconnectionEntity userConnectionEntity = userConnectionManager.findByProviderIdAndProviderUserIdAndIdType(remoteUser.getUserId(), shibIdentityProvider, remoteUser.getIdType());
    if (userConnectionEntity != null) {
        LOGGER.info("Found existing user connection: {}", userConnectionEntity);
        HeaderCheckResult checkHeadersResult = institutionalSignInManager.checkHeaders(parseOriginalHeaders(userConnectionEntity.getHeadersJson()), headers);
        if (!checkHeadersResult.isSuccess()) {
            mav.addObject("headerCheckFailed", true);
            return mav;
        }
        ProfileEntity profile = profileEntityCacheManager.retrieve(userConnectionEntity.getOrcid());
        if (profile.getUsing2FA()) {
            return new ModelAndView("institutional_2FA");
        }
        try {
            notifyUser(shibIdentityProvider, userConnectionEntity);
            processAuthentication(remoteUser, userConnectionEntity);
        } catch (AuthenticationException e) {
            // this should never happen
            SecurityContextHolder.getContext().setAuthentication(null);
            LOGGER.warn("User {0} should have been logged-in via Shibboleth, but was unable to due to a problem", remoteUser, e);
        }
        return new ModelAndView("redirect:" + calculateRedirectUrl(request, response));
    } else {
        // To avoid confusion, force the user to login to ORCID again
        mav.addObject("linkType", "shibboleth");
        mav.addObject("firstName", (headers.get(InstitutionalSignInManager.GIVEN_NAME_HEADER) == null) ? "" : headers.get(InstitutionalSignInManager.GIVEN_NAME_HEADER));
        mav.addObject("lastName", (headers.get(InstitutionalSignInManager.SN_HEADER) == null) ? "" : headers.get(InstitutionalSignInManager.SN_HEADER));
    }
    return mav;
}
Also used : HeaderCheckResult(org.orcid.pojo.HeaderCheckResult) RemoteUser(org.orcid.pojo.RemoteUser) AuthenticationException(org.springframework.security.core.AuthenticationException) ModelAndView(org.springframework.web.servlet.ModelAndView) UserconnectionEntity(org.orcid.persistence.jpa.entities.UserconnectionEntity) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with UserconnectionEntity

use of org.orcid.persistence.jpa.entities.UserconnectionEntity in project ORCID-Source by ORCID.

the class SocialController method signinHandler.

@RequestMapping(value = { "/access" }, method = RequestMethod.GET)
public ModelAndView signinHandler(HttpServletRequest request, HttpServletResponse response) {
    SocialType connectionType = socialContext.isSignedIn(request, response);
    if (connectionType != null) {
        Map<String, String> userMap = retrieveUserDetails(connectionType);
        String providerId = connectionType.value();
        String userId = socialContext.getUserId();
        UserconnectionEntity userConnectionEntity = userConnectionManager.findByProviderIdAndProviderUserId(userMap.get("providerUserId"), providerId);
        if (userConnectionEntity != null) {
            if (userConnectionEntity.isLinked()) {
                ProfileEntity profile = profileEntityCacheManager.retrieve(userConnectionEntity.getOrcid());
                if (profile.getUsing2FA()) {
                    return new ModelAndView("social_2FA");
                }
                UserconnectionPK pk = new UserconnectionPK(userId, providerId, userMap.get("providerUserId"));
                String aCredentials = new StringBuffer(providerId).append(":").append(userMap.get("providerUserId")).toString();
                PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(userConnectionEntity.getOrcid(), aCredentials);
                token.setDetails(getOrcidProfileUserDetails(userConnectionEntity.getOrcid()));
                Authentication authentication = authenticationManager.authenticate(token);
                userConnectionManager.updateLoginInformation(pk);
                SecurityContextHolder.getContext().setAuthentication(authentication);
                return new ModelAndView("redirect:" + calculateRedirectUrl(request, response));
            } else {
                ModelAndView mav = new ModelAndView();
                mav.setViewName("social_link_signin");
                mav.addObject("providerId", providerId);
                mav.addObject("accountId", getAccountIdForDisplay(userMap));
                mav.addObject("linkType", "social");
                mav.addObject("emailId", (userMap.get("email") == null) ? "" : userMap.get("email"));
                mav.addObject("firstName", (userMap.get("firstName") == null) ? "" : userMap.get("firstName"));
                mav.addObject("lastName", (userMap.get("lastName") == null) ? "" : userMap.get("lastName"));
                return mav;
            }
        } else {
            throw new UsernameNotFoundException("Could not find an orcid account associated with the email id.");
        }
    } else {
        throw new UsernameNotFoundException("Could not find an orcid account associated with the email id.");
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Authentication(org.springframework.security.core.Authentication) ModelAndView(org.springframework.web.servlet.ModelAndView) SocialType(org.orcid.frontend.spring.web.social.config.SocialType) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) UserconnectionEntity(org.orcid.persistence.jpa.entities.UserconnectionEntity) UserconnectionPK(org.orcid.persistence.jpa.entities.UserconnectionPK) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

UserconnectionEntity (org.orcid.persistence.jpa.entities.UserconnectionEntity)11 UserconnectionPK (org.orcid.persistence.jpa.entities.UserconnectionPK)5 Date (java.util.Date)4 Test (org.junit.Test)4 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 SocialType (org.orcid.frontend.spring.web.social.config.SocialType)3 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)3 HeaderCheckResult (org.orcid.pojo.HeaderCheckResult)2 RemoteUser (org.orcid.pojo.RemoteUser)2 DBUnitTest (org.orcid.test.DBUnitTest)2 Authentication (org.springframework.security.core.Authentication)2 AuthenticationException (org.springframework.security.core.AuthenticationException)2 PreAuthenticatedAuthenticationToken (org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 Timestamp (java.sql.Timestamp)1 Random (java.util.Random)1 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)1