Best way to check if session is valid?

I'm putting the finishing touches on an Ansible lookup plugin that works with the 1PW CLI. Everything is working, but there's one problem I haven't been able to solve reliably: determining whether a user is currently signed in. I can check for the presence of the environment variable, but that doesn't tell me if the session has just timed out.

What I'm currently doing is attempting to get a fake item (op get item notanitem) and then checking the return code and stdout for the string "You are not currently signed in.". That works, but it's inelegant and more tightly bound than I'd prefer (breaking if you change the message, for example). It's also slower than it needs to be.

Is there any better way of doing this right now? If not, consider this a feature request. :)

1Password Version: CLI 0.3
Extension Version: Not Provided
OS Version: Not Provided
Sync Type: Not Provided


  • rickfillionrickfillion Junior Member

    Team Member

    Hi @scottsb,

    There's currently no good way to do that. This is something I'd like us to add, as I think it's necessary for a bunch of purposes.

    op get account would probably be one of better commands to use for that if you're looking for something efficient.

    If you're not logged in, you should get a non-0 return status, which would probably be a better check than looking at the string.

    I'd also like it if we returned errors to you in a way that was better than just logging them as text. What we currently show as the error belongs in a log file, not as something consumed by apps. It'd be much better if you got back something like {status:401, message:"Authentication Required"}.


  • Thanks. Checking for the return status from that command is definitely a lot better: it's at least 90% of the way there. Getting errors as JSON would be great, but that's even not needed for my use case here. The only improvement would be a command that's more explicit, but get account runs fairly quickly (as opposed to get item ) and allows cleaner logic around the return code.

  • rickfillionrickfillion Junior Member

    Team Member

    90% is a damn good start. I hope that we can get that to 100%. :)


Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file