aboutsummaryrefslogtreecommitdiff
path: root/src/main.lisp
blob: ca44e96745249e871e116486cf1ee42c9ce0dddd (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
(in-package :oneliners.api)

;;; DATA DEFINITIONS

(defclass invite (db:store-object)
  ((code
    :reader invite-code
    :initform (uuid)
    :index-type bknr.indices:string-unique-index
    :index-reader invite-by-code)
   (from
    :reader invite-from
    :initarg :from)
   (created-at
    :reader created-at
    :initform (get-universal-time)))
  (:metaclass db:persistent-class))

(defclass contributor (db:store-object)
  ((handle
    :accessor contributor-handle
    :initarg :handle
    :initform (error "Contributors must have a name.")
    :index-type bknr.indices:string-unique-index
    :index-reader contributor-by-handle)
   (salt
    :reader contributor-salt
    :initform (uuid)
    :type string
    :documentation "Per user salt for password hashing.")
   (hashed-pw
    :accessor hashed-pw
    :initform nil
    :type string
    :documentation "Hashed password for this contributor. Note, this
    value is hashed with the server salt and the contributor-salt.")
   (adminp
    :accessor adminp
    :initform nil
    :documentation "indicates whether or not this contributor has admin privileges."))
  (:metaclass db:persistent-class))

(defparameter +auth-cookie-name+ "olauthtoken")

(defclass api-access (db:store-object)
  ((token
    :reader api-token
    :initform (uuid)    
    :index-type bknr.indices:string-unique-index
    :index-reader access-by-token)
   (contributor
    :reader api-contributor
    :initarg :contributor
    :index-type bknr.indices:unique-index
    :index-reader access-by-contributor))
  (:metaclass db:persistent-class))

(defgeneric revoke-access (what)
  (:documentation "Effectively deletes an api-access instance.")
  (:method ((access api-access))
    (db:with-transaction ()
      (db:delete-object access)))
  (:method ((token string))
    (a:when-let ((access (access-by-token token)))
      (revoke-access access)))
  (:method ((contributor contributor))
    (a:when-let ((access (access-by-contributor contributor)))
      (revoke-access access))))

(defclass oneliner (db:store-object)
  ((oneliner
    :accessor oneliner 
    :initarg :oneliner
    :initform (error "Onliner required"))
   (tags
    :accessor oneliner-tags
    :initarg :tags
    :initform nil
    :index-type bknr.indices:hash-list-index 
    :index-initargs (:test 'equal)
    :index-reader oneliners-by-tag
    :documentation "The commands that this oneliner principally involves.")
   (brief
    :accessor oneliner-brief
    :initarg :brief
    :initform (error "Oneliners need a brief title")
    :documentation "A short description of the oneliner.")
   (description
    :accessor oneliner-description
    :initarg :description
    :initform "")
   (created-by
    :reader created-by
    :initform (error "oneliners must be made by a contributor")
    :initarg :created-by)
   (created-at
    :reader created-at
    :initform (get-universal-time))
   (edited-at
    :accessor edited-at
    :initform nil
    :documentation "A universal time recording the last time of edit")
   (last-edited-by
    :accessor last-edited-by
    :initform nil
    :documentation "a contributor instance, the last person to edit thiscommand.")
   (flagged-by
    :accessor flagged-by
    :initform nil
    :documentation "NIL or :anonymous or a CONTRIBUTOR object.")
   (lockedp
    :accessor lockedp
    :initform nil
    :documentation "Prevents editing until unliked. Only users with
    admin priviliges can lock/unlock."))
  (:metaclass db:persistent-class))

(defmethod json:%to-json ((instance oneliner))
  (with-slots
        (db::id oneliner tags brief description
         created-at edited-at last-edited-by created-by
         flagged-by audited-by lockedp)
      instance
    (json:with-object
      (json:write-key-value "id" db::id)
      (json:write-key-value "oneliner" oneliner)
      (json:write-key-value "tags" tags)
      (json:write-key-value "brief" brief)
      (json:write-key-value "description" description)
      (json:write-key-value "createdAt" created-at)
      (json:write-key-value "editedAt" (if edited-at edited-at :null))
      (json:write-key-value "createdBy" (contributor-handle created-by))
      (json:write-key-value "isFlagged" (if (not (null flagged-by)) t :false))
      (json:write-key-value "isLocked" (if lockedp t :false)))))



;;; SERVICE CONTROL

(defvar *server* nil)
(defvar *cleaning-thread* nil)
(defvar *runningp* nil)
(defvar *instance-salt* "change me"
  "This is salt used for password hashing and login recovery")

(defparameter +data-store-directory-name+
  "oneliners-api-datastore")

(defun data-store-path (store-dir)
  (let ((store-dir (or store-dir (pathname-directory (user-homedir-pathname)))))
    (make-pathname
     :directory (append store-dir (list +data-store-directory-name+)))))

(defun initialize-datastore (store-dir)
  (ensure-directories-exist (data-store-path store-dir))
  (make-instance
   'db:mp-store
   :directory (data-store-path store-dir)
   :subsystems (list (make-instance 'db:store-object-subsystem))))

(defun ensure-datastore (store-dir)
  (unless (boundp 'db:*store*)
    (initialize-datastore store-dir)))

(defun ensure-server (port address)
  (unless *server*
    (setf *server* (lzb:create-server :port port :address address))
    (set-canned-responses)))

(defun set-canned-responses ()
  (lzb:set-canned-response *server* 400 "Bad Request" "text/plain")
  (lzb:set-canned-response *server* 401 "Unauthorized" "text/plain")
  (lzb:set-canned-response *server* 403 "Forbidden" "text/plain")
  (lzb:set-canned-response *server* 404 "Not Found" "text/plain")
  (lzb:set-canned-response *server* 500 "Server Error" "text/plain"))

(defun start
    (&key
       (port 8888)
       (address "127.0.0.1")
       (salt "change me")
       store-dir)
  (setf *instance-salt* salt )
  (ensure-datastore store-dir)
  (ensure-server port address)
  (lzb:install-app *server* (lzb:app))
  (lzb:start-server *server*)
  (setf *runningp* t)
  (start-cleaning-thread))


(defun start-cleaning-thread (&key (run-period 3600))
  ;; when the thread  was stopped properly.
  (when (and *cleaning-thread* (bt:thread-alive-p *cleaning-thread*))
    (bt:destroy-thread *cleaning-thread*))
  (setf *cleaning-thread*
        (bt:make-thread
         (lambda ()
           (loop while *runningp*
                 do (sleep run-period)
                    (handler-case (routine-cleaning)
                      (error (e) (print e))))))))

(defun stop ()
  (setf *runningp* nil)
  (when *server*
    (lzb:stop-server *server*)))

(defparameter +invite-lifetime+ (* 60 60 24)
  "Invites expire after 24 hours")

(defun expired-invite-p (invite &optional (current-time (get-universal-time)))
  (> (- current-time (created-at invite))
     +invite-lifetime+))

(defun routine-cleaning ()
  (let ((now (get-universal-time))) 
    (a:when-let (expired-invites
                 (remove-if-not #$(expired-invite-p $invite now) (db:store-objects-with-class 'invite)))
      (db:with-transaction ()
        (mapc #'db:delete-object expired-invites)))))


;;; API DEFINITION AND PROVISIONING

(defparameter +oneliners-description+
  "TBD")

(lzb:provision-app ()
  :title "Oneliners Wiki API"
  :version "0.0.1"
  :desc +oneliners-description+
  :content-type "application/json"
  :auth 'api-token-authorization)

(defun api-token-authorization ()
  ;; presently if the token merely exists then that's good enough.
  (request-contributor)) 


;;; DATABASE TRANSACTIONS

(defun redeem-invite (invite handle password)
  (db:with-transaction ()
    (with-slots (salt hashed-pw)  (make-instance 'contributor :handle handle)
      (setf hashed-pw (pw-hash password salt)))
    (db:delete-object invite)))


(defun make-api-access (contributor)
  (db:with-transaction ()
    (make-instance 'api-access :contributor contributor)))

(defun make-new-oneliner (contributor plist)
  (with-plist
      (oneliner tags brief description) plist
      (unless brief
        (http-err 400 "Oneliner requires a breif description"))
      (unless oneliner
        (http-err 400 "Oneliner cannot be blank"))

      (db:with-transaction ()
        (make-instance 'oneliner
                       :created-by contributor
                       :description (or description "")
                       :tags tags
                       :oneliner oneliner
                       :brief brief))))

(defun query-oneliners (&key tags notflagged limit)
  ;; inefficient but easy to express 
  (let ((ols (oneliners-with-all-tags tags)))
    (a:subseq* (if notflagged
                   (remove-if #'flagged-by ols)
                   ols)
               0 (or limit 10))))


;;; ROUTE VARIABLE AND PARAMATER PARSERS

(defun an-int (string)
  "An Integer"
  (parse-integer string))

(defun a-string (string)
  "A String"
  string)

(defun a-csl (s)
  "A list of strings separated by commas. e.g. \"foo,bar,goo\""
  (mapcar #'str:trim (str:split "," s)))

(defun a-boolean (s)
  "Either \"true\" or \"false\"/"
  (cond ((string-equal s "true") t)
        ((string-equal s "false") nil)
        (t (error "String ~s is neither 'true' nor 'false'" s))))

(defun a-page-key (key)
  "A page key"
  key)

(defun an-invite-code (code)
  "An invite code."
  (a:if-let (invite (invite-by-code code))
    invite
    (http-err 404)))

(defun a-user-handle (handle)
  "A Contributor's Handle"
  (a:if-let (contributor (contributor-by-handle handle))
    contributor
    (http-err 404)))

(defun a-short-string (string)
  "A string at most 50 characters in  length"
  (when (< 50 (length string))
    (http-err 400 "String Too Long"))
  string)


;;; ENDPOINT DEFINITIONS

(defendpoint* :post "/redeem/:invite an-invite-code:"
  ((username a-string)
   (password1 a-string)
   (password2 a-string))
  ()
  "Redeem an invite code"
  (unless (equal password1 password2)
    (http-err 400 "Passwords dont match"))
  (when (contributor-by-handle username)
    (http-err 403 (format nil "The name ~a is already taken." username)))
  (redeem-invite invite username password1)
  "true")

(defendpoint* :post "/request-token/:contributor a-user-handle:"
  ((password a-string))
  ()
  "Authenticate a user and reply with an api token"
  (cond ((equal (pw-hash password (contributor-salt contributor))
                (hashed-pw contributor))
         (to-json 
          (a:if-let (access (access-by-contributor contributor))
            (list :token (api-token access))         ; return extant tokens
            (list :token (api-token (make-api-access contributor)))))) ; or make a new one 
        (t
         (http-err 401))))

(defendpoint* :post "/add-oneliner" ()
  (:auth t)
  (make-new-oneliner (request-contributor)  (lzb:request-body))
  "true")

(defendpoint* :get "/search" ((tags a-csl)
                              (limit an-int)
                              (notflagged a-boolean))
  ()
  "A search endpoint returning a JSON encoded array of Oneliner Entries.

**Note**: either command or keywords are required.
"
  (if tags
      (to-json
       (list :oneliners (query-oneliners :tags tags
                                         :notflagged notflagged
                                         :limit limit))) 

      (t                                  ; else responde with 400
       (http-err 400))))

;;; HELPERS

(defun slot-name-of (class name)
  "Returns the symbol naming a slot in the class class. Returns NIL if
there is no such slot.  Useful for converting keywords into slot
names.  NAME must be a symbol or a string."
  (assert (or (stringp name) (symbolp name)))
  (let ((name (if (symbolp name) (symbol-name name) name)))
    (loop for slot-def in (closer-mop:class-slots (find-class class))
          for slot-name = (closer-mop:slot-definition-name slot-def)
          when (string-equal name (symbol-name slot-name))
            return slot-name)))

(defun initarg-keyword (thing)
  (a:make-keyword
   (string-upcase
    (if (symbolp thing) (symbol-name thing) thing))))

(defun json-plist->initarg-keywords (plist)
  (loop for (k v . more) on plist by #'cddr
        collect (initarg-keyword k)
        collect v))

(defun object-with-id (id-string)
  "Integer id of the desired entity.."
  (db:store-object-with-id (parse-integer id-string)))

(defparameter +updatable-oneliner-slot-keywords+
  '(:|oneliner| :|commands| :|brief| :|description|))

(defun valid-oneliner-update-data-p (jsonplist)
  "Checks the fields of jsonplist, return t if they are sufficient to update a oneliner entry."
  (loop for (k v . more) on jsonplist by #'cddr
        always (member k +updatable-oneliner-slot-keywords+)))

(defun update-oneliner (contributor oneliner json-body)
  "Accepts a decoded json body, a plist, and "
  (assert (valid-oneliner-update-data-p json-body))
  (db:with-transaction ()
    (loop for (k v . more) on json-body
          do (setf (slot-value oneliner (slot-name-of 'oneliner k)) v))
    (setf (edit-history oneliner) (get-universal-time))))

(defun valid-oneliner-init-data-p (plist)
  "dchecks the fields in plist,returns t if they are sufficient to create a new oneliner."
  ;; right now, just aliasing valid-oneliner-update-data-p
  (valid-oneliner-update-data-p plist))

(defun add-oneliner-to-db (contributor json-plist)
  "adds a new oneliner to the database, returning it upon success "
  (assert (valid-oneliner-init-data-p json-plist))
  (db:with-transaction ()
    (apply 'make-instance 'oneliner
           :created-at (get-universal-time)
           :created-by contributor
           (json-plist->initarg-keywords json-plist))))

(defun pw-hash (plaintext salt)
  "Hash plaintext using SALT and the value of *INSTANCE-SALT*"
  (flexi-streams:octets-to-string 
   (ironclad:digest-sequence
    :sha3
    (flexi-streams:string-to-octets (concatenate 'string *instance-salt* salt plaintext)
                                    :external-format :utf-8))
   :external-format :latin1))

(defun uuid ()
  (format nil "~a" (uuid:make-v1-uuid)))


(defun oneliner-mentions-any (ol keywords)
  "A case insensitive search for the presence of any of KEYWORDS in the oneliner OL."
  (with-slots (text breif description) ol
    (loop
      for word in keywords
      thereis (search word text :test #'char-equal)
      thereis (search word breif :test #'char-equal)
      thereis (search word description :test #'char-equal))))


(defun oneliners-with-all-tags (tags)
  (reduce #'intersection (mapcar #'oneliners-by-tag tags)))


(defun to-json (thing)
  (let ((jonathan:*false-value* :false)
        (jonathan:*null-value* :null))
    (jonathan:to-json thing)))

(defun request-contributor ()
  (a:when-let (access (access-by-token (lzb:request-cookie +auth-cookie-name+)))
    (api-contributor access)))