@Override public void revokeApiKeys(String project, String masterKey) { apiKeys.getOrDefault(project, ImmutableList.of()) .removeIf(e -> e.masterKey().equals(masterKey)); }
@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()); }
@JsonRequest @Path("/update-user-access") @ProtectEndpoint(writeOperation = true) public SuccessMessage updateUserAccess(@Named("user_id") Project project, @ApiParam("email") String email, @ApiParam(value = "read_permission") boolean readPermission, @ApiParam(value = "write_permission") boolean writePermission, @ApiParam(value = "master_permission") boolean masterPermission, @ApiParam(value = "active_ui_features") WebUserService.UIFeatures activeUiFeatures) { Optional<WebUser> user = service.getUser(project.userId); if (!user.get().projects.stream() .anyMatch(e -> e.apiKeys.stream().anyMatch(a -> a.masterKey() != null))) { throw new RakamException(FORBIDDEN); } service.giveAccessToExistingUser(project.project, user.get().id, email, readPermission, writePermission, masterPermission, activeUiFeatures); return SuccessMessage.success(); }
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) { } } }
@JsonRequest @Path("/give-user-access") @ProtectEndpoint(writeOperation = true) public SuccessMessage giveUserAccess(@Named("user_id") Project project, @ApiParam("email") String email, @ApiParam(value = "keys") ApiKeyService.ProjectApiKeys keys, @ApiParam(value = "read_permission") boolean readPermission, @ApiParam(value = "write_permission") boolean writePermission, @ApiParam(value = "master_permission") boolean masterPermission, @ApiParam(value = "active_ui_features", required = false) WebUserService.UIFeatures activeUiFeatures) { Optional<WebUser> user = service.getUser(project.userId); if (!user.get().projects.stream() .anyMatch(e -> e.apiKeys.stream().anyMatch(a -> a.masterKey() != null))) { throw new RakamException(FORBIDDEN); } service.giveAccessToUser(project.project, user.get().id, email, keys, readPermission, writePermission, masterPermission, activeUiFeatures, Optional.empty()); return SuccessMessage.success(); }
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(); } } }
@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 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())); } }
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(); } }
@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); }