[LON-CAPA-cvs] cvs: loncom /auth switchserver.pm

raeburn raeburn at source.lon-capa.org
Sun Dec 2 08:56:11 EST 2018


raeburn		Sun Dec  2 13:56:11 2018 EDT

  Modified files:              
    /loncom/auth	switchserver.pm 
  Log:
  - Cookie creation moved to &balancer_cookieid() to facilitate re-use.
  
  
Index: loncom/auth/switchserver.pm
diff -u loncom/auth/switchserver.pm:1.44 loncom/auth/switchserver.pm:1.45
--- loncom/auth/switchserver.pm:1.44	Sun Dec  2 13:52:56 2018
+++ loncom/auth/switchserver.pm	Sun Dec  2 13:56:10 2018
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Switch Servers Handler
 #
-# $Id: switchserver.pm,v 1.44 2018/12/02 13:52:56 raeburn Exp $
+# $Id: switchserver.pm,v 1.45 2018/12/02 13:56:10 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -68,6 +68,24 @@
     return OK;
 }
 
+sub balancer_cookieid {
+    my ($r,$desthost,$uname,$udom) = @_;
+    my @hosts = &Apache::lonnet::current_machine_ids();
+    my $newcookieid;
+    unless (grep(/^\Q$desthost\E$/, at hosts)) {
+        my $balancedir=$r->dir_config('lonBalanceDir');
+        $newcookieid = &md5_hex(&md5_hex(time.{}.rand().$$));
+        my $cookie = $udom.'_'.$uname.'_'.$newcookieid;
+        my $balcookie = "balanceID=$cookie; path=/; HttpOnly;";
+        if (open(my $fh,'>',"$balancedir/$cookie.id")) {
+            print $fh $desthost;
+            close($fh);
+            $r->headers_out->add('Set-cookie' => $balcookie);
+        }
+    }
+    return $newcookieid;
+}
+
 sub flush_course_logs {
     &Apache::lonnet::flushcourselogs();
     return OK;
@@ -116,11 +134,11 @@
     }
 
     my $skip_canhost_check = '';
+    my $now = time;
     if ($env{'form.role'}) {
         if (!exists($env{'user.role.'.$env{'form.role'}})) {
             delete($env{'form.role'});
         } else {
-            my $now = time;
             my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.role'}});
             if (($start && $start > $now) || ($end && $end < $now)) {
                 delete($env{'form.role'});
@@ -176,7 +194,6 @@
             }
         }
     }
-    my $now = time;
     my %temp=('switchserver' => $now.':'.$env{'form.otherserver'},
 	      $env{'form.role'});
     &Apache::lonnet::put('email_status',\%temp);
@@ -224,19 +241,8 @@
 
         unless (($found_server eq $env{'form.otherserver'}) &&
                 ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
-            my @hosts = &Apache::lonnet::current_machine_ids();
-            my $desthost = $env{'form.otherserver'};
-            unless (grep(/^\Q$desthost\E$/, at hosts)) {
-                my $balancedir=$r->dir_config('lonBalanceDir');
-                $newcookieid = &md5_hex(&md5_hex($now.{}.rand().$$));
-                my $cookie = $env{'user.domain'}.'_'.$env{'user.name'}.'_'.$newcookieid;
-                my $balcookie = "balanceID=$cookie; path=/; HttpOnly;";
-                if (open(my $fh,'>',"$balancedir/$cookie.id")) {
-                    print $fh $env{'form.otherserver'};
-                    close($fh);
-                }
-                $r->headers_out->add('Set-cookie' => $balcookie);
-            }
+            $newcookieid = &balancer_cookieid($r,$env{'form.otherserver'},
+                                              $env{'user.name'},$env{'user.domain'});
         }
     }
 




More information about the LON-CAPA-cvs mailing list