@JsonRequest @ApiOperation(value = "Check API Keys") @Path("/check-api-keys") public List<Boolean> checkApiKeys(@ApiParam("keys") List<ProjectApiKeys> keys, @ApiParam("project") String project) { return keys.stream().map(key -> { try { Consumer<String> stringConsumer = e -> { if (!e.equals(project.toLowerCase(Locale.ENGLISH))) { throw new RakamException(FORBIDDEN); } }; Optional.ofNullable(key.masterKey()).map(k -> apiKeyService.getProjectOfApiKey(k, MASTER_KEY)).ifPresent(stringConsumer); Optional.ofNullable(key.readKey()).map(k -> apiKeyService.getProjectOfApiKey(k, READ_KEY)).ifPresent(stringConsumer); Optional.ofNullable(key.writeKey()).map(k -> apiKeyService.getProjectOfApiKey(k, WRITE_KEY)).ifPresent(stringConsumer); return true; } catch (RakamException e) { return false; } }).collect(Collectors.toList()); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof ApiKeyService.ProjectApiKeys) { ApiKeyService.ProjectApiKeys that = (ApiKeyService.ProjectApiKeys) o; return ((this.masterKey == null) ? (that.masterKey() == null) : this.masterKey.equals(that.masterKey())) && ((this.readKey == null) ? (that.readKey() == null) : this.readKey.equals(that.readKey())) && ((this.writeKey == null) ? (that.writeKey() == null) : this.writeKey.equals(that.writeKey())); } return false; }
private String getKey(ProjectApiKeys keys, AccessKeyType type) { switch (type) { case MASTER_KEY: return keys.masterKey(); case READ_KEY: return keys.readKey(); case WRITE_KEY: return keys.writeKey(); default: throw new IllegalStateException(); } }
try { dbi.inTransaction((Handle handle, TransactionStatus transactionStatus) -> { Integer apiKeyId = saveApiKeys(handle, userId, projectId, keys.readKey(), keys.writeKey(), keys.masterKey());
@Test public void testRevokeApiKeys() throws Exception { ApiKeyService.ProjectApiKeys apiKeys = getApiKeyService().createApiKeys(PROJECT_NAME); getApiKeyService().revokeApiKeys(PROJECT_NAME, apiKeys.masterKey()); try { getApiKeyService().getProjectOfApiKey(apiKeys.readKey(), AccessKeyType.READ_KEY); fail(); } catch (RakamException e) { } try { getApiKeyService().getProjectOfApiKey(apiKeys.writeKey(), AccessKeyType.WRITE_KEY); fail(); } catch (RakamException e) { } try { getApiKeyService().getProjectOfApiKey(apiKeys.masterKey(), AccessKeyType.MASTER_KEY); fail(); } catch (RakamException e) { } } }
public String getKey(AccessKeyType accessKeyType) { switch (accessKeyType) { case WRITE_KEY: return writeKey(); case MASTER_KEY: return masterKey(); case READ_KEY: return readKey(); default: throw new IllegalStateException(); } } }
private ProjectApiKeys transformKeys(ProjectApiKeys apiKeys) { if (projectConfig.getPassphrase() == null) { return ProjectApiKeys.create(apiKeys.masterKey(), apiKeys.readKey(), apiKeys.writeKey()); } else { return ProjectApiKeys.create( CryptUtil.encryptAES(apiKeys.masterKey(), projectConfig.getPassphrase()), CryptUtil.encryptAES(apiKeys.readKey(), projectConfig.getPassphrase()), CryptUtil.encryptAES(apiKeys.writeKey(), projectConfig.getPassphrase())); } }
@Test public void testCreateApiKeys() throws Exception { ApiKeyService.ProjectApiKeys testing = getApiKeyService().createApiKeys(PROJECT_NAME); assertEquals(getApiKeyService().getProjectOfApiKey(testing.readKey(), AccessKeyType.READ_KEY), PROJECT_NAME); assertEquals(getApiKeyService().getProjectOfApiKey(testing.writeKey(), AccessKeyType.WRITE_KEY), PROJECT_NAME); assertEquals(getApiKeyService().getProjectOfApiKey(testing.masterKey(), AccessKeyType.MASTER_KEY), PROJECT_NAME); }