Search in sources :

Example 81 with ProfileEntity

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

the class OrcidTokenStoreServiceImpl method getOAuth2AuthenticationFromDetails.

private OAuth2Authentication getOAuth2AuthenticationFromDetails(OrcidOauth2TokenDetail details) {
    if (details != null) {
        ClientDetailsEntity clientDetailsEntity = clientDetailsEntityCacheManager.retrieve(details.getClientDetailsId());
        Authentication authentication = null;
        AuthorizationRequest request = null;
        if (clientDetailsEntity != null) {
            //Check member is not locked                
            orcidOAuth2RequestValidator.validateClientIsEnabled(clientDetailsEntity);
            Set<String> scopes = OAuth2Utils.parseParameterList(details.getScope());
            request = new AuthorizationRequest(clientDetailsEntity.getClientId(), scopes);
            request.setAuthorities(clientDetailsEntity.getAuthorities());
            Set<String> resourceIds = new HashSet<>();
            resourceIds.add(details.getResourceId());
            request.setResourceIds(resourceIds);
            request.setApproved(details.isApproved());
            ProfileEntity profile = details.getProfile();
            if (profile != null) {
                authentication = new OrcidOauth2UserAuthentication(profile, details.isApproved());
            }
        }
        return new OrcidOAuth2Authentication(request, authentication, details.getTokenValue());
    }
    throw new InvalidTokenException("Token not found");
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) OrcidOauth2UserAuthentication(org.orcid.core.oauth.OrcidOauth2UserAuthentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OrcidOAuth2Authentication(org.orcid.core.oauth.OrcidOAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OrcidOauth2UserAuthentication(org.orcid.core.oauth.OrcidOauth2UserAuthentication) OrcidOAuth2Authentication(org.orcid.core.oauth.OrcidOAuth2Authentication) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) HashSet(java.util.HashSet)

Example 82 with ProfileEntity

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

the class DefaultPermissionChecker method performUserChecks.

private void performUserChecks(OAuth2Authentication oAuth2Authentication, ScopePathType requiredScope, OrcidMessage orcidMessage, String orcid) {
    ProfileEntity principal = (ProfileEntity) oAuth2Authentication.getPrincipal();
    String userOrcid = principal.getId();
    if (orcidMessage != null && orcidMessage.getOrcidProfile() != null && orcidMessage.getOrcidProfile().getOrcidIdentifier() != null && StringUtils.isNotBlank(orcid)) {
        String messageOrcid = orcidMessage.getOrcidProfile().getOrcidIdentifier().getPath();
        // the request is invalid
        if (!messageOrcid.equals(orcid)) {
            throw new IllegalArgumentException("The ORCID in the body and the URI do not match. Body ORCID: " + messageOrcid + " URI ORCID: " + orcid + " do NOT match.");
        }
    }
    // through
    if (userOrcid.equals(orcid)) {
        return;
    } else {
        if (profileDao.isProfileDeprecated(orcid)) {
            ProfileEntity entity = profileEntityCacheManager.retrieve(orcid);
            Map<String, String> params = new HashMap<String, String>();
            StringBuffer primary = new StringBuffer(baseUrl).append("/").append(entity.getPrimaryRecord().getId());
            params.put(OrcidDeprecatedException.ORCID, primary.toString());
            if (entity.getDeprecatedDate() != null) {
                XMLGregorianCalendar calendar = DateUtils.convertToXMLGregorianCalendar(entity.getDeprecatedDate());
                params.put(OrcidDeprecatedException.DEPRECATED_DATE, calendar.toString());
            }
            throw new OrcidDeprecatedException(params);
        }
    }
    throw new AccessControlException("You do not have the required permissions.");
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) HashMap(java.util.HashMap) OrcidDeprecatedException(org.orcid.core.exception.OrcidDeprecatedException) AccessControlException(java.security.AccessControlException) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity)

Example 83 with ProfileEntity

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

the class JpaJaxbEntityAdapterToProfileEntityTest method initDelegationProfiles.

