Skip to content

Commit

Permalink
update upon pr
Browse files Browse the repository at this point in the history
  • Loading branch information
milost77 committed Nov 20, 2023
1 parent 3d09abf commit 5e4e2e7
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 52 deletions.
35 changes: 18 additions & 17 deletions languages/php/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,50 @@ 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.
Client will be initialized with the default client settings if they are not provided
via env variables.
You can initialize ClientSettings and its setting before passing it to the BitwardenClient.

```php
$this->clientSettings->apiUrl = getenv('API_URL') ?: 'https://api.bitwarden.com';
$this->clientSettings->identityUrl = getenv('IDENTITY_URL') ?: 'https://identity.bitwarden.com';
$this->clientSettings->userAgent = getenv('USER_AGENT') ?: 'SDK';
$this->clientSettings->deviceType = getenv('DEVICE_TYPE') ?: 'SDK';
$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';
```

Authorization can be performed using access token like so:

```php
$access_token = '<you access token here>';
$bitwarden_sdk = new \Bitwarden\Sdk\BitwardenSDK();
$bitwarden_sdk = new \Bitwarden\Sdk\BitwardenClient($client_settings);
$result = $bitwarden_sdk->authorize($access_token);
```

After successful authorization you can interact with client to manage your projects and secrets.
```php
// get project
$res = $bitwarden_sdk->projectsClient->get("00056058-cc70-4cd2-baea-b0810134a729");
$res = $bitwarden_sdk->client->get("00056058-cc70-4cd2-baea-b0810134a729");
// list projects
$res = $bitwarden_sdk->projectsClient->list('5688da1f-cc25-41d7-bb9f-b0740144ef1d');
$res = $bitwarden_sdk->client->list('5688da1f-cc25-41d7-bb9f-b0740144ef1d');
// create project
$res = $bitwarden_sdk->projectsClient->create('php project', '5688da1f-cc25-41d7-bb9f-b0740144ef1d');
$res = $bitwarden_sdk->client->create('php project', '5688da1f-cc25-41d7-bb9f-b0740144ef1d');
// update project
$res = $bitwarden_sdk->projectsClient->put('920fe206-ab3b-429d-a4b7-b0ac00e17acf', 'php project awesome', '5688da1f-cc25-41d7-bb9f-b0740144ef1d');
$res = $bitwarden_sdk->client->put('920fe206-ab3b-429d-a4b7-b0ac00e17acf', 'php project awesome', '5688da1f-cc25-41d7-bb9f-b0740144ef1d');
// delete project
$res = $bitwarden_sdk->projectsClient->delete(['920fe206-ab3b-429d-a4b7-b0ac00e17acf']);
$res = $bitwarden_sdk->client->delete(['920fe206-ab3b-429d-a4b7-b0ac00e17acf']);
```

Similarly, you interact with secrets:
```php
// get secret
$res = $bitwarden_sdk->secretsClient->get("75d3a7ff-30ed-433a-91aa-b099016e4833");
$res = $bitwarden_sdk->secrets->get("75d3a7ff-30ed-433a-91aa-b099016e4833");
// list secrets
$res = $bitwarden_sdk->secretsClient->list("5688da1f-cc25-41d7-bb9f-b0740144ef1d");
$res = $bitwarden_sdk->secrets->list("5688da1f-cc25-41d7-bb9f-b0740144ef1d");
// create secret
$res = $bitwarden_sdk->secretsClient->create("New Key", "hello world", "5688da1f-cc25-41d7-bb9f-b0740144ef1d", ["b23818dd-827b-4a22-b97a-b07e010ae9d4"], "123");
$res = $bitwarden_sdk->secrets->create("New Key", "hello world", "5688da1f-cc25-41d7-bb9f-b0740144ef1d", ["b23818dd-827b-4a22-b97a-b07e010ae9d4"], "123");
// update secret
$res = $bitwarden_sdk->secretsClient->update("901d102d-af7d-46a1-99f5-b0a6017e2f07", "hello world 2", "hello", "5688da1f-cc25-41d7-bb9f-b0740144ef1d", ["b23818dd-827b-4a22-b97a-b07e010ae9d4"], "123");
$res = $bitwarden_sdk->secrets->update("901d102d-af7d-46a1-99f5-b0a6017e2f07", "hello world 2", "hello", "5688da1f-cc25-41d7-bb9f-b0740144ef1d", ["b23818dd-827b-4a22-b97a-b07e010ae9d4"], "123");
// delete secret
$res = $bitwarden_sdk->secretsClient->delete(["380b5c30-d8fc-472d-a514-b0ac00f17071"]);
$res = $bitwarden_sdk->secrets->delete(["380b5c30-d8fc-472d-a514-b0ac00f17071"]);
```


