|
| 1 | +# 1Password Connect Python SDK |
| 2 | + |
| 3 | +The 1Password Connect SDK provides access to the 1Password via 1Password Connect hosted on your infrastructure. The library is intended to be used by Python applications to simplify accessing `items` in 1Password `vaults`. |
| 4 | + |
| 5 | +## Prerequisites: |
| 6 | + |
| 7 | +- 1Password Connect deployed in your infrastructure. |
| 8 | +## Installation |
| 9 | + |
| 10 | +`pip install https://github.com/1Password/connect-sdk-python/archive/v{version}.zip` |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +**Import 1Password Connect Python SDK** |
| 15 | + |
| 16 | +```python |
| 17 | +import onepasswordconnectsdk |
| 18 | +``` |
| 19 | + |
| 20 | +**Environment Variables** |
| 21 | + |
| 22 | +- **OP_CONNECT_TOKEN** – The token to be used to authenticate with the 1Password Connect API. |
| 23 | +- **OP_VAULT** - The default vault to fetch items from if not specified. |
| 24 | + |
| 25 | +**Creating a Client** |
| 26 | + |
| 27 | +There are two methods available for creating a client: |
| 28 | + |
| 29 | +- `new_client_from_environment`: Builds a new client for interacting with 1Password Connect using the `OP_CONNECT_TOKEN` *environment variable (ie a 1Password Connect API token)* and the hostname of 1Password Connect. |
| 30 | +- `new_client`: Builds a new client for interacting with 1Password Connect. Accepts the hostname of 1Password Connect and the API token generated for the application. |
| 31 | + |
| 32 | +```python |
| 33 | +:from onepasswordconnectsdk.client import ( |
| 34 | + Client, |
| 35 | + new_client_from_environment |
| 36 | +) |
| 37 | + |
| 38 | +# creating client using OP_CONNECT_TOKEN environment variable |
| 39 | +client: Client = new_client_from_environment( |
| 40 | + "{1Password_Connect_Host}") |
| 41 | + |
| 42 | +# creating client by supplying hostname and 1Password Connect API token |
| 43 | +client: Client = new_client_from_environment( |
| 44 | + "{1Password_Connect_Host}", |
| 45 | + "{1Password_Connect_API_Token}") |
| 46 | +``` |
| 47 | + |
| 48 | +**Get Item** |
| 49 | + |
| 50 | +Get a specific item by item and vault uuids: |
| 51 | + |
| 52 | +```python |
| 53 | +client.get_item("{item_id}", "{vault_id}") |
| 54 | +``` |
| 55 | + |
| 56 | +**Get Item By Title** |
| 57 | + |
| 58 | +Get item by item title and vault id |
| 59 | + |
| 60 | +```python |
| 61 | +client.get_item_by_title("{item_title}", "{vault_id}") |
| 62 | +``` |
| 63 | + |
| 64 | +**Get Items** |
| 65 | + |
| 66 | +Get a summarized list of all items for a specified vault |
| 67 | + |
| 68 | +```python |
| 69 | +client.get_items("{vault_id}") |
| 70 | +``` |
| 71 | + |
| 72 | +**Delete Item** |
| 73 | + |
| 74 | +Delete an item by item and vault ids: |
| 75 | + |
| 76 | +```python |
| 77 | +client.delete_item("{item_id}", "{vault_id}") |
| 78 | +``` |
| 79 | + |
| 80 | +**Create Item** |
| 81 | + |
| 82 | +Create an item in the specified vault. |
| 83 | + |
| 84 | +```python |
| 85 | +# Example item creation. Create an item with your desired arguments. |
| 86 | +item = onepasswordconnectsdk.models.FullItem(vault=ItemVault(id="av223f76ydutdngislnkbz6z5u"), |
| 87 | + id="kp2td65r4wbuhocwhhijpdbfqq", |
| 88 | + title="newtitle", |
| 89 | + category="LOGIN", |
| 90 | + tags=["1password-connect"], |
| 91 | + fields=[FullItemAllOfFields(value="new_user", |
| 92 | + purpose="USERNAME")], |
| 93 | + ) |
| 94 | +client.create_item("{vault_id}", item) |
| 95 | +``` |
| 96 | + |
| 97 | +**Update Item** |
| 98 | + |
| 99 | +Item the item with the specified item and vault ids. The existing item will be overwritten with the newly supplied item. |
| 100 | + |
| 101 | +```python |
| 102 | +# Example item creation. Create an item with your desired arguments. |
| 103 | +item = onepasswordconnectsdk.models.FullItem(vault=ItemVault(id="av223f76ydutdngislnkbz6z5u"), |
| 104 | + id="kp2td65r4wbuhocwhhijpdbfqq", |
| 105 | + title="newtitle", |
| 106 | + category="LOGIN", |
| 107 | + tags=["1password-connect"], |
| 108 | + fields=[FullItemAllOfFields(value="new_user", |
| 109 | + purpose="USERNAME")], |
| 110 | + ) |
| 111 | +client.update_item("{item_id}", "{vault_id}", item) |
| 112 | +``` |
| 113 | + |
| 114 | +**Get Vault** |
| 115 | + |
| 116 | +Get vault by vault id |
| 117 | + |
| 118 | +```python |
| 119 | +client.get_vault("{vault_id}") |
| 120 | +``` |
| 121 | + |
| 122 | +**Get Vaults** |
| 123 | + |
| 124 | +Retrieve all vaults available to the service account. |
| 125 | + |
| 126 | +```python |
| 127 | +client.get_vaults() |
| 128 | +``` |
| 129 | + |
| 130 | +**Load Configuration** |
| 131 | + |
| 132 | +Users can create `classes` or `dicts` that describe fields they wish to get the values from in 1Password. Two convienience methods are provided that will handle the fetching of values for these fields: |
| 133 | + |
| 134 | +- **load_dict**: Takes a dictionary with keys specifying the user desired naming scheme of the values to return. Each key's value is a dictionary that includes information on where to find the item field value in 1Password. This returns a dictionary of user specified keys with values retrieved from 1Password |
| 135 | +- **load**: Takes an object with class attributes annotated with tags describing where to find desired fields in 1Password. Manipulates given object and fills attributes in with 1Password item field values. |
| 136 | + |
| 137 | +```python |
| 138 | +# example dict configuration for onepasswordconnectsdk.load_dict(CONFIG) |
| 139 | +CONFIG = { |
| 140 | + "server": { |
| 141 | + "opitem": "My database item", |
| 142 | + "opfield": "specific_section.hostname", |
| 143 | + "opvault": "some_vault_id", |
| 144 | + }, |
| 145 | + "database": { |
| 146 | + "opitem": "My database item", |
| 147 | + "opfield": ".database", |
| 148 | + }, |
| 149 | + "username": { |
| 150 | + "opitem": "My database item", |
| 151 | + "opfield": ".username", |
| 152 | + }, |
| 153 | + "password": { |
| 154 | + "opitem": "My database item", |
| 155 | + "opfield": ".password", |
| 156 | + }, |
| 157 | +} |
| 158 | + |
| 159 | +values_dict = onepasswordconnectsdk.load_dict(CONFIG) |
| 160 | +``` |
| 161 | + |
| 162 | +```python |
| 163 | +# example class configuration for onepasswordconnectsdk.load(CONFIG) |
| 164 | +class Config: |
| 165 | + server: 'opitem:"My database item" opvault:some_vault_id opfield:specific_section.hostname' = None |
| 166 | + database: 'opitem:"My database item" opfield:.database' = None |
| 167 | + username: 'opitem:"My database item" opfield:.username' = None |
| 168 | + password: 'opitem:"My database item" opfield:.password' = None |
| 169 | + |
| 170 | +CONFIG = Config() |
| 171 | + |
| 172 | +values_object = onepasswordconnectsdk.load(CONFIG) |
| 173 | +``` |
| 174 | + |
| 175 | +## Development |
| 176 | + |
| 177 | +**Testing** |
| 178 | + |
| 179 | +```bash |
| 180 | +$ pytest |
| 181 | +``` |
| 182 | + |
| 183 | +## Security |
| 184 | + |
| 185 | +1Password requests you practice responsible disclosure if you discover a vulnerability. |
| 186 | + |
| 187 | +Please file requests via [**BugCrowd**](https://bugcrowd.com/agilebits). |
| 188 | + |
| 189 | +For information about security practices, please visit our [Security homepage](https://bugcrowd.com/agilebits). |
0 commit comments