Search in sources :

Example 11 with ClientDetailsEntity

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

the class OrcidClientGroupManagerImpl method createAndPersistClientProfile.

/**
     * Creates a new client and set the group orcid as the owner of that client
     * 
     * @param groupOrcid
     *            The group owner for this client
     * @param client
     *            The new client
     * @return the new OrcidClient
     */
public OrcidClient createAndPersistClientProfile(String groupOrcid, OrcidClient client) throws OrcidClientGroupManagementException {
    if (!isAllowedToAddNewClient(groupOrcid))
        throw new OrcidClientGroupManagementException("Your contract allows you to have only 1 client.");
    ProfileEntity groupProfileEntity = profileDao.find(groupOrcid);
    checkAndSetClientType(client, groupProfileEntity.getGroupType());
    // Use the client details service to create the client details
    ClientDetailsEntity clientDetailsEntity = createClientDetails(groupOrcid, client, client.getType());
    // Link the client to the copy of the profile cached in
    // memory by Hibernate
    SortedSet<ClientDetailsEntity> clientProfileEntities = groupProfileEntity.getClients();
    if (clientProfileEntities == null) {
        clientProfileEntities = new TreeSet<>(new OrcidEntityIdComparator<String>());
        groupProfileEntity.setClients(clientProfileEntities);
    }
    clientProfileEntities.add(clientDetailsEntity);
    return adapter.toOrcidClient(clientDetailsEntity);
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) OrcidEntityIdComparator(org.orcid.persistence.jpa.entities.OrcidEntityIdComparator) OrcidClientGroupManagementException(org.orcid.core.exception.OrcidClientGroupManagementException) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity)

Example 12 with ClientDetailsEntity

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

the class OrcidClientGroupManagerImpl method createClientDetails.

private ClientDetailsEntity createClientDetails(String groupOrcid, OrcidClient orcidClient, ClientType clientType) {
    Set<String> clientResourceIds = new HashSet<String>();
    clientResourceIds.add("orcid");
    Set<String> clientAuthorizedGrantTypes = new HashSet<String>();
    clientAuthorizedGrantTypes.add("client_credentials");
    clientAuthorizedGrantTypes.add("authorization_code");
    clientAuthorizedGrantTypes.add("refresh_token");
    Set<RedirectUri> redirectUrisToAdd = new HashSet<RedirectUri>();
    if (orcidClient.getRedirectUris() != null) {
        redirectUrisToAdd.addAll(orcidClient.getRedirectUris().getRedirectUri());
    }
    List<String> clientGrantedAuthorities = new ArrayList<String>();
    clientGrantedAuthorities.add("ROLE_CLIENT");
    String name = orcidClient.getDisplayName();
    String description = orcidClient.getShortDescription();
    String website = orcidClient.getWebsite();
    String idp = orcidClient.getIdp();
    Boolean allowAutoDeprecate = orcidClient.getAllowAutoDeprecate();
    ClientDetailsEntity clientDetails = clientDetailsManager.createClientDetails(groupOrcid, name, description, idp, website, clientType, createScopes(clientType), clientResourceIds, clientAuthorizedGrantTypes, redirectUrisToAdd, clientGrantedAuthorities, allowAutoDeprecate);
    return clientDetails;
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) ArrayList(java.util.ArrayList) RedirectUri(org.orcid.jaxb.model.clientgroup.RedirectUri) HashSet(java.util.HashSet)

Example 13 with ClientDetailsEntity

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

the class MembersManagerImpl method getClient.

@Override
public Client getClient(String clientId) {
    Client result = new Client();
    ClientDetailsEntity clientDetailsEntity = clientDetailsManager.findByClientId(clientId);
    if (clientDetailsEntity != null) {
        result = Client.valueOf(clientDetailsEntity);
        //Set member name
        result.setMemberName(Text.valueOf(clientDetailsManager.getMemberName(clientId)));
        //Set client secret
        if (clientDetailsEntity.getClientSecrets() != null) {
            for (ClientSecretEntity secret : clientDetailsEntity.getClientSecrets()) {
                if (secret.isPrimary()) {
                    result.setClientSecret(Text.valueOf(encryptionManager.decryptForInternalUse(secret.getClientSecret())));
                }
            }
        }
    } else {
        result.getErrors().add(getMessage("admin.edit_client.invalid_orcid"));
    }
    return result;
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) ClientSecretEntity(org.orcid.persistence.jpa.entities.ClientSecretEntity) OrcidClient(org.orcid.jaxb.model.clientgroup.OrcidClient) Client(org.orcid.pojo.ajaxForm.Client)

Example 14 with ClientDetailsEntity

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

the class OrcidRefreshTokenChecker method validateRequest.

public void validateRequest(String grantType, TokenRequest tokenRequest, Long requestTimeInMillis) {
    String authorization = tokenRequest.getRequestParameters().get(OrcidOauth2Constants.AUTHORIZATION);
    String clientId = tokenRequest.getClientId();
    String scopes = tokenRequest.getRequestParameters().get(OAuth2Utils.SCOPE);
    Long expireIn = tokenRequest.getRequestParameters().containsKey(OrcidOauth2Constants.EXPIRES_IN) ? Long.valueOf(tokenRequest.getRequestParameters().get(OrcidOauth2Constants.EXPIRES_IN)) : 0L;
    String refreshToken = tokenRequest.getRequestParameters().get(OrcidOauth2Constants.REFRESH_TOKEN);
    OrcidOauth2TokenDetail token = orcidOauth2TokenDetailDao.findByTokenValue(authorization);
    // Verify the token belongs to this client
    if (!clientId.equals(token.getClientDetailsId())) {
        throw new IllegalArgumentException("This token doesnt belong to the given client");
    }
    // Verify client is enabled
    ClientDetailsEntity clientDetails = clientDetailsEntityCacheManager.retrieve(clientId);
    orcidOAuth2RequestValidator.validateClientIsEnabled(clientDetails);
    // Verify the token is not expired
    if (token.getTokenExpiration() != null) {
        if (token.getTokenExpiration().before(new Date())) {
            throw new InvalidTokenException("Access token expired: " + authorization);
        }
    }
    // Verify access token and refresh token are linked
    if (!refreshToken.equals(token.getRefreshTokenValue())) {
        throw new InvalidTokenException("Token and refresh token does not match");
    }
    // Verify the token is not disabled
    if (token.getTokenDisabled() != null && token.getTokenDisabled()) {
        throw new InvalidTokenException("Parent token is disabled");
    }
    // Verify scopes are not wider than the token scopes
    if (PojoUtil.isEmpty(scopes)) {
        scopes = token.getScope();
    } else {
        Set<ScopePathType> requiredScopes = ScopePathType.getScopesFromSpaceSeparatedString(scopes);
        Set<ScopePathType> simpleTokenScopes = ScopePathType.getScopesFromSpaceSeparatedString(token.getScope());
        // This collection contains all tokens that should be allowed given
        // the scopes that the parent token contains
        Set<ScopePathType> combinedTokenScopes = new HashSet<ScopePathType>();
        for (ScopePathType scope : simpleTokenScopes) {
            combinedTokenScopes.addAll(scope.combined());
        }
        // combinedTokenScopes
        for (ScopePathType scope : requiredScopes) {
            if (!combinedTokenScopes.contains(scope)) {
                throw new InvalidScopeException("The given scope '" + scope.value() + "' is not allowed for the parent token");
            }
        }
    }
    // Validate the expiration for the new token is no later than the parent
    // token expiration.
    long parentTokenExpiration = token.getTokenExpiration() == null ? System.currentTimeMillis() : token.getTokenExpiration().getTime();
    if (expireIn > parentTokenExpiration) {
        throw new IllegalArgumentException("Token expiration can't be after " + token.getTokenExpiration());
    }
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) ScopePathType(org.orcid.jaxb.model.message.ScopePathType) InvalidScopeException(org.springframework.security.oauth2.common.exceptions.InvalidScopeException) OrcidOauth2TokenDetail(org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail) Date(java.util.Date) HashSet(java.util.HashSet)

Example 15 with ClientDetailsEntity

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

the class ClientDetailsManagerReadOnlyImpl method getClient.

@Override
public Client getClient(String clientId) {
    Date lastModified = clientDetailsDao.getLastModified(clientId);
    ClientDetailsEntity clientDetailsEntity = clientDetailsDao.findByClientId(clientId, lastModified.getTime());
    return jpaJaxbClientAdapter.toClient(clientDetailsEntity);
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) Date(java.util.Date)

Aggregations

ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)149 Test (org.junit.Test)75 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)57 BaseTest (org.orcid.core.BaseTest)51 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)33 Date (java.util.Date)23 Transactional (org.springframework.transaction.annotation.Transactional)16 HashSet (java.util.HashSet)15 DBUnitTest (org.orcid.test.DBUnitTest)15 HashMap (java.util.HashMap)14 Authentication (org.springframework.security.core.Authentication)13 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)13 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)11 Work (org.orcid.jaxb.model.record_v2.Work)9 Before (org.junit.Before)8 ArrayList (java.util.ArrayList)7 OrcidClient (org.orcid.jaxb.model.clientgroup.OrcidClient)7 ClientSecretEntity (org.orcid.persistence.jpa.entities.ClientSecretEntity)7 OrcidProfile (org.orcid.jaxb.model.message.OrcidProfile)6 Funding (org.orcid.jaxb.model.record_v2.Funding)6