From d81d37fab6261afa94a170a0cff003b20d1b9421 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 29 Mar 2015 21:20:31 -0400 Subject: make the authentication flow nicer --- lib/WWW/Pocket.pm | 34 ++++++++++++++++++++-------------- lib/WWW/Pocket/Script.pm | 48 +++++++++++++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/lib/WWW/Pocket.pm b/lib/WWW/Pocket.pm index 54c038d..76c4c53 100644 --- a/lib/WWW/Pocket.pm +++ b/lib/WWW/Pocket.pm @@ -5,18 +5,25 @@ use HTTP::Tiny; use JSON::PP; has consumer_key => ( - is => 'rw', - isa => 'Str', + is => 'ro', + isa => 'Str', + required => 1, ); has access_token => ( - is => 'rw', - isa => 'Str', + is => 'ro', + isa => 'Str', + lazy => 1, + default => sub { die "You must authenticate first." }, + predicate => 'has_access_token', + writer => '_set_access_token', ); has username => ( - is => 'rw', - isa => 'Str', + is => 'ro', + isa => 'Str', + predicate => 'has_username', + writer => '_set_username', ); has base_uri => ( @@ -34,14 +41,14 @@ has ua => ( sub start_authentication { my $self = shift; - my ($consumer_key, $redirect_uri) = @_; + my ($redirect_uri) = @_; - return if $self->consumer_key && $self->access_token; + return if $self->has_access_token; my $response = $self->_request( $self->base_uri . 'oauth/request', { - consumer_key => $consumer_key, + consumer_key => $self->consumer_key, redirect_uri => $redirect_uri, }, ); @@ -50,19 +57,18 @@ sub start_authentication { sub finish_authentication { my $self = shift; - my ($consumer_key, $code) = @_; + my ($code) = @_; my $response = $self->_request( $self->base_uri . 'oauth/authorize', { - consumer_key => $consumer_key, + consumer_key => $self->consumer_key, code => $code, }, ); - $self->consumer_key($consumer_key); - $self->access_token($response->{access_token}); - $self->username($response->{username}); + $self->_set_access_token($response->{access_token}); + $self->_set_username($response->{username}); } sub add { diff --git a/lib/WWW/Pocket/Script.pm b/lib/WWW/Pocket/Script.pm index 119894a..cf749b5 100644 --- a/lib/WWW/Pocket/Script.pm +++ b/lib/WWW/Pocket/Script.pm @@ -9,10 +9,11 @@ use Path::Class; use WWW::Pocket; has consumer_key => ( - is => 'ro', - isa => 'Str', - lazy => 1, - default => sub { die "consumer_key is required to authenticate" }, + is => 'ro', + isa => 'Str', + lazy => 1, + default => sub { die "consumer_key is required to authenticate" }, + predicate => '_has_consumer_key', ); has redirect_uri => ( @@ -35,16 +36,13 @@ has pocket => ( default => sub { my $self = shift; - my $pocket = WWW::Pocket->new; my $credentials_file = file($self->credentials_file); if (-e $credentials_file) { - $self->_apply_credentials($pocket, $credentials_file); + return $self->_apply_credentials($credentials_file); } else { - $self->_authenticate($pocket); + return $self->_authenticate; } - - $pocket }, ); @@ -89,32 +87,48 @@ sub retrieve { sub _apply_credentials { my $self = shift; - my ($pocket, $file) = @_; + my ($file) = @_; my ($consumer_key, $access_token, $username) = $file->slurp(chomp => 1); - $pocket->consumer_key($consumer_key); - $pocket->access_token($access_token); - $pocket->username($username); + return WWW::Pocket->new( + consumer_key => $consumer_key, + access_token => $access_token, + username => $username, + ); } sub _authenticate { my $self = shift; - my ($pocket) = @_; - my $consumer_key = $self->consumer_key; + my $consumer_key = $self->_has_consumer_key + ? $self->consumer_key + : $self->_prompt_for_consumer_key; + + my $pocket = WWW::Pocket->new(consumer_key => $consumer_key); + my $redirect_uri = $self->redirect_uri; - my $code = $pocket->start_authentication($consumer_key, $redirect_uri); + my $code = $pocket->start_authentication($redirect_uri); print "Visit https://getpocket.com/auth/authorize?request_token=${code}&redirect_uri=${redirect_uri} and log in. When you're done, press enter to continue.\n"; ; - $pocket->finish_authentication($consumer_key, $code); + $pocket->finish_authentication($code); my $fh = file($self->credentials_file)->openw; $fh->write($pocket->consumer_key . "\n"); $fh->write($pocket->access_token . "\n"); $fh->write($pocket->username . "\n"); $fh->close; + + return $pocket; +} + +sub _prompt_for_consumer_key { + my $self = shift; + + print "Enter your consumer key: "; + chomp(my $key = ); + return $key; } sub _pretty_print { -- cgit v1.2.3-54-g00ecf