-
Notifications
You must be signed in to change notification settings - Fork 3
/
clath.lisp
248 lines (217 loc) · 9.07 KB
/
clath.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
(in-package :clath)
#|
FIXME: Stuff in this file will need to be rethought when a more general
login manager is developed.
|#
(defparameter *clath-app-address* "clath/")
(defparameter *logout-extension* "logout/")
(defun login-app (base-url app-address)
;;FIXME: Doesn't work first time this is called.
(initialize-secrets)
(let ((app (make-instance 'ningle:<app>)))
(dolist (pr (available-providers))
(let ((name (provider-url-string pr)))
(if (uses-north-p pr)
(progn
(setf (ningle:route
app (concatenate 'string "/" *login-extension-north* name)
:method :get)
(lambda (params)
(declare (ignore params))
(let ((*server-url* base-url)
(*clath-app-address* app-address))
(login-action-north pr))))
(setf (ningle:route
app
(concatenate 'string "/" *callback-extension-north* name)
:method :get)
(lambda (params)
(let ((*server-url* base-url)
(*clath-app-address* app-address))
(callback-action-north pr params #'logged-in)))))
(progn
(setf (ningle:route
app (concatenate 'string "/" *login-extension* name)
:method :get)
(lambda (params)
(declare (ignore params))
(let ((*server-url* base-url)
(*clath-app-address* app-address))
(login-action pr))))
(setf (ningle:route
app (concatenate 'string "/" *callback-extension* name)
:method :get)
(lambda (params)
(let ((*server-url* base-url)
(*clath-app-address* app-address))
(callback-action pr params #'logged-in))))))))
(setf (ningle:route app (concatenate 'string "/" *login-extension*)
:method :get)
(lambda (params)
(let ((*server-url* base-url)
(*clath-app-address* app-address))
(login-page params))))
(setf (ningle:route app (concatenate 'string "/" *logout-extension*)
:method :get)
(lambda (params)
(let ((*server-url* base-url)
(*clath-app-address* app-address))
(logout-page params))))
app))
(defun component (base-url &key (extension *clath-app-address*))
(lambda (app)
(let* ((server-url (concatenate 'string base-url extension))
(lapp (lack.component:to-app
(login-app server-url extension))))
(lambda (env)
(let ((*server-url* server-url)
(in-app
(under-path-p
(concatenate 'string "/" extension) (getf env :path-info))))
(if in-app
(funcall lapp (repath-clack-env env in-app))
(let* ((*clath-app-address* extension)
(res (funcall app env)))
(if (and (consp res) (eql 403 (car res)))
(not-logged-page env res)
res))))))))
(defun secrets-from-ubiquitous ()
(ubiquitous:restore 'clath)
(let ((providers (loop for (k . v) on *provider-info* by #'cddr collect k)))
(mapcan (lambda (pr)
(when (and (ubiquitous:value pr) (not (provider-disabled? pr)))
(list pr
(list* :client-id (ubiquitous:value pr :client-id)
:secret (ubiquitous:value pr :secret)
(when (ubiquitous:value pr :key)
(list :key (ubiquitous:value pr :key)))))))
providers)))
(defun initialize-secrets ()
(setf *provider-secrets* (secrets-from-ubiquitous))
t) ;;Don't dump the secrets to the console when called interactively
;;Run initialize-secrets to make an enable or disable come into effect
(defun disable-provider (pr)
(ubiquitous:restore 'clath)
(setf (ubiquitous:value pr :disabled) t))
(defun enable-provider (pr)
(ubiquitous:restore 'clath)
(ubiquitous:remvalue pr :disabled))
(defun provider-disabled? (pr)
(ubiquitous:restore 'clath)
(ubiquitous:value pr :disabled))
(defparameter *provider-icon-size* "25px")
(defparameter *provider-button-css*
"padding: 1px 10px;
border: 1px outset buttonborder;
border-radius: 3px;
color: buttontext;
background-color: buttonface;
text-decoration: none;
display: flex;
align-items: center;
width: 17rem;
height: 2.5rem;")
(defparameter *provider-container-css*
" .provider-container svg {
width:25px;
height:25px;
display:inline;
}")
(defun login-links ()
(with-html-output-to-string (s)
(:div
(dolist (pr (available-providers))
(when-let ((ifile (provider-icon pr)))
(htm (:div
:style "margin: 0.5rem;"
(:a :href (make-login-url pr)
:class "provider-container"
:style *provider-button-css*
(str (alexandria:read-file-into-string ifile))
(:span
:style "display: flex; justify-content: center; flex-grow: 1;"
(str (format nil "Sign in with ~a" (provider-string pr))))))))))))
(defun clath-page-wrapper (title body-func)
"Redefine this function to customize the look of all Clath pages."
(with-html-output-to-string (s)
(:html
(:head (:title title)
(:style :type "text/css" (str *provider-container-css*)))
(:body (str (funcall body-func))))))
(defun clath-login-page ()
"Redefine this function to customize the login page."
(clath-page-wrapper "Login"
(lambda ()
(with-html-output-to-string (s)
(:h2 "Choose a login provider")
(str (login-links))))))
(defun login-page (params)
"Internal portion of clath-login-page"
(when-let ((dest (assoc "destination" params :test #'equal)))
(setf (gethash :clath-destination (ningle:context :session)) (cdr dest)))
(clath-login-page))
(defun clath-not-logged-page ()
"Redefine this function to customize the not-logged-in page."
(clath-page-wrapper "Please Log In"
(lambda ()
(with-html-output-to-string (s)
(:h1 "Not logged in")
(:h2 "Choose a login provider")
(str (login-links))))))
(defun not-logged-page (env result)
"Internal portion of clath-not-logged-page"
(setf (gethash :clath-destination (getf env :lack.session))
(url-from-env env))
;; Result is the 403 page that came back from down-stack. We want to preserve the headers
;; while swapping out the page body.
(list
(car result)
(second result)
(list
;; Generated page may have headers. If so, just take the page text.
(let ((page (clath-not-logged-page)))
(if (listp page)
(car (third page))
page)))))
(defun clath-logout-page ()
"Redefine this function to customize the logout page."
(clath-page-wrapper "Logged Out"
(lambda ()
(with-html-output-to-string (s)
(:h1 "Logged out")))))
(defvar *in-logout-page* nil
"For code that implements login links. Self destination links should not be used on the logout page, lest the user get stuck in an auto-logout loop. This variable will be set when the logout page is being generated.")
(defun logout-page (params)
(declare (ignore params))
(logout-action)
(logged-out)
(let ((*in-logout-page* t))
(clath-logout-page)))
(defun logout-url ()
(concatenate 'string "/" *clath-app-address* *logout-extension*))
(defun login-url ()
(concatenate 'string "/" *clath-app-address* *login-extension*))
;;FIXME: :username and :display-name extraction are getting really messy.
;; Should split out into different methods for providers.
(defun logged-in ()
(let* ((uinfo (gethash :clath-userinfo (ningle:context :session)))
(uname (userinfo-get-user-id t uinfo)))
(unless uname
(if-let ((msg (assoc-cdr :message uinfo)))
(error (format nil "Message from OAuth server: ~a" msg))
(error "Couldn't find user ID in OAuth return data.")))
(setf (gethash :username (ningle:context :session))
(format nil "~a@~a" uname
(string-downcase
(gethash :clath-provider (ningle:context :session)))))
(setf (gethash :display-name (ningle:context :session))
(or (cdr (assoc-or
'(:preferred--username :nickname :login :name :display--name
:given--name :email)
uinfo))
(format nil "~a ~a"
(assoc-cdr :first-name uinfo)
(assoc-cdr :last-name uinfo))))))
(defun logged-out ()
(remhash :username (ningle:context :session))
(remhash :display-name (ningle:context :session)))