Search in sources :

Example 1 with NewClientRegistrationProviderEntity

use of io.gravitee.rest.api.model.configuration.application.registration.NewClientRegistrationProviderEntity in project gravitee-management-rest-api by gravitee-io.

the class ClientRegistrationProvidersResourceTest method shouldCreateSubscription.

@Test
public void shouldCreateSubscription() {
    reset(clientRegistrationService);
    NewClientRegistrationProviderEntity newClientRegistrationProviderEntity = new NewClientRegistrationProviderEntity();
    newClientRegistrationProviderEntity.setName("my-client-registration-provider-name");
    newClientRegistrationProviderEntity.setDiscoveryEndpoint("my-client-registration-provider-discovery-endpoint");
    newClientRegistrationProviderEntity.setInitialAccessTokenType(InitialAccessTokenType.INITIAL_ACCESS_TOKEN);
    newClientRegistrationProviderEntity.setInitialAccessToken("my-client-registration-provider-initial-access-token");
    ClientRegistrationProviderEntity createdClientRegistrationProvider = new ClientRegistrationProviderEntity();
    createdClientRegistrationProvider.setId("my-client-registration-provider-id");
    when(clientRegistrationService.create(any())).thenReturn(createdClientRegistrationProvider);
    final Response response = envTarget().request().post(Entity.json(newClientRegistrationProviderEntity));
    assertEquals(CREATED_201, response.getStatus());
    assertEquals(envTarget().path("my-client-registration-provider-id").getUri().toString(), response.getHeaders().getFirst(HttpHeaders.LOCATION));
}
Also used : Response(javax.ws.rs.core.Response) NewClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.NewClientRegistrationProviderEntity) NewClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.NewClientRegistrationProviderEntity) ClientRegistrationProviderEntity(io.gravitee.rest.api.model.configuration.application.registration.ClientRegistrationProviderEntity) Test(org.junit.Test)

Example 2 with NewClientRegistrationProviderEntity

use of io.gravitee.rest.api.model.configuration.application.registration.NewClientRegistrationProviderEntity 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

ClientRegistrationProviderEntity (io.gravitee.rest.api.model.configuration.application.registration.ClientRegistrationProviderEntity)2 NewClientRegistrationProviderEntity (io.gravitee.rest.api.model.configuration.application.registration.NewClientRegistrationProviderEntity)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 ClientRegistrationProvider (io.gravitee.repository.management.model.ClientRegistrationProvider)1 UpdateClientRegistrationProviderEntity (io.gravitee.rest.api.model.configuration.application.registration.UpdateClientRegistrationProviderEntity)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 DiscoveryBasedDynamicClientRegistrationProviderClient (io.gravitee.rest.api.service.impl.configuration.application.registration.client.DiscoveryBasedDynamicClientRegistrationProviderClient)1 DynamicClientRegistrationProviderClient (io.gravitee.rest.api.service.impl.configuration.application.registration.client.DynamicClientRegistrationProviderClient)1 Date (java.util.Date)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1