Search in sources :

Example 1 with UserconnectionEntity

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

the class SocialAjaxAuthenticationSuccessHandler method linkSocialAccount.

public void linkSocialAccount(HttpServletRequest request, HttpServletResponse response) {
    SocialType connectionType = socialContext.isSignedIn(request, response);
    if (connectionType != null) {
        Map<String, String> userMap = retrieveUserDetails(connectionType);
        String providerId = connectionType.value();
        UserconnectionEntity userConnectionEntity = userConnectionManager.findByProviderIdAndProviderUserId(userMap.get("providerUserId"), providerId);
        if (userConnectionEntity != null) {
            if (!userConnectionEntity.isLinked()) {
                userConnectionEntity.setLinked(true);
                userConnectionEntity.setEmail(userMap.get("email"));
                userConnectionEntity.setOrcid(getRealUserOrcid());
                userConnectionManager.update(userConnectionEntity);
            }
        } 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) SocialType(org.orcid.frontend.spring.web.social.config.SocialType) UserconnectionEntity(org.orcid.persistence.jpa.entities.UserconnectionEntity)

Example 2 with UserconnectionEntity

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

the class ShibbolethController method post2FAVerificationCode.

@RequestMapping(value = { "/2FA/submitCode.json" }, method = RequestMethod.POST)
@ResponseBody
public TwoFactorAuthenticationCodes post2FAVerificationCode(@RequestBody TwoFactorAuthenticationCodes codes, HttpServletRequest request, HttpServletResponse response, @RequestHeader Map<String, String> headers) {
    checkEnabled();
    String shibIdentityProvider = headers.get(InstitutionalSignInManager.SHIB_IDENTITY_PROVIDER_HEADER);
    RemoteUser remoteUser = institutionalSignInManager.retrieveRemoteUser(headers);
    if (remoteUser == null) {
        LOGGER.info("Failed federated log in for {}", shibIdentityProvider);
        identityProviderManager.incrementFailedCount(shibIdentityProvider);
        codes.setRedirectUrl(orcidUrlManager.getBaseUrl() + "/shibboleth/signin");
        return codes;
    }
    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()) {
            codes.setRedirectUrl(orcidUrlManager.getBaseUrl() + "/shibboleth/signin");
            return codes;
        }
        validate2FACodes(userConnectionEntity.getOrcid(), codes);
        if (!codes.getErrors().isEmpty()) {
            return codes;
        }
        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);
        }
        codes.setRedirectUrl(calculateRedirectUrl(request, response));
        return codes;
    } else {
        codes.setRedirectUrl(orcidUrlManager.getBaseUrl() + "/shibboleth/signin");
        return codes;
    }
}
Also used : HeaderCheckResult(org.orcid.pojo.HeaderCheckResult) RemoteUser(org.orcid.pojo.RemoteUser) AuthenticationException(org.springframework.security.core.AuthenticationException) UserconnectionEntity(org.orcid.persistence.jpa.entities.UserconnectionEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with UserconnectionEntity

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

the class SocialController method post2FAVerificationCode.

@RequestMapping(value = { "/2FA/submitCode.json" }, method = RequestMethod.POST)
@ResponseBody
public TwoFactorAuthenticationCodes post2FAVerificationCode(@RequestBody TwoFactorAuthenticationCodes codes, 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()) {
                validate2FACodes(userConnectionEntity.getOrcid(), codes);
                if (!codes.getErrors().isEmpty()) {
                    return codes;
                }
                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);
                codes.setRedirectUrl(calculateRedirectUrl(request, response));
            } else {
                codes.setRedirectUrl(orcidUrlManager.getBaseUrl() + "/social/access");
            }
        } 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.");
    }
    return codes;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Authentication(org.springframework.security.core.Authentication) 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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with UserconnectionEntity

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

the class ProfileEntityManagerImplTest method testDeprecateProfile.

@Test
public void testDeprecateProfile() throws Exception {
    UserconnectionPK pk = new UserconnectionPK();
    pk.setProviderid("providerId");
    pk.setProvideruserid("provideruserid");
    pk.setUserid("4444-4444-4444-4441");
    UserconnectionEntity userConnection = new UserconnectionEntity();
    userConnection.setAccesstoken("blah");
    userConnection.setConnectionSatus(UserConnectionStatus.STARTED);
    userConnection.setDisplayname("blah");
    userConnection.setDateCreated(new Date());
    userConnection.setLastModified(new Date());
    userConnection.setEmail("blah@blah.com");
    userConnection.setOrcid("4444-4444-4444-4441");
    userConnection.setId(pk);
    userConnection.setRank(1);
    userConnectionDao.persist(userConnection);
    ProfileEntity profileEntityToDeprecate = profileEntityCacheManager.retrieve("4444-4444-4444-4441");
    assertNull(profileEntityToDeprecate.getPrimaryRecord());
    boolean result = profileEntityManager.deprecateProfile("4444-4444-4444-4441", "4444-4444-4444-4442", ProfileEntity.USER_DRIVEN_DEPRECATION, null);
    assertTrue(result);
    profileEntityToDeprecate = profileEntityCacheManager.retrieve("4444-4444-4444-4441");
    assertNotNull(profileEntityToDeprecate.getPrimaryRecord());
    assertNotNull(profileEntityToDeprecate.getDeprecatedMethod());
    assertEquals(ProfileEntity.USER_DRIVEN_DEPRECATION, profileEntityToDeprecate.getDeprecatedMethod());
    assertEquals("4444-4444-4444-4442", profileEntityToDeprecate.getPrimaryRecord().getId());
    assertEquals(0, userConnectionDao.findByOrcid("4444-4444-4444-4441").size());
}
Also used : UserconnectionPK(org.orcid.persistence.jpa.entities.UserconnectionPK) UserconnectionEntity(org.orcid.persistence.jpa.entities.UserconnectionEntity) Date(java.util.Date) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 5 with UserconnectionEntity

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

the class InstitutionalSignInManagerTest method testDontPersistIfUserConnectionAlreadyExists.

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

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