Skip to content

Commit

Permalink
sm/sm-874: Fix Python SDK integration
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonhurst committed Sep 12, 2023
1 parent c384428 commit bfa8d94
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions crates/sdk-schemas/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ fn main() -> Result<()> {
write_schema_for_response! {
bitwarden::auth::login::ApiKeyLoginResponse,
bitwarden::auth::login::PasswordLoginResponse,
bitwarden::auth::login::AccessTokenLoginResponse,
bitwarden::secrets_manager::secrets::SecretIdentifiersResponse,
bitwarden::secrets_manager::secrets::SecretResponse,
bitwarden::secrets_manager::secrets::SecretsResponse,
Expand Down
19 changes: 13 additions & 6 deletions languages/python/BitwardenClient/bitwarden_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
from typing import Any, List
from typing import Any, List, Optional
from uuid import UUID
import bitwarden_py
from .schemas import ClientSettings, Command, PasswordLoginRequest, PasswordLoginResponse, ResponseForPasswordLoginResponse, ResponseForSecretIdentifiersResponse, ResponseForSecretResponse, ResponseForSecretsDeleteResponse, ResponseForSyncResponse, ResponseForUserAPIKeyResponse, SecretCreateRequest, SecretGetRequest, SecretIdentifiersRequest, SecretIdentifiersResponse, SecretPutRequest, SecretResponse, SecretVerificationRequest, SecretsCommand, SecretsDeleteRequest, SecretsDeleteResponse, SyncRequest, SyncResponse, UserAPIKeyResponse

from .schemas import ClientSettings, Command, PasswordLoginRequest, PasswordLoginResponse, ResponseForPasswordLoginResponse, ResponseForSecretIdentifiersResponse, ResponseForSecretResponse, ResponseForSecretsDeleteResponse, ResponseForSyncResponse, ResponseForUserAPIKeyResponse, SecretCreateRequest, SecretGetRequest, SecretIdentifiersRequest, SecretIdentifiersResponse, SecretPutRequest, SecretResponse, SecretVerificationRequest, SecretsCommand, SecretsDeleteRequest, SecretsDeleteResponse, SyncRequest, SyncResponse, UserAPIKeyResponse, AccessTokenLoginRequest, AccessTokenLoginResponse, ResponseForAccessTokenLoginResponse

class BitwardenClient:
def __init__(self, settings: ClientSettings = None):
Expand All @@ -18,6 +18,12 @@ def password_login(self, email: str, password: str) -> ResponseForPasswordLoginR
)
return ResponseForPasswordLoginResponse.from_dict(result)

def access_token_login(self, access_token: str) -> AccessTokenLoginResponse:
result = self._run_command(
Command(access_token_login=AccessTokenLoginRequest(access_token))
)
return ResponseForAccessTokenLoginResponse.from_dict(result)

def get_user_api_key(self, secret: str, is_otp: bool = False) -> ResponseForUserAPIKeyResponse:
result = self._run_command(
Command(get_user_api_key=SecretVerificationRequest(
Expand All @@ -38,7 +44,6 @@ def _run_command(self, command: Command) -> Any:
response_json = self.inner.run_command(json.dumps(command.to_dict()))
return json.loads(response_json)


class SecretsClient:
def __init__(self, client: BitwardenClient):
self.client = client
Expand All @@ -52,10 +57,12 @@ def get(self, id: str) -> ResponseForSecretResponse:
def create(self, key: str,
note: str,
organization_id: str,
value: str) -> ResponseForSecretResponse:
value: str,
project_ids: Optional[List[UUID]] = None
) -> ResponseForSecretResponse:
result = self.client._run_command(
Command(secrets=SecretsCommand(
create=SecretCreateRequest(key, note, organization_id, value)))
create=SecretCreateRequest(key, note, organization_id, value, project_ids)))
)
return ResponseForSecretResponse.from_dict(result)

Expand Down
6 changes: 5 additions & 1 deletion languages/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
```bash
pip install setuptools_rust
```
- dateutil
```bash
pip install python-dateutil
```

# Installation

Expand All @@ -18,7 +22,7 @@ From the `languages/python/` directory,
python3 ./setup.py develop
```

Move the the resulting `.so` file to `bitwarden_py.so`, if it isn't already there.
Rename the the resulting `.so` file to `bitwarden_py.so`, if it isn't already there.

# Run

Expand Down
10 changes: 3 additions & 7 deletions languages/python/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@

logging.basicConfig(level=logging.DEBUG)

result = client.password_login("[email protected]", "asdfasdf")
print(result)
print(client.get_user_api_key("asdfasdf"))
result = client.access_token_login("access token here")

sync = client.sync()
secret = client.secrets().create("TEST_SECRET", "This is a test secret", "organization id here", "Secret1234!", ["project id here"])

secret = client.secrets().create("TEST_SECRET", "This is a test secret",
sync.data.profile.organizations[0].id, "Secret1234!")
print(secret)
input("Press Enter to delete the secret...")

client.secrets().delete([secret.data.id])

0 comments on commit bfa8d94

Please sign in to comment.