| 1 | package Catalyst::Plugin::Authentication::Credential::JugemKey; | ||
| 2 | |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '0.04'; | ||
| 7 | |||
| 8 | 0 | ▶ | use WebService::JugemKey::Auth; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use UNIVERSAL::require; | ||
| 10 | use NEXT; | ||
| 11 | |||
| 12 | sub setup { | ||
| 13 | my $c = shift; | ||
| 14 | |||
| 15 | my $config = $c->config->{authentication}->{jugemkey} ||= {}; | ||
| 16 | |||
| 17 | $config->{jugemkey_object} ||= do { | ||
| 18 | ( $config->{user_class} | ||
| 19 | ||= 'Catalyst::Plugin::Authentication::User::Hash' )->require; | ||
| 20 | |||
| 21 | my $api = WebService::JugemKey::Auth->new({ | ||
| 22 | api_key => $config->{api_key}, | ||
| 23 | secret => $config->{secret}, | ||
| 24 | }); | ||
| 25 | $api->perms( $config->{perms} ); | ||
| 26 | $api; | ||
| 27 | }; | ||
| 28 | |||
| 29 | $c->NEXT::setup(@_); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub authenticate_jugemkey_url { | ||
| 33 | my ($c, $params) = @_; | ||
| 34 | $c->config->{authentication}->{jugemkey}->{jugemkey_object}->uri_to_login($params); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub authenticate_jugemkey_get_token { | ||
| 38 | my $c = shift; | ||
| 39 | |||
| 40 | my $config = $c->config->{authentication}->{jugemkey}; | ||
| 41 | my $jugemkey = $config->{jugemkey_object}; | ||
| 42 | |||
| 43 | my $frob = $c->req->params->{frob} or return; | ||
| 44 | if ( my $user = $jugemkey->get_token($frob) ) { | ||
| 45 | $c->log->debug("Successfully get token of user '$user->name'.") | ||
| 46 | if $c->debug; | ||
| 47 | |||
| 48 | my $store = $config->{store} || $c->default_auth_store; | ||
| 49 | if ( $store | ||
| 50 | and my $store_user = $store->get_user( $user->name, $user ) ) | ||
| 51 | { | ||
| 52 | $c->set_authenticated($store_user); | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | $user = $config->{user_class}->new($user); | ||
| 56 | $c->set_authenticated($user); | ||
| 57 | } | ||
| 58 | |||
| 59 | return $user; | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | $c->log->debug( | ||
| 63 | sprintf "Failed to authenticate jugemkey. Reason: '%s'", | ||
| 64 | $jugemkey->errstr, ) | ||
| 65 | if $c->debug; | ||
| 66 | |||
| 67 | return; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | =head1 NAME | ||
| 73 | |||
| 74 | Catalyst::Plugin::Authentication::Credential::JugemKey - JugemKey authentication plugin for Catalyst | ||
| 75 | |||
| 76 | =head1 VERSION | ||
| 77 | |||
| 78 | Version 0.04 | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | =head1 SYNOPSIS | ||
| 83 | |||
| 84 | # load plugin and setup | ||
| 85 | use Catalyst qw( | ||
| 86 | Authentication | ||
| 87 | Authentication::Credential::JugemKey | ||
| 88 | Session | ||
| 89 | Session::Store::FastMmap | ||
| 90 | Session::State::Cookie | ||
| 91 | ); | ||
| 92 | |||
| 93 | __PACKAGE__->config->{authentication}->{jugemkey} = { | ||
| 94 | api_key => 'your api_key', | ||
| 95 | secret => 'your shared secret', | ||
| 96 | perms => 'permission', | ||
| 97 | }; | ||
| 98 | |||
| 99 | # in controller | ||
| 100 | # redirect login url | ||
| 101 | sub login : Path('/jugemkey/login') { | ||
| 102 | my ( $self, $c ) = @_; | ||
| 103 | $c->res->redirect( | ||
| 104 | $c->authenticate_jugemkey_url({ | ||
| 105 | callback_url => 'http://your_callback_url/jugemkey/auth', | ||
| 106 | param1 => 'value1', | ||
| 107 | param2 => 'value2', | ||
| 108 | }) | ||
| 109 | ); | ||
| 110 | } | ||
| 111 | |||
| 112 | # callback url | ||
| 113 | sub auth : Path('/jugemkey/auth') { | ||
| 114 | my ( $self, $c ) = @_; | ||
| 115 | |||
| 116 | if ( my $user = $c->authenticate_jugemkey_get_token ) { | ||
| 117 | # login successful | ||
| 118 | $c->session->{name} = $user->name; | ||
| 119 | $c->session->{token} = $user->token; | ||
| 120 | $c->res->redirect( $c->uri_for('/') ); | ||
| 121 | } | ||
| 122 | else { | ||
| 123 | # something wrong | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | =head1 METHODS | ||
| 128 | |||
| 129 | =over 2 | ||
| 130 | |||
| 131 | =item authenticate_jugemkey_url | ||
| 132 | |||
| 133 | Creates login url. | ||
| 134 | |||
| 135 | =item authenticate_jugemkey_get_token | ||
| 136 | |||
| 137 | Exchange frob for token and JugemKey user name. | ||
| 138 | |||
| 139 | =back | ||
| 140 | |||
| 141 | =head1 INTERNAL METHODS | ||
| 142 | |||
| 143 | =over 1 | ||
| 144 | |||
| 145 | =item setup | ||
| 146 | |||
| 147 | =back | ||
| 148 | |||
| 149 | =head1 SEE ALSO | ||
| 150 | |||
| 151 | L<WebService::JugemKey::Auth>, L<http://jugemkey.jp/api/auth/> | ||
| 152 | |||
| 153 | =head1 AUTHOR | ||
| 154 | |||
| 155 | Gosuke Miyashita, C<< <gosukenator at gmail.com> >> | ||
| 156 | |||
| 157 | =head1 BUGS | ||
| 158 | |||
| 159 | Please report any bugs or feature requests to | ||
| 160 | C<bug-catalyst-plugin-authentication-credential-jugemkey at rt.cpan.org>, or through the web interface at | ||
| 161 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-Authentication-Credential-JugemKey>. | ||
| 162 | I will be notified, and then you'll automatically be notified of progress on | ||
| 163 | your bug as I make changes. | ||
| 164 | |||
| 165 | =head1 SUPPORT | ||
| 166 | |||
| 167 | You can find documentation for this module with the perldoc command. | ||
| 168 | |||
| 169 | perldoc Catalyst::Plugin::Authentication::Credential::JugemKey | ||
| 170 | |||
| 171 | You can also look for information at: | ||
| 172 | |||
| 173 | =over 4 | ||
| 174 | |||
| 175 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 176 | |||
| 177 | L<http://annocpan.org/dist/Catalyst-Plugin-Authentication-Credential-JugemKey> | ||
| 178 | |||
| 179 | =item * CPAN Ratings | ||
| 180 | |||
| 181 | L<http://cpanratings.perl.org/d/Catalyst-Plugin-Authentication-Credential-JugemKey> | ||
| 182 | |||
| 183 | =item * RT: CPAN's request tracker | ||
| 184 | |||
| 185 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-Authentication-Credential-JugemKey> | ||
| 186 | |||
| 187 | =item * Search CPAN | ||
| 188 | |||
| 189 | L<http://search.cpan.org/dist/Catalyst-Plugin-Authentication-Credential-JugemKey> | ||
| 190 | |||
| 191 | =back | ||
| 192 | |||
| 193 | =head1 ACKNOWLEDGEMENTS | ||
| 194 | |||
| 195 | =head1 COPYRIGHT & LICENSE | ||
| 196 | |||
| 197 | Copyright 2006 Gosuke Miyashita, all rights reserved. | ||
| 198 | |||
| 199 | This program is free software; you can redistribute it and/or modify it | ||
| 200 | under the same terms as Perl itself. | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | 1; # End of Catalyst::Plugin::Authentication::Credential::JugemKey | ||