| 1 | package Plack::Middleware::HatenaOAuth; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use parent 'Plack::Middleware'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Plack::Util::Accessor qw(consumer_key consumer_secret consumer login_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Plack::Session; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use OAuth::Lite::Consumer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use constant +{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | SITE => q{https://www.hatena.com}, | |||||||||||||||||||||||||||
| 17 | REQUEST_TOKEN_PATH => q{/oauth/initiate}, | ||||||||||||||||||||||||||||
| 18 | ACCESS_TOKEN_PATH => q{/oauth/token}, | ||||||||||||||||||||||||||||
| 19 | AUTHORIZE_PATH => q{https://www.hatena.ne.jp/oauth/authorize}, | ||||||||||||||||||||||||||||
| 20 | USER_INFO_URL => q{https://n.hatena.ne.jp/applications/my.json}, | ||||||||||||||||||||||||||||
| 21 | 3 | }; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=4 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 25 | 2 | ▶ | die join( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->consumer_key and $self->consumer_secret
MC/DC0%0/2
$self->consumer_key and $self->consumer_secret
$self->consumer_key
$self->consumer_secret
Truth table33%1/3
A and B
A= $self->consumer_keyB= $self->consumer_secret
| |||||||||||||||||||||||||||||
| 26 | "\n", | ||||||||||||||||||||||||||||
| 27 | 'No consumer_key or consumer_secret specified.', | ||||||||||||||||||||||||||||
| 28 | 'Get one by following the instructions on http://developer.hatena.ne.jp/en/documents/auth/apis/oauth/consumer', | ||||||||||||||||||||||||||||
| 29 | ) unless $self->consumer_key and $self->consumer_secret; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | $self->consumer(OAuth::Lite::Consumer->new( | ||||||||||||||||||||||||||||
| 32 | consumer_key => $self->consumer_key, | ||||||||||||||||||||||||||||
| 33 | consumer_secret => $self->consumer_secret, | ||||||||||||||||||||||||||||
| 34 | site => SITE, | ||||||||||||||||||||||||||||
| 35 | request_token_path => REQUEST_TOKEN_PATH, | ||||||||||||||||||||||||||||
| 36 | access_token_path => ACCESS_TOKEN_PATH, | ||||||||||||||||||||||||||||
| 37 | authorize_path => AUTHORIZE_PATH, | ||||||||||||||||||||||||||||
| 38 | 2 | ▶ | ($self->{ua} ? (ua => $self->{ua}) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | )); | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | ▶ | sub _get_request_token { | ||||||||||||||||||||||||||
|
Subroutine
_get_request_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 3 | my ($self, $callback_url) = @_; | |||||||||||||||||||||||||||
| 44 | 3 | return $self->consumer->get_request_token( | |||||||||||||||||||||||||||
| 45 | callback_url => $callback_url, | ||||||||||||||||||||||||||||
| 46 | scope => 'read_public', | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | sub _get_access_token { | ||||||||||||||||||||||||||
|
Subroutine
_get_access_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 2 | my ($self, $verifier, $request_token) = @_; | |||||||||||||||||||||||||||
| 52 | 2 | return $self->consumer->get_access_token( | |||||||||||||||||||||||||||
| 53 | token => $request_token, | ||||||||||||||||||||||||||||
| 54 | verifier => $verifier, | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | sub _get_user_info { | ||||||||||||||||||||||||||
|
Subroutine
_get_user_info: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||
| 59 | 1 | my ($self, $access_token) = @_; | |||||||||||||||||||||||||||
| 60 | 1 | my $res = $self->consumer->request( | |||||||||||||||||||||||||||
| 61 | method => 'POST', | ||||||||||||||||||||||||||||
| 62 | url => USER_INFO_URL, | ||||||||||||||||||||||||||||
| 63 | token => $access_token, | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | 1 | ▶ | $res->is_success or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | return eval { decode_json($res->decoded_content || $res->content) }; | ||||||||||||||||||||||||||
|
Condition33%1/3
$res->decoded_content || $res->content
MC/DC0%0/2
$res->decoded_content || $res->content
$res->decoded_content
$res->content
Truth table33%1/3
A || B
A= $res->decoded_contentB= $res->content
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | sub _error { | ||||||||||||||||||||||||||
|
Subroutine
_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 2 | my ($self, $code, $message) = @_; | |||||||||||||||||||||||||||
| 71 | return [ | ||||||||||||||||||||||||||||
| 72 | 2 | $code, | |||||||||||||||||||||||||||
| 73 | [ 'Content-Type' => 'text/plain' ], | ||||||||||||||||||||||||||||
| 74 | [ $message ], | ||||||||||||||||||||||||||||
| 75 | ]; | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 5 | ▶ | sub _login_handler { | ||||||||||||||||||||||||||
|
Subroutine
_login_handler: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 79 | 5 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 80 | 5 | my $session = Plack::Session->new($env); | |||||||||||||||||||||||||||
| 81 | 5 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||
| 82 | 5 | my $res = $req->new_response(200); | |||||||||||||||||||||||||||
| 83 | 5 | my $consumer = $self->consumer; | |||||||||||||||||||||||||||
| 84 | 5 | my $verifier = $req->parameters->{oauth_verifier}; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 5 | ▶ | if (!$verifier) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 3 | ▶ | my $request_token = $self->_get_request_token( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | [ split /\?/, $req->uri, 2]->[0], | ||||||||||||||||||||||||||||
| 89 | ) or return $self->_error(500, sprintf( | ||||||||||||||||||||||||||||
| 90 | "Could not get an OAuth request token from %s\nMessage: %s", | ||||||||||||||||||||||||||||
| 91 | SITE, | ||||||||||||||||||||||||||||
| 92 | $consumer->errstr, | ||||||||||||||||||||||||||||
| 93 | )); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 2 | $session->set(hatenaoauth_request_token => $request_token); | |||||||||||||||||||||||||||
| 96 | 2 | $session->set(hatenaoauth_location => $req->parameters->{location}); | |||||||||||||||||||||||||||
| 97 | 2 | $res->redirect($consumer->url_to_authorize(token => $request_token)); | |||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||
| 99 | 2 | ▶ | my $access_token = $self->_get_access_token( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | $verifier, | ||||||||||||||||||||||||||||
| 101 | $session->get('hatenaoauth_request_token'), | ||||||||||||||||||||||||||||
| 102 | ) or return $self->_error(500, sprintf( | ||||||||||||||||||||||||||||
| 103 | "Could not get an OAuth access token from %s\nMessage: %s", | ||||||||||||||||||||||||||||
| 104 | SITE, | ||||||||||||||||||||||||||||
| 105 | $consumer->errstr, | ||||||||||||||||||||||||||||
| 106 | )); | ||||||||||||||||||||||||||||
| 107 | 1 | $session->remove('hatenaoauth_request_token'); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | my $user_info = $self->_get_user_info($access_token); | |||||||||||||||||||||||||||
| 110 | 1 | ▶ | $session->set('hatenaoauth_user_info', $user_info) if $user_info; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | $res->redirect($session->get('hatenaoauth_location') || '/'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$session->get('hatenaoauth_location') || '/'
MC/DC0%0/1
$session->get('hatenaoauth_location') || '/'
$session->get('hatenaoauth_location')
Truth table50%1/2
A
A= $session->get('hatenaoauth_location')
| |||||||||||||||||||||||||||||
| 112 | 1 | $session->remove('hatenaoauth_location'); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 3 | return $res->finalize; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 6 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 6 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 120 | 6 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||
| 121 | 6 | ▶ | return $self->_login_handler($env) if $req->path eq $self->login_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 1 | return $self->app->($env); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3 | 1; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||