Search in sources :

Example 1 with ApiKey

use of models.ApiKey in project civiform by seattle-uat.

the class ApiKeyServiceTest method retireApiKey_keyAlreadyRetired_throws.

@Test
public void retireApiKey_keyAlreadyRetired_throws() {
    ApiKey apiKey = apiKeyService.createApiKey(buildForm(ImmutableMap.of("keyName", "test key 1", "expiration", "2020-01-30", "subnet", "0.0.0.1/32")), adminProfile).getApiKey();
    apiKeyService.retireApiKey(apiKey.id, adminProfile);
    NotChangeableException exception = assertThrows(NotChangeableException.class, () -> apiKeyService.retireApiKey(apiKey.id, adminProfile));
    assertThat(exception).hasMessage(String.format("ApiKey %s is already retired", apiKey));
}
Also used : NotChangeableException(controllers.admin.NotChangeableException) ApiKey(models.ApiKey) Test(org.junit.Test)

Example 2 with ApiKey

use of models.ApiKey in project civiform by seattle-uat.

the class ApiKeyServiceTest method retireApiKey_retiresAnApiKey.

@Test
public void retireApiKey_retiresAnApiKey() {
    ApiKey apiKey = apiKeyService.createApiKey(buildForm(ImmutableMap.of("keyName", "test key 1", "expiration", "2020-01-30", "subnet", "0.0.0.1/32")), adminProfile).getApiKey();
    apiKeyService.retireApiKey(apiKey.id, adminProfile);
    apiKey.refresh();
    assertThat(apiKey.isRetired()).isTrue();
    assertThat(apiKey.getRetiredBy().get()).isEqualTo(adminProfile.getAuthorityId().join());
    assertThat(apiKey.getRetiredTime()).isPresent();
}
Also used : ApiKey(models.ApiKey) Test(org.junit.Test)

Example 3 with ApiKey

use of models.ApiKey in project civiform by seattle-uat.

the class ApiKeyRepositoryTest method insert_persistsANewKey.

@Test
public void insert_persistsANewKey() {
    ApiKey foundKey;
    ApiKeyGrants grants = new ApiKeyGrants();
    grants.grantProgramPermission("program-a", ApiKeyGrants.Permission.READ);
    ApiKey apiKey = new ApiKey(grants);
    apiKey.setName("key name").setKeyId("key-id").setCreatedBy("test@example.com").setSaltedKeySecret("secret").setSubnet("0.0.0.0/32").setExpiration(Instant.ofEpochSecond(100));
    ApiKeyRepository repo = instanceOf(ApiKeyRepository.class);
    repo.insert(apiKey).toCompletableFuture().join();
    long id = apiKey.id;
    foundKey = repo.lookupApiKey(id).toCompletableFuture().join().get();
    assertThat(foundKey.id).isEqualTo(id);
    foundKey = repo.lookupApiKey("key-id").toCompletableFuture().join().get();
    assertThat(foundKey.id).isEqualTo(id);
    assertThat(foundKey.getName()).isEqualTo("key name");
    assertThat(foundKey.getKeyId()).isEqualTo("key-id");
    assertThat(foundKey.getCreatedBy()).isEqualTo("test@example.com");
    assertThat(foundKey.getSaltedKeySecret()).isEqualTo("secret");
    assertThat(foundKey.getSubnet()).isEqualTo("0.0.0.0/32");
    assertThat(foundKey.getExpiration()).isEqualTo(Instant.ofEpochSecond(100));
    assertThat(foundKey.getGrants().hasProgramPermission("program-a", ApiKeyGrants.Permission.READ)).isTrue();
}
Also used : ApiKey(models.ApiKey) ApiKeyGrants(auth.ApiKeyGrants) Test(org.junit.Test)

Example 4 with ApiKey

use of models.ApiKey in project civiform by seattle-uat.

the class ApiKeyServiceTest method createApiKey_createsAnApiKey.

@Test
public void createApiKey_createsAnApiKey() {
    resourceCreator.insertActiveProgram("test program");
    DynamicForm form = buildForm(ImmutableMap.of("keyName", "test key", "expiration", "2020-01-30", "subnet", "0.0.0.1/32", "grant-program-read[test-program]", "true"));
    ApiKeyCreationResult apiKeyCreationResult = apiKeyService.createApiKey(form, adminProfile);
    assertThat(apiKeyCreationResult.isSuccessful()).isTrue();
    String credentialString = apiKeyCreationResult.getCredentials();
    byte[] keyIdBytes = Base64.getDecoder().decode(credentialString);
    String keyId = Iterables.get(Splitter.on(':').split(new String(keyIdBytes, StandardCharsets.UTF_8)), 0);
    ApiKey apiKey = apiKeyRepository.lookupApiKey(keyId).toCompletableFuture().join().get();
    assertThat(apiKey.getName()).isEqualTo("test key");
    assertThat(apiKey.getSubnet()).isEqualTo("0.0.0.1/32");
    assertThat(apiKey.getExpiration()).isEqualTo(dateConverter.parseIso8601DateToStartOfDateInstant("2020-01-30"));
    assertThat(apiKey.getGrants().hasProgramPermission("test-program", Permission.READ)).isTrue();
}
Also used : ApiKey(models.ApiKey) DynamicForm(play.data.DynamicForm) Test(org.junit.Test)

Example 5 with ApiKey

use of models.ApiKey in project civiform by seattle-uat.

the class ApiKeyIndexView method render.

public Content render(Http.Request request, PaginationResult<ApiKey> apiKeyPaginationResult, ImmutableSet<String> allProgramNames) {
    String title = "API Keys";
    ContainerTag headerDiv = div().withClasses(Styles.FLEX, Styles.PLACE_CONTENT_BETWEEN, Styles.MY_8).with(h1(title).withClasses(Styles.MY_4), new LinkElement().setHref(controllers.admin.routes.AdminApiKeysController.newOne().url()).setId("new-api-key-button").setText("New API Key").asButton());
    ContainerTag contentDiv = div().withClasses(Styles.PX_20).with(headerDiv);
    for (ApiKey apiKey : apiKeyPaginationResult.getPageContents()) {
        contentDiv.with(renderApiKey(request, apiKey, buildProgramSlugToName(allProgramNames)));
    }
    HtmlBundle htmlBundle = layout.getBundle().setTitle(title).addMainContent(contentDiv);
    return layout.renderCentered(htmlBundle);
}
Also used : HtmlBundle(views.HtmlBundle) ApiKey(models.ApiKey) LinkElement(views.components.LinkElement) ContainerTag(j2html.tags.ContainerTag)

Aggregations

ApiKey (models.ApiKey)11 Test (org.junit.Test)6 ApiKeyGrants (auth.ApiKeyGrants)3 NotChangeableException (controllers.admin.NotChangeableException)2 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)2 ContainerTag (j2html.tags.ContainerTag)1 Optional (java.util.Optional)1 CompletionException (java.util.concurrent.CompletionException)1 SubnetUtils (org.apache.commons.net.util.SubnetUtils)1 PlayWebContext (org.pac4j.play.PlayWebContext)1 DynamicForm (play.data.DynamicForm)1 ApiKeyService (services.apikey.ApiKeyService)1 HtmlBundle (views.HtmlBundle)1 LinkElement (views.components.LinkElement)1