1Password / onepassword-sdk-python

https://developer.1password.com/docs/sdks/
MIT License
74 stars 6 forks source link

I still get `otpauth://totp/Whatever:user?secret=xxx` instead of actual OTP values #99

Closed simkimsia closed 1 month ago

simkimsia commented 1 month ago

With the latest releases of the Python SDK (v0.1.0-beta.12+) you are now able to read and write OTP field information.

If you have any other issues with this, please feel free to open another issue.

Originally posted by @MOmarMiraj in https://github.com/1Password/onepassword-sdk-python/issues/59#issuecomment-2271921471

I am now using git+ssh://git@github.com/1Password/onepassword-sdk-python.git@v0.1.0-beta.12 and sadly I am still getting back OTP info as a otpauth string instead of the actual OTP values which is a 6 digit number that changes devery 30 seconds or so

ohinibla commented 1 month ago

I am now using git+ssh://git@github.com/1Password/onepassword-sdk-python.git@v0.1.0-beta.12 and sadly I am still getting back OTP info as a otpauth string instead of the actual OTP values which is a 6 digit number that changes devery 30 seconds or so

You can get the OTP code from the item with item.get() method, as shown in example code:

    # 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)
simkimsia commented 1 month ago

for those newbies:

  1. you need to know the difference between vault, item, and itemfield
  2. you need to know the vault id and item id

find out vault id via listing all vaults

https://developer.1password.com/docs/sdks/list-vaults-items#list-vaults

# Gets all vaults in an account.
vaults = await client.vaults.list_all()

async for vault in vaults:
    print(vault.id)

find out item id via listing all items inside the chosen vault

https://developer.1password.com/docs/sdks/list-vaults-items#list-items

# Gets all items in a vault.
items = await client.items.list_all(vault_id)

async for item in items:
    print(item.id)

fetch the item by vault id and item id

https://developer.1password.com/docs/sdks/manage-items#get-an-item

# Gets an item.
item = await client.items.get(created_item.vault_id, created_item.id)

print(dict(item))

list all itemfields of a specific item and use field_type to identify the totp

https://developer.1password.com/docs/sdks/manage-items#get-a-one-time-password

# Retrieves a one-time password from an item.
for f in created_item.fields:
    if f.fieldType == "Totp":
        if f.details.content.error_message is not None:
            print(f.details.content.error_message)
        else:
            print(f.details.content.code)

How I put everything together

do things step by step oterhwise is not possible. because it's hard to know the id from the 1password app unlike secret reference.

    # step 1
    vaults = await client.vaults.list_all()

    async for vault in vaults:
        print(vault.id)
        print(vault.title)

    # step 2 save the id after printing all the vaults
    vault_id = "VAULTID"  # for title='VAULT TITLE'

   # step 3 show all items of the vault
    items = await client.items.list_all(vault_id)

    async for item in items:
        print(item.id)
        print(item)

    # step 4 save the item id
    item_id = "ITEMID"  # for title = 'ITEM TITLE'

    # step 5 get the item by vault and item id and then print the code
    the_item_with_totp = await client.items.get(vault_id, item_id)

    print(dict(the_item_with_totp))

    # Fetch a totp code from the item
    for f in the_item_with_totp.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)