-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathexample.py
More file actions
94 lines (79 loc) · 2.9 KB
/
example.py
File metadata and controls
94 lines (79 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import asyncio
import os
from onepassword import *
async def main():
# Gets your service account token from the OP_SERVICE_ACCOUNT_TOKEN environment variable.
token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN")
# Connects to 1Password.
client = await Client.authenticate(
auth=token,
# Set the following to your own integration name and version.
integration_name="My 1Password Integration",
integration_version="v1.0.0",
)
vaults = await client.vaults.list_all()
async for vault in vaults:
print(vault.title)
items = await client.items.list_all(vault.id)
async for item in items:
print(item.title)
# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
value = await client.secrets.resolve("op://vault/item/field")
print(value)
# Create an Item and add it to your vault.
to_create = ItemCreateParams(
title="MyName",
category="Login",
vault_id="q73bqltug6xoegr3wkk2zkenoq",
fields=[
ItemField(
id="username",
title="username",
field_type="Text",
section_id=None,
value="mynameisjeff",
details=None,
),
ItemField(
id="password",
title="password",
field_type="Concealed",
section_id=None,
value="jeff",
details=None,
),
ItemField(
id="onetimepassword",
title="one-time-password",
field_type="Totp",
section_id="totpsection",
value="otpauth://totp/my-example-otp?secret=jncrjgbdjnrncbjsr&issuer=1Password",
details=None,
),
],
sections=[
ItemSection(id="", title=""),
ItemSection(id="totpsection", title=""),
],
)
created_item = await client.items.create(to_create)
print(dict(created_item))
# Fetch a totp code from the item
for f in created_item.fields:
if f.field_type == "Totp":
if f.details.content.error_message is not None:
print(f.details.content.error_message)
else:
print(f.details.content.code)
# Retrieve an item from your vault.
item = await client.items.get(created_item.vault_id, created_item.id)
print(dict(item))
# Update a field in your item (change the password)
updated_item = Item(**dict(item))
next(f for f in updated_item.fields if f.title == "password").value = "new_pass"
updated_item = await client.items.put(updated_item)
print(dict(updated_item))
# Delete a item from your vault.
await client.items.delete(created_item.vault_id, updated_item.id)
if __name__ == "__main__":
asyncio.run(main())