Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
simkimsia opened this issue Aug 13, 2024 · 2 comments

Comments

@simkimsia
Copy link

simkimsia commented Aug 13, 2024

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 #59 (comment)

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
Copy link

ohinibla commented Aug 13, 2024

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
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants