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