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-1402 - review and update php sdk #1032

Merged
merged 42 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
67cd747
SM-1266: Compose case sensitive imports
mzieniukbw Jul 5, 2024
d124c37
SM-1266: Consistent FFI library directories
mzieniukbw Jul 5, 2024
1c4839a
SM-1266: Fix outdated secret get by ids command
mzieniukbw Jul 5, 2024
54f4bdf
SM-1266: More verbose example
mzieniukbw Jul 5, 2024
2beea1e
SM-1266: Composer lock update (excluding dependencies)
mzieniukbw Jul 5, 2024
d147f3b
SM-1266: PHP documentation update
mzieniukbw Jul 5, 2024
cb87747
Revert "SM-1266: Consistent FFI library directories"
mzieniukbw Jul 11, 2024
e58a8a9
SM-1266: Native library locations docs update
mzieniukbw Jul 11, 2024
510ebb0
SM-1266: Native library locations relative to src
mzieniukbw Jul 12, 2024
7f03f3e
feat: `login_access_token` with state_file; update examples
tangowithfoxtrot Sep 3, 2024
4a973df
Merge branch 'main' into sm/sm-1266-php-docs
vphan916 Sep 4, 2024
862431c
Merge branch 'main' into sm-1402-review-and-update-php-sdk
tangowithfoxtrot Sep 5, 2024
6c6cd36
Merge branch 'main' into sm-1402-review-and-update-php-sdk
tangowithfoxtrot Sep 6, 2024
00b1647
fix: getbyids
tangowithfoxtrot Sep 6, 2024
c8ae6b1
refactor: projects.put -> projects.update
tangowithfoxtrot Sep 6, 2024
513b888
feat: secret syncing
tangowithfoxtrot Sep 6, 2024
5f8d322
Manually generated secretSyncRequest schema
tangowithfoxtrot Sep 6, 2024
7767ffc
udpate function args
tangowithfoxtrot Sep 6, 2024
c44f52a
tweak output format
tangowithfoxtrot Sep 6, 2024
ef6a725
move loginaccesstoken into authclient
tangowithfoxtrot Sep 6, 2024
0c9500d
Manually update PHP schemas
tangowithfoxtrot Sep 9, 2024
6e9741b
Merge branch 'main' into sm-1402-review-and-update-php-sdk
tangowithfoxtrot Sep 9, 2024
931d781
fix schema casing; newlines
tangowithfoxtrot Sep 10, 2024
efbf655
revert fully-qualified call to ClientSettings; unnecessary
tangowithfoxtrot Sep 10, 2024
1810d8c
Merge branch 'main' into sm-1402-review-and-update-php-sdk
tangowithfoxtrot Sep 10, 2024
31a2f8e
Merge branch 'sm/sm-1266-php-docs' into sm-1402-review-and-update-phpโ€ฆ
tangowithfoxtrot Sep 10, 2024
13acbc2
add basic sync example to readme
tangowithfoxtrot Sep 10, 2024
653f47b
add state file to example
tangowithfoxtrot Sep 10, 2024
e5a0596
Update languages/php/README.md
tangowithfoxtrot Sep 11, 2024
729eb3d
rm secretSync because git is being weird
tangowithfoxtrot Sep 11, 2024
7fecc77
fix weird directory naming in remote git branch
tangowithfoxtrot Sep 11, 2024
e20a6b4
pr feedback; refactor auth
tangowithfoxtrot Sep 11, 2024
7bc8b9c
update readme
tangowithfoxtrot Sep 11, 2024
4b1f271
update example
tangowithfoxtrot Sep 11, 2024
534d94b
more specific exception message
tangowithfoxtrot Sep 11, 2024
5a8d730
apply grammar suggestions
tangowithfoxtrot Sep 12, 2024
1ffd2d9
apply formatting suggestions
tangowithfoxtrot Sep 12, 2024
64e984c
Update languages/php/README.md
tangowithfoxtrot Sep 13, 2024
49b5023
[SM-1402] Automatic PHP schema generation (#1036)
mzieniukbw Sep 18, 2024
a57e2ed
add link to access token
tangowithfoxtrot Sep 18, 2024
028c43d
update project name
tangowithfoxtrot Sep 18, 2024
f255347
Revert "add link to access token"
tangowithfoxtrot Sep 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions languages/php/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
vendor
src/lib/
src/Schemas/
56 changes: 56 additions & 0 deletions languages/php/INSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# PHP Installation

## Introduction

Composer is used to build the 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 the SDK yourself, see the [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 STATE_FILE="<state_file>"
export ORGANIZATION_ID="<organization_id>"
export API_URL="https://api.bitwarden.com"
export IDENTITY_URL="https://identity.bitwarden.com"

php example.php
```
133 changes: 77 additions & 56 deletions languages/php/README.md
mzieniukbw marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,100 +1,121 @@
# 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.

## 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].
tangowithfoxtrot marked this conversation as resolved.
Show resolved Hide resolved

### 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>";
tangowithfoxtrot marked this conversation as resolved.
Show resolved Hide resolved
$state_file = "<state-file>";
$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);
$bitwarden_client->auth->login_access_token($access_token, $state_file);
mzieniukbw marked this conversation as resolved.
Show resolved Hide resolved
```

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"
$res = $bitwarden_client->projects->create($name, $organization_id);
```php
$name = "PHP project";
$res = $bitwarden_client->projects->create($organization_id, $name);
$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->update($organization_id, $project_id, $name);
```

// 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($organization_id, $key, $value, $note, [$project_id]);
$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 secrets
### List all secrets

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

### Update secret

```php
$key = "Updated key";
$note = "Updated note";
$value = "Updated value";
$res = $bitwarden_client->secrets->update($organization_id, $secret_id, $key, $value, $note, [$project_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);
### Sync secrets

// delete secret
$res = $bitwarden_sdk->secrets->delete([$secret_id]);
```php
$last_synced_date = "2024-09-01T00:00:00Z";
$res = $bitwarden_client->secrets->sync($organization_id, $last_synced_date);
```

### 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/
4 changes: 2 additions & 2 deletions languages/php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"version": "0.1.0",
"require": {
"php": "^8.0",
"swaggest/json-schema": "^0.12.42",
"ext-ffi": "*"
},
"autoload": {
"psr-4": {
"Bitwarden\\Sdk\\": "src/"
}
},
"files": ["src/Schemas/Schemas.php"]
},
"authors": [
{
Expand Down
Loading
Loading