git.fiddlerwoaroof.com
Browse code

Cleanup and new utilities

Cleaned up some of the login validation code.

Added a couple functions for handling unauthorized users more sanely.

fiddlerwoaroof authored on 22/05/2016 08:08:27
Showing 1 changed files
... ...
@@ -62,6 +62,7 @@
62 62
          (lambda ,args
63 63
            (declare (ignorable ,@args))
64 64
            ,@body)))
65
+
65 66
 (defun gen-state (len)
66 67
   (with-output-to-string (stream)
67 68
     (let ((*print-base* 36))
... ...
@@ -131,17 +132,36 @@
131 132
   (alexandria:with-gensyms (session userinfo)
132 133
     `(my-with-context-variables ((,session session))
133 134
        (with-session-values ((,userinfo userinfo)) ,session
134
-         (handler-case
135
-           (if (null ,userinfo)
136
-             (error 'user-not-logged-in)
137
-             (progn ,@body))
138
-           (error (c)
135
+         (if ,userinfo
136
+           (progn ,@body) 
137
+           (progn
139 138
              (setf ,userinfo nil)
140
-             (error c)))))))
139
+             (format t "Clearing all the infos")
140
+             (error 'user-not-logged-in)))))))
141 141
 
142 142
 (defmacro setup-oid-connect (app args &body callback)
143 143
   `(cl-oid-connect::bind-oid-connect-routes ,app (lambda ,args ,@callback)))
144 144
 
145
+(defun save-redirect (path)
146
+  (with-session-values (next-page) (context :session)
147
+    (setf next-page path)))
148
+
149
+(defun call-with-login (authorized-cb unauthorized-cb)
150
+  (handler-case
151
+    (ensure-logged-in
152
+      (funcall authorized-cb))
153
+    (user-not-logged-in (c)
154
+                        (funcall unauthorized-cb c))))
155
+
156
+(defmacro with-login (handler (sub (sym) &body unauthorized-action))
157
+  (unless (eq sub :unauthorized)
158
+    (error 'error "unauthorized clause must start with \"unauthorized\""))
159
+  `(call-with-login
160
+     (lambda ()
161
+       ,handler)
162
+     (lambda (,sym)
163
+       ,@unauthorized-action)))
164
+
145 165
 (flet ((handle-no-user (main-body handler-body)
146 166
          `(handler-case (ensure-logged-in ,@main-body)
147 167
             (user-not-logged-in (e) (declare (ignorable e))
... ...
@@ -153,10 +173,10 @@
153 173
 
154 174
   (defmacro require-login (&body body)
155 175
     "Redirects to /login if not logged in."
156
-    (handle-no-user body
157
-                    `((with-session-values (next-page) (context :session)
158
-                        (setf next-page (lack.request:request-path-info *request*))
159
-                        '(302 (:location "/login")))))))
176
+    (handle-no-user
177
+      body
178
+      `((save-redirect (lack.request:request-path-info *request*))
179
+        '(302 (:location "/login"))))))
160 180
 
161 181
 (defmacro redirect-if-necessary (sessionvar &body body)
162 182
   (with-gensyms (session)