blib/lib/Catalyst/Authentication/Credential/TypeKey.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Catalyst::Authentication::Credential::TypeKey;
2 2 ▶use Catalyst::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 0 ▶use Authen::TypeKey;
Subroutine BEGIN: called CC=1 SCAR=2.6
4 use Moose;
5 use MooseX::Types::Common::String qw/ NonEmptySimpleStr SimpleStr/;
6 use namespace::autoclean;
7  
8 our $VERSION = '0.005';
9  
10 has [qw/ key_url version /] => (
11 isa => NonEmptySimpleStr,
12 is => 'ro',
13 required => 1,
14 );
15  
16 has [qw/ key_cache skip_expiry_check /] => (
17 isa => NonEmptySimpleStr,
18 is => 'ro',
19 required => 0,
20 );
21  
22 has _authen_typekey => (
23 is => 'ro',
24 isa => 'Authen::TypeKey',
25 lazy => 1,
26 builder => '_build__authen_typekey',
27 );
28  
29 sub _build__authen_typekey {
30 my ( $self ) = @_;
31 my $tk = Authen::TypeKey->new();
32 foreach my $name ( 'key_url', 'version',
33 'key_cache', 'skip_expiry_check' ) {
34 $tk->$name( $self->$name ) if $self->$name;
35 }
36 return $tk;
37 }
38  
39 sub BUILDARGS {
40 my ( $class, $config, $app, $realm ) = @_;
41 unless ( $config->{version} and $config->{key_url} ) {
42 Catalyst::Exception->throw(
43 __PACKAGE__ . " credential for realm " . $realm->name . " missing version and key_url"
44 );
45 }
46 return $config;
47 }
48  
49 =head1 NAME
50  
51 Catalyst::Authentication::Credential::TypeKey - TypeKey authentication
52 (in new Catalyst Authentication )
53  
54 =head1 VERSION
55  
56 Version 0.005
57  
58 =head1 SYNOPSIS
59  
60 Authenticate Catalyst apps with TypeKey system.
61  
62 Uses the Catalyst::Plugin::Authentication system.
63 In MyApp.pm
64  
65 use Catalyst qw(
66 ...
67 Authentication
68 ...
69 );
70  
71 __PACKAGE__->config(
72 'authentication' => {
73 typekey => {
74 credential => {
75 class => 'TypeKey',
76 #Config below relies heavly on Authen::TypeKey
77 key_cache => '/var/cache/webapp/myapp/',
78 version => '1',
79 skip_expiry_check => '1',
80 key_url => 'http://www.typekey.com/extras/regkeys.txt',
81 },
82 ...
83 },
84 },
85 );
86  
87  
88 In your controller
89  
90 sub login : Local {
91 # body...
92 if ( $c->authenticate( { email => $c->req->param('email') }, 'typekey') ) {
93 # SUCCESS
94 } else {
95 # FAILED
96 }
97 }
98  
99  
100 =head1 TODO
101  
102 =head1 METHODS
103  
104 =cut
105  
106 =head1 SEE ALSO
107  
108 L<Authen::TypeKey>, L<Catalyst>, L<Catalyst::Plugin::Authentication>.
109  
110 =head1 AUTHOR
111  
112 zdk
113  
114 The idea was from https://github.com/omega/catalyst-authentication-credential-typekey
115  
116 =head1 LICENSE
117  
118 This library is free software . You can redistribute it and/or modify it under
119 the same terms as perl itself.
120  
121 =cut
122  
123  
124 =head2 authenticate
125  
126 Standard authentication method
127  
128 =cut
129  
130 sub authenticate {
131 my ( $self, $c, $realm, $auth_info ) = @_;
132  
133 my $res = $self->_authen_typekey->verify( $c->req );
134  
135 if (! $res ) {
136 $c->log->debug( $self->_authen_typekey->errstr ) if $c->debug;
137 return;
138 }
139  
140 $c->model( $realm->{store}->{config}->{user_class} )->find_or_create( $auth_info );
141  
142 return unless $auth_info;
143 my $user = $realm->find_user( $auth_info, $c );
144 unless ( $user ) {
145 $c->log->error('Authenticated user, but could not locate in our Store!');
146 return;
147 }
148 return $user;
149 }
150  
151 __PACKAGE__->meta->make_immutable;
152 1;
153  
154 __END__
↑ Summary