File Coverage

lib/Plack/Middleware/OAuth/Handler/AccessTokenV2.pm
Criterion Covered Total %
statement 24 65 36.9
branch 0 18 0.0
condition 0 8 0.0
subroutine 8 11 72.7
pod 1 3 33.3
total 33 105 31.4


line stmt bran cond sub pod time code
1             package Plack::Middleware::OAuth::Handler::AccessTokenV2;
2 2     2   12 use parent qw(Plack::Middleware::OAuth::Handler);
  2         5  
  2         78  
3 2     2   146 use URI;
  2         4  
  2         50  
4 2     2   11 use URI::Query;
  2         3  
  2         54  
5 2     2   12 use LWP::UserAgent;
  2         4  
  2         41  
6 2     2   10 use Plack::Middleware::OAuth::AccessToken;
  2         4  
  2         63  
7 2     2   22 use JSON::Any;
  2         3  
  2         23  
8 2     2   409 use warnings;
  2         4  
  2         69  
9 2     2   13 use strict;
  2         4  
  2         1542  
10              
11             sub build_args {
12 0     0 0   my ($self,$code) = @_;
13 0           my $config = $self->config;
14 0   0       my %args = (
15             client_id => $config->{client_id},
16             client_secret => $config->{client_secret},
17             redirect_uri => $config->{redirect_uri} || $self->default_callback,
18             scope => $config->{scope},
19             grant_type => $config->{grant_type},
20             code => $code,
21             );
22 0           return %args;
23             }
24              
25             sub get_access_token {
26 0     0 0   my ($self,$code,%args) = @_;
27 0           my $config = $self->config;
28 0           my $provider = $self->provider;
29 0           my $uri = URI->new( $config->{access_token_url} );
30 0           my $ua = LWP::UserAgent->new;
31 0           my $ua_response;
32              
33 0   0       my $method = $config->{request_method} || 'GET';
34 0 0         if( $method eq 'GET' ) {
    0          
35 0           $uri->query_form( %args );
36 0           $ua_response = $ua->get( $uri );
37             }
38             elsif( $method eq 'POST' ) {
39 0           $ua_response = $ua->post( $uri , \%args );
40             }
41              
42             # process response content...
43 0           my $response_content = $ua_response->content;
44 0           my $content_type = $ua_response->header('Content-Type');
45 0           my $atkn;
46              
47 0 0 0       if( $content_type =~ m{json} || $content_type =~ m{javascript} ) {
48              
49 0           my $params = JSON::Any->new->decode( $response_content );
50 0           $atkn = Plack::Middleware::OAuth::AccessToken->new(
51             version => $config->{version}, # oauth version
52             provider => $provider,
53             params => {
54             %$params,
55             code => $code,
56             }
57             );
58              
59             } else {
60 0           my $qq = URI::Query->new( $ua_response->content );
61 0           my %params = $qq->hash;
62 0           $atkn = Plack::Middleware::OAuth::AccessToken->new(
63             version => $config->{version}, # oauth version
64             provider => $provider,
65             params => {
66             %params,
67             code => $code
68             }
69             );
70             }
71 0           return $atkn;
72             }
73              
74             sub run {
75 0     0 1   my $self = $_[0];
76 0           my $code = $self->param('code');
77              
78             # https://graph.facebook.com/oauth/access_token?
79             # client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&
80             # client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE
81 0           my %args = $self->build_args($code);
82 0           my $token = $self->get_access_token( $code , %args );
83              
84 0 0         if( $token->has_error ) {
85 0 0         $self->on_error->( $self, $token ) if $self->on_error;
86             }
87              
88 0 0         unless( $token ) {
89 0 0         return $self->on_error->( $self ) if $self->on_error;
90 0           return $self->render( 'OAuth failed.' );
91             }
92              
93             # register oauth args to session
94 0           my $env = $self->env;
95 0           my $provider = $self->provider;
96              
97 0           $token->register_session($env);
98              
99 0           my $res;
100 0 0         $res = $self->on_success->( $self, $token ) if $self->on_success;
101 0 0         return $res if $res;
102              
103             # for testing
104 0           return $self->to_yaml( $token );
105             }
106              
107             1;