-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.osl
More file actions
174 lines (141 loc) · 3.92 KB
/
handlers.osl
File metadata and controls
174 lines (141 loc) · 3.92 KB
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
def handleHealth(*gin.Context c) (
c.JSON(200, { ok: true })
)
def handleStatus(*gin.Context c) (
string token = getCookie(c, "auth_token")
if token == "" (
c.JSON(200, { authenticated: false, setup: false })
return
)
boolean hasStorage = storageExists(token)
c.JSON(200, { authenticated: true, setup: hasStorage })
)
def handleCheckSetup(*gin.Context c) (
string token = getCookie(c, "auth_token")
if token == "" (
c.JSON(200, { setup: false })
return
)
boolean hasStorage = storageExists(token)
c.JSON(200, { setup: hasStorage })
)
def handleVerifyParams(*gin.Context c) (
string token = getCookie(c, "auth_token")
if token == "" (
c.JSON(401, { error: "not authenticated" })
return
)
if !storageExists(token) (
c.JSON(200, { setup: false })
return
)
string salt = getSalt(token)
string verifier = getVerifier(token)
number iterations = getIterations(token)
c.JSON(200, { salt: salt, verifier: verifier, iterations: iterations })
)
def handleSession(*gin.Context c) (
string token = getCookie(c, "auth_token")
if token == "" (
c.JSON(401, { ok: false })
return
)
string session = generateSessionToken()
setSession(token, session)
c.SetCookie("session_token", session, 14400, "/", "", false, true)
c.JSON(200, { ok: true })
)
def handleSetup(*gin.Context c) (
string token = getCookie(c, "auth_token")
if token == "" (
c.JSON(401, { ok: false, error: "not authenticated" })
return
)
if storageExists(token) (
c.JSON(400, { ok: false, error: "already setup" })
return
)
object body = {}
any err = c.ShouldBindJSON(@body)
if err != null (
c.JSON(400, { ok: false, error: "invalid request" })
return
)
string salt = body.salt.toStr()
string verifier = body.verifier.toStr()
number iterations = body.iterations.toNum()
if iterations == 0 (
iterations = 100000
)
if salt == "" or verifier == "" (
c.JSON(400, { ok: false, error: "missing salt or verifier" })
return
)
err = createInitialStorage(token, salt, verifier, iterations)
if err != null (
c.JSON(500, { ok: false, error: "failed to create storage" })
return
)
string session = generateSessionToken()
setSession(token, session)
c.SetCookie("session_token", session, 14400, "/", "", false, true)
c.JSON(200, { ok: true })
)
def handleGetEntries(*gin.Context c) (
string token = c.MustGet("auth").toStr()
string salt = getSalt(token)
if salt == "" (
c.JSON(500, { ok: false, error: "storage error" })
return
)
number iterations = getIterations(token)
array entries = getEntries(token)
c.JSON(200, { ok: true, salt: salt, iterations: iterations, entries: entries })
)
def handleAddEntry(*gin.Context c) (
string token = c.MustGet("auth").toStr()
object body = {}
any err = c.ShouldBindJSON(@body)
if err != null (
c.JSON(400, { ok: false, error: "invalid request" })
return
)
string id = crypto.uuidv4()
if body["id"] != null (
id = body["id"].toStr()
)
object entry = {
id: id,
nonce: body["nonce"].toStr(),
ciphertext: body["ciphertext"].toStr()
}
err = addEntry(token, entry)
if err != null (
c.JSON(500, { ok: false, error: "failed to add entry" })
return
)
c.JSON(200, { ok: true, id: id })
)
def handleDeleteEntry(*gin.Context c) (
string token = c.MustGet("auth").toStr()
string id = c.Query("id")
if id == "" (
c.JSON(400, { ok: false, error: "missing id" })
return
)
any err = deleteEntry(token, id)
if err != null (
c.JSON(500, { ok: false, error: "failed to delete entry" })
return
)
c.JSON(200, { ok: true })
)
def handleLogout(*gin.Context c) (
c.SetCookie("session_token", "", -1, "/", "", false, true)
c.SetCookie("auth_token", "", -1, "/", "", false, true)
c.Redirect(302, "/")
)
def handleGetSize(*gin.Context c) (
string token = c.MustGet("auth").toStr()
c.JSON(200, { size: getStorageSize(token) })
)