From d9fbaa7a1ccac71db56eff72c599fd43b52fe442 Mon Sep 17 00:00:00 2001 From: Milos Trifunovic Date: Mon, 27 Nov 2023 22:39:02 +0100 Subject: [PATCH] add bitwarden settings --- .gitignore | 3 - languages/php/README.md | 23 +++--- languages/php/example.php | 2 +- languages/php/src/BitwardenClient.php | 6 +- languages/php/src/BitwardenSettings.php | 54 +++++++++++++ .../php/src/schemas/ApiKeyLoginRequest.php | 51 ------------ .../php/src/schemas/FingerprintRequest.php | 43 ---------- languages/php/src/schemas/Kdf.php | 32 -------- languages/php/src/schemas/KdfArgon2id.php | 48 ------------ languages/php/src/schemas/KdfPBKDF2.php | 34 -------- .../php/src/schemas/PasswordLoginRequest.php | 60 -------------- languages/php/src/schemas/SyncRequest.php | 32 -------- .../php/src/schemas/TwoFactorRequest.php | 78 ------------------- 13 files changed, 71 insertions(+), 395 deletions(-) create mode 100644 languages/php/src/BitwardenSettings.php delete mode 100644 languages/php/src/schemas/ApiKeyLoginRequest.php delete mode 100644 languages/php/src/schemas/FingerprintRequest.php delete mode 100644 languages/php/src/schemas/Kdf.php delete mode 100644 languages/php/src/schemas/KdfArgon2id.php delete mode 100644 languages/php/src/schemas/KdfPBKDF2.php delete mode 100644 languages/php/src/schemas/PasswordLoginRequest.php delete mode 100644 languages/php/src/schemas/SyncRequest.php delete mode 100644 languages/php/src/schemas/TwoFactorRequest.php diff --git a/.gitignore b/.gitignore index 013e3ecba..3f459493b 100644 --- a/.gitignore +++ b/.gitignore @@ -35,9 +35,6 @@ crates/bitwarden-napi/sdk-napi.*.node # Complied TypeScript client crates/bitwarden-napi/dist -# PHP -languages/php/vendor - # Uniffi languages/swift/BitwardenFFI.xcframework languages/swift/tmp diff --git a/languages/php/README.md b/languages/php/README.md index 3a5eb7978..ba849b0e1 100644 --- a/languages/php/README.md +++ b/languages/php/README.md @@ -16,22 +16,23 @@ If you are not using the standalone version of this library, file will be placed ## Usage To interact with the client first you need to obtain the access token from Bitwarden. -You can initialize ClientSettings and its setting before passing it to the BitwardenClient. +You can then initialize BitwardenSettings passing $api_url and $identity_url if needed. These parameteres are +optional and if they are not defined, BitwardenSettings instance will try to get these values from ENV, and +if they are not defined there as well, it will use defaults: `https://api.bitwarden.com` as api_url and +`https://identity.bitwarden.com` as identity_url. You can also pass device type as argument but that is entirely +optional. -```php -$client_settings = new \Bitwarden\Sdk\Schemas\ClientSettings() -$client_settings->apiUrl = getenv('API_URL') ?: 'https://api.bitwarden.com'; -$client_settings->identityUrl = getenv('IDENTITY_URL') ?: 'https://identity.bitwarden.com'; -$client_settings->userAgent = getenv('USER_AGENT') ?: 'SDK'; -$client_settings->deviceType = getenv('DEVICE_TYPE') ?: 'SDK'; -``` +Passing BitwardenSettings instance to BitwardenClient will initialize it. Before using the client you must +be authorized by calling the access_token_login method passing your Bitwarden access token to it. -Authorization can be performed using access token like so: ```php -$access_token = ''; +$api_url = ""; +$identity_url = ""; +$client_settings = new \Bitwarden\Sdk\BitwardenSettings(); + $bitwarden_client = new \Bitwarden\Sdk\BitwardenClient($client_settings); -$result = $bitwarden_client->access_token_login($access_token); +$res = $bitwarden_client->access_token_login($access_token); ``` After successful authorization you can interact with client to manage your projects and secrets. diff --git a/languages/php/example.php b/languages/php/example.php index a07da707e..687018f44 100644 --- a/languages/php/example.php +++ b/languages/php/example.php @@ -5,7 +5,7 @@ $access_token = ''; $organization_id = ""; -$client_settings = new \Bitwarden\Sdk\Schemas\ClientSettings(); +$client_settings = new \Bitwarden\Sdk\BitwardenSettings(); $bitwarden_client = new \Bitwarden\Sdk\BitwardenClient($client_settings); $res = $bitwarden_client->access_token_login($access_token); diff --git a/languages/php/src/BitwardenClient.php b/languages/php/src/BitwardenClient.php index 79168cee1..bae1a0dff 100644 --- a/languages/php/src/BitwardenClient.php +++ b/languages/php/src/BitwardenClient.php @@ -23,9 +23,11 @@ class BitwardenClient private FFI\CData $handle; - public function __construct(ClientSettings $clientSettings) + public function __construct(BitwardenSettings $bitwardenSettings) { - $this->clientSettings = $clientSettings; + $this->clientSettings = new ClientSettings(); + $this->clientSettings->apiUrl = $bitwardenSettings->get_api_url(); + $this->clientSettings->identityUrl = $bitwardenSettings->get_identity_url(); $this->bitwarden_lib = new BitwardenLib(); $this->handle = $this->bitwarden_lib->init($this->clientSettings); diff --git a/languages/php/src/BitwardenSettings.php b/languages/php/src/BitwardenSettings.php new file mode 100644 index 000000000..4e4dbff56 --- /dev/null +++ b/languages/php/src/BitwardenSettings.php @@ -0,0 +1,54 @@ +api_url = getenv('API_URL') ?: 'https://api.bitwarden.com'; + $this->identity_url = getenv('IDENTITY_URL') ?: 'https://identity.bitwarden.com'; + } else { + $this->api_url = $api_url; + } + + if (is_null($identity_url)) + { + $this->identity_url = getenv('IDENTITY_URL') ?: 'https://identity.bitwarden.com'; + } else { + $this->identity_url = $identity_url; + } + + $this->device_type = $device_type ? isset($device_type) : ""; + } + + public function get_api_url(): string + { + return $this->api_url; + } + + public function get_identity_url(): string + { + return $this->identity_url; + } + + public function get_user_agent(): string + { + return $this->user_agent; + } + + public function get_device_type(): string + { + return $this->device_type; + } +} diff --git a/languages/php/src/schemas/ApiKeyLoginRequest.php b/languages/php/src/schemas/ApiKeyLoginRequest.php deleted file mode 100644 index 79c98e509..000000000 --- a/languages/php/src/schemas/ApiKeyLoginRequest.php +++ /dev/null @@ -1,51 +0,0 @@ -clientId = Schema::string(); - $properties->clientId->description = "Bitwarden account client_id"; - $properties->clientSecret = Schema::string(); - $properties->clientSecret->description = "Bitwarden account client_secret"; - $properties->password = Schema::string(); - $properties->password->description = "Bitwarden account master password"; - $ownerSchema->type = Schema::OBJECT; - $ownerSchema->additionalProperties = false; - $ownerSchema->description = "Login to Bitwarden with Api Key"; - $ownerSchema->required = array( - self::names()->clientId, - self::names()->clientSecret, - self::names()->password, - ); - $ownerSchema->setFromRef('#/definitions/ApiKeyLoginRequest'); - } -} diff --git a/languages/php/src/schemas/FingerprintRequest.php b/languages/php/src/schemas/FingerprintRequest.php deleted file mode 100644 index 30bc74fc9..000000000 --- a/languages/php/src/schemas/FingerprintRequest.php +++ /dev/null @@ -1,43 +0,0 @@ -fingerprintMaterial = Schema::string(); - $properties->fingerprintMaterial->description = "The input material, used in the fingerprint generation process."; - $properties->publicKey = Schema::string(); - $properties->publicKey->description = "The user's public key encoded with base64."; - $ownerSchema->type = Schema::OBJECT; - $ownerSchema->additionalProperties = false; - $ownerSchema->required = array( - self::names()->fingerprintMaterial, - self::names()->publicKey, - ); - $ownerSchema->setFromRef('#/definitions/FingerprintRequest'); - } -} \ No newline at end of file diff --git a/languages/php/src/schemas/Kdf.php b/languages/php/src/schemas/Kdf.php deleted file mode 100644 index 6e9ce1e5c..000000000 --- a/languages/php/src/schemas/Kdf.php +++ /dev/null @@ -1,32 +0,0 @@ -argon2id = KdfArgon2id::schema(); - $ownerSchema->type = Schema::OBJECT; - $ownerSchema->additionalProperties = false; - $ownerSchema->required = array( - self::names()->argon2id, - ); - } -} \ No newline at end of file diff --git a/languages/php/src/schemas/KdfArgon2id.php b/languages/php/src/schemas/KdfArgon2id.php deleted file mode 100644 index 919b6276e..000000000 --- a/languages/php/src/schemas/KdfArgon2id.php +++ /dev/null @@ -1,48 +0,0 @@ -iterations = Schema::integer(); - $properties->iterations->minimum = 1.0; - $properties->iterations->format = "uint32"; - $properties->memory = Schema::integer(); - $properties->memory->minimum = 1.0; - $properties->memory->format = "uint32"; - $properties->parallelism = Schema::integer(); - $properties->parallelism->minimum = 1.0; - $properties->parallelism->format = "uint32"; - $ownerSchema->type = Schema::OBJECT; - $ownerSchema->additionalProperties = false; - $ownerSchema->required = array( - self::names()->iterations, - self::names()->memory, - self::names()->parallelism, - ); - } -} diff --git a/languages/php/src/schemas/KdfPBKDF2.php b/languages/php/src/schemas/KdfPBKDF2.php deleted file mode 100644 index 31c635adb..000000000 --- a/languages/php/src/schemas/KdfPBKDF2.php +++ /dev/null @@ -1,34 +0,0 @@ -iterations = Schema::integer(); - $properties->iterations->minimum = 1.0; - $properties->iterations->format = "uint32"; - $ownerSchema->type = Schema::OBJECT; - $ownerSchema->additionalProperties = false; - $ownerSchema->required = array( - self::names()->iterations, - ); - } -} diff --git a/languages/php/src/schemas/PasswordLoginRequest.php b/languages/php/src/schemas/PasswordLoginRequest.php deleted file mode 100644 index 44826a879..000000000 --- a/languages/php/src/schemas/PasswordLoginRequest.php +++ /dev/null @@ -1,60 +0,0 @@ -email = Schema::string(); - $properties->email->description = "Bitwarden account email address"; - $properties->password = Schema::string(); - $properties->password->description = "Bitwarden account master password"; - $properties->twoFactor = new Schema(); - $properties->twoFactor->anyOf[0] = TwoFactorRequest::schema(); - $properties->twoFactor->anyOf[1] = Schema::null(); - $properties->kdf = new Schema(); - $propertiesKdfAllOf0 = new Schema(); - $propertiesKdfAllOf0->oneOf[0] = Kdf::schema(); - $propertiesKdfAllOf0->oneOf[1] = Kdf::schema(); - $propertiesKdfAllOf0->setFromRef('#/definitions/Kdf'); - $properties->kdf->allOf[0] = $propertiesKdfAllOf0; - $properties->kdf->description = "Kdf from prelogin"; - $ownerSchema->type = Schema::OBJECT; - $ownerSchema->additionalProperties = false; - $ownerSchema->description = "Login to Bitwarden with Username and Password"; - $ownerSchema->required = array( - self::names()->email, - self::names()->kdf, - self::names()->password, - ); - $ownerSchema->setFromRef('#/definitions/PasswordLoginRequest'); - } -} diff --git a/languages/php/src/schemas/SyncRequest.php b/languages/php/src/schemas/SyncRequest.php deleted file mode 100644 index 888f953bd..000000000 --- a/languages/php/src/schemas/SyncRequest.php +++ /dev/null @@ -1,32 +0,0 @@ -excludeSubdomains = (new Schema())->setType([Schema::BOOLEAN, Schema::NULL]); - $properties->excludeSubdomains->description = "Exclude the subdomains from the response, defaults to false"; - $ownerSchema->type = Schema::OBJECT; - $ownerSchema->additionalProperties = false; - $ownerSchema->setFromRef('#/definitions/SyncRequest'); - } -} diff --git a/languages/php/src/schemas/TwoFactorRequest.php b/languages/php/src/schemas/TwoFactorRequest.php deleted file mode 100644 index a0de95210..000000000 --- a/languages/php/src/schemas/TwoFactorRequest.php +++ /dev/null @@ -1,78 +0,0 @@ -token = Schema::string(); - $properties->token->description = "Two-factor Token"; - $properties->provider = new Schema(); - $propertiesProviderAllOf0 = Schema::string(); - $propertiesProviderAllOf0->enum = array( - self::AUTHENTICATOR, - self::EMAIL, - self::DUO, - self::YUBIKEY, - self::U2F, - self::REMEMBER, - self::ORGANIZATION_DUO, - self::WEB_AUTHN, - ); - $propertiesProviderAllOf0->setFromRef('#/definitions/TwoFactorProvider'); - $properties->provider->allOf[0] = $propertiesProviderAllOf0; - $properties->provider->description = "Two-factor provider"; - $properties->remember = Schema::boolean(); - $properties->remember->description = "Two-factor remember"; - $ownerSchema->type = Schema::OBJECT; - $ownerSchema->additionalProperties = false; - $ownerSchema->required = array( - self::names()->provider, - self::names()->remember, - self::names()->token, - ); - $ownerSchema->setFromRef('#/definitions/TwoFactorRequest'); - } -}