Expand Down
42 changes: 28 additions & 14 deletions languages/php/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,41 @@
require_once 'vendor/autoload.php';

$access_token = '<you access token here>';
$bitwarden_sdk = new \Bitwarden\Sdk\BitwardenSDK();
$organization_id = "<your organization id here>";

$client_settings = new \Bitwarden\Sdk\Schemas\ClientSettings();

$bitwarden_sdk = new \Bitwarden\Sdk\BitwardenClient($client_settings);
$res = $bitwarden_sdk->authorize($access_token);

// create project
$res = $bitwarden_sdk->projects->create('php project', $organization_id);
$project_id = $res->id;

// get project
$res = $bitwarden_sdk->projectsClient->get("00056058-cc70-4cd2-baea-b0810134a729");
$res = $bitwarden_sdk->projects->get($project_id);

// list projects
$res = $bitwarden_sdk->projectsClient->list('5688da1f-cc25-41d7-bb9f-b0740144ef1d');
// create project
$res = $bitwarden_sdk->projectsClient->create('php project', '5688da1f-cc25-41d7-bb9f-b0740144ef1d');
$res = $bitwarden_sdk->projects->list($organization_id);

// update project
$res = $bitwarden_sdk->projectsClient->put('920fe206-ab3b-429d-a4b7-b0ac00e17acf', 'php project awesome', '5688da1f-cc25-41d7-bb9f-b0740144ef1d');
// delete project
$res = $bitwarden_sdk->projectsClient->delete(['920fe206-ab3b-429d-a4b7-b0ac00e17acf']);
$res = $bitwarden_sdk->projects->put($project_id, 'php test awesome', $organization_id);

// create secret
$res = $bitwarden_sdk->secrets->create("New Key", "hello world", $organization_id, [$project_id], "123");
$secret_id = $res->id;

// get secret
$res = $bitwarden_sdk->secretsClient->get("75d3a7ff-30ed-433a-91aa-b099016e4833");
$res = $bitwarden_sdk->secrets->get($secret_id);

// list secrets
$res = $bitwarden_sdk->secretsClient->list("5688da1f-cc25-41d7-bb9f-b0740144ef1d");
// create secret
$res = $bitwarden_sdk->secretsClient->create("New Key", "hello world", "5688da1f-cc25-41d7-bb9f-b0740144ef1d", ["b23818dd-827b-4a22-b97a-b07e010ae9d4"], "123");
$res = $bitwarden_sdk->secrets->list($organization_id);

// update secret
$res = $bitwarden_sdk->secretsClient->update("901d102d-af7d-46a1-99f5-b0a6017e2f07", "hello world 2", "hello", "5688da1f-cc25-41d7-bb9f-b0740144ef1d", ["b23818dd-827b-4a22-b97a-b07e010ae9d4"], "123");
$res = $bitwarden_sdk->secrets->update($secret_id, "hello world 2", "hello", $organization_id, [$project_id], "123");

// delete secret
$res = $bitwarden_sdk->secretsClient->delete(["380b5c30-d8fc-472d-a514-b0ac00f17071"]);
$res = $bitwarden_sdk->secrets->delete([$secret_id]);

