]> git.plutz.net Git - cgilite/commitdiff
allow configuration of session timeout, make timestamp variable reusable
authorPaul Hänsch <paul@plutz.net>
Sat, 30 Nov 2019 12:30:49 +0000 (13:30 +0100)
committerPaul Hänsch <paul@plutz.net>
Sat, 30 Nov 2019 12:30:49 +0000 (13:30 +0100)
session.sh

index 8ed8d8808e0655cc8b89c6ce7bb0df318b0b571f..ee5c4993a72df047fe0fc67b3c384f2b5733be48 100755 (executable)
@@ -3,6 +3,9 @@
 [ -n "$include_session" ] && return 0
 include_session="$0"
 
+_DATE="$(date +%s)"
+SESSION_TIMEOUT="${SESSION_TIMEOUT:-7200}"
+
 server_key(){
   IDFILE="${IDFILE:-${_DATA:-.}/serverkey}"
   if [ "$(stat -c %s "$IDFILE")" -ne 512 ] || ! cat "$IDFILE"; then
@@ -27,7 +30,7 @@ randomid(){
 }
 
 timeid(){
-  d=$(($(date +%s) % 4294967296))
+  d=$(($_DATE % 4294967296))
   { printf "$(
       printf \\%o \
         $((d / 16777216 % 256)) \
@@ -60,17 +63,16 @@ update_session(){
   
   checksig="$(printf %s "$sid" "$time" "$serverkey" | sha256sum)"
   checksig="${checksig%% *}"
-  d=$(date +%s)
   
   if ! [ "$checksig" = "$sig" \
-    -a "$time" -ge "$d" \
+    -a "$time" -ge "$_DATE" \
     -a "$(printf %s "$sid" |checkid)" ] 2>&-
   then
     debug Setting up new session
     sid="$(randomid)"
   fi
 
-  time=$(( $(date +%s) + 7200 ))
+  time=$(( $_DATE + $SESSION_TIMEOUT ))
   sig="$(printf %s "$sid" "$time" "$serverkey" |sha256sum)"
   sig="${sig%% *}"
   printf %s\\n "${sid}-${time}-${sig}"