Search in sources :

Example 26 with ScopePathType

use of org.orcid.jaxb.model.message.ScopePathType in project ORCID-Source by ORCID.

the class AddScopesToExistingClients method updateScopes.

private void updateScopes(ClientDetailsEntity clientDetails) {
    for (ScopePathType scope : scopes) {
        boolean alreadyHaveReadPublicScope = false;
        for (ClientScopeEntity existingScope : clientDetails.getClientScopes()) {
            if (scope.value().equals(existingScope.getScopeType())) {
                alreadyHaveReadPublicScope = true;
                break;
            }
        }
        if (!alreadyHaveReadPublicScope) {
            ClientScopeEntity clientScope = new ClientScopeEntity();
            clientScope.setClientDetailsEntity(clientDetails);
            clientScope.setScopeType(scope.value());
            clientScope.setDateCreated(new Date());
            clientScope.setLastModified(new Date());
            clientDetails.getClientScopes().add(clientScope);
            clientDetailsManager.merge(clientDetails);
            clientsUpdated += 1;
            System.out.println("Client " + clientDetails.getId() + " has been updated");
        } else {
            System.out.println("Client " + clientDetails.getId() + " already have the " + scope.value() + " scope");
        }
    }
}
Also used : ClientScopeEntity(org.orcid.persistence.jpa.entities.ClientScopeEntity) ScopePathType(org.orcid.jaxb.model.message.ScopePathType) Date(java.util.Date)

Example 27 with ScopePathType

use of org.orcid.jaxb.model.message.ScopePathType in project ORCID-Source by ORCID.

the class OrcidClientGroupManagerImpl method updateClientDetailsEntityFromClient.

/**
     * Updates an existing profile entity with the information that comes in a
     * OrcidClient
     * 
     * @param client
     *            The OrcidClient that contains the new information
     * @param clientDetailsEntity
     *            The client profile entity that will be updated
     * @param isUpdate
     *            Indicates if this will be an update or is a new client
     */
private void updateClientDetailsEntityFromClient(OrcidClient client, ClientDetailsEntity clientDetailsEntity, boolean isUpdate) {
    clientDetailsEntity.setClientType(client.getType());
    clientDetailsEntity.setClientName(client.getDisplayName());
    clientDetailsEntity.setClientDescription(client.getShortDescription());
    clientDetailsEntity.setClientWebsite(client.getWebsite());
    clientDetailsEntity.setPersistentTokensEnabled(client.isPersistentTokenEnabled());
    clientDetailsEntity.setAuthenticationProviderId(client.getIdp());
    clientDetailsEntity.setAllowAutoDeprecate(client.getAllowAutoDeprecate());
    Set<ClientRedirectUriEntity> clientRedirectUriEntities = clientDetailsEntity.getClientRegisteredRedirectUris();
    Map<String, ClientRedirectUriEntity> clientRedirectUriEntitiesMap = ClientRedirectUriEntity.mapByUriAndType(clientRedirectUriEntities);
    clientRedirectUriEntities.clear();
    Set<RedirectUri> redirectUrisToAdd = new HashSet<RedirectUri>();
    redirectUrisToAdd.addAll(client.getRedirectUris().getRedirectUri());
    for (RedirectUri redirectUri : redirectUrisToAdd) {
        String rUriKey = ClientRedirectUriEntity.getUriAndTypeKey(redirectUri);
        // If there is a redirect uri with the same uri
        if (clientRedirectUriEntitiesMap.containsKey(rUriKey)) {
            // Check if it have the same scope and update it
            // If it doesnt have the same scope
            ClientRedirectUriEntity existingEntity = clientRedirectUriEntitiesMap.get(rUriKey);
            // Update the scopes
            List<ScopePathType> clientPredefinedScopes = redirectUri.getScope();
            if (clientPredefinedScopes != null) {
                existingEntity.setPredefinedClientScope(ScopePathType.getScopesAsSingleString(clientPredefinedScopes));
            }
            existingEntity.setUriActType(redirectUri.getActType());
            existingEntity.setUriGeoArea(redirectUri.getGeoArea());
            // Add to the list
            clientRedirectUriEntities.add(existingEntity);
        } else {
            ClientRedirectUriEntity clientRedirectUriEntity = new ClientRedirectUriEntity(redirectUri.getValue(), clientDetailsEntity);
            List<ScopePathType> clientPredefinedScopes = redirectUri.getScope();
            clientRedirectUriEntity.setRedirectUriType(redirectUri.getType().value());
            String allPreDefScopes = null;
            if (clientPredefinedScopes != null) {
                allPreDefScopes = ScopePathType.getScopesAsSingleString(clientPredefinedScopes);
            }
            clientRedirectUriEntity.setPredefinedClientScope(allPreDefScopes);
            clientRedirectUriEntity.setUriActType(redirectUri.getActType());
            clientRedirectUriEntity.setUriGeoArea(redirectUri.getGeoArea());
            clientRedirectUriEntities.add(clientRedirectUriEntity);
        }
    }
}
Also used : ScopePathType(org.orcid.jaxb.model.message.ScopePathType) RedirectUri(org.orcid.jaxb.model.clientgroup.RedirectUri) ClientRedirectUriEntity(org.orcid.persistence.jpa.entities.ClientRedirectUriEntity) HashSet(java.util.HashSet)

