root/websites/mobilecat/trunk/lib/MobileCat/Base/MobileAuth.pm

Revision 16852, 1.7 kB (checked in by tomi-ru, 19 months ago)

登録系

Line 
1package MobileCat::Base::MobileAuth;
2use strict;
3use warnings;
4use base qw/ Catalyst::Controller Catalyst::Component::ACCEPT_CONTEXT /;
5__PACKAGE__->mk_accessors(qw( cidr ));
6
7use Net::CIDR::MobileJP;
8
9sub new {
10    my $self = shift->next::method(@_);
11   
12    $self->config({
13        check_ua        => 1,
14        check_ip        => 1,
15        error_stash_key => 'auth_error',
16        %$self,
17    });
18   
19    $self->cidr( Net::CIDR::MobileJP->new ) if $self->config->{check_ip};
20   
21    $self;
22}
23
24sub confirm_mobile :Private {
25    my ($self) = @_;
26   
27    if ($self->config->{check_ua} &&
28        $self->context->req->mobile_agent->is_non_mobile) {
29        $self->_failed('user_agent is non_mobile');
30        return;
31    }
32   
33    if ($self->config->{check_ip} &&
34        $self->cidr->get_carrier($self->context->req->address) eq 'N') {
35        $self->_failed('invalid mobile ip');
36        return;
37    }
38   
39    if (not eval { $self->context->req->mobile_agent->user_id }) {
40        $self->_failed('mobile_id required');
41        return;
42    }
43   
44    return 1;
45}
46
47sub authenticate_mobileid :Private {
48    my ($self, $authinfo) = @_;
49   
50    return unless $self->confirm_mobile;
51     
52    $self->context->logout;
53   
54    $authinfo ||= {};
55    $authinfo->{username} = $self->context->req->mobile_agent->user_id;
56   
57    if (my $user = $self->context->authenticate($authinfo)) {
58        return $user;
59    } else {
60        $self->_failed('login failed');
61        return;
62    }
63}
64
65sub _failed {
66    my ($self, $reason) = @_;
67   
68    $self->context->log->debug('Failed to authenticate MobileID. Reason:'. $reason);
69   
70    $self->context->stash->{ $self->config->{error_stash_key} } = $reason
71        if $self->config->{error_stash_key};
72}
73
741;
Note: See TracBrowser for help on using the browser.