@Before
public void initDelegationProfiles() {
    ProfileEntity entityReceivingPermission = new ProfileEntity();
    entityReceivingPermission.setId("1111-1111-1111-1115");
    profileDao.merge(entityReceivingPermission);
    ProfileEntity entityGivingPermission = new ProfileEntity();
    entityGivingPermission.setId("2222-2222-2222-2229");
    profileDao.merge(entityGivingPermission);
    ProfileEntity source1 = new ProfileEntity();
    source1.setId("2111-1111-1111-1114");
    profileDao.merge(source1);
}
Also used : ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Before(org.junit.Before)

Example 84 with ProfileEntity

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

the class JpaJaxbEntityAdapterToOrcidProfileTest method testToOrcidProfileWithNewWayOfDoingEmails.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testToOrcidProfileWithNewWayOfDoingEmails() throws SAXException, IOException {
    ProfileEntity profileEntity = profileDao.find("4444-4444-4444-4445");
    long start = System.currentTimeMillis();
    OrcidProfile orcidProfile = adapter.toOrcidProfile(profileEntity);
    System.out.println("Took: " + Long.toString(System.currentTimeMillis() - start));
    ContactDetails contactDetails = orcidProfile.getOrcidBio().getContactDetails();
    Email primaryEmail = contactDetails.retrievePrimaryEmail();
    assertNotNull(primaryEmail);
    assertTrue(primaryEmail.isPrimary());
    assertTrue(primaryEmail.isCurrent());
    assertFalse(primaryEmail.isVerified());
    assertEquals(Visibility.PRIVATE, primaryEmail.getVisibility());
    assertEquals("andrew@timothy.com", primaryEmail.getValue());
    assertEquals("4444-4444-4444-4441", primaryEmail.getSource());
    Email nonPrimaryEmail = contactDetails.getEmailByString("andrew2@timothy.com");
    assertNotNull(nonPrimaryEmail);
    assertFalse(nonPrimaryEmail.isPrimary());
    assertFalse(nonPrimaryEmail.isCurrent());
    assertFalse(nonPrimaryEmail.isVerified());
    assertEquals(Visibility.PRIVATE, nonPrimaryEmail.getVisibility());
    assertEquals("andrew2@timothy.com", nonPrimaryEmail.getValue());
    assertEquals("4444-4444-4444-4441", nonPrimaryEmail.getSource());
    assertEquals(2, contactDetails.getEmail().size());
    validateAgainstSchema(new OrcidMessage(orcidProfile));
}
Also used : OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) Email(org.orcid.jaxb.model.message.Email) ContactDetails(org.orcid.jaxb.model.message.ContactDetails) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 85 with ProfileEntity

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

the class JpaJaxbEntityAdapterToOrcidProfileTest method testToOrcidProfile.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testToOrcidProfile() throws SAXException, IOException {
    ProfileEntity profileEntity = profileDao.find("4444-4444-4444-4443");
    long start = System.currentTimeMillis();
    OrcidProfile orcidProfile = adapter.toOrcidProfile(profileEntity);
    System.out.println("Took: " + Long.toString(System.currentTimeMillis() - start));
    System.out.println(orcidProfile);
    checkOrcidProfile(orcidProfile);
    validateAgainstSchema(new OrcidMessage(orcidProfile));
}
Also used : OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)231 Test (org.junit.Test)65 Date (java.util.Date)64 Transactional (org.springframework.transaction.annotation.Transactional)58 DBUnitTest (org.orcid.test.DBUnitTest)44 HashMap (java.util.HashMap)41 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)37 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)36 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)33 HashSet (java.util.HashSet)30 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)28 Rollback (org.springframework.test.annotation.Rollback)25 RecordNameEntity (org.orcid.persistence.jpa.entities.RecordNameEntity)20 Set (java.util.Set)16 ArrayList (java.util.ArrayList)15 OrcidProfile (org.orcid.jaxb.model.message.OrcidProfile)14 EmailEntity (org.orcid.persistence.jpa.entities.EmailEntity)14 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)13 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)13 Authentication (org.springframework.security.core.Authentication)12