Example 28 with ScopePathType

use of org.orcid.jaxb.model.message.ScopePathType in project ORCID-Source by ORCID.

the class ThirdPartyLinkManagerImpl method getClients.

private List<OrcidClient> getClients(RedirectUriType rut) {
    List<OrcidClient> orcidClients = new ArrayList<OrcidClient>();
    List<ClientRedirectUriEntity> entitiesWithPredefinedScopes = clientRedirectDao.findClientDetailsWithRedirectScope();
    for (ClientRedirectUriEntity entity : entitiesWithPredefinedScopes) {
        if (rut.value().equals(entity.getRedirectUriType())) {
            ClientDetailsEntity clientDetails = entity.getClientDetailsEntity();
            RedirectUri redirectUri = new RedirectUri(entity.getRedirectUri());
            String prefefinedScopes = entity.getPredefinedClientScope();
            redirectUri.setScope(new ArrayList<ScopePathType>(ScopePathType.getScopesFromSpaceSeparatedString(prefefinedScopes)));
            redirectUri.setType(RedirectUriType.fromValue(entity.getRedirectUriType()));
            redirectUri.setActType(entity.getUriActType());
            redirectUri.setGeoArea(entity.getUriGeoArea());
            RedirectUris redirectUris = new RedirectUris();
            redirectUris.getRedirectUri().add(redirectUri);
            OrcidClient minimalClientDetails = new OrcidClient();
            minimalClientDetails.setDisplayName(clientDetails.getClientName());
            minimalClientDetails.setShortDescription(clientDetails.getClientDescription());
            minimalClientDetails.setClientId(clientDetails.getClientId());
            minimalClientDetails.setRedirectUris(redirectUris);
            orcidClients.add(minimalClientDetails);
        }
    }
    return orcidClients;
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) ScopePathType(org.orcid.jaxb.model.message.ScopePathType) OrcidClient(org.orcid.jaxb.model.clientgroup.OrcidClient) ArrayList(java.util.ArrayList) RedirectUri(org.orcid.jaxb.model.clientgroup.RedirectUri) RedirectUris(org.orcid.jaxb.model.clientgroup.RedirectUris) ClientRedirectUriEntity(org.orcid.persistence.jpa.entities.ClientRedirectUriEntity)

Example 29 with ScopePathType

use of org.orcid.jaxb.model.message.ScopePathType in project ORCID-Source by ORCID.

the class OrcidClientGroupManagerTest method testCreateOrcidClientGroup.

