| 1 | package Hatena::API::Auth; | ||||||||||
| 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 | 3 | our $VERSION = 0.05; | |||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use base qw (Class::Accessor::Fast Class::ErrorHandler); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use URI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 3 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=14.6
| |||||||||||
| 13 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 3 | our $HAVE_JSON_SYCK; | |||||||||
| 15 | 0 | eval { require JSON::Syck; $HAVE_JSON_SYCK = 1 }; | |||||||||
| 16 | 3 | ▶ | eval { require JSON } unless $HAVE_JSON_SYCK; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 3 | ▶ | Carp::croak("JSON::Syck or JSON required to use " . __PACKAGE__) if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | *_parse_json = | ||||||||||
| 19 | 0 | ▶ | $HAVE_JSON_SYCK ? sub { JSON::Syck::Load($_[1]) } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 20 | 3 | ▶ | : sub { JSON::jsonToObj($_[1]) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 3 | __PACKAGE__->mk_accessors(qw(api_key secret)); | |||||||||
| 24 | |||||||||||
| 25 | 7 | ▶ | sub uri_to_login { | ||||||||
|
Subroutine
uri_to_login: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 26 | 7 | my $self = shift; | |||||||||
| 27 | 0 | ▶ | my %parameters = ref $_[0] eq 'HASH' ? %{$_[0]} : @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 7 | my $uri = URI->new('http://auth.hatena.ne.jp/auth'); | |||||||||
| 29 | 7 | my $request = { | |||||||||
| 30 | api_key => $self->api_key, | ||||||||||
| 31 | %parameters, | ||||||||||
| 32 | }; | ||||||||||
| 33 | 7 | $uri->query_form( | |||||||||
| 34 | %$request, | ||||||||||
| 35 | api_sig => $self->api_sig($request), | ||||||||||
| 36 | ); | ||||||||||
| 37 | 7 | return $uri; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 9 | ▶ | sub api_sig { | ||||||||
|
Subroutine
api_sig: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 41 | 9 | my $self = shift; | |||||||||
| 42 | 9 | my $args = shift; | |||||||||
| 43 | 9 | my $sig = $self->secret; | |||||||||
| 44 | 9 | for my $key (sort {$a cmp $b} keys %{$args}) { | |||||||||
| 45 | 18 | ▶ | my $value = $args->{$key} ? $args->{$key} : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 18 | $sig .= $key . $value; | |||||||||
| 47 | } | ||||||||||
| 48 | 9 | return Digest::MD5::md5_hex($sig); | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 2 | ▶ | sub ua { | ||||||||
|
Subroutine
ua: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 52 | 2 | my $self = shift; | |||||||||
| 53 | 2 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | $self->{_ua} = shift; | |||||||||
| 55 | } else { | ||||||||||
| 56 | 2 | ▶ | $self->{_ua} and return $self->{_ua}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 1 | $self->{_ua} = LWP::UserAgent->new; | |||||||||
| 58 | 1 | $self->{_ua}->agent(join '/', __PACKAGE__, __PACKAGE__->VERSION); | |||||||||
| 59 | } | ||||||||||
| 60 | 1 | $self->{_ua}; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 1 | ▶ | sub _get_auth_as_json { | ||||||||
|
Subroutine
_get_auth_as_json: called CC=3 SCAR=11.2
| |||||||||||
| 64 | 1 | my $self = shift; | |||||||||
| 65 | 1 | ▶ | my $cert = shift or croak "You must specify your cert as an argument."; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 1 | my $uri = URI->new('http://auth.hatena.ne.jp/api/auth.json'); | |||||||||
| 67 | 1 | my $request = { | |||||||||
| 68 | api_key => $self->api_key, | ||||||||||
| 69 | cert => $cert, | ||||||||||
| 70 | }; | ||||||||||
| 71 | 1 | $uri->query_form( | |||||||||
| 72 | %$request, | ||||||||||
| 73 | api_sig => $self->api_sig($request), | ||||||||||
| 74 | ); | ||||||||||
| 75 | 1 | my $res = $self->ua->get($uri->as_string); | |||||||||
| 76 | 1 | ▶ | $res->is_success ? $res->content : $self->error($res->status_line); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 2 | ▶ | sub login { | ||||||||
|
Subroutine
login: called CC=4 SCAR=17.2
Pod
documented
| |||||||||||
| 80 | 2 | my $self = shift; | |||||||||
| 81 | 2 | ▶ | my $cert = shift or croak "Invalid argumet (no cert)"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 2 | ▶ | my $auth = $self->_get_auth_as_json($cert) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | or return $self->error($self->errstr); | ||||||||||
| 84 | 1 | my $json = $self->_parse_json($auth); | |||||||||
| 85 | 0 | ▶ | if ($json->{has_error}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | return $self->error($json->{error}->{message}); | |||||||||
| 87 | } else { | ||||||||||
| 88 | 0 | return Hatena::API::Auth::User->new($json->{user}); | |||||||||
| 89 | } | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | package Hatena::API::Auth::User; | ||||||||||
| 93 | 3 | ▶ | use base qw(Class::Accessor::Fast); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 94 | |||||||||||
| 95 | 3 | __PACKAGE__->mk_accessors(qw(name image_url thumbnail_url)); | |||||||||
| 96 | |||||||||||
| 97 | 3 | 1; | |||||||||
| 98 | |||||||||||
| 99 | __END__ | ||||||||||