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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 337 - (download) (as text) (annotate)
Wed Jun 5 18:32:12 2002 UTC (17 years, 7 months ago) by sh002i
File size: 5670 byte(s)
more improvements, i think.

    1 ################################################################################
    2 # WeBWorK mod_perl (c) 1995-2002 WeBWorK Team, Univeristy of Rochester
    3 # $Id$
    4 ################################################################################
    5 
    6 package WeBWorK::DB::Auth;
    7 
    8 # there should be a `use' line for each database type
    9 use WeBWorK::DB::GDBM;
   10 
   11 # new($invocant, $courseEnv)
   12 # $invocant implicitly set by caller
   13 # $courseEnv  an instance of CourseEnvironment
   14 sub new($$) {
   15   my $invocant = shift;
   16   my $class = ref($invocant) || $invocant;
   17   my $courseEnv = shift;
   18   my $dbModule = fullyQualifiedPackageName($courseEnv->{dbInfo}->{auth_type});
   19   my $self = {
   20     password_file    => $courseEnv->{dbInfo}->{auth_passwd_file},
   21     permissions_file => $courseEnv->{dbInfo}->{auth_perm_file},
   22     keys_file        => $courseEnv->{dbInfo}->{auth_keys_file},
   23     key_timeout      => $courseEnv->{sessionKeyTimeout},
   24   };
   25   $self->{password_db}    = $dbModule->new($self->{password_file});
   26   $self->{permissions_db} = $dbModule->new($self->{permissions_file});
   27   $self->{keys_db}        = $dbModule->new($self->{keys_file});
   28   bless $self, $class;
   29   return $self;
   30 }
   31 
   32 sub fullyQualifiedPackageName($) {
   33   my $n = shift;
   34   my $package = __PACKAGE__;
   35   $package =~ s/([^:]*)$/$n/;
   36   return $package;
   37 }
   38 
   39 sub connect($$$) {
   40   my $self = shift;
   41   my $db = shift;
   42   my $mode = shift;
   43   return if defined $self->{$db."_db"};
   44   $self->{$db."_db"} = $self->{dbModule}->new($db."_file", $mode);
   45   $self->{$db."_db"}->connect();
   46 }
   47 
   48 sub disconnect($$) {
   49   my $self = shift;
   50   my $db = shift;
   51   return unless defined $self->{$db."_db"};
   52   $self->{$db."_db"}->disconnect;
   53 }
   54 
   55 # -----
   56 
   57 sub getUsers($) {
   58   my $self = shift;
   59   return unless $self->{password_db}->connect("ro");
   60   my @password_users = keys %{$self->{password_db}->hashRef};
   61   $self->{password_db}->disconnect;
   62   return unless $self->{permissions_db}->connect("ro");
   63   my @permissions_users = keys %{$self->{permissions_db}->hashRef};
   64   $self->{permissions_db}->disconnect;
   65   return unless $self->{keys_db}->connect("ro");
   66   my @keys_users = keys %{$self->{keys_db}->hashRef};
   67   $self->{keys_db}->disconnect;
   68   my %all_users;
   69   $all_users{$_}++ foreach (@password_users, @permissions_users, @keys_users);
   70   return keys %all_users;
   71 }
   72 
   73 # -----
   74 
   75 sub getPassword($$) {
   76   my $self = shift;
   77   my $user = shift;
   78   return unless $self->{password_db}->connect("ro");
   79   my $result = $self->{password_db}->hashRef->{$user};
   80   $self->{password_db}->disconnect;
   81   return $result;
   82 }
   83 
   84 sub setPassword($$$) {
   85   my $self = shift;
   86   my $user = shift;
   87   my $password = crypt shift, join "", ('.','/','0'..'9','A'..'Z','a'..'z')[rand 64, rand 64];
   88   $self->{password_db}->connect("rw");
   89   $self->{password_db}->hashRef->{$user} = $password;
   90   $self->{password_db}->disconnect;
   91 }
   92 
   93 sub verifyPassword($$$) {
   94   my $self = shift;
   95   my $user = shift;
   96   my $password = shift;
   97   my $real_password = $self->getPassword($user);
   98   $password = crypt $password, $real_password;
   99   return $password eq $real_password;
  100 }
  101 
  102 sub deletePassword($$) {
  103   my $self = shift;
  104   my $user = shift;
  105   $self->{password_db}->connect("rw");
  106   delete $self->{password_db}->hashRef->{$user};
  107   $self->{password_db}->disconnect;
  108 }
  109 
  110 # -----
  111 
  112 sub getKey($$) {
  113   my $self = shift;
  114   my $user = shift;
  115   return unless $self->{keys_db}->connect("ro");
  116   my $result = $self->{keys_db}->hashRef->{$user};
  117   $self->{keys_db}->disconnect;
  118   my ($key, $timestamp) = defined $result ? split /\s+/, $result : (undef, undef);
  119   return defined $result ? split /\s+/, $result : undef;
  120 }
  121 
  122 sub setKey($$$$) {
  123   my $self = shift;
  124   my $user = shift;
  125   my $key = shift;
  126   my $timestamp = shift;
  127   my $key_string = "$key $timestamp";
  128   $self->{keys_db}->connect("rw");
  129   $self->{keys_db}->hashRef->{$user} = $key_string;
  130   $self->{keys_db}->disconnect;
  131 }
  132 
  133 sub verifyKey($$$$$) {
  134   my $self = shift;
  135   my $user = shift;
  136   my $key = shift;
  137   my $timestamp = shift;
  138 
  139   my ($real_key, $real_timestamp) = $self->getKey($user);
  140   if ($key eq $real_key and $timestamp <= $real_timestamp+$self->{key_timeout}) {
  141     $self->setKey($user, $key, $timestamp);
  142     return 1;
  143   } else {
  144     return 0;
  145   }
  146 }
  147 
  148 sub deleteKey($$) {
  149   my $self = shift;
  150   my $user = shift;
  151   $self->{keys_db}->connect("rw");
  152   delete $self->{keys_db}->hashRef->{$user};
  153   $self->{keys_db}->disconnect;
  154 }
  155 
  156 # -----
  157 
  158 sub getPermissions($$) {
  159   my $self = shift;
  160   my $user = shift;
  161   return unless $self->{permissions_db}->connect("ro");
  162   my $result = $self->{permissions_db}->hashRef->{$user};
  163   $self->{permissions_db}->disconnect;
  164   return $result;
  165 }
  166 
  167 sub setPermissions($$$) {
  168   my $self = shift;
  169   my $user = shift;
  170   my $permissions = shift;
  171   $self->{permissions_db}->connect("rw");
  172   $self->{permissions_db}->hashRef->{$user} = $permissions;
  173   $self->{permissions_db}->disconnect;
  174 }
  175 
  176 sub deletePermissions($$) {
  177   my $self = shift;
  178   my $user = shift;
  179   $self->{permissions_db}->connect("rw");
  180   delete $self->{permissions_db}->hashRef->{$user};
  181   $self->{permissions_db}->disconnect;
  182 }
  183 
  184 # ----- ghetto for stupid functions -----
  185 
  186 sub change_user_in_password_file($$$) {
  187   my $self = shift;
  188   my $user = shift;
  189   my $new_user = shift;
  190   $self->{password_db}->connect("rw");
  191   my $pwhash = $self->{password_db}->hashRef; # make things easier
  192   if (exists $pwhash->{$user}) {
  193     $pwhash->{$new_user} = $pwhash->{$user};
  194     delete $pwhash->{$user};
  195   }
  196   $self->{password_db}->disconnect;
  197 }
  198 
  199 sub change_user_in_permissions_file($$$) {
  200   my $self = shift;
  201   my $user = shift;
  202   my $new_user = shift;
  203   $self->{permissions_db}->connect("rw");
  204   my $permhash = $self->{permissions_db}->hashRef; # make things easier
  205   if (exists $permhash->{$user}) {
  206     $permhash->{$new_user} = $permhash->{$user};
  207     delete $permhash->{$user};
  208   }
  209   $self->{permissions_db}->disconnect;
  210 }
  211 
  212 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9