[system] / trunk / webwork2 / lib / WeBWorK / DB / Auth.pm Repository:
ViewVC logotype

Diff of /trunk/webwork2/lib/WeBWorK/DB/Auth.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 317 Revision 330
1################################################################################
2# WeBWorK mod_perl (c) 1995-2002 WeBWorK Team, Univeristy of Rochester
3# $Id$
4################################################################################
5
1package WeBWorK::DB::Auth; 6package WeBWorK::DB::Auth;
2 7
3# there should be a `use' line for each database type 8# there should be a `use' line for each database type
4use WeBWorK::DB::GDBM; 9use WeBWorK::DB::GDBM;
5 10
6# params: class, course environment 11# new($invocant, $courseEnv)
12# $invocant implicitly set by caller
13# $courseEnv an instance of CourseEnvironment
7sub new($$) { 14sub new($$) {
8 my $proto = shift; 15 my $invocant = shift;
9 my $class = ref($proto) || $proto; 16 my $class = ref($invocant) || $invocant;
10 my $courseEnv = shift; 17 my $courseEnv = shift;
11 my $dbModule = fullyQualifiedPackageName($courseEnv->{auth_db_type}); 18 my $dbModule = fullyQualifiedPackageName($courseEnv->{dbInfo}->{auth_type});
12 my $self = { 19 my $self = {
13 password_file => $courseEnv->{something}, 20 password_file => $courseEnv->{dbInfo}->{auth_passwd_file},
14 permissions_file => $courseEnv->{something}, 21 permissions_file => $courseEnv->{dbInfo}->{auth_perm_file},
15 keys_file => $courseEnv->{something}, 22 keys_file => $courseEnv->{dbInfo}->{auth_keys_file},
23 key_timeout => $courseEnv->{sessionKeyTimeout},
16 }; 24 };
17 $self->{password_db} = $self->{dbModule}->new($self->{password_file}); 25 $self->{password_db} = $dbModule->new($self->{password_file});
18 $self->{permissions_db} = $self->{dbModule}->new($self->{permissions_file}); 26 $self->{permissions_db} = $dbModule->new($self->{permissions_file});
19 $self->{keys_db} = $self->{dbModule}->new($self->{keys_file}); 27 $self->{keys_db} = $dbModule->new($self->{keys_file});
20 bless $self, $class; 28 bless $self, $class;
21 return $self; 29 return $self;
22} 30}
23 31
24sub fullyQualifiedPackageName($) { 32sub fullyQualifiedPackageName($) {
25 my $n = shift; 33 my $n = shift;
26 my $package = "__PACKAGE__"; 34 my $package = __PACKAGE__;
27 $package =~ s/([^:]*)$/$n/; 35 $package =~ s/([^:]*)$/$n/;
28 return $package; 36 return $package;
29} 37}
30 38
31sub connect($$$) { 39sub connect($$$) {
56} 64}
57 65
58sub setPassword($$$) { 66sub setPassword($$$) {
59 my $self = shift; 67 my $self = shift;
60 my $user = shift; 68 my $user = shift;
61 my $password = shift;
62 $password = crypt $password, join "", ('.','/','0'..'9','A'..'Z','a'..'z')[rand 64, rand 64] 69 my $password = crypt shift, join "", ('.','/','0'..'9','A'..'Z','a'..'z')[rand 64, rand 64];
63 $self->{password_db}->connect("rw"); 70 $self->{password_db}->connect("rw");
64 $self->{password_db}->hashRef()->{$user} = $password; 71 $self->{password_db}->hashRef()->{$user} = $password;
65 $self->{password_db}->disconnect(); 72 $self->{password_db}->disconnect();
66} 73}
67 74
68sub verifyPassword($$$) { 75sub verifyPassword($$$) {
69 my $self = shift; 76 my $self = shift;
70 my $user = shift; 77 my $user = shift;
71 my $password = shift; 78 my $password = shift;
72 $self->{password_db}->connect("ro"); 79 my $real_password = $self->getPassword($user);
73 my $result = $self->{password_db}->hashRef()->{$user} eq $password; 80 $password = crypt $password, $real_password;
74 $self->{password_db}->disconnect(); 81 return $password eq $real_password;
75 return $result;
76} 82}
77 83
78sub deletePassword($$) { 84sub deletePassword($$) {
79 my $self = shift; 85 my $self = shift;
80 my $user = shift; 86 my $user = shift;
89 my $self = shift; 95 my $self = shift;
90 my $user = shift; 96 my $user = shift;
91 $self->{keys_db}->connect("ro"); 97 $self->{keys_db}->connect("ro");
92 my $result = $self->{keys_db}->hashRef()->{$user}; 98 my $result = $self->{keys_db}->hashRef()->{$user};
93 $self->{keys_db}->disconnect(); 99 $self->{keys_db}->disconnect();
94 my ($key, $timestamp) = split /\s+/, $result; 100 my ($key, $timestamp) = defined $result ? split /\s+/, $result : (undef, undef);
95 return $key, $timestamp; 101 return defined $result ? split /\s+/, $result : undef;
96} 102}
97 103
98sub setKey($$$$) { 104sub setKey($$$$) {
99 my $self = shift; 105 my $self = shift;
100 my $user = shift; 106 my $user = shift;
104 $self->{keys_db}->connect("rw"); 110 $self->{keys_db}->connect("rw");
105 $self->{keys_db}->hashRef()->{$user} = $key_string; 111 $self->{keys_db}->hashRef()->{$user} = $key_string;
106 $self->{keys_db}->disconnect(); 112 $self->{keys_db}->disconnect();
107} 113}
108 114
109sub verifyKey($$$) { 115sub verifyKey($$$$$) {
110 my $self = shift; 116 my $self = shift;
111 my $user = shift; 117 my $user = shift;
112 my $key = shift; 118 my $key = shift;
113 $self->{keys_db}->connect("ro"); 119 my $timestamp = shift;
114 my $result = $self->{keys_db}->hashRef()->{$user}; 120
115 $self->{keys_db}->disconnect(); 121 my ($real_key, $real_timestamp) = $self->getKey($user);
116 my ($real_key, $timestamp) = split /\s+/, $result; 122 if ($key eq $real_key and $timestamp <= $real_timestamp+$self->{key_timeout}) {
117 return $key eq $real_key; 123 $self->setKey($user, $key, $timestamp);
118 # DANGER DANGER! this function no longer updates timestamp! 124 return 1;
125 } else {
126 return 0;
127 }
119} 128}
120 129
121sub deleteKey($$) { 130sub deleteKey($$) {
122 my $self = shift; 131 my $self = shift;
123 my $user = shift; 132 my $user = shift;
140sub setPermissions($$$) { 149sub setPermissions($$$) {
141 my $self = shift; 150 my $self = shift;
142 my $user = shift; 151 my $user = shift;
143 my $permissions = shift; 152 my $permissions = shift;
144 $self->{permissions_db}->connect("rw"); 153 $self->{permissions_db}->connect("rw");
145 $self->{permissions_db}->hashRef()->{$user} = $key; 154 $self->{permissions_db}->hashRef()->{$user} = $permissions;
146 $self->{permissions_db}->disconnect(); 155 $self->{permissions_db}->disconnect();
147} 156}
148 157
149sub deletePermissions($$) { 158sub deletePermissions($$) {
150 my $self = shift; 159 my $self = shift;
160 my $self = shift; 169 my $self = shift;
161 my $user = shift; 170 my $user = shift;
162 my $new_user = shift; 171 my $new_user = shift;
163 $self->{password_db}->connect("rw"); 172 $self->{password_db}->connect("rw");
164 my $pwhash = $self->{password_db}->hashRef(); # make things easier 173 my $pwhash = $self->{password_db}->hashRef(); # make things easier
165 if (exists $pwhash->{user}) { 174 if (exists $pwhash->{$user}) {
166 $pwhash->{new_user} = $pwhash->{user}; 175 $pwhash->{$new_user} = $pwhash->{$user};
167 delete $pwhash->{user}; 176 delete $pwhash->{$user};
168 } 177 }
169 $self->{password_db}->disconnect(); 178 $self->{password_db}->disconnect();
170} 179}
171 180
172sub change_user_in_permissions_file($$$) { 181sub change_user_in_permissions_file($$$) {
173 my $self = shift; 182 my $self = shift;
174 my $user = shift; 183 my $user = shift;
175 my $new_user = shift; 184 my $new_user = shift;
176 $self->{permissions_db}->connect("rw"); 185 $self->{permissions_db}->connect("rw");
177 my $permhash = $self->{permissions_db}->hashRef(); # make things easier 186 my $permhash = $self->{permissions_db}->hashRef(); # make things easier
178 if (exists $permhash->{user}) { 187 if (exists $permhash->{$user}) {
179 $permhash->{new_user} = $permhash->{user}; 188 $permhash->{$new_user} = $permhash->{$user};
180 delete $permhash->{user}; 189 delete $permhash->{$user};
181 } 190 }
182 $self->disconnect{permissions_db}->(); 191 $self->{permissions_db}->disconnect();
183} 192}
184 193
185=pod 1941;
186sub create_db {
187 my ($fileName, $permissions) =@_;
188 my %pwhash;
189 my $pw_obj;
190 &Global::tie_hash('PW_FH',\$pw_obj,\%pwhash, $fileName,'W',$permissions);
191 &Global::untie_hash('PW_FH',\$pw_obj,\%pwhash, $fileName);
192
193 chmod($permissions, $fileName) or
194 wwerror($0, "Can't do chmod($permissions, $fileName)");
195 chown(-1,$Global::numericalGroupID,$fileName) or
196 wwerror($0, "Can't do chown(-1,$Global::numericalGroupID,$fileName)");
197
198}
199=cut

Legend:
Removed from v.317  
changed lines
  Added in v.330

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9