@Test
public void testCreateOrcidClientGroup() {
    OrcidClientGroup createdGroup = orcidClientGroupManager.createOrUpdateOrcidClientGroup(group);
    assertNotNull(createdGroup);
    assertEquals(group.getGroupName(), createdGroup.getGroupName());
    assertEquals(group.getEmail(), createdGroup.getEmail());
    assertNotNull(createdGroup.getGroupOrcid());
    List<OrcidClient> createdClients = createdGroup.getOrcidClient();
    assertNotNull(createdClients);
    assertEquals(2, createdClients.size());
    Map<String, OrcidClient> createdClientsMappedByName = new HashMap<String, OrcidClient>();
    for (OrcidClient createdClient : createdClients) {
        assertNotNull(createdClient.getClientId());
        assertNotNull(createdClient.getClientSecret());
        assertNotNull(createdClient.getIdp());
        createdClientsMappedByName.put(createdClient.getDisplayName(), createdClient);
    }
    OrcidClient complexityClient = createdClientsMappedByName.get("Ecological Complexity");
    assertNotNull(complexityClient);
    assertEquals("http://www.journals.elsevier.com/ecological-complexity", complexityClient.getWebsite());
    assertEquals("An International Journal on Biocomplexity in the Environment and Theoretical Ecology", complexityClient.getShortDescription());
    List<RedirectUri> createdRedirectUris = complexityClient.getRedirectUris().getRedirectUri();
    assertNotNull(createdRedirectUris);
    assertEquals(1, createdRedirectUris.size());
    assertEquals("http://www.journals.elsevier.com/ecological-complexity/orcid-callback", createdRedirectUris.get(0).getValue());
    assertEquals("grant-read-wizard", createdRedirectUris.get(0).getType().value());
    List<ScopePathType> scopesForRedirect = createdRedirectUris.get(0).getScope();
    assertTrue(scopesForRedirect.size() == 2);
    assertTrue(scopesForRedirect.contains(ScopePathType.ORCID_PROFILE_CREATE) && scopesForRedirect.contains(ScopePathType.ORCID_BIO_READ_LIMITED));
    assertEquals("http://www.journals.elsevier.com/ecological-complexity/orcid-callback", createdRedirectUris.get(0).getValue());
    assertEquals("grant-read-wizard", createdRedirectUris.get(0).getType().value());
    // Look up client details directly to check scopes
    ClientDetailsEntity complexityEntity = clientDetailsManager.findByClientId(complexityClient.getClientId());
    Set<String> clientScopeTypes = complexityEntity.getScope();
    assertNotNull(clientScopeTypes);
    assertTrue(clientScopeTypes.contains("/orcid-bio/update"));
    assertTrue(clientScopeTypes.contains("/orcid-bio/read-limited"));
    assertTrue(clientScopeTypes.contains("/orcid-works/read-limited"));
    assertFalse(clientScopeTypes.contains("/orcid-profile/create"));
    assertTrue(clientScopeTypes.contains("/authenticate"));
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) ScopePathType(org.orcid.jaxb.model.message.ScopePathType) OrcidClientGroup(org.orcid.jaxb.model.clientgroup.OrcidClientGroup) HashMap(java.util.HashMap) OrcidClient(org.orcid.jaxb.model.clientgroup.OrcidClient) RedirectUri(org.orcid.jaxb.model.clientgroup.RedirectUri) Test(org.junit.Test) BaseTest(org.orcid.core.BaseTest)

Example 30 with ScopePathType

use of org.orcid.jaxb.model.message.ScopePathType in project ORCID-Source by ORCID.

