Search in sources :

Example 1 with ClientAlreadyExistsException

use of org.springframework.security.oauth2.provider.ClientAlreadyExistsException in project alexa-oauth-sample by alexa-samples.

the class DynamoDBClientDetailsDAO method addClientDetails.

/**
 * Add a new {@link ClientDetails} into Database.
 *
 * @param clientDetails client details to be added.
 * @throws ClientAlreadyExistsException if client details already exists.
 */
@Override
public void addClientDetails(ClientDetails clientDetails) throws ClientAlreadyExistsException {
    OAuthClientDetails oAuthClientDetails = dynamoDBMapper.load(OAuthClientDetails.class, clientDetails.getClientId());
    if (oAuthClientDetails != null) {
        throw new ClientAlreadyExistsException("client already exists: " + clientDetails.getClientId());
    }
    addOrUpdateClientDetails(clientDetails);
}
Also used : OAuthClientDetails(com.oauth.server.dto.OAuthClientDetails) ClientAlreadyExistsException(org.springframework.security.oauth2.provider.ClientAlreadyExistsException)

Example 2 with ClientAlreadyExistsException

use of org.springframework.security.oauth2.provider.ClientAlreadyExistsException in project uaa by cloudfoundry.

the class ClientAdminBootstrap method addNewClients.

private void addNewClients() {
    Set<Map.Entry<String, Map<String, Object>>> entries = clients.entrySet();
    entries.removeIf(entry -> clientsToDelete.contains(entry.getKey()));
    for (Map.Entry<String, Map<String, Object>> entry : entries) {
        String clientId = entry.getKey();
        Map<String, Object> map = entry.getValue();
        if (map.get("authorized-grant-types") == null) {
            throw new InvalidClientDetailsException("Client must have at least one authorized-grant-type. client ID: " + clientId);
        }
        BaseClientDetails client = new BaseClientDetails(clientId, (String) map.get("resource-ids"), (String) map.get("scope"), (String) map.get("authorized-grant-types"), (String) map.get("authorities"), getRedirectUris(map));
        client.setClientSecret(map.get("secret") == null ? "" : (String) map.get("secret"));
        Integer validity = (Integer) map.get("access-token-validity");
        Boolean override = (Boolean) map.get("override");
        if (override == null) {
            override = defaultOverride;
        }
        Map<String, Object> info = new HashMap<>(map);
        if (validity != null) {
            client.setAccessTokenValiditySeconds(validity);
        }
        validity = (Integer) map.get("refresh-token-validity");
        if (validity != null) {
            client.setRefreshTokenValiditySeconds(validity);
        }
        // UAA does not use the resource ids in client registrations
        client.setResourceIds(Collections.singleton("none"));
        if (client.getScope().isEmpty()) {
            client.setScope(Collections.singleton("uaa.none"));
        }
        if (client.getAuthorities().isEmpty()) {
            client.setAuthorities(Collections.singleton(UaaAuthority.UAA_NONE));
        }
        if (client.getAuthorizedGrantTypes().contains(GRANT_TYPE_AUTHORIZATION_CODE)) {
            client.getAuthorizedGrantTypes().add(GRANT_TYPE_REFRESH_TOKEN);
        }
        for (String key : Arrays.asList("resource-ids", "scope", "authorized-grant-types", "authorities", "redirect-uri", "secret", "id", "override", "access-token-validity", "refresh-token-validity", "show-on-homepage", "app-launch-url", "app-icon")) {
            info.remove(key);
        }
        client.setAdditionalInformation(info);
        try {
            clientRegistrationService.addClientDetails(client, IdentityZone.getUaaZoneId());
        } catch (ClientAlreadyExistsException e) {
            if (override) {
                logger.debug("Overriding client details for " + clientId);
                clientRegistrationService.updateClientDetails(client, IdentityZone.getUaaZoneId());
                if (didPasswordChange(clientId, client.getClientSecret())) {
                    clientRegistrationService.updateClientSecret(clientId, client.getClientSecret(), IdentityZone.getUaaZoneId());
                }
            } else {
                // ignore it
                logger.debug(e.getMessage());
            }
        }
        if (map.containsKey("use-bcrypt-prefix") && "true".equals(map.get("use-bcrypt-prefix"))) {
            jdbcTemplate.update("update oauth_client_details set client_secret=concat(?, client_secret) where client_id = ?", "{bcrypt}", clientId);
        }
        for (String s : Arrays.asList(GRANT_TYPE_AUTHORIZATION_CODE, GRANT_TYPE_IMPLICIT)) {
            if (client.getAuthorizedGrantTypes().contains(s) && isMissingRedirectUris(client)) {
                throw new InvalidClientDetailsException(s + " grant type requires at least one redirect URL. ClientID: " + client.getClientId());
            }
        }
        ClientMetadata clientMetadata = buildClientMetadata(map, clientId);
        clientMetadataProvisioning.update(clientMetadata, IdentityZone.getUaaZoneId());
    }
}
Also used : BaseClientDetails(org.springframework.security.oauth2.provider.client.BaseClientDetails) HashMap(java.util.HashMap) ClientAlreadyExistsException(org.springframework.security.oauth2.provider.ClientAlreadyExistsException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ClientAlreadyExistsException

use of org.springframework.security.oauth2.provider.ClientAlreadyExistsException in project uaa by cloudfoundry.

the class ClientAdminEndpointsTests method testHandleClientAlreadyExists.

@Test
void testHandleClientAlreadyExists() {
    ResponseEntity<InvalidClientDetailsException> result = endpoints.handleClientAlreadyExists(new ClientAlreadyExistsException("No such client: foo"));
    assertEquals(HttpStatus.CONFLICT, result.getStatusCode());
}
Also used : ClientAlreadyExistsException(org.springframework.security.oauth2.provider.ClientAlreadyExistsException) Test(org.junit.jupiter.api.Test)

Aggregations

ClientAlreadyExistsException (org.springframework.security.oauth2.provider.ClientAlreadyExistsException)3 OAuthClientDetails (com.oauth.server.dto.OAuthClientDetails)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1 BaseClientDetails (org.springframework.security.oauth2.provider.client.BaseClientDetails)1