Commit | Line | Data |
3bb8633b |
1 | package Catalyst::Plugin::Authenticate::TypeKey; |
2 | |
3 | use strict; |
4 | use Authen::TypeKey; |
5 | use Carp (); |
6 | use File::Spec; |
7 | |
8 | our $VERSION = '0.1'; |
9 | |
10 | sub authenticate_typekey { |
11 | my ( $c, $email, $name, $nick, $ts, $sig, $options ) = @_; |
12 | |
13 | unless ( @_ == 6 || ( @_ == 7 && ref($options) eq 'HASH' ) ) { |
1f912a4a |
14 | Carp::croak('usage: $c->authenticate_typekey( $email, $name, $nick, $ts, $sig [, \%options ] )'); |
3bb8633b |
15 | } |
16 | |
17 | unless ( @_ == 7 ) { |
18 | $options = {}; |
19 | } |
20 | |
21 | my $config = $c->config->{authenticate}->{typekey}; |
22 | |
23 | my $token = $options->{token} || $config->{token} || undef; |
24 | my $expires = $options->{expires} || $config->{expires} || 0; |
25 | my $version = $options->{version} || $config->{version} || 1.1; |
26 | my $cache = $options->{cache} || $config->{cache} || File::Spec->catfile( File::Spec->tmpdir, 'regkeys.txt' ); |
27 | my $keys = $options->{keys} || $config->{keys} || 'http://www.typekey.com/extras/regkeys.txt'; |
28 | |
29 | my $typekey = Authen::TypeKey->new; |
30 | $typekey->expires($expires); |
31 | $typekey->key_cache($cache); |
32 | $typekey->key_url($keys); |
33 | $typekey->token($token); |
34 | $typekey->version($version); |
35 | |
36 | my $parameters = { |
37 | email => $email, |
38 | name => $name, |
39 | nick => $nick, |
40 | ts => $ts, |
41 | sig => $sig |
42 | }; |
43 | |
44 | unless ( $typekey->verify($parameters) ) { |
45 | my $error = $typekey->errstr; |
46 | $c->log->debug(qq/Failed to authenticate user '$name'. Reason: '$error'/); |
47 | return 0; |
48 | } |
49 | |
50 | $c->log->debug( qq/Successfully authenticated user '$name'./); |
51 | return 1; |
52 | } |
53 | |
54 | 1; |
55 | |
56 | __END__ |
57 | |
58 | =head1 NAME |
59 | |
60 | Catalyst::Plugin::Authenticate::TypeKey - TypeKey Authentication |
61 | |
62 | =head1 SYNOPSIS |
63 | |
64 | use Catalyst qw[Authenticate::TypeKey]; |
65 | |
66 | MyApp->config->{authenticate}->{typekey} = { |
67 | token => 'xxxxxxxxxxxxxxxxxxxx' |
68 | }; |
69 | |
70 | if ( $c->authenticate_typekey( $email, $name, $nick, $ts, $sig ) ) { |
71 | # successful autentication |
72 | } |
73 | |
74 | =head1 DESCRIPTION |
75 | |
76 | TypeKey Authentication. |
77 | |
78 | =head1 SEE ALSO |
79 | |
80 | L<Authen::TypeKey>, L<Catalyst>. |
81 | |
82 | =head1 AUTHOR |
83 | |
84 | Christian Hansen, C<ch@ngmedia.com> |
85 | |
86 | =head1 LICENSE |
87 | |
88 | This library is free software . You can redistribute it and/or modify it under |
89 | the same terms as perl itself. |
90 | |
91 | =cut |