the class OrcidClientGroupManagerTest method testUpdateOrcidClientGroup.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testUpdateOrcidClientGroup() {
    OrcidClientGroup createdGroup = transactionTemplate.execute(new TransactionCallback<OrcidClientGroup>() {

        public OrcidClientGroup doInTransaction(TransactionStatus status) {
            return orcidClientGroupManager.createOrUpdateOrcidClientGroup(group);
        }
    });
    createdGroup.setEmail("admin@somethingelse.com");
    for (OrcidClient createdClient : createdGroup.getOrcidClient()) {
        if ("Ecological Complexity".equals(createdClient.getDisplayName())) {
            createdClient.setWebsite("wwww.ecologicalcomplexity.com");
        }
    }
    OrcidClientGroup updatedGroup = orcidClientGroupManager.createOrUpdateOrcidClientGroup(createdGroup);
    assertNotNull(updatedGroup);
    assertEquals("Elsevier", updatedGroup.getGroupName());
    assertEquals("admin@somethingelse.com", updatedGroup.getEmail());
    assertNotNull(updatedGroup.getGroupOrcid());
    List<OrcidClient> updatedClients = updatedGroup.getOrcidClient();
    assertNotNull(updatedClients);
    assertEquals(2, updatedClients.size());
    Map<String, OrcidClient> updatedClientsMappedByName = new HashMap<String, OrcidClient>();
    for (OrcidClient upatedClient : updatedClients) {
        assertNotNull(upatedClient.getClientId());
        assertNotNull(upatedClient.getClientSecret());
        updatedClientsMappedByName.put(upatedClient.getDisplayName(), upatedClient);
    }
    OrcidClient complexityClient = updatedClientsMappedByName.get("Ecological Complexity");
    assertNotNull(complexityClient);
    assertEquals("wwww.ecologicalcomplexity.com", complexityClient.getWebsite());
    assertEquals("An International Journal on Biocomplexity in the Environment and Theoretical Ecology", complexityClient.getShortDescription());
    List<RedirectUri> updatedRedirectUris = complexityClient.getRedirectUris().getRedirectUri();
    assertNotNull(updatedRedirectUris);
    assertEquals(1, updatedRedirectUris.size());
    Collections.sort(updatedRedirectUris, new Comparator<RedirectUri>() {

        public int compare(RedirectUri redirectUri1, RedirectUri redirectUri2) {
            return ((String) redirectUri1.getValue()).compareToIgnoreCase((String) redirectUri1.getValue());
        }
    });
    assertEquals("http://www.journals.elsevier.com/ecological-complexity/orcid-callback", updatedRedirectUris.get(0).getValue());
    List<ScopePathType> scopesForRedirect = updatedRedirectUris.get(0).getScope();
    assertTrue(scopesForRedirect.size() == 2);
    assertTrue(scopesForRedirect.contains(ScopePathType.ORCID_PROFILE_CREATE) && scopesForRedirect.contains(ScopePathType.ORCID_BIO_READ_LIMITED));
}
Also used : OrcidClientGroup(org.orcid.jaxb.model.clientgroup.OrcidClientGroup) HashMap(java.util.HashMap) OrcidClient(org.orcid.jaxb.model.clientgroup.OrcidClient) TransactionStatus(org.springframework.transaction.TransactionStatus) RedirectUri(org.orcid.jaxb.model.clientgroup.RedirectUri) ScopePathType(org.orcid.jaxb.model.message.ScopePathType) Test(org.junit.Test) BaseTest(org.orcid.core.BaseTest) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ScopePathType (org.orcid.jaxb.model.message.ScopePathType)30 HashSet (java.util.HashSet)16 Test (org.junit.Test)9 OrcidOAuth2Authentication (org.orcid.core.oauth.OrcidOAuth2Authentication)9 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)8 Transactional (org.springframework.transaction.annotation.Transactional)8 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)6 GrantedAuthority (org.springframework.security.core.GrantedAuthority)6 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)6 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)6 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)6 ArrayList (java.util.ArrayList)5 RedirectUri (org.orcid.jaxb.model.clientgroup.RedirectUri)5 OrcidMessage (org.orcid.jaxb.model.message.OrcidMessage)5 DBUnitTest (org.orcid.test.DBUnitTest)5 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)5 Date (java.util.Date)4 OrcidOauth2TokenDetail (org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail)4 Authentication (org.springframework.security.core.Authentication)4 Rollback (org.springframework.test.annotation.Rollback)4