From 7ec2183ab8194a05c994c9c67909dec538da1096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= Date: Thu, 14 Dec 2023 15:11:08 +0100 Subject: [PATCH] Improve SDK bindings examples (#377) ## Type of change ``` - [ ] Bug fix - [ ] New feature development - [x] Tech debt (refactoring, code cleanup, dependency upgrades, etc) - [ ] Build/deploy pipeline (DevOps) - [ ] Other ``` ## Objective Updated all the samples to load access token, organization id and urls from env variables. This made it much easier to quickly test all the bindings. Also added one Java example. --- languages/cpp/examples/Wrapper.cpp | 15 +++---- languages/go/example/example.go | 1 + languages/java/Example.java | 33 +++++++++++++++ .../com/bitwarden/sdk/ExampleProgram.java | 42 ------------------- languages/php/example.php | 10 +++-- languages/ruby/examples/example.rb | 13 +++--- 6 files changed, 56 insertions(+), 58 deletions(-) create mode 100644 languages/java/Example.java delete mode 100644 languages/java/src/main/java/com/bitwarden/sdk/ExampleProgram.java diff --git a/languages/cpp/examples/Wrapper.cpp b/languages/cpp/examples/Wrapper.cpp index df4aa164c..e94980698 100644 --- a/languages/cpp/examples/Wrapper.cpp +++ b/languages/cpp/examples/Wrapper.cpp @@ -7,6 +7,9 @@ int main() { const char* accessTokenEnv = std::getenv("ACCESS_TOKEN"); const char* organizationIdEnv = std::getenv("ORGANIZATION_ID"); + const char* apiUrl = std::getenv("API_URL"); + const char* identityUrl = std::getenv("IDENTITY_URL"); + if (!accessTokenEnv || !organizationIdEnv) { std::cerr << "Error: Environment variables ACCESS_TOKEN or ORGANIZATION_ID not set." << std::endl; return 1; @@ -15,15 +18,13 @@ int main() { std::string accessToken = accessTokenEnv; std::string organizationId = organizationIdEnv; - - - // Optional - commented to use default values - // BitwardenSettings bitwardenSettings; - // bitwardenSettings.set_api_url(""); - // bitwardenSettings.set_identity_url(""); + // Configuring the URLS is optional, remove them to use the default values + BitwardenSettings bitwardenSettings; + bitwardenSettings.set_api_url(apiUrl); + bitwardenSettings.set_identity_url(identityUrl); // Create a Bitwarden client instance - BitwardenClient bitwardenClient = BitwardenClient(); + BitwardenClient bitwardenClient = BitwardenClient(bitwardenSettings); // // Access token login bitwardenClient.accessTokenLogin(accessToken); // Organization ID diff --git a/languages/go/example/example.go b/languages/go/example/example.go index 5935d8002..154c14618 100644 --- a/languages/go/example/example.go +++ b/languages/go/example/example.go @@ -9,6 +9,7 @@ import ( ) func main() { + // Configuring the URLS is optional, set them to nil to use the default values apiURL := os.Getenv("API_URL") identityURL := os.Getenv("IDENTITY_URL") diff --git a/languages/java/Example.java b/languages/java/Example.java new file mode 100644 index 000000000..eacf23f8a --- /dev/null +++ b/languages/java/Example.java @@ -0,0 +1,33 @@ +import java.lang.System; +import java.util.UUID; + +import com.bitwarden.sdk.*; +import com.bitwarden.sdk.schema.*; + +class Example { + public static void main(String[] args) { + + String accessToken = System.getenv("ACCESS_TOKEN"); + UUID organizationId = UUID.fromString(System.getenv("ORGANIZATION_ID")); + String apiUrl = System.getenv("API_URL"); + String identityUrl = System.getenv("IDENTITY_URL"); + + // Configuring the URLS is optional, remove them to use the default values + BitwardenSettings bitwardenSettings = new BitwardenSettings(); + bitwardenSettings.setApiUrl(apiUrl); + bitwardenSettings.setIdentityUrl(identityUrl); + BitwardenClient client = new BitwardenClient(bitwardenSettings); + client.accessTokenLogin(accessToken); + + + ProjectResponse project = client.projects().create(organizationId, "Test Project"); + ProjectsResponse list = client.projects().list(organizationId); + + SecretResponse secret = client.secrets().create("Secret Key", "Secret Value", "Secret Note", organizationId, new UUID[] { project.getID() }); + + System.out.println("Secret: " + secret.getValue()); + + client.secrets().delete(new UUID[] { secret.getID() }); + client.projects().delete(new UUID[] { project.getID() }); + } +} diff --git a/languages/java/src/main/java/com/bitwarden/sdk/ExampleProgram.java b/languages/java/src/main/java/com/bitwarden/sdk/ExampleProgram.java deleted file mode 100644 index f3ad53036..000000000 --- a/languages/java/src/main/java/com/bitwarden/sdk/ExampleProgram.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.bitwarden.sdk; - -import com.bitwarden.sdk.schema.*; - -import java.util.UUID; - -public class ExampleProgram { - - public static void main(String[] args) { - BitwardenSettings bitwardenSettings = new BitwardenSettings(); - bitwardenSettings.setApiUrl("https://api.bitwarden.com"); - bitwardenSettings.setIdentityUrl("https://identity.bitwarden.com"); - - try (BitwardenClient bitwardenClient = new BitwardenClient(bitwardenSettings)) { - APIKeyLoginResponse apiKeyLoginResponse = bitwardenClient.accessTokenLogin(""); - - UUID organizationId = UUID.fromString(""); - ProjectResponse projectResponse = bitwardenClient.projects().create(organizationId, "NewTestProject"); - UUID projectId = projectResponse.getID(); - ProjectsResponse projectsResponse = bitwardenClient.projects().list(organizationId); - projectResponse = bitwardenClient.projects().get(projectId); - projectResponse = bitwardenClient.projects().update(projectId, organizationId, "NewTestProject2"); - - String key = "key"; - String value = "value"; - String note = "note"; - SecretResponse secretResponse = bitwardenClient.secrets().create(key, value, note, organizationId, - new UUID[]{projectId}); - UUID secretId = secretResponse.getID(); - SecretIdentifiersResponse secretIdentifiersResponse = bitwardenClient.secrets().list(organizationId); - secretResponse = bitwardenClient.secrets().get(secretId); - key = "key2"; - value = "value2"; - note = "note2"; - secretResponse = bitwardenClient.secrets().update(secretId, key, value, note, organizationId, - new UUID[]{projectId}); - - SecretsDeleteResponse secretsDeleteResponse = bitwardenClient.secrets().delete(new UUID[]{secretId}); - ProjectsDeleteResponse projectsDeleteResponse = bitwardenClient.projects().delete(new UUID[]{projectId}); - } - } -} diff --git a/languages/php/example.php b/languages/php/example.php index 0fdb6930a..7eafcb96a 100644 --- a/languages/php/example.php +++ b/languages/php/example.php @@ -2,10 +2,14 @@ require_once 'vendor/autoload.php'; -$access_token = ''; -$organization_id = ""; +$access_token = getenv('ACCESS_TOKEN'); +$organization_id = getenv('ORGANIZATION_ID'); -$client_settings = new \Bitwarden\Sdk\BitwardenSettings(); +// Configuring the URLS is optional, set them to null to use the default values +$api_url = getenv('API_URL'); +$identity_url = getenv('IDENTITY_URL'); + +$client_settings = new \Bitwarden\Sdk\BitwardenSettings($api_url, $identity_url); $bitwarden_client = new \Bitwarden\Sdk\BitwardenClient($client_settings); $bitwarden_client->access_token_login($access_token); diff --git a/languages/ruby/examples/example.rb b/languages/ruby/examples/example.rb index c0deeb437..09192aa5d 100644 --- a/languages/ruby/examples/example.rb +++ b/languages/ruby/examples/example.rb @@ -1,13 +1,14 @@ # NOTE - for example purpose only - import gem instead require 'bitwarden-sdk' -token = '' -organization_id = '' +token = ENV['ACCESS_TOKEN'] +organization_id = ENV['ORGANIZATION_ID'] -bitwarden_settings = BitwardenSDK::BitwardenSettings.new( - 'https://api.bitwarden.com', - 'https://identity.bitwarden.com/connect/token' -) +# Configuring the URLS is optional, set them to nil to use the default values +api_url = ENV['API_URL'] +identity_url = ENV['IDENTITY_URL'] + +bitwarden_settings = BitwardenSDK::BitwardenSettings.new(api_url, identity_url) bw_client = BitwardenSDK::BitwardenClient.new(bitwarden_settings) response = bw_client.access_token_login(token)