forked from shvetsm/funcade
/
tools.clj
74 lines (60 loc) · 1.97 KB
/
tools.clj
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
(ns funcade.tools
(:require [jsonista.core :as json]
[clojure.string :as s]
[jsonista.core :as j])
(:import [org.apache.commons.codec.binary Base64]
[java.time Instant]))
(def mapper (j/object-mapper {:decode-key-fn keyword}))
(defn auth-header
([token]
(auth-header token "Bearer"))
([token prefix]
{"Authorization" (str prefix " " token)}))
(defn decode64 [xs]
(-> xs Base64/decodeBase64 String.))
(defn decode-jwt [token]
(->> (s/split token #"\.")
(take 2) ;; don't decode the signature (for now)
(map (comp json/read-value decode64))))
(defn value? [v]
(or (number? v)
(seq v)))
(defn value [v]
(when (value? v)
v))
(defn ts->seconds [ts]
(some->> ts
str ;; some return string, some return long
(take 10) ;; some return in ms, some in seconds
(apply str)
Long/valueOf))
(defn to-seconds [vs]
(let [now (.toEpochMilli (Instant/now))]
(-> (or (first (filter value vs)) ;; if any has value take that
now) ;; otherwise take "now"
ts->seconds)))
(defn timebox
"some systems return issued, some issued-at, some iat, etc.
some do not return expires/expires-at/exp
some return ts is seconds, others in ms
timebox picks values if provided,
compliments with values that are not provided
and assocs issued-at and expires-at in seconds"
[{:keys [issued issued-at iat expires expires-at exp] :as t}]
(let [issued-at (to-seconds [iat issued issued-at])
expires-at (to-seconds [exp expires expires-at (+ issued-at 3600)])] ;; 1 hour in case expires-at is missing
(-> t
(assoc :issued-at issued-at)
(assoc :expires-at expires-at))))
(defn fmv
"apply f to each value v of map m"
[m f]
(into {}
(for [[k v] m]
[k (f v)])))
(defn fmk
"apply f to each key k of map m"
[m f]
(into {}
(for [[k v] m]
[(f k) v])))