From e9e0ee2df185d1f8cd98d17f9ddcaf6f655ef3d7 Mon Sep 17 00:00:00 2001 From: Sanko Robinson Date: Sat, 27 Jan 2024 14:38:24 -0500 Subject: [PATCH] Adding add-app-password, revoke-app-password, and list-app-passwords commands --- README.md | 36 ++++++++++++++++++++++++++++++++++++ lib/App/bsky.pm | 26 ++++++++++++++++++++------ script/bsky | 28 ++++++++++++++++++++++++++++ t/00_compile.t | 18 +++++++++--------- t/01_client.t | 20 +++++++++++++------- 5 files changed, 106 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index a444e10..a7ff5b5 100644 --- a/README.md +++ b/README.md @@ -376,6 +376,42 @@ Show notifications. --json boolean flag; content is printed as JSON objects if given ``` +## add-app-password + +``` +bsky add-app-password "Your app name" +``` + +Create a new App password. + +Note that you must be logged in with the account password to add a new app password. + +## revoke-app-password + +``` +bsky revoke-app-password "Your app name" +``` + +Delete App password. + +Note that you must be logged in with the account password to revoke an app password. + +## list-app-passwords + +``` +bsky list-app-passwords + +bsky list-app-passwords --json +``` + +Show App passwords. + +### Options + +``` +--json boolean flag; content is printed as JSON objects if given +``` + ## login ``` diff --git a/lib/App/bsky.pm b/lib/App/bsky.pm index 78d41b4..b041fe1 100644 --- a/lib/App/bsky.pm +++ b/lib/App/bsky.pm @@ -476,16 +476,30 @@ package App::bsky 0.02 { ...; } - method cmd_listapppasswords ($handle) { - ...; + method cmd_listapppasswords (@args) { + GetOptionsFromArray( \@args, 'json' => \my $json ); + my $passwords = $bsky->server_listAppPasswords; + my @passwords = @{ $passwords->{passwords} }; + if ($json) { + $self->say( JSON::Tiny::to_json [ map { $_->_raw } @passwords ] ); + } + else { + $self->say( '%s (%s)', $_->name, $_->createdAt->_raw ) for @passwords; + } + scalar @passwords; } - method cmd_addapppassword ($handle) { - ...; + method cmd_addapppassword ($name) { + my $res = $bsky->server_createAppPassword($name); + if ( builtin::blessed $res->{appPassword} ) { + $self->say( 'App name: %s', $res->{appPassword}->name ); + $self->say( 'Password: %s', $res->{appPassword}->password ); + } + 1; } - method cmd_revokeapppassword ($handle) { - ...; + method cmd_revokeapppassword ($name) { + $bsky->server_revokeAppPassword($name) ? 1 : 0; } method cmd_config ( $field //= (), $value //= () ) { diff --git a/script/bsky b/script/bsky index a90bdf1..3aa21fe 100644 --- a/script/bsky +++ b/script/bsky @@ -305,6 +305,34 @@ Show notifications. --all boolean flag, show all notifications --json boolean flag; content is printed as JSON objects if given +=head2 add-app-password + + bsky add-app-password "Your app name" + +Create a new App password. + +Note that you must be logged in with the account password to add a new app password. + +=head2 revoke-app-password + + bsky revoke-app-password "Your app name" + +Delete App password. + +Note that you must be logged in with the account password to revoke an app password. + +=head2 list-app-passwords + + bsky list-app-passwords + + bsky list-app-passwords --json + +Show App passwords. + +=head3 Options + + --json boolean flag; content is printed as JSON objects if given + =head2 login bsky login [ident] [password] [--host http://bsky.social] diff --git a/t/00_compile.t b/t/00_compile.t index c69d076..fe2355a 100644 --- a/t/00_compile.t +++ b/t/00_compile.t @@ -12,18 +12,18 @@ subtest 'commands' => sub { cmd_timeline cmd_tl cmd_stream cmd_thread - cmd_post cmd_delete - cmd_vote cmd_votes - cmd_repost cmd_reposts - cmd_follow cmd_unfollow cmd_follows cmd_followers - cmd_block cmd_unblock cmd_blocks + cmd_post cmd_delete + cmd_vote cmd_votes + cmd_repost cmd_reposts + cmd_follow cmd_unfollow cmd_follows cmd_followers + cmd_block cmd_unblock cmd_blocks cmd_login - cmd_notifications cmd_notif - cmd_invitecodes cmd_listapppasswords - cmd_addapppassword cmd_revokeapppassword + cmd_notifications cmd_notif + cmd_invitecodes + cmd_listapppasswords cmd_addapppassword cmd_revokeapppassword cmd_config cmd_help - cmd_VERSION cmd_version + cmd_VERSION cmd_version ]; }; subtest 'internals' => sub { diff --git a/t/01_client.t b/t/01_client.t index 339cb25..4608bfc 100644 --- a/t/01_client.t +++ b/t/01_client.t @@ -51,7 +51,9 @@ my $mock = mock 'App::bsky::CLI' => ( my $code = shift; @err = (); $code->(); - wantarray ? @err : join "\n", @err; + my $msg = join "\n", @say; + note $msg; + wantarray ? @say : $msg if @say; } sub is_say(&) { @@ -59,7 +61,9 @@ my $mock = mock 'App::bsky::CLI' => ( @say = (); $code->(); return 0 if !$say[0]; - wantarray ? @say : join "\n", @say if @say; + my $msg = join "\n", @say; + note $msg; + wantarray ? @say : $msg if @say; } } # @@ -82,7 +86,7 @@ is is_say { client->run(qw[config wrap]) }, 0, 'config wrap == 0'; # subtest 'live' => sub { my $todo = todo 'Rate limit or another login info error...'; - eval { + todo 'using the web... things may go wrong that are not our fault' => sub { subtest 'login ... ... (error)' => sub { my $client; like warning { @@ -161,10 +165,12 @@ subtest 'live' => sub { qr[did:plc:qvzn322kmcvd7xtnips5xaun], 'thread at://...'; like is_say { client->run(qw[thread at://did:plc:qdvyf5jhuxqx667ay7k7nagl/app.bsky.feed.post/3kju327qezs2n --json]) }, qr[^{], 'thread --json at://...'; - like is_say { client->run(qw[notifications]) }, qr[did:plc:pwqewimhd3rxc4hg6ztwrcyj], 'notifications'; - like is_say { client->run(qw[notifications --json]) }, qr[^{], 'notifications --json'; - like is_say { client->run(qw[show-session]) }, qr[did:plc:pwqewimhd3rxc4hg6ztwrcyj], 'show-session'; - like is_say { client->run(qw[show-session --json]) }, qr[^{], 'show-session --json'; + like is_say { client->run(qw[list-app-passwords]) }, qr[Test Suite - bsky], 'list-app-passwords'; + like is_say { client->run(qw[list-app-passwords --json]) }, qr[^\[\{], 'list-app-passwords --json'; + like is_say { client->run(qw[notifications]) }, qr[did:plc:pwqewimhd3rxc4hg6ztwrcyj], 'notifications'; + like is_say { client->run(qw[notifications --json]) }, qr[^{], 'notifications --json'; + like is_say { client->run(qw[show-session]) }, qr[did:plc:pwqewimhd3rxc4hg6ztwrcyj], 'show-session'; + like is_say { client->run(qw[show-session --json]) }, qr[^{], 'show-session --json'; } }; done_testing;