| 1 | package Password::OnePassword::OPCLI 0.002; | ||
| 2 | # ABSTRACT: get items out of 1Password with the "op" CLI | ||
| 3 | |||
| 4 | use v5.36.0; | ||
| 5 | |||
| 6 | use Carp (); | ||
| 7 | use IPC::Run qw(run timeout); | ||
| 8 | use JSON::MaybeXS qw(decode_json); | ||
| 9 | |||
| 10 | #pod =head1 SYNOPSIS | ||
| 11 | #pod | ||
| 12 | #pod B<Achtung!> The interface for this library might change a lot. The author is | ||
| 13 | #pod still figuring out how to make it make sense. That's partly because he doesn't | ||
| 14 | #pod want to think too hard about errors, and partly because the C<op://> URL scheme | ||
| 15 | #pod used by 1Password isn't really sufficient for his use. Still, this is roughly | ||
| 16 | #pod how you can use it: | ||
| 17 | #pod | ||
| 18 | #pod my $one_pw = Password::OnePassword::OPCLI->new; | ||
| 19 | #pod | ||
| 20 | #pod # Get the string found in one field in your 1Password storage: | ||
| 21 | #pod my $string = $one_pw->get_field("op://Private/PAUSE API/credential"); | ||
| 22 | #pod | ||
| 23 | #pod # Get the complete document for an item, as a hashref: | ||
| 24 | #pod my $pw_item = $one_pw->get_item("op://Work/GitHub"); | ||
| 25 | #pod | ||
| 26 | #pod =cut | ||
| 27 | |||
| 28 | #pod =method new | ||
| 29 | #pod | ||
| 30 | #pod my $one_pw = Password::OnePassword::OPCLI->new; | ||
| 31 | #pod | ||
| 32 | #pod This is a do-almost-nothing constructor. It's only here so that methods are | ||
| 33 | #pod instance methods, not class methods. Someday, there may be more arguments to | ||
| 34 | #pod this, but for now, there are not. | ||
| 35 | #pod | ||
| 36 | #pod =cut | ||
| 37 | |||
| 38 | sub new ($class, @rest) { | ||
| 39 | Carp::croak("too many arguments given to constructor, which takes none") | ||
| 40 | if @rest; | ||
| 41 | |||
| 42 | bless {}, $class; | ||
| 43 | } | ||
| 44 | |||
| 45 | #pod =method get_item | ||
| 46 | #pod | ||
| 47 | #pod my $hashref = $one_pw->get_item($locator); | ||
| 48 | #pod | ||
| 49 | #pod This looks up an item in 1Password, returning a hashref representing the secret | ||
| 50 | #pod from 1Password. | ||
| 51 | #pod | ||
| 52 | #pod The C<$locator> should be I<either> a Password::OnePassword::OPCLI::Locator | ||
| 53 | #pod object or a string that coerced into one, for which see L</LOCATOR STRINGS>. | ||
| 54 | #pod | ||
| 55 | #pod If the locator specifies a field name, an exception will be raised. | ||
| 56 | #pod | ||
| 57 | #pod =cut | ||
| 58 | |||
| 59 | sub get_item ($self, $locator) { | ||
| 60 | unless (ref $locator) { | ||
| 61 | $locator = Password::OnePassword::OPCLI::Locator->_from_string($locator); | ||
| 62 | } | ||
| 63 | |||
| 64 | if (defined $locator->field) { | ||
| 65 | Carp::croak("passed field-level locator to get_item; drop the field part or use get_field"); | ||
| 66 | } | ||
| 67 | |||
| 68 | my @op_command = ( | ||
| 69 | qw(op item get), | ||
| 70 | (defined $locator->vault ? ('--vault', $locator->vault) : ()), | ||
| 71 | ('--format', 'json'), | ||
| 72 | $locator->item, | ||
| 73 | ); | ||
| 74 | |||
| 75 | local $ENV{OP_ACCOUNT} = $locator->account // $ENV{OP_ACCOUNT}; | ||
| 76 | open(my $proc, '-|', @op_command) or Carp::croak("can't spawn op: $!"); | ||
| 77 | |||
| 78 | my $json = join q{}, <$proc>; | ||
| 79 | |||
| 80 | # TODO: Log $? and $!, do something better. -- rjbs, 2024-05-03 | ||
| 81 | close($proc) or Carp::croak("problem running 'op item get'"); | ||
| 82 | |||
| 83 | return decode_json($json); | ||
| 84 | } | ||
| 85 | |||
| 86 | #pod =method get_field | ||
| 87 | #pod | ||
| 88 | #pod my $str = $one_pw->get_field($locator); | ||
| 89 | #pod | ||
| 90 | #pod This looks up an item in 1Password, using the C<op read> command. | ||
| 91 | #pod | ||
| 92 | #pod The C<$locator> should be I<either> a Password::OnePassword::OPCLI::Locator | ||
| 93 | #pod object or a string that coerced into one, for which see L</LOCATOR STRINGS>. | ||
| 94 | #pod The string you get from using the "Copy Secret Reference" feature of 1Password, | ||
| 95 | #pod as long as the 1Password account is not ambiguous at runtime. | ||
| 96 | #pod | ||
| 97 | #pod If the locator does not specify a field name, an exception will be raised. | ||
| 98 | #pod | ||
| 99 | #pod It will return the string form of whatever is stored in that field. If it | ||
| 100 | #pod can't find the field, if it can't authenticate, or in any case other than | ||
| 101 | #pod "everything worked", it will raise an exception. | ||
| 102 | #pod | ||
| 103 | #pod =cut | ||
| 104 | |||
| 105 | sub get_field ($self, $locator) { | ||
| 106 | $self->_call_op_read_for_field_ref($locator); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _call_op_read_for_field_ref ($self, $locator, $arg = {}) { | ||
| 110 | unless (ref $locator) { | ||
| 111 | $locator = Password::OnePassword::OPCLI::Locator->_from_string($locator); | ||
| 112 | } | ||
| 113 | |||
| 114 | unless (defined $locator->field) { | ||
| 115 | Carp::croak("locator provided to get_field does not specify a field name"); | ||
| 116 | } | ||
| 117 | |||
| 118 | my $url = $locator->_as_op_url; | ||
| 119 | |||
| 120 | # I don't like this. The problem, *in part*, is that you can't just pass the | ||
| 121 | # op:// URI through URI.pm, because its ->as_string will encode spaces to | ||
| 122 | # %20, but that isn't permitted in "op read". This probably has a better | ||
| 123 | # workaround, but the goal right now is just to make the method work. | ||
| 124 | # -- rjbs, 2024-06-09 | ||
| 125 | if ($arg->{attribute}) { | ||
| 126 | $url .= "?attribute=$arg->{attribute}"; | ||
| 127 | } | ||
| 128 | |||
| 129 | my @op_command = ( | ||
| 130 | qw(op read), | ||
| 131 | $url, | ||
| 132 | ); | ||
| 133 | |||
| 134 | local $ENV{OP_ACCOUNT} = $locator->account // $ENV{OP_ACCOUNT}; | ||
| 135 | open(my $proc, '-|', @op_command) or Carp::croak("can't spawn op: $!"); | ||
| 136 | |||
| 137 | my $str = join q{}, <$proc>; | ||
| 138 | |||
| 139 | # TODO: Log $? and $!, do something better. -- rjbs, 2024-05-03 | ||
| 140 | close($proc) or Carp::croak("problem running 'op read'"); | ||
| 141 | |||
| 142 | chomp $str; | ||
| 143 | return $str; | ||
| 144 | } | ||
| 145 | |||
| 146 | #pod =method get_otp | ||
| 147 | #pod | ||
| 148 | #pod my $otp = $one_pw->get_otp($locator); | ||
| 149 | #pod | ||
| 150 | #pod This looks up an item in 1Password, using the C<op read> command. The item is | ||
| 151 | #pod assumed to be an OTP-type field. Instead of returning the field's value, which | ||
| 152 | #pod would be the TOTP secret, this method will return the one-time password for the | ||
| 153 | #pod current time. | ||
| 154 | #pod | ||
| 155 | #pod The C<$locator> argument works the same as the argument to the C<get_field> | ||
| 156 | #pod method. | ||
| 157 | #pod | ||
| 158 | #pod If C<op> can't find the field, if the field isn't an OTP field, if it can't | ||
| 159 | #pod authenticate, or in any case other than "everything worked", the library will | ||
| 160 | #pod raise an exception. | ||
| 161 | #pod | ||
| 162 | #pod =cut | ||
| 163 | |||
| 164 | sub get_otp ($self, $locator) { | ||
| 165 | $self->_call_op_read_for_field_ref($locator, { | ||
| 166 | # This is stupid, see _call_op_read_for_field_ref. | ||
| 167 | attribute => 'otp', | ||
| 168 | }); | ||
| 169 | } | ||
| 170 | |||
| 171 | #pod =head1 LOCATOR STRINGS | ||
| 172 | #pod | ||
| 173 | #pod 1Password offers C<op://> URLs for fetching things via C<op>, but they're not | ||
| 174 | #pod quite good enough, at least for this author's needs. First off, if you use the | ||
| 175 | #pod "Copy Secret Reference" feature of 1Password, you'll end up with a string like | ||
| 176 | #pod this on your clipboard: | ||
| 177 | #pod | ||
| 178 | #pod op://Private/Super Mario Fan Club/password | ||
| 179 | #pod | ||
| 180 | #pod This refers to a single I<field> in the vault item. You can pass this to the | ||
| 181 | #pod C<op read> command. If you want to fetch the whole secret item, though, you | ||
| 182 | #pod I<can't> just drop the third part of the path to pass to C<op item get>. If | ||
| 183 | #pod you have that URL and want to get the whole item, you need to parse it and | ||
| 184 | #pod build a command-line invocation yourself. | ||
| 185 | #pod | ||
| 186 | #pod There's a worse problem, too. A two-part (item, not field) URL makes sense | ||
| 187 | #pod because you just drop one piece of data from the three-part URL. But these | ||
| 188 | #pod URLs are also I<missing> a place for the account. If you've got more than one | ||
| 189 | #pod 1Password account on your laptop, like both work and personal, you can't | ||
| 190 | #pod unambiguously specify a credential with only a string. This really undercuts | ||
| 191 | #pod the value of the C<op://> URIs as (for example) environment variables. You end | ||
| 192 | #pod up having to set a I<second> environment variable indicating which account to | ||
| 193 | #pod use, and if you need to access more than one vault in a program, the complexity | ||
| 194 | #pod piles up. | ||
| 195 | #pod | ||
| 196 | #pod Password::OnePassword::OPCLI works with "locator" objects, which the user | ||
| 197 | #pod shouldn't really need to think about. The user of the library can pass in a | ||
| 198 | #pod string that can be parsed into a locator, either as a normal three-part | ||
| 199 | #pod C<op://> URL, or as a bogus-but-comprehensible two-part URL, or as an | ||
| 200 | #pod OPCLI-specific string like this: | ||
| 201 | #pod | ||
| 202 | #pod opcli:a=${Account}:v=${Vault}:i=${Item}:f=${Field} | ||
| 203 | #pod | ||
| 204 | #pod Order is not important and only the item property is required. To represent | ||
| 205 | #pod the URL above (C<op://Private/Super Mario Fan Club/password>) in this format, | ||
| 206 | #pod you'd write: | ||
| 207 | #pod | ||
| 208 | #pod opcli:v=Private:i=Super Mario Fan Club:f=password | ||
| 209 | #pod | ||
| 210 | #pod Later, if you realized that you need to specify an account, you could tack it | ||
| 211 | #pod on the end: | ||
| 212 | #pod | ||
| 213 | #pod opcli:v=Private:i=Super Mario Fan Club:f=password:a=Personal | ||
| 214 | #pod | ||
| 215 | #pod The value of property will be URI-decoded before use. This won't matter, | ||
| 216 | #pod generally, but you'll need to know if it you want to use locators with C<%> or | ||
| 217 | #pod C<:> in property values, at least. | ||
| 218 | #pod | ||
| 219 | #pod =cut | ||
| 220 | |||
| 221 | package Password::OnePassword::OPCLI::Locator 0.002 { | ||
| 222 | use Moo; | ||
| 223 | use v5.36.0; | ||
| 224 | |||
| 225 | use URI::Escape (); | ||
| 226 | |||
| 227 | has account => (is => 'ro'); | ||
| 228 | has vault => (is => 'ro'); | ||
| 229 | has item => (is => 'ro'); | ||
| 230 | has field => (is => 'ro'); | ||
| 231 | |||
| 232 | sub _as_op_url ($self) { | ||
| 233 | return sprintf "op://%s/%s/%s", | ||
| 234 | $self->vault // Carp::confess("tried to build op:// URL without vault name"), | ||
| 235 | $self->item // Carp::confess("tried to build op:// URL without item identifier"), | ||
| 236 | $self->field // Carp::confess("tried to build op:// URL without field name"); | ||
| 237 | } | ||
| 238 | |||
| 239 | sub _from_string ($class, $str) { | ||
| 240 | my $account; | ||
| 241 | my $vault; | ||
| 242 | my $item; | ||
| 243 | my $field; | ||
| 244 | |||
| 245 | if ($str =~ m{\Aop://([^/]+)/([^/]+)(/([^/]*))?\z}) { | ||
| 246 | $vault = $1; | ||
| 247 | $item = $2; | ||
| 248 | $field = $3; | ||
| 249 | } elsif ($str =~ m{\Aopcli:}) { | ||
| 250 | # opcli:a=${Account}:v=${Vault}:i=${Item}:f=${Field} | ||
| 251 | my (undef, @hunks) = split /:/, $str; | ||
| 252 | my %got; | ||
| 253 | state %known_k = map {; $_ => 1 } qw(a v i f); | ||
| 254 | |||
| 255 | for my $hunk (@hunks) { | ||
| 256 | my ($k, $v) = split /=/, $hunk, 2; | ||
| 257 | $known_k{$k} || Carp::croak("unknown key in 1Password locator: $hunk"); | ||
| 258 | exists $got{$k} && Carp::croak("saw $k= twice in 1Password locator"); | ||
| 259 | length $v || Carp::croak("empty $k= value in 1Password locator"); | ||
| 260 | |||
| 261 | $got{$k} = $v; | ||
| 262 | } | ||
| 263 | |||
| 264 | ($account, $vault, $item, $field) | ||
| 265 | = map {; length $_ ? URI::Escape::uri_unescape($_) : $_ } | ||
| 266 | @got{ qw( a v i f ) }; | ||
| 267 | } else { | ||
| 268 | $item = $str; | ||
| 269 | } | ||
| 270 | |||
| 271 | unless (length $item) { | ||
| 272 | Carp::confess("empty item identifier in 1Password locator string"); | ||
| 273 | } | ||
| 274 | |||
| 275 | return $class->new({ | ||
| 276 | account => $account, | ||
| 277 | vault => $vault, | ||
| 278 | item => $item, | ||
| 279 | field => $field, | ||
| 280 | }); | ||
| 281 | } | ||
| 282 | |||
| 283 | no Moo; | ||
| 284 | } | ||
| 285 | |||
| 286 | 1; | ||
| 287 | |||
| 288 | __END__ |