annotate Lib/Security/Auth.pm @ 29:37160f7c8edb

minor changes
author Sergey
date Wed, 21 Oct 2009 17:30:20 +0400
parents 03e58a454b20
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 package Security::Auth;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 use Security;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5 use DateTime;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 use Digest::MD5 qw(md5_hex);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7 our @ISA = qw(Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 our $Package;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 our $DataSource;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12 srand time;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15 DeclareProperty DS => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 DeclareProperty SecPackage => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19 {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 my $i = 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 sub GenSSID() {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 return md5_hex(time,rand,$i++);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27 my ($this,%args) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 $this->{$DS} = $args{'DS'} or die new Exception('A data source is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29 $this->{$SecPackage} = $args{'SecPackage'} or die new Exception('A security package is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32 sub AuthenticateUser {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33 my ($this,$Name,$SecData) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 my $User = $this->{$DS}->FindUser($Name);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36 if (not $User or not $User->Active ) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 return new Security::AuthResult (
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 State => Security::AUTH_FAILED,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39 AuthModule => $this
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 if (my $StoredData = $this->{$DS}->GetUserAuthData($User,$this->{$SecPackage})) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 my $AuthData = $this->{$SecPackage}->ConstructAuthData($StoredData->AuthData);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 if ((my $status = $AuthData->DoAuth($SecData)) != Security::AUTH_FAILED) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 $AuthData = $this->{$SecPackage}->NewAuthData(GenSSID);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48 return new Security::AuthResult (
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49 State => $status,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 Session => $this->{$DS}->CreateSession(GenSSID,$User,$AuthData),
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51 ClientSecData => $AuthData->ClientAuthData,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52 AuthModule => $this
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53 )
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55 return new Security::AuthResult (
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56 State => Security::AUTH_FAILED,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57 AuthModule => $this
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61 # the user isn't allowed to authenticate using this method
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 return new Security::AuthResult (
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63 State => Security::AUTH_FAILED,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 AuthModule => $this
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 sub AuthenticateSession {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 my ($this,$SSID,$SecData) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 my $Session = $this->{$DS}->LoadSession($SSID) or return new Security::AuthResult(State => Security::AUTH_FAILED);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 my $AuthData = $this->{$SecPackage}->ConstructAuthData($Session->SecData);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 if ((my $status = $AuthData->DoAuth($SecData)) != Security::AUTH_FAILED) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77 $Session->SecData($AuthData->SessionAuthData);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78 $Session->LastUsage(DateTime->now());
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 return new Security::AuthResult(State => $status, Session => $Session, ClientSecData => $AuthData->ClientAuthData, AuthModule => $this);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81 $this->{$DS}->CloseSession($Session);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 return new Security::AuthResult(State => Security::AUTH_FAILED, AuthModule => $this);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86 sub CreateUser {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 my ($this,$uname,$description,$active,$secData) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 my $user = $this->{$DS}->CreateUser($uname,$description,$active);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90 $this->{$DS}->SetUserAuthData($user,$this->{$SecPackage},$this->{$SecPackage}->NewAuthData($secData));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 return $user;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95 sub try_construct {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 my $package = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97 return $package->can('construct') ? $package->construct() : $package;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100 sub construct {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 $Package or die new Exception('A security package is reqiured');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102 $DataSource or die new Exception('A data source is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103 eval "require $DataSource;" or die new Exception('Failed to load the data source module',$@) if not ref $DataSource;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104 eval "require $Package;" or die new Exception('Failed to load the security package module',$@) if not ref $Package;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105 return __PACKAGE__->new(DS => try_construct($DataSource), SecPackage => try_construct($Package));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
108 1;