// delete project
$res = $bitwarden_sdk->projects->delete([$project_id]);
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
use Swaggest\JsonDiff\Exception;


class BitwardenSDK
class BitwardenClient
{
private BitwardenLib $bitwarden_lib;

private ClientSettings $clientSettings;

public ProjectsClient $projectsClient;
public ProjectsClient $projects;

public SecretsClient $secretsClient;
public SecretsClient $secrets;

private CommandRunner $commandRunner;

private FFI\CData $handle;

public function __construct()
public function __construct(ClientSettings $clientSettings)
{
$this->clientSettings = new ClientSettings();
$this->clientSettings = $clientSettings;
$this->clientSettings->apiUrl = getenv('API_URL') ?: 'https://api.bitwarden.com';
$this->clientSettings->identityUrl = getenv('IDENTITY_URL') ?: 'https://identity.bitwarden.com';
$this->clientSettings->userAgent = getenv('USER_AGENT') ?: 'SDK';
Expand All @@ -35,17 +35,17 @@ public function __construct()
$this->handle = $this->bitwarden_lib->init($this->clientSettings);

$this->commandRunner = new CommandRunner($this->bitwarden_lib, $this->handle);
$this->projectsClient = new ProjectsClient($this->commandRunner);
$this->secretsClient = new SecretsClient($this->commandRunner);
$this->projects = new ProjectsClient($this->commandRunner);
$this->secrets = new SecretsClient($this->commandRunner);
}

/**
* @throws \Exception
*/
public function authorize(string $access_token)
public function authorize(string $accessTokenLogin)
{
$access_token_request = new AccessTokenLoginRequest();
$access_token_request->accessToken = $access_token;
$access_token_request->accessToken = $accessTokenLogin;
$command = new Command();
$command->accessTokenLogin = $access_token_request->jsonSerialize();
$result = $this->commandRunner->run($command);
Expand All @@ -60,7 +60,7 @@ public function authorize(string $access_token)
return $result;
}

public function free_mem()
public function __destruct()
{
$this->bitwarden_lib->free_mem();
}
Expand Down
6 changes: 3 additions & 3 deletions languages/php/src/BitwardenLib.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function __construct()
if (PHP_OS === 'WINNT') {
$lib_file = '/lib/windows-x64/bitwarden_c.dll';
if (file_exists($lib_file) == false) {
$lib_file = __DIR__.'../../../../target/debug/bitwarden_c.dll';
$lib_file = __DIR__.'/../../../target/debug/bitwarden_c.dll';
}
} elseif (PHP_OS === 'Linux') {
$lib_file = '/lib/ubuntu-x64/libbitwarden_c.so';
if (file_exists($lib_file) == false) {
$lib_file = __DIR__.'../../../../target/debug/libbitwarden_c.so';
$lib_file = __DIR__.'/../../../target/debug/libbitwarden_c.so';
}
} elseif (PHP_OS === 'Darwin') {
$architecture = trim(exec('uname -m'));
Expand All @@ -39,7 +39,7 @@ public function __construct()
$lib_file = __DIR__.'/lib/macos-arm64/libbitwarden_c.dylib';
}
if (file_exists($lib_file) == false) {
$lib_file = __DIR__.'../../../../target/debug/libbitwarden_c.dylib';
$lib_file = __DIR__.'/../../../target/debug/libbitwarden_c.dylib';
}
}

Expand Down
15 changes: 7 additions & 8 deletions languages/php/src/CommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ public function __construct(BitwardenLib $bitwardenLib, $handle)
public function run(Command $command): \stdClass
{
$result = $this->bitwardenLib->run_command($command);
if (isset($result->data)) {
if ($result->success == true) {
return $result->data;
}
if (isset($result->error))
{
throw new \Exception($result->error);
}
if ($result->success == true) {
return $result->data;
}

if (isset($result->errorMessage))
{
throw new \Exception($result->errorMessage);
}
throw new \Exception("Unknown error occurred");
}
Expand Down

0 comments on commit 5e4e2e7

Please sign in to comment.