| 1 | package WebService::Spotify::OAuth2; | ||
| 2 | use Moo; | ||
| 3 | use Method::Signatures; | ||
| 4 | use IO::File; | ||
| 5 | use LWP::UserAgent; | ||
| 6 | use URI::QueryParam; | ||
| 7 | use JSON; | ||
| 8 | use MIME::Base64; | ||
| 9 | use Data::Dumper; | ||
| 10 | |||
| 11 | our $VERSION = '1.002'; | ||
| 12 | |||
| 13 | has 'client_id' => ( is => 'rw', required => 1 ); | ||
| 14 | has 'client_secret' => ( is => 'rw', required => 1 ); | ||
| 15 | has 'redirect_uri' => ( is => 'rw', required => 1 ); | ||
| 16 | has 'state' => ( is => 'rw' ); | ||
| 17 | has 'scope' => ( is => 'rw' ); | ||
| 18 | has 'cache_path' => ( is => 'rw' ); | ||
| 19 | has 'trace' => ( is => 'rw', default => 0 ); | ||
| 20 | |||
| 21 | has 'oauth_authorize_url' => ( is => 'rw', default => 'https://accounts.spotify.com/authorize' ); | ||
| 22 | has 'oauth_token_url' => ( is => 'rw', default => 'https://accounts.spotify.com/api/token' ); | ||
| 23 | |||
| 24 | has 'user_agent' => ( | ||
| 25 | is => 'rw', | ||
| 26 | default => sub { | ||
| 27 | my $ua = LWP::UserAgent->new; | ||
| 28 | $ua->agent("WebService::Spotify::OAuth2/$VERSION"); | ||
| 29 | return $ua; | ||
| 30 | } | ||
| 31 | ); | ||
| 32 | |||
| 33 | method get_cached_token { | ||
| 34 | my $token_info; | ||
| 35 | if ($self->cache_path) { | ||
| 36 | |||
| 37 | if (my $fh = IO::File->new('< ' . $self->cache_path)) { | ||
| 38 | local $/; | ||
| 39 | $token_info = from_json( <$fh> ); | ||
| 40 | $fh->close; | ||
| 41 | } | ||
| 42 | |||
| 43 | $token_info = $self->refresh_access_token($token_info->{refresh_token}) if $self->is_token_expired($token_info); | ||
| 44 | } | ||
| 45 | return $token_info; | ||
| 46 | } | ||
| 47 | |||
| 48 | method save_token_info ($token_info) { | ||
| 49 | if ($self->cache_path) { | ||
| 50 | my $fh = IO::File->new('> ' . $self->cache_path) || die "Could not create cache file $@"; | ||
| 51 | print $fh to_json($token_info); | ||
| 52 | $fh->close; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | method is_token_expired ($token_info) { | ||
| 57 | return ($token_info and $token_info->{expires_at} ? ($token_info->{expires_at} < time) : 0); | ||
| 58 | } | ||
| 59 | |||
| 60 | method get_authorize_url { | ||
| 61 | my %payload = ( | ||
| 62 | client_id => $self->client_id, | ||
| 63 | response_type => 'code', | ||
| 64 | redirect_uri => $self->redirect_uri | ||
| 65 | ); | ||
| 66 | $payload{scope} = $self->scope if $self->scope; | ||
| 67 | $payload{state} = $self->state if $self->state; | ||
| 68 | |||
| 69 | my $uri = URI->new( $self->oauth_authorize_url ); | ||
| 70 | $uri->query_param( $_, $payload{$_} ) for keys %payload; | ||
| 71 | |||
| 72 | return $uri->as_string; | ||
| 73 | } | ||
| 74 | |||
| 75 | method parse_response_code ($response) { | ||
| 76 | my $code = [split /&/, [split /\?code=/, $response]->[1]]->[0]; | ||
| 77 | chomp($code); | ||
| 78 | return $code; | ||
| 79 | } | ||
| 80 | |||
| 81 | method get_access_token ($code) { | ||
| 82 | my $payload = { | ||
| 83 | grant_type => 'authorization_code', | ||
| 84 | code => $code, | ||
| 85 | redirect_uri => $self->redirect_uri | ||
| 86 | }; | ||
| 87 | $payload->{scope} = $self->scope if $self->scope; | ||
| 88 | $payload->{state} = $self->state if $self->state; | ||
| 89 | |||
| 90 | my $token_info = $self->_post( $self->oauth_token_url, $payload ); | ||
| 91 | |||
| 92 | if ($token_info) { | ||
| 93 | die("Token error: $token_info->{error}" . ($token_info->{error_description} ? " ($token_info->{error_description})" : '') ) if $token_info->{error}; | ||
| 94 | $self->save_token_info($token_info); | ||
| 95 | } | ||
| 96 | |||
| 97 | return $token_info; | ||
| 98 | } | ||
| 99 | |||
| 100 | method refresh_access_token ($refresh_token) { | ||
| 101 | my $payload = { | ||
| 102 | grant_type => 'refresh_token', | ||
| 103 | refresh_token => $refresh_token | ||
| 104 | }; | ||
| 105 | |||
| 106 | my $token_info = $self->_post( $self->oauth_token_url, $payload ); | ||
| 107 | |||
| 108 | if ($token_info) { | ||
| 109 | die("Token error: $token_info->{error}" . ($token_info->{error_description} ? " ($token_info->{error_description})" : '') ) if $token_info->{error}; | ||
| 110 | $token_info->{expires_at} = time + $token_info->{expires_in}; | ||
| 111 | $token_info->{refresh_token} ||= $refresh_token; | ||
| 112 | $self->save_token_info($token_info); | ||
| 113 | } | ||
| 114 | |||
| 115 | return $token_info; | ||
| 116 | } | ||
| 117 | |||
| 118 | method _post ($uri, $payload) { | ||
| 119 | my $headers = $self->_auth_headers; | ||
| 120 | my $response = $self->user_agent->post( $uri, $payload, %$headers ); | ||
| 121 | |||
| 122 | $self->_log("POST", $uri); | ||
| 123 | $self->_log("HEAD", Dumper $headers); | ||
| 124 | $self->_log("DATA", Dumper $payload); | ||
| 125 | $self->_log("RESP", $response->content); | ||
| 126 | |||
| 127 | #die $response->status_line unless $response->is_success; | ||
| 128 | |||
| 129 | return from_json( $response->content ); | ||
| 130 | } | ||
| 131 | |||
| 132 | method _auth_headers { | ||
| 133 | my $auth_header = encode_base64( $self->client_id . ':' . $self->client_secret, '' ); | ||
| 134 | return { 'Authorization' => 'Basic ' . $auth_header }; | ||
| 135 | } | ||
| 136 | |||
| 137 | method _log (@strings) { | ||
| 138 | print join(' ', @strings) . "\n" if $self->trace; | ||
| 139 | } | ||
| 140 | |||
| 141 | 1; |