Search in sources :

Example 1 with ApplicationSummary

use of org.orcid.pojo.ApplicationSummary in project ORCID-Source by ORCID.

the class ProfileEntityManagerImplTest method testDontGetDuplicatedApplications.

@SuppressWarnings("unused")
@Test
public void testDontGetDuplicatedApplications() {
    Long seed = System.currentTimeMillis();
    Date expiration = new Date(System.currentTimeMillis() + 10000);
    // Displayed
    OrcidOauth2TokenDetail token1 = createToken(CLIENT_ID_1, "token-1-" + seed, USER_ORCID, expiration, "/read-limited", false);
    // Displayed
    OrcidOauth2TokenDetail token2 = createToken(CLIENT_ID_1, "token-2-" + seed, USER_ORCID, expiration, "/orcid-profile/read-limited", false);
    // Displayed
    OrcidOauth2TokenDetail token3 = createToken(CLIENT_ID_1, "token-3-" + seed, USER_ORCID, expiration, "/activities/update", false);
    OrcidOauth2TokenDetail token4 = createToken(CLIENT_ID_1, "token-4-" + seed, USER_ORCID, expiration, "/read-limited", false);
    OrcidOauth2TokenDetail token5 = createToken(CLIENT_ID_1, "token-5-" + seed, USER_ORCID, expiration, "/orcid-profile/read-limited", false);
    OrcidOauth2TokenDetail token6 = createToken(CLIENT_ID_1, "token-6-" + seed, USER_ORCID, expiration, "/activities/update", false);
    OrcidOauth2TokenDetail token7 = createToken(CLIENT_ID_1, "token-7-" + seed, USER_ORCID, expiration, "/read-limited", false);
    OrcidOauth2TokenDetail token8 = createToken(CLIENT_ID_1, "token-8-" + seed, USER_ORCID, expiration, "/orcid-profile/read-limited", false);
    OrcidOauth2TokenDetail token9 = createToken(CLIENT_ID_1, "token-9-" + seed, USER_ORCID, expiration, "/activities/update", false);
    // Displayed
    OrcidOauth2TokenDetail token10 = createToken(CLIENT_ID_1, "token-10-" + seed, USER_ORCID, expiration, "/person/read-limited", false);
    OrcidOauth2TokenDetail token11 = createToken(CLIENT_ID_1, "token-11-" + seed, USER_ORCID, expiration, "/person/read-limited", false);
    List<ApplicationSummary> applications = profileEntityManager.getApplications(USER_ORCID);
    assertNotNull(applications);
    assertEquals(4, applications.size());
    boolean found1 = false, found2 = false, found3 = false, found10 = false;
    for (ApplicationSummary summary : applications) {
        assertNotNull(summary.getTokenId());
        if (summary.getTokenId().equals(String.valueOf(token1.getId()))) {
            found1 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token2.getId()))) {
            found2 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token3.getId()))) {
            found3 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token10.getId()))) {
            found10 = true;
        }
    }
    assertTrue(found1);
    assertTrue(found2);
    assertTrue(found3);
    assertTrue(found10);
    // Revoke them to check revoking one revokes all the ones with the same scopes
    profileEntityManager.disableApplication(token1.getId(), USER_ORCID);
    profileEntityManager.disableApplication(token2.getId(), USER_ORCID);
    applications = profileEntityManager.getApplications(USER_ORCID);
    assertNotNull(applications);
    assertEquals(2, applications.size());
    found1 = found2 = found3 = found10 = false;
    for (ApplicationSummary summary : applications) {
        assertNotNull(summary.getTokenId());
        if (summary.getTokenId().equals(String.valueOf(token1.getId()))) {
            found1 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token2.getId()))) {
            found2 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token3.getId()))) {
            found3 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token10.getId()))) {
            found10 = true;
        }
    }
    assertFalse(found1);
    assertFalse(found2);
    assertTrue(found3);
    assertTrue(found10);
    // Revoke them all
    profileEntityManager.disableApplication(token3.getId(), USER_ORCID);
    profileEntityManager.disableApplication(token10.getId(), USER_ORCID);
    applications = profileEntityManager.getApplications(USER_ORCID);
    assertNotNull(applications);
    assertTrue(applications.isEmpty());
}
Also used : ApplicationSummary(org.orcid.pojo.ApplicationSummary) Date(java.util.Date) OrcidOauth2TokenDetail(org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 2 with ApplicationSummary

use of org.orcid.pojo.ApplicationSummary in project ORCID-Source by ORCID.

the class ProfileEntityManagerImpl method getApplications.

@Override
public List<ApplicationSummary> getApplications(String orcid) {
    List<OrcidOauth2TokenDetail> tokenDetails = orcidOauth2TokenService.findByUserName(orcid);
    List<ApplicationSummary> applications = new ArrayList<ApplicationSummary>();
    Map<Pair<String, Set<ScopePathType>>, ApplicationSummary> existingApplications = new HashMap<Pair<String, Set<ScopePathType>>, ApplicationSummary>();
    if (tokenDetails != null && !tokenDetails.isEmpty()) {
        for (OrcidOauth2TokenDetail token : tokenDetails) {
            if (token.getTokenDisabled() == null || !token.getTokenDisabled()) {
                ClientDetailsEntity client = clientDetailsEntityCacheManager.retrieve(token.getClientDetailsId());
                if (client != null) {
                    ApplicationSummary applicationSummary = new ApplicationSummary();
                    // Check the scopes
                    Set<ScopePathType> scopesGrantedToClient = ScopePathType.getScopesFromSpaceSeparatedString(token.getScope());
                    Map<ScopePathType, String> scopePathMap = new HashMap<ScopePathType, String>();
                    String scopeFullPath = ScopePathType.class.getName() + ".";
                    for (ScopePathType tempScope : scopesGrantedToClient) {
                        try {
                            scopePathMap.put(tempScope, localeManager.resolveMessage(scopeFullPath + tempScope.toString()));
                        } catch (NoSuchMessageException e) {
                            LOGGER.warn("No message to display for scope " + tempScope.toString());
                        }
                    }
                    // the application summary element
                    if (!scopePathMap.isEmpty()) {
                        applicationSummary.setScopePaths(scopePathMap);
                        applicationSummary.setOrcidHost(orcidUrlManager.getBaseHost());
                        applicationSummary.setOrcidUri(orcidUrlManager.getBaseUrl() + "/" + client.getId());
                        applicationSummary.setOrcidPath(client.getId());
                        applicationSummary.setName(client.getClientName());
                        applicationSummary.setWebsiteValue(client.getClientWebsite());
                        applicationSummary.setApprovalDate(token.getDateCreated());
                        applicationSummary.setTokenId(String.valueOf(token.getId()));
                        // Add member information
                        if (!PojoUtil.isEmpty(client.getGroupProfileId())) {
                            ProfileEntity member = profileEntityCacheManager.retrieve(client.getGroupProfileId());
                            applicationSummary.setGroupOrcidPath(member.getId());
                            applicationSummary.setGroupName(getMemberDisplayName(member));
                        }
                        if (shouldBeAddedToTheApplicationsList(applicationSummary, scopesGrantedToClient, existingApplications)) {
                            applications.add(applicationSummary);
                        }
                    }
                }
            }
        }
    }
    return applications;
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) NoSuchMessageException(org.springframework.context.NoSuchMessageException) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ApplicationSummary(org.orcid.pojo.ApplicationSummary) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) ScopePathType(org.orcid.jaxb.model.message.ScopePathType) OrcidOauth2TokenDetail(org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail) Pair(org.apache.commons.lang3.tuple.Pair)

Example 3 with ApplicationSummary

use of org.orcid.pojo.ApplicationSummary in project ORCID-Source by ORCID.

the class ProfileEntityManagerImpl method getApplications.

@Override
public List<ApplicationSummary> getApplications(String orcid) {
    List<OrcidOauth2TokenDetail> tokenDetails = orcidOauth2TokenService.findByUserName(orcid);
    List<ApplicationSummary> applications = new ArrayList<ApplicationSummary>();
    Map<Pair<String, Set<ScopePathType>>, ApplicationSummary> existingApplications = new HashMap<Pair<String, Set<ScopePathType>>, ApplicationSummary>();
    if (tokenDetails != null && !tokenDetails.isEmpty()) {
        for (OrcidOauth2TokenDetail token : tokenDetails) {
            if (token.getTokenDisabled() == null || !token.getTokenDisabled()) {
                ClientDetailsEntity client = clientDetailsEntityCacheManager.retrieve(token.getClientDetailsId());
                if (client != null) {
                    ApplicationSummary applicationSummary = new ApplicationSummary();
                    // Check the scopes
                    Set<ScopePathType> scopesGrantedToClient = ScopePathType.getScopesFromSpaceSeparatedString(token.getScope());
                    Map<ScopePathType, String> scopePathMap = new HashMap<ScopePathType, String>();
                    String scopeFullPath = ScopePathType.class.getName() + ".";
                    for (ScopePathType tempScope : scopesGrantedToClient) {
                        try {
                            scopePathMap.put(tempScope, localeManager.resolveMessage(scopeFullPath + tempScope.toString()));
                        } catch (NoSuchMessageException e) {
                            LOGGER.warn("No message to display for scope " + tempScope.toString());
                        }
                    }
                    // the application summary element
                    if (!scopePathMap.isEmpty()) {
                        applicationSummary.setScopePaths(scopePathMap);
                        applicationSummary.setOrcidHost(orcidUrlManager.getBaseHost());
                        applicationSummary.setOrcidUri(orcidUrlManager.getBaseUriHttp() + "/" + client.getId());
                        applicationSummary.setOrcidPath(client.getId());
                        applicationSummary.setName(client.getClientName());
                        applicationSummary.setWebsiteValue(client.getClientWebsite());
                        applicationSummary.setApprovalDate(token.getDateCreated());
                        applicationSummary.setTokenId(String.valueOf(token.getId()));
                        // Add member information
                        if (!PojoUtil.isEmpty(client.getGroupProfileId())) {
                            ProfileEntity member = profileEntityCacheManager.retrieve(client.getGroupProfileId());
                            applicationSummary.setGroupOrcidPath(member.getId());
                            applicationSummary.setGroupName(getMemberDisplayName(member));
                        }
                        if (shouldBeAddedToTheApplicationsList(applicationSummary, scopesGrantedToClient, existingApplications)) {
                            applications.add(applicationSummary);
                        }
                    }
                }
            }
        }
    }
    return applications;
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) NoSuchMessageException(org.springframework.context.NoSuchMessageException) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ApplicationSummary(org.orcid.pojo.ApplicationSummary) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) ScopePathType(org.orcid.jaxb.model.message.ScopePathType) OrcidOauth2TokenDetail(org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail) Pair(org.apache.commons.lang3.tuple.Pair)

Example 4 with ApplicationSummary

use of org.orcid.pojo.ApplicationSummary in project ORCID-Source by ORCID.

the class ProfileEntityManagerImplTest method testGetApplications.

@Test
public void testGetApplications() {
    Date expiration = new Date(System.currentTimeMillis() + 10000);
    OrcidOauth2TokenDetail token1 = createToken(CLIENT_ID_1, "token-1", USER_ORCID, expiration, "/read-limited", false);
    OrcidOauth2TokenDetail token2 = createToken(CLIENT_ID_1, "token-2", USER_ORCID, expiration, "/orcid-profile/read-limited", false);
    OrcidOauth2TokenDetail token3 = createToken(CLIENT_ID_1, "token-3", USER_ORCID, expiration, "/activities/update", false);
    OrcidOauth2TokenDetail token4 = createToken(CLIENT_ID_1, "token-4", USER_ORCID, expiration, "/activities/read-limited", false);
    OrcidOauth2TokenDetail token5 = createToken(CLIENT_ID_1, "token-5", USER_ORCID, expiration, "/orcid-works/read-limited", false);
    List<ApplicationSummary> applications = profileEntityManager.getApplications(USER_ORCID);
    assertNotNull(applications);
    assertEquals(5, applications.size());
    boolean found1 = false, found2 = false, found3 = false, found4 = false, found5 = false;
    for (ApplicationSummary summary : applications) {
        assertNotNull(summary.getTokenId());
        if (summary.getTokenId().equals(String.valueOf(token1.getId()))) {
            found1 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token2.getId()))) {
            found2 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token3.getId()))) {
            found3 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token4.getId()))) {
            found4 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token5.getId()))) {
            found5 = true;
        }
    }
    assertTrue(found1);
    assertTrue(found2);
    assertTrue(found3);
    assertTrue(found4);
    assertTrue(found5);
    // Assert we can delete them
    profileEntityManager.disableApplication(token1.getId(), USER_ORCID);
    profileEntityManager.disableApplication(token5.getId(), USER_ORCID);
    found1 = found2 = found3 = found4 = found5 = false;
    applications = profileEntityManager.getApplications(USER_ORCID);
    assertEquals(3, applications.size());
    for (ApplicationSummary summary : applications) {
        assertNotNull(summary.getTokenId());
        if (summary.getTokenId().equals(String.valueOf(token1.getId()))) {
            found1 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token2.getId()))) {
            found2 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token3.getId()))) {
            found3 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token4.getId()))) {
            found4 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token5.getId()))) {
            found5 = true;
        }
    }
    assertFalse(found1);
    assertTrue(found2);
    assertTrue(found3);
    assertTrue(found4);
    assertFalse(found5);
    // Revoke the others
    profileEntityManager.disableApplication(token2.getId(), USER_ORCID);
    profileEntityManager.disableApplication(token3.getId(), USER_ORCID);
    profileEntityManager.disableApplication(token4.getId(), USER_ORCID);
    applications = profileEntityManager.getApplications(USER_ORCID);
    assertNotNull(applications);
    assertTrue(applications.isEmpty());
}
Also used : ApplicationSummary(org.orcid.pojo.ApplicationSummary) Date(java.util.Date) OrcidOauth2TokenDetail(org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 5 with ApplicationSummary

use of org.orcid.pojo.ApplicationSummary in project ORCID-Source by ORCID.

the class ProfileEntityManagerImplTest method testGetApplications.

@Test
public void testGetApplications() {
    Date expiration = new Date(System.currentTimeMillis() + 10000);
    OrcidOauth2TokenDetail token1 = createToken(CLIENT_ID_1, "token-1", USER_ORCID, expiration, "/read-limited", false);
    OrcidOauth2TokenDetail token2 = createToken(CLIENT_ID_1, "token-2", USER_ORCID, expiration, "/orcid-profile/read-limited", false);
    OrcidOauth2TokenDetail token3 = createToken(CLIENT_ID_1, "token-3", USER_ORCID, expiration, "/activities/update", false);
    OrcidOauth2TokenDetail token4 = createToken(CLIENT_ID_1, "token-4", USER_ORCID, expiration, "/activities/read-limited", false);
    OrcidOauth2TokenDetail token5 = createToken(CLIENT_ID_1, "token-5", USER_ORCID, expiration, "/orcid-works/read-limited", false);
    List<ApplicationSummary> applications = profileEntityManager.getApplications(USER_ORCID);
    assertNotNull(applications);
    assertEquals(5, applications.size());
    boolean found1 = false, found2 = false, found3 = false, found4 = false, found5 = false;
    for (ApplicationSummary summary : applications) {
        assertNotNull(summary.getTokenId());
        if (summary.getTokenId().equals(String.valueOf(token1.getId()))) {
            found1 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token2.getId()))) {
            found2 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token3.getId()))) {
            found3 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token4.getId()))) {
            found4 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token5.getId()))) {
            found5 = true;
        }
    }
    assertTrue(found1);
    assertTrue(found2);
    assertTrue(found3);
    assertTrue(found4);
    assertTrue(found5);
    // Assert we can delete them
    profileEntityManager.disableApplication(token1.getId(), USER_ORCID);
    profileEntityManager.disableApplication(token5.getId(), USER_ORCID);
    found1 = found2 = found3 = found4 = found5 = false;
    applications = profileEntityManager.getApplications(USER_ORCID);
    assertEquals(3, applications.size());
    for (ApplicationSummary summary : applications) {
        assertNotNull(summary.getTokenId());
        if (summary.getTokenId().equals(String.valueOf(token1.getId()))) {
            found1 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token2.getId()))) {
            found2 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token3.getId()))) {
            found3 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token4.getId()))) {
            found4 = true;
        } else if (summary.getTokenId().equals(String.valueOf(token5.getId()))) {
            found5 = true;
        }
    }
    assertFalse(found1);
    assertTrue(found2);
    assertTrue(found3);
    assertTrue(found4);
    assertFalse(found5);
    // Revoke the others
    profileEntityManager.disableApplication(token2.getId(), USER_ORCID);
    profileEntityManager.disableApplication(token3.getId(), USER_ORCID);
    profileEntityManager.disableApplication(token4.getId(), USER_ORCID);
    applications = profileEntityManager.getApplications(USER_ORCID);
    assertNotNull(applications);
    assertTrue(applications.isEmpty());
}
Also used : ApplicationSummary(org.orcid.pojo.ApplicationSummary) Date(java.util.Date) OrcidOauth2TokenDetail(org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Aggregations

OrcidOauth2TokenDetail (org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail)6 ApplicationSummary (org.orcid.pojo.ApplicationSummary)6 Date (java.util.Date)4 Test (org.junit.Test)4 DBUnitTest (org.orcid.test.DBUnitTest)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 Pair (org.apache.commons.lang3.tuple.Pair)2 ScopePathType (org.orcid.jaxb.model.message.ScopePathType)2 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)2 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)2 NoSuchMessageException (org.springframework.context.NoSuchMessageException)2