Skip to content

Commit

Permalink
PHP SDK implementation (#316)
Browse files Browse the repository at this point in the history
## Type of change

Implemented PHP library that wraps native C library and exposed its
commands through BitwardenClient class.

```
- [ ] Bug fix
- [ x] New feature development
- [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other
```

## Objective

To provide PHP interface for bitwarden c library files by which you can
use PHP code to work with Bitwarden API. It implements CRUD requests for
projects and secrets.

## Code changes

TODO: Updating package repository - will hosted on Packagist.

---------

Co-authored-by: Daniel García <[email protected]>
  • Loading branch information
milost77 and dani-garcia authored Dec 1, 2023
1 parent b6c6532 commit e219efa
Show file tree
Hide file tree
Showing 31 changed files with 1,919 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/publish-php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Publish PHP SDK

on:
pull_request:
branches:
- master

jobs:
build_rust:
uses: ./.github/workflows/build-rust-cross-platform.yml

setup_php:
name: Setup PHP
runs-on: ubuntu-22.04
needs:
- build_rust

steps:
- name: Checkout Repository
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@7fdd3ece872ec7ec4c098ae5ab7637d5e0a96067 # 2.26.0
with:
php-version: "8.0"
tools: composer
extensions: ext-ffi

- name: Composer check
run: |
composer install
composer validate
working-directory: languages/php/

- name: Download x86_64-apple-darwin files
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: libbitwarden_c_files-x86_64-apple-darwin
path: temp/macos-x64

- name: Download aarch64-apple-darwin files
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: libbitwarden_c_files-aarch64-apple-darwin
path: temp/macos-arm64

- name: Download x86_64-unknown-linux-gnu files
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: libbitwarden_c_files-x86_64-unknown-linux-gnu
path: temp/ubuntu-x64

- name: Download x86_64-pc-windows-msvc files
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: libbitwarden_c_files-x86_64-pc-windows-msvc
path: temp/windows-x64

- name: Copy lib files
run: |
mkdir -p languages/php/src/lib/macos-arm64
mkdir -p languages/php/src/lib/ubuntu-x64
mkdir -p languages/php/src/lib/macos-x64
mkdir -p languages/php/src/lib/windows-x64
platforms=("macos-arm64" "ubuntu-x64" "macos-x64" "windows-x64")
files=("libbitwarden_c.dylib" "libbitwarden_c.so" "libbitwarden_c.dylib" "bitwarden_c.dll")
for ((i=0; i<${#platforms[@]}; i++)); do
cp "temp/${platforms[$i]}/${files[$i]}" "languages/php/src/lib/${platforms[$i]}/${files[$i]}"
done
- name: Publish version
run: curl -XPOST -H'content-type:application/json' 'https://packagist.org/api/update-package?username=malirobot&apiToken=${{secrets.PACKAGIST_KEY}}' -d'{"repository":{"url":"https://packagist.org/packages/bitwarden/sdk"}}'
working-directory: languages/php/
2 changes: 2 additions & 0 deletions languages/php/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
vendor
100 changes: 100 additions & 0 deletions languages/php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# 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


## 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.

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.


```php
$access_token = '<your token here>';
$api_url = "<api url>";
$identity_url = "<identity url>";
$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>";

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

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

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

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

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

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

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

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

```

Similarly, you interact with secrets:
```php
$organization_id = "<your organization id here>";

// 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 secret
$res = $bitwarden_sdk->secrets->get($secret_id);

// list secrets
$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);

// delete secret
$res = $bitwarden_sdk->secrets->delete([$secret_id]);
```


[Bitwarden Secrets Manager]: https://bitwarden.com/products/secrets-manager/
22 changes: 22 additions & 0 deletions languages/php/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "bitwarden/sdk",
"description": "PHP bindings for interacting with the Bitwarden Secrets Manager. This is a beta release and might be missing some functionality.",
"type": "library",
"keywords": ["bitwarden","sdk","password-manager"],
"homepage": "https://github.com/bitwarden/sdk",
"require": {
"php": "^8.0",
"swaggest/json-schema": "^0.12.42",
"ext-ffi": "*"
},
"autoload": {
"psr-4": {
"Bitwarden\\Sdk\\": "src/"
}
},
"authors": [
{
"name": "Bitwarden Inc."
}
]
}
Loading

0 comments on commit e219efa

Please sign in to comment.