[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 334 - (download) (as text) (annotate)
Fri May 31 20:46:20 2002 UTC (17 years, 7 months ago) by sh002i
File size: 5062 byte(s)
developers, developers, developers, developers, developers, developers,
developers, developers, developers, developers, developers, developers.

    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 getPassword($$) {
   58   my $self = shift;
   59   my $user = shift;
   60   return unless $self->{password_db}->connect("rw");
   61   my $result = $self->{password_db}->hashRef->{$user};
   62   $self->{password_db}->disconnect;
   63   return $result;
   64 }
   65 
   66 sub setPassword($$$) {
   67   my $self = shift;
   68   my $user = shift;
   69   my $password = crypt shift, join "", ('.','/','0'..'9','A'..'Z','a'..'z')[rand 64, rand 64];
   70   $self->{password_db}->connect("rw");
   71   $self->{password_db}->hashRef->{$user} = $password;
   72   $self->{password_db}->disconnect;
   73 }
   74 
   75 sub verifyPassword($$$) {
   76   my $self = shift;
   77   my $user = shift;
   78   my $password = shift;
   79   my $real_password = $self->getPassword($user);
   80   $password = crypt $password, $real_password;
   81   return $password eq $real_password;
   82 }
   83 
   84 sub deletePassword($$) {
   85   my $self = shift;
   86   my $user = shift;
   87   $self->{password_db}->connect("rw");
   88   delete $self->{password_db}->hashRef->{$user};
   89   $self->{password_db}->disconnect;
   90 }
   91 
   92 # -----
   93 
   94 sub getKey($$) {
   95   my $self = shift;
   96   my $user = shift;
   97   return unless $self->{keys_db}->connect("rw");
   98   my $result = $self->{keys_db}->hashRef->{$user};
   99   $self->{keys_db}->disconnect;
  100   my ($key, $timestamp) = defined $result ? split /\s+/, $result : (undef, undef);
  101   return defined $result ? split /\s+/, $result : undef;
  102 }
  103 
  104 sub setKey($$$$) {
  105   my $self = shift;
  106   my $user = shift;
  107   my $key = shift;
  108   my $timestamp = shift;
  109   my $key_string = "$key $timestamp";
  110   $self->{keys_db}->connect("rw");
  111   $self->{keys_db}->hashRef->{$user} = $key_string;
  112   $self->{keys_db}->disconnect;
  113 }
  114 
  115 sub verifyKey($$$$$) {
  116   my $self = shift;
  117   my $user = shift;
  118   my $key = shift;
  119   my $timestamp = shift;
  120 
  121   my ($real_key, $real_timestamp) = $self->getKey($user);
  122   if ($key eq $real_key and $timestamp <= $real_timestamp+$self->{key_timeout}) {
  123     $self->setKey($user, $key, $timestamp);
  124     return 1;
  125   } else {
  126     return 0;
  127   }
  128 }
  129 
  130 sub deleteKey($$) {
  131   my $self = shift;
  132   my $user = shift;
  133   $self->{keys_db}->connect("rw");
  134   delete $self->{keys_db}->hashRef->{$user};
  135   $self->{keys_db}->disconnect;
  136 }
  137 
  138 # -----
  139 
  140 sub getPermissions($$) {
  141   my $self = shift;
  142   my $user = shift;
  143   return unless $self->{permissions_db}->connect("rw");
  144   my $result = $self->{permissions_db}->hashRef->{$user};
  145   $self->{permissions_db}->disconnect;
  146   return $result;
  147 }
  148 
  149 sub setPermissions($$$) {
  150   my $self = shift;
  151   my $user = shift;
  152   my $permissions = shift;
  153   $self->{permissions_db}->connect("rw");
  154   $self->{permissions_db}->hashRef->{$user} = $permissions;
  155   $self->{permissions_db}->disconnect;
  156 }
  157 
  158 sub deletePermissions($$) {
  159   my $self = shift;
  160   my $user = shift;
  161   $self->{permissions_db}->connect("rw");
  162   delete $self->{permissions_db}->hashRef->{$user};
  163   $self->{permissions_db}->disconnect;
  164 }
  165 
  166 # ----- ghetto for stupid functions -----
  167 
  168 sub change_user_in_password_file($$$) {
  169   my $self = shift;
  170   my $user = shift;
  171   my $new_user = shift;
  172   $self->{password_db}->connect("rw");
  173   my $pwhash = $self->{password_db}->hashRef; # make things easier
  174   if (exists $pwhash->{$user}) {
  175     $pwhash->{$new_user} = $pwhash->{$user};
  176     delete $pwhash->{$user};
  177   }
  178   $self->{password_db}->disconnect;
  179 }
  180 
  181 sub change_user_in_permissions_file($$$) {
  182   my $self = shift;
  183   my $user = shift;
  184   my $new_user = shift;
  185   $self->{permissions_db}->connect("rw");
  186   my $permhash = $self->{permissions_db}->hashRef; # make things easier
  187   if (exists $permhash->{$user}) {
  188     $permhash->{$new_user} = $permhash->{$user};
  189     delete $permhash->{$user};
  190   }
  191   $self->{permissions_db}->disconnect;
  192 }
  193 
  194 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9