| 1 | package Catalyst::Plugin::Authentication::Credential::Hatena; | ||
| 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 | our $VERSION = '0.04'; | ||
| 6 | |||
| 7 | 3 | ▶ | use Hatena::API::Auth; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use UNIVERSAL::require; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use NEXT; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Catalyst::Plugin::Authentication::Credential::Hatena - Hatena authentication for Catalyst | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | # load plugin and setup | ||
| 18 | use Catalyst qw( | ||
| 19 | Authentication | ||
| 20 | Authentication::Credential::Hatena | ||
| 21 | |||
| 22 | Session | ||
| 23 | Session::Store::FastMmap | ||
| 24 | Session::State::Cookie | ||
| 25 | ); | ||
| 26 | |||
| 27 | __PACKAGE__->config->{authentication}->{hatena} = { | ||
| 28 | api_key => 'your api_key', | ||
| 29 | secret => 'your shared secret', | ||
| 30 | }; | ||
| 31 | |||
| 32 | |||
| 33 | # in controller | ||
| 34 | # redirect login url | ||
| 35 | sub login : Path('/hatena/login') { | ||
| 36 | my ( $self, $c ) = @_; | ||
| 37 | |||
| 38 | $c->res->redirect( $c->authenticate_hatena_url ); | ||
| 39 | } | ||
| 40 | |||
| 41 | # callback url | ||
| 42 | sub auth : Path('/hatena/auth') { | ||
| 43 | my ( $self, $c ) = @_; | ||
| 44 | |||
| 45 | if ( $c->authenticate_hatena ) { | ||
| 46 | # login successful | ||
| 47 | $c->res->redirect( $c->uri_for('/') ); | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | # something wrong | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | =head1 DESCRIPTION | ||
| 55 | |||
| 56 | This module provide authentication via Hatena, using its api. | ||
| 57 | |||
| 58 | =head1 SEE ALSO | ||
| 59 | |||
| 60 | L<Hatena::API::Auth>, http://auth.hatena.ne.jp/ | ||
| 61 | |||
| 62 | =head1 EXTENDED METHODS | ||
| 63 | |||
| 64 | =head2 setup | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub setup { | ||
| 69 | my $c = shift; | ||
| 70 | |||
| 71 | my $config = $c->config->{authentication}->{hatena} ||= {}; | ||
| 72 | |||
| 73 | $config->{hatena_object} ||= do { | ||
| 74 | ( $config->{user_class} | ||
| 75 | ||= 'Catalyst::Plugin::Authentication::User::Hash' )->require; | ||
| 76 | |||
| 77 | Hatena::API::Auth->new( | ||
| 78 | { api_key => $config->{api_key}, | ||
| 79 | secret => $config->{secret}, | ||
| 80 | } | ||
| 81 | ); | ||
| 82 | }; | ||
| 83 | |||
| 84 | $c->NEXT::setup(@_); | ||
| 85 | } | ||
| 86 | |||
| 87 | =head1 METHODS | ||
| 88 | |||
| 89 | =head2 authenticate_hatena_url | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub authenticate_hatena_url { | ||
| 94 | shift->config->{authentication}->{hatena}->{hatena_object}->uri_to_login(@_); | ||
| 95 | } | ||
| 96 | |||
| 97 | =head2 authenticate_hatena | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub authenticate_hatena { | ||
| 102 | my $c = shift; | ||
| 103 | |||
| 104 | my $config = $c->config->{authentication}->{hatena}; | ||
| 105 | my $hatena = $config->{hatena_object}; | ||
| 106 | |||
| 107 | my $cert = $c->req->params->{cert} or return; | ||
| 108 | |||
| 109 | if ( my $user = $hatena->login($cert) ) { | ||
| 110 | $c->log->debug("Successfully authenticated user '$user->{name}'.") | ||
| 111 | if $c->debug; | ||
| 112 | |||
| 113 | my $store = $config->{store} || $c->default_auth_store; | ||
| 114 | if ( $store | ||
| 115 | and my $store_user = $store->get_user( $user->name, $user ) ) | ||
| 116 | { | ||
| 117 | $c->set_authenticated($store_user); | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | $user = $config->{user_class}->new($user); | ||
| 121 | $c->set_authenticated($user); | ||
| 122 | } | ||
| 123 | |||
| 124 | return 1; | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | $c->log->debug( | ||
| 128 | sprintf "Failed to authenticate hatena. Reason: '%s'", | ||
| 129 | $hatena->errstr, ) | ||
| 130 | if $c->debug; | ||
| 131 | |||
| 132 | return; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | =head1 AUTHOR | ||
| 137 | |||
| 138 | Daisuke Murase <typester@cpan.org> | ||
| 139 | |||
| 140 | =head1 COPYRIGHT | ||
| 141 | |||
| 142 | This program is free software; you can redistribute | ||
| 143 | it and/or modify it under the same terms as Perl itself. | ||
| 144 | |||
| 145 | The full text of the license can be found in the | ||
| 146 | LICENSE file included with this module. | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | 1; | ||