| 1 | package Catalyst::Plugin::Authentication::Credential::Livedoor; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | our $VERSION = '0.03'; | ||
| 6 | |||
| 7 | 1 | ▶ | use NEXT; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use WebService::Livedoor::Auth; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use UNIVERSAL::require; | ||
| 10 | |||
| 11 | sub setup { | ||
| 12 | my $c = shift; | ||
| 13 | my $config = $c->config->{authentication}->{livedoor} || {}; | ||
| 14 | unless (exists $config->{ldauth_object}) { | ||
| 15 | $config->{user_class} ||= | ||
| 16 | 'Catalyst::Plugin::Authentication::User::Hash'; | ||
| 17 | $config->{user_class}->require; | ||
| 18 | $config->{ldauth_object} = WebService::Livedoor::Auth->new({ | ||
| 19 | app_key => $config->{app_key}, | ||
| 20 | secret => $config->{secret}, | ||
| 21 | }); | ||
| 22 | } | ||
| 23 | $c->NEXT::setup(@_); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub authenticate_livedoor_url { | ||
| 27 | my ($c, $query) = @_; | ||
| 28 | $query ||= {}; | ||
| 29 | my %q; | ||
| 30 | if ($c->config->{authentication}->{livedoor}->{get_livedoor_id}) { | ||
| 31 | $q{perms} = 'id'; | ||
| 32 | } | ||
| 33 | else { | ||
| 34 | $q{perms} = 'userhash'; | ||
| 35 | } | ||
| 36 | my $uri = $c->config->{authentication}->{livedoor}->{ldauth_object}->uri_to_login({%{$query}, %q}); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub authenticate_livedoor { | ||
| 40 | my $c = shift; | ||
| 41 | my $config = $c->config->{authentication}->{livedoor}; | ||
| 42 | my $auth = $config->{ldauth_object}; | ||
| 43 | if (my $res = $auth->validate_response($c->req)) { | ||
| 44 | my $id = $res->userhash; | ||
| 45 | if ($config->{get_livedoor_id}) { | ||
| 46 | if (my $livedoor_id = $auth->get_livedoor_id($res)) { | ||
| 47 | $id = $livedoor_id; | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | $c->log->debug('failed to authenticate livedoor. Reason: %s', $auth->errstr) | ||
| 51 | if $c->debug; | ||
| 52 | return; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | my $user = { | ||
| 56 | userhash => $res->userhash, | ||
| 57 | livedoor_id => $res->livedoor_id, | ||
| 58 | }; | ||
| 59 | my $store = $config->{store} || $c->default_auth_store; | ||
| 60 | if ($store and my $store_user = $store->get_user($id, $user)) { | ||
| 61 | $c->set_authenticated($store_user); | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | $user = $config->{user_class}->new($user); | ||
| 65 | $c->set_authenticated($user); | ||
| 66 | } | ||
| 67 | return 1; | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | $c->log->debug('failed to authenticate livedoor. Reason: %s', $auth->errstr) | ||
| 71 | if $c->debug; | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | 1; | ||
| 78 | __END__ | ||