Search in sources :

Example 1 with DynamicClientRegistrationProviderClient

use of io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient in project gravitee-management-rest-api by gravitee-io.

the class ClientRegistrationServiceImpl method register.

@Override
public ClientRegistrationResponse register(NewApplicationEntity application) {
    // Create an OAuth client
    Set<ClientRegistrationProviderEntity> providers = findAll();
    if (providers == null || providers.isEmpty()) {
        throw new MissingDynamicClientRegistrationProviderException();
    }
    // For now, took the first provider
    ClientRegistrationProviderEntity provider = providers.iterator().next();
    // Get provider client
    DynamicClientRegistrationProviderClient registrationProviderClient = getDCRClient(false, provider);
    ClientRegistrationRequest clientRegistrationRequest = convert(application);
    if (provider.getSoftwareId() != null && !provider.getSoftwareId().isEmpty()) {
        clientRegistrationRequest.setSoftwareId(provider.getSoftwareId());
    }
    return registrationProviderClient.register(clientRegistrationRequest);
}
Also used : NewClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.NewClientRegistrationProviderEntity) UpdateClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.UpdateClientRegistrationProviderEntity) ClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.ClientRegistrationProviderEntity) ClientRegistrationRequest(io.gravitee.rest.api.service.impl.configuration.application.registration.client.register.ClientRegistrationRequest) DynamicClientRegistrationProviderClient(io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient) DiscoveryBasedDynamicClientRegistrationProviderClient(io.gravitee.rest.api.service.impl.configuration.application.registration.client.DiscoveryBasedDynamicClientRegistrationProviderClient)

Example 2 with DynamicClientRegistrationProviderClient

use of io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient in project gravitee-management-rest-api by gravitee-io.

the class ClientRegistrationServiceImpl method update.

@Override
public ClientRegistrationResponse update(String previousRegistrationResponse, UpdateApplicationEntity application) {
    try {
        ClientRegistrationResponse registrationResponse = mapper.readValue(previousRegistrationResponse, ClientRegistrationResponse.class);
        if (registrationResponse.getRegistrationAccessToken() == null || registrationResponse.getRegistrationAccessToken().isEmpty() || registrationResponse.getRegistrationClientUri() == null || registrationResponse.getRegistrationClientUri().isEmpty()) {
            throw new RegisteredClientNotUpdatableException();
        }
        // Update an OAuth client
        Set<ClientRegistrationProviderEntity> providers = findAll();
        if (providers == null || providers.isEmpty()) {
            throw new MissingDynamicClientRegistrationProviderException();
        }
        // For now, took the first provider
        ClientRegistrationProviderEntity provider = providers.iterator().next();
        // Get provider client
        DynamicClientRegistrationProviderClient registrationProviderClient = getDCRClient(false, provider);
        ClientRegistrationRequest registrationRequest = mapper.readValue(previousRegistrationResponse, ClientRegistrationRequest.class);
        registrationRequest.setSoftwareId(provider.getSoftwareId());
        return registrationProviderClient.update(registrationResponse.getRegistrationAccessToken(), registrationResponse.getRegistrationClientUri(), convert(registrationRequest, application), application.getSettings().getoAuthClient().getClientId());
    } catch (Exception ex) {
        LOGGER.error("Unexpected error while updating a client", ex);
        return null;
    }
}
Also used : ClientRegistrationResponse(io.gravitee.rest.api.service.impl.configuration.application.registration.client.register.ClientRegistrationResponse) NewClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.NewClientRegistrationProviderEntity) UpdateClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.UpdateClientRegistrationProviderEntity) ClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.ClientRegistrationProviderEntity) ClientRegistrationRequest(io.gravitee.rest.api.service.impl.configuration.application.registration.client.register.ClientRegistrationRequest) DynamicClientRegistrationProviderClient(io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient) DiscoveryBasedDynamicClientRegistrationProviderClient(io.gravitee.rest.api.service.impl.configuration.application.registration.client.DiscoveryBasedDynamicClientRegistrationProviderClient) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) IOException(java.io.IOException)

Example 3 with DynamicClientRegistrationProviderClient

use of io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient in project gravitee-management-rest-api by gravitee-io.

the class ClientRegistrationServiceImpl method update.

@Override
public ClientRegistrationProviderEntity update(String id, UpdateClientRegistrationProviderEntity updateClientRegistrationProvider) {
    try {
        LOGGER.debug("Update client registration provider {}", updateClientRegistrationProvider);
        Optional<ClientRegistrationProvider> optClientRegistrationProvider = clientRegistrationProviderRepository.findById(id);
        if (!optClientRegistrationProvider.isPresent()) {
            throw new ClientRegistrationProviderNotFoundException(updateClientRegistrationProvider.getName());
        }
        if (updateClientRegistrationProvider.getInitialAccessTokenType() == InitialAccessTokenType.INITIAL_ACCESS_TOKEN && (updateClientRegistrationProvider.getInitialAccessToken() == null || updateClientRegistrationProvider.getInitialAccessToken().isEmpty())) {
            throw new EmptyInitialAccessTokenException();
        }
        ClientRegistrationProvider clientRegistrationProvider = convert(updateClientRegistrationProvider);
        // Check renew_client_secret configuration
        renewClientSecretSupport(clientRegistrationProvider);
        clientRegistrationProvider.setId(id);
        DynamicClientRegistrationProviderClient registrationProviderClient = getDCRClient(true, convert(clientRegistrationProvider));
        // Ensure that the client credentials are valid
        registrationProviderClient.getInitialAccessToken();
        LOGGER.debug("Found a DCR Client for provider: {}", clientRegistrationProvider.getName(), registrationProviderClient);
        final ClientRegistrationProvider clientProviderToUpdate = optClientRegistrationProvider.get();
        clientRegistrationProvider.setId(id);
        clientRegistrationProvider.setCreatedAt(clientProviderToUpdate.getCreatedAt());
        clientRegistrationProvider.setUpdatedAt(new Date());
        ClientRegistrationProvider updatedClientRegistrationProvider = clientRegistrationProviderRepository.update(clientRegistrationProvider);
        // Audit
        auditService.createEnvironmentAuditLog(singletonMap(CLIENT_REGISTRATION_PROVIDER, id), ClientRegistrationProvider.AuditEvent.CLIENT_REGISTRATION_PROVIDER_CREATED, clientRegistrationProvider.getUpdatedAt(), clientProviderToUpdate, updatedClientRegistrationProvider);
        return convert(updatedClientRegistrationProvider);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to update client registration provider {}", updateClientRegistrationProvider, ex);
        throw new TechnicalManagementException("An error occurs while trying to update " + updateClientRegistrationProvider, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ClientRegistrationProvider(io.gravitee.repository.management.model.ClientRegistrationProvider) DynamicClientRegistrationProviderClient(io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient) DiscoveryBasedDynamicClientRegistrationProviderClient(io.gravitee.rest.api.service.impl.configuration.application.registration.client.DiscoveryBasedDynamicClientRegistrationProviderClient) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 4 with DynamicClientRegistrationProviderClient

use of io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient in project gravitee-management-rest-api by gravitee-io.

the class ClientRegistrationServiceImpl method renewClientSecret.

@Override
public ClientRegistrationResponse renewClientSecret(String previousRegistrationResponse) {
    try {
        ClientRegistrationResponse registrationResponse = mapper.readValue(previousRegistrationResponse, ClientRegistrationResponse.class);
        if (registrationResponse.getRegistrationAccessToken() == null || registrationResponse.getRegistrationAccessToken().isEmpty() || registrationResponse.getRegistrationClientUri() == null || registrationResponse.getRegistrationClientUri().isEmpty()) {
            throw new RegisteredClientNotUpdatableException();
        }
        Set<ClientRegistrationProviderEntity> providers = findAll();
        if (providers == null || providers.isEmpty()) {
            throw new MissingDynamicClientRegistrationProviderException();
        }
        // For now, take the first provider
        ClientRegistrationProviderEntity provider = providers.iterator().next();
        // Get provider client
        DynamicClientRegistrationProviderClient registrationProviderClient = getDCRClient(false, provider);
        String renewClientSecretEndpoint = provider.getRenewClientSecretEndpoint();
        TemplateEngine templateEngine = TemplateEngine.templateEngine();
        templateEngine.getTemplateContext().setVariable("client_id", registrationResponse.getClientId());
        if (registrationProviderClient instanceof DiscoveryBasedDynamicClientRegistrationProviderClient) {
            ((DiscoveryBasedDynamicClientRegistrationProviderClient) registrationProviderClient).getMetadata().forEach((s, o) -> templateEngine.getTemplateContext().setVariable(s, o));
        }
        return registrationProviderClient.renewClientSecret(provider.getRenewClientSecretMethod(), templateEngine.getValue(renewClientSecretEndpoint, String.class), registrationResponse.getRegistrationAccessToken());
    } catch (IOException ioe) {
        LOGGER.error("Unexpected error while updating a client", ioe);
        return null;
    }
}
Also used : TemplateEngine(io.gravitee.el.TemplateEngine) DiscoveryBasedDynamicClientRegistrationProviderClient(io.gravitee.rest.api.service.impl.configuration.application.registration.client.DiscoveryBasedDynamicClientRegistrationProviderClient) ClientRegistrationResponse(io.gravitee.rest.api.service.impl.configuration.application.registration.client.register.ClientRegistrationResponse) NewClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.NewClientRegistrationProviderEntity) UpdateClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.UpdateClientRegistrationProviderEntity) ClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.ClientRegistrationProviderEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) IOException(java.io.IOException) DynamicClientRegistrationProviderClient(io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient) DiscoveryBasedDynamicClientRegistrationProviderClient(io.gravitee.rest.api.service.impl.configuration.application.registration.client.DiscoveryBasedDynamicClientRegistrationProviderClient)

Example 5 with DynamicClientRegistrationProviderClient

use of io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient in project gravitee-management-rest-api by gravitee-io.

the class ClientRegistrationServiceImpl method create.

@Override
public ClientRegistrationProviderEntity create(NewClientRegistrationProviderEntity newClientRegistrationProvider) {
    try {
        LOGGER.debug("Create client registration provider {}", newClientRegistrationProvider);
        Set<ClientRegistrationProviderEntity> clientRegistrationProviders = this.findAll();
        // For now, we are supporting only a single client registration provider.
        if (clientRegistrationProviders.size() == 1) {
            throw new IllegalStateException("Until now, supports only a single client registration provider. " + "Please update the existing one: " + clientRegistrationProviders.iterator().next().getName());
        }
        Optional<ClientRegistrationProvider> optClientRegistrationProvider = clientRegistrationProviderRepository.findById(IdGenerator.generate(newClientRegistrationProvider.getName()));
        if (optClientRegistrationProvider.isPresent()) {
            throw new ClientRegistrationProviderAlreadyExistsException(newClientRegistrationProvider.getName());
        }
        if (newClientRegistrationProvider.getInitialAccessTokenType() == InitialAccessTokenType.INITIAL_ACCESS_TOKEN && (newClientRegistrationProvider.getInitialAccessToken() == null || newClientRegistrationProvider.getInitialAccessToken().isEmpty())) {
            throw new EmptyInitialAccessTokenException();
        }
        ClientRegistrationProvider clientRegistrationProvider = convert(newClientRegistrationProvider);
        // Check renew_client_secret configuration
        renewClientSecretSupport(clientRegistrationProvider);
        clientRegistrationProvider.setId(UuidString.generateRandom());
        DynamicClientRegistrationProviderClient registrationProviderClient = getDCRClient(true, convert(clientRegistrationProvider));
        // Ensure that the client credentials are valid
        registrationProviderClient.getInitialAccessToken();
        LOGGER.debug("Found a DCR Client for provider: {}", clientRegistrationProvider.getName(), registrationProviderClient);
        // Set date fields
        clientRegistrationProvider.setCreatedAt(new Date());
        clientRegistrationProvider.setUpdatedAt(clientRegistrationProvider.getCreatedAt());
        ClientRegistrationProvider createdClientRegistrationProvider = clientRegistrationProviderRepository.create(clientRegistrationProvider);
        auditService.createEnvironmentAuditLog(singletonMap(CLIENT_REGISTRATION_PROVIDER, createdClientRegistrationProvider.getId()), ClientRegistrationProvider.AuditEvent.CLIENT_REGISTRATION_PROVIDER_CREATED, createdClientRegistrationProvider.getUpdatedAt(), null, createdClientRegistrationProvider);
        return convert(createdClientRegistrationProvider);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create client registration provider {}", newClientRegistrationProvider, ex);
        throw new TechnicalManagementException("An error occurs while trying to create " + newClientRegistrationProvider, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NewClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.NewClientRegistrationProviderEntity) UpdateClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.UpdateClientRegistrationProviderEntity) ClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.ClientRegistrationProviderEntity) ClientRegistrationProvider(io.gravitee.repository.management.model.ClientRegistrationProvider) DynamicClientRegistrationProviderClient(io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient) DiscoveryBasedDynamicClientRegistrationProviderClient(io.gravitee.rest.api.service.impl.configuration.application.registration.client.DiscoveryBasedDynamicClientRegistrationProviderClient) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

DiscoveryBasedDynamicClientRegistrationProviderClient (io.gravitee.rest.api.service.impl.configuration.application.registration.client.DiscoveryBasedDynamicClientRegistrationProviderClient)5 DynamicClientRegistrationProviderClient (io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient)5 ClientRegistrationProviderEntity (io.gravitee.rest.api.model.configuration.application.registration.ClientRegistrationProviderEntity)4 NewClientRegistrationProviderEntity (io.gravitee.rest.api.model.configuration.application.registration.NewClientRegistrationProviderEntity)4 UpdateClientRegistrationProviderEntity (io.gravitee.rest.api.model.configuration.application.registration.UpdateClientRegistrationProviderEntity)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)3 ClientRegistrationProvider (io.gravitee.repository.management.model.ClientRegistrationProvider)2 ClientRegistrationRequest (io.gravitee.rest.api.service.impl.configuration.application.registration.client.register.ClientRegistrationRequest)2 ClientRegistrationResponse (io.gravitee.rest.api.service.impl.configuration.application.registration.client.register.ClientRegistrationResponse)2 IOException (java.io.IOException)2 Date (java.util.Date)2 TemplateEngine (io.gravitee.el.TemplateEngine)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1