Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SM-1266] Review PHP docs #878

Closed
wants to merge 10 commits into from
55 changes: 55 additions & 0 deletions languages/php/INSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# PHP Installation

## Introduction

Composer is used to build PHP Bitwarden client library.

## Prerequisites

- PHP >= 8.0
- FFI extension enabled in PHP configuration
- Composer
- Bitwarden SDK native library.
- Expected in one of below locations, depending on the OS and architecture.
The `src` is relative path to the [src](./src) directory.
- Windows x86_64: `src\lib\windows-x64\bitwarden_c.dll`
- Linux x86_64: `src/lib/linux-x64/libbitwarden_c.so`
- macOS x86_64: `src/lib/macos-x64/libbitwarden_c.dylib`
- macOS aarch64: `src/lib/macos-arm64/libbitwarden_c.dylib`
- If you prefer to build SDK yourself, see [SDK README.md](../../README.md) for instructions.

## Build Commands

```shell
composer install
```

## Example

### macOS

#### Install Prerequisites

Use brew Composer and PHP

```shell
brew install php
brew install composer
```

#### Build Commands

```shell
composer install
```

## Example SDK Usage Project

```shell
export ACCESS_TOKEN="<access_token>"
export ORGANIZATION_ID="<organization_id>"
export API_URL="https://api.bitwarden.com"
export IDENTITY_URL="https://identity.bitwarden.com"

php example.php
```
124 changes: 69 additions & 55 deletions languages/php/README.md
Original file line number Diff line number Diff line change
@@ -1,100 +1,114 @@
# Bitwarden Secrets Manager SDK wrapper for PHP

PHP bindings for interacting with the [Bitwarden Secrets Manager]. This is a beta release and might be missing some functionality.
Supported are CRUD operations on project and secret entities.
PHP bindings for interacting with the [Bitwarden Secrets Manager]. This is a beta release and might be missing some
functionality.

## Installation

Requirements:
- PHP >= 8.0
- Composer
- Bitwarden C libraries which you can generate using BitwardenSDK and following instructions in its readme (requires Rust). https://github.com/bitwarden/sdk
If you are not using the standalone version of this library, file will be placed in `target/debug` folder if you are using from BitwardenSDK repository.
- Access token for the Bitwarden account

See the [installation instructions](./INSTALL.md)

## Usage

To interact with the client first you need to obtain the access token from Bitwarden.
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.
### Create access token

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.
To interact with the client first you need to obtain the access token from Bitwarden.
Review the help documentation on [Access Tokens].

### Create new Bitwarden client

```php
$access_token = '<your token here>';
$api_url = "<api url>";
$identity_url = "<identity url>";
require_once 'vendor/autoload.php';

$access_token = '<access-token>';
$organization_id = "<organization-id>";
$api_url = "https://api.bitwarden.com";
$identity_url = "https://identity.bitwarden.com";

$bitwarden_settings = new \Bitwarden\Sdk\BitwardenSettings($api_url, $identity_url);

$bitwarden_client = new \Bitwarden\Sdk\BitwardenClient($bitwarden_settings);
$bitwarden_client->access_token_login($access_token);
```

After successful authorization you can interact with client to manage your projects and secrets.
```php
$organization_id = "<your organization id here>";
Initialize `BitwardenSettings` by passing `$api_url` and `$identity_url` or set to null to use the defaults.
The default for `api_url` is `https://api.bitwarden.com` and for `identity_url` is `https://identity.bitwarden.com`.

$bitwarden_client = new \Bitwarden\Sdk\BitwardenClient($bitwarden_settings);
$res = $bitwarden_client->access_token_login($access_token);
### Create new project

// create project
$name = "PHP project"
```php
$name = "PHP project";
$res = $bitwarden_client->projects->create($name, $organization_id);
$project_id = $res->id;
```

### Get project

// get project
```php
$res = $bitwarden_client->projects->get($project_id);
```

### List all projects

// list projects
```php
$res = $bitwarden_client->projects->list($organization_id);
```

// update project
$name = "Updated PHP project"
$res = $bitwarden_client->projects->put($project_id, $name, $organization_id);
### Update project

// get secret
$res = $bitwarden_client->secrets->get($secret_id);
```php
$name = "Updated PHP project";
$res = $bitwarden_client->projects->put($project_id, $name, $organization_id);
```

// list secrets
$res = $bitwarden_client->secrets->list($organization_id);
### Delete project

// delete project
```php
$res = $bitwarden_client->projects->delete([$project_id]);
```

### Create new secret

```php
$key = "Secret key";
$note = "Secret note";
$value = "Secret value";
$res = $bitwarden_client->secrets->create($key, $note, $organization_id, [$project_id], $value);
$secret_id = $res->id;
```

Similarly, you interact with secrets:
### Get secret

```php
$organization_id = "<your organization id here>";
$res = $bitwarden_client->secrets->get($secret_id);
```

// create secret
$key = "AWS secret key";
$note = "Private account";
$secret = "76asaj,Is_)"
$res = $bitwarden_client->secrets->create($key, $note, $organization_id, [$project_id], $secret);
$secret_id = $res->id;
### Get multiple secrets

// get secret
$res = $bitwarden_sdk->secrets->get($secret_id);
```php
$res = $bitwarden_client->secrets->get_by_ids([$secret_id]);
```

### List all secrets

// list secrets
```php
$res = $bitwarden_client->secrets->list($organization_id);
```

// update secret
$note = "Updated account";
$key = "AWS private updated"
$secret = "7uYTE,:Aer"
$res = $bitwarden_client->secrets->update($secret_id, $key, $note, $organization_id, [$project_id], $secret);
### Update secret

// delete secret
$res = $bitwarden_sdk->secrets->delete([$secret_id]);
```php
$key = "Updated key";
$note = "Updated note";
$value = "Updated value";
$res = $bitwarden_client->secrets->update($secret_id, $key, $note, $organization_id, [$project_id], $value);
```

### Delete secret

```php
$res = $bitwarden_client->secrets->delete([$secret_id]);
```

[Access Tokens]: https://bitwarden.com/help/access-tokens/

[Bitwarden Secrets Manager]: https://bitwarden.com/products/secrets-manager/
2 changes: 1 addition & 1 deletion languages/php/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions languages/php/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,34 @@
// create project
$res = $bitwarden_client->projects->create('php project', $organization_id);
$project_id = $res->id;
echo "Project: $project_id\n";

// get project
$res = $bitwarden_client->projects->get($project_id);

// list projects
$res = $bitwarden_client->projects->list($organization_id);
$project_count = count($res->data);
echo "Projects count: $project_count\n";

// update project
$res = $bitwarden_client->projects->put($project_id, 'php test awesome', $organization_id);

// create secret
$res = $bitwarden_client->secrets->create("New Key", "hello world", $organization_id, [$project_id], "123");
$secret_id = $res->id;
echo "Secret: $secret_id\n";

// get secret
$res = $bitwarden_client->secrets->get($secret_id);

// get secrets
$res = $bitwarden_client->secrets->get_by_ids([$secret_id]);

// list secrets
$res = $bitwarden_client->secrets->list($organization_id);
$secret_count = count($res->data);
echo "Secrets count: $secret_count\n";

// update secret
$res = $bitwarden_client->secrets->update($secret_id, "hello world 2", "hello", $organization_id, [$project_id], "123");
Expand Down
2 changes: 1 addition & 1 deletion languages/php/src/BitwardenClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Bitwarden\Sdk;

use Bitwarden\Sdk\Schemas\AccessTokenLoginRequest;
use Bitwarden\Sdk\schemas\ClientSettings;
use Bitwarden\Sdk\Schemas\ClientSettings;
use Bitwarden\Sdk\Schemas\Command;
use FFI;
use Swaggest\JsonDiff\Exception;
Expand Down
4 changes: 2 additions & 2 deletions languages/php/src/BitwardenLib.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function __construct()
$lib_file = null;

if (PHP_OS === 'WINNT') {
$lib_file = '/lib/windows-x64/bitwarden_c.dll';
$lib_file = __DIR__.'/lib/windows-x64/bitwarden_c.dll';
if (file_exists($lib_file) == false) {
$lib_file = __DIR__.'/../../../target/debug/bitwarden_c.dll';
}
} elseif (PHP_OS === 'Linux') {
$lib_file = '/lib/linux-x64/libbitwarden_c.so';
$lib_file = __DIR__.'/lib/linux-x64/libbitwarden_c.so';
if (file_exists($lib_file) == false) {
$lib_file = __DIR__.'/../../../target/debug/libbitwarden_c.so';
}
Expand Down
2 changes: 1 addition & 1 deletion languages/php/src/SecretsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function get_by_ids(array $secret_ids): \stdClass
$project_get_by_ids_request->ids = $secret_ids;
$project_get_by_ids_request->validate();
$secrets_command = new SecretsCommand();
$secrets_command->get_by_ids = $project_get_by_ids_request->jsonSerialize();
$secrets_command->getByIds = $project_get_by_ids_request->jsonSerialize();
Thomas-Avery marked this conversation as resolved.
Show resolved Hide resolved
return $this->run_secret_command($secrets_command);
}

Expand Down
Loading