-
Notifications
You must be signed in to change notification settings - Fork 1
/
link.go
385 lines (330 loc) · 9.3 KB
/
link.go
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
package views
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"path"
"time"
petname "github.com/dustinkirkland/golang-petname"
"github.com/go-chi/chi"
"github.com/spf13/viper"
"github.com/tnyie/tny-api/models"
"github.com/tnyie/tny-api/util"
)
const (
LEASE_TIME = time.Hour * 24 * 30 * 6
)
// CreateLink creates a new link
func CreateLink(w http.ResponseWriter, r *http.Request) {
var link models.Link
bd, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Couldn't read json body when creating link\n", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
err = json.Unmarshal(bd, &link)
if err != nil {
log.Println("malformed create query\n", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
user, authenticated, admin := util.CheckLogin(r, "")
if !authenticated && !admin {
link.OwnerID = ""
} else {
link.OwnerID = user.UID
}
if link.OwnerID == "" || link.Slug == "" {
tryNew:
link.Slug = petname.Generate(2, "-")
// if link exists, and no error, try a new petname
if err = link.GetBySlug(); err == nil {
goto tryNew
}
}
// set to zero, so GORM can set it to current time
link.UpdatedAt, link.CreatedAt = 0, 0
link.Lease = time.Now().Add(LEASE_TIME).Unix()
err = link.Create()
if err != nil {
http.Error(w, http.StatusText(http.StatusConflict), http.StatusConflict)
log.Println(err)
return
}
encoded, err := json.Marshal(link)
if err != nil {
log.Println("Couldn't parse link as json\n", err)
w.WriteHeader(http.StatusInternalServerError)
}
log.Println("New link created by UID: ", link.OwnerID, " with Slug: ", link.Slug)
respondJSON(w, encoded, http.StatusCreated)
}
// RedirectSlug takes the link's slug as a parameter, and redirects request
func RedirectSlug(w http.ResponseWriter, r *http.Request) {
var link models.Link
link.Slug = chi.URLParam(r, "slug")
err := link.GetBySlug()
if err != nil {
log.Println("Search error\n", err)
w.WriteHeader(http.StatusNotFound)
}
curr_time := time.Now().Unix()
if link.URL != "" {
if link.Lease == 0 || link.Lease > curr_time {
if link.UnlockTime < curr_time {
if link.Password != "" {
log.Println("Link requires password, passing to other handler")
http.Redirect(w, r,
"https://"+viper.GetString("tny.ui.url")+"/redirect/"+link.Slug,
http.StatusTemporaryRedirect,
)
return
}
log.Println("Redirecting to", link.URL)
http.Redirect(w, r, link.URL, http.StatusTemporaryRedirect)
setVisit(&link)
return
} else {
log.Println("Link not unlocked yet")
log.Println(link.UnlockTime, " < ", curr_time)
w.WriteHeader(404)
return
}
} else {
log.Println("Link lease expired")
w.WriteHeader(404)
}
} else {
log.Println("Link corrupted")
w.WriteHeader(404)
}
}
// GetLink returns a link object
func GetLinkFromSlug(w http.ResponseWriter, r *http.Request) {
var link models.Link
link.Slug = chi.URLParam(r, "slug")
err := link.GetBySlug()
if err != nil {
w.WriteHeader(http.StatusNotFound)
log.Println("Error getting link from id\n", err)
return
}
_, authorized, admin := util.CheckLogin(r, link.OwnerID)
if !authorized && !admin {
w.WriteHeader(http.StatusUnauthorized)
log.Println("user not authorized to access link object")
return
}
encoded, err := json.Marshal(&link)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println("Failed to marshal link to json\n", err)
return
}
respondJSON(w, encoded, http.StatusOK)
}
func GetLink(w http.ResponseWriter, r *http.Request) {
var link models.Link
link.ID = chi.URLParam(r, "id")
err := link.Get()
if err != nil {
w.WriteHeader(http.StatusNotFound)
log.Println("Error getting link from id\n", err)
return
}
_, authorized, admin := util.CheckLogin(r, link.OwnerID)
if !authorized && !admin {
w.WriteHeader(http.StatusUnauthorized)
log.Println("user not authorized to access link object")
return
}
encoded, err := json.Marshal(&link)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println("Failed to marshal link to json\n", err)
return
}
respondJSON(w, encoded, http.StatusOK)
}
// GetAuthenticatedLink returns the
func GetAuthenticatedLink(w http.ResponseWriter, r *http.Request) {
var link models.Link
link.Slug = chi.URLParam(r, "slug")
err := link.GetBySlug()
if err != nil {
log.Println("Search error\n", err)
w.WriteHeader(http.StatusNotFound)
return
}
curr_time := time.Now().Unix()
if link.URL != "" {
if link.Lease == 0 || link.Lease > curr_time {
if link.UnlockTime < curr_time {
if link.Password != "" {
jsonMap := make(map[string]interface{})
bd, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Error reading json body")
return
}
err = json.Unmarshal(bd, &jsonMap)
if err != nil {
log.Println("Couldn't parse json body")
return
}
if jsonMap["password"] != "" {
if jsonMap["password"] == link.Password {
encoded, err := json.Marshal(&models.GenericResponse{Data: link.URL})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println("Couldn't generate response")
return
}
respondJSON(w, encoded, http.StatusOK)
setVisit(&link)
return
}
w.WriteHeader(http.StatusUnauthorized)
log.Println("incorrect password")
return
}
w.WriteHeader(http.StatusUnauthorized)
log.Println("no password provided for authenticated link")
return
}
w.WriteHeader(http.StatusUnauthorized)
log.Println("Link has no password")
return
}
log.Println("link not unlocked yet")
w.WriteHeader(http.StatusFound)
return
}
log.Println("Link lease exceeded")
}
log.Println("link is invalid")
// if link is missing url or has expired, 404
w.WriteHeader(http.StatusNotFound)
}
// setVisit updates visits for a given link object in database
func setVisit(link *models.Link) {
visit := &models.Visit{LinkID: link.ID}
err := visit.Create()
if err != nil {
log.Println("Couldn't create visit obj\n", err)
}
link.Visits += 1
err = link.Update()
if err != nil {
log.Println("Couldnt update link\n", err)
}
}
// GetLinksByUser checks for authorized user and returns all links owned by user
func GetLinksByUser(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
_, authorized, admin := util.CheckLogin(r, id)
if !authorized && !admin {
log.Println("User unauthorized to access resource")
w.WriteHeader(http.StatusUnauthorized)
return
}
links, err := models.GetLinksByUser(id)
if err != nil {
log.Println("Error getting user links\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
encoded, err := json.Marshal(links)
if err != nil {
log.Println("Error marshalling data\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
respondJSON(w, encoded, http.StatusOK)
}
// UpdateLinkLease updates link lease time if user is
// authenticated, link is expired, and user owns resource
func UpdateLinkLease(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
log.Println("Updating link lease for link id ", id)
link := &models.Link{ID: id}
err := link.Get()
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
_, authorized, admin := util.CheckLogin(r, link.OwnerID)
if !authorized && !admin {
w.WriteHeader(http.StatusUnauthorized)
log.Println("User is not authorized to modify link lease")
return
}
err = link.Put(link.OwnerID, "lease", time.Now().Add(time.Hour*24*30).Unix())
if err != nil {
w.WriteHeader(http.StatusConflict)
log.Println("Error updating link lease\n", err)
return
}
w.WriteHeader(http.StatusOK)
}
// PutLinkAttribute updates a given attribute
func PutLinkAttribute(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
log.Println("Updating link of id ", id)
link := &models.Link{ID: id}
err := link.Get()
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
_, authorized, admin := util.CheckLogin(r, link.OwnerID)
if !authorized && !admin {
w.WriteHeader(http.StatusUnauthorized)
return
}
log.Println("Updating link ", link)
data := make(map[string]interface{})
bd, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Couldn't ready json body\n", err)
w.WriteHeader(http.StatusBadRequest)
return
}
if err = json.Unmarshal(bd, &data); err != nil {
log.Println(string(bd))
log.Println("Couldn't unmarhsal json\n", err)
w.WriteHeader(http.StatusBadRequest)
return
}
switch path.Base(r.URL.Path) {
case "url":
err = link.Put(link.OwnerID, "url", data["url"])
}
if err != nil {
log.Println(err)
}
encoded, err := json.Marshal(link)
if err != nil {
log.Println("Error updating link")
w.WriteHeader(http.StatusInternalServerError)
}
respondJSON(w, encoded, http.StatusOK)
}
// DeleteLink deletes a given link
func DeleteLink(w http.ResponseWriter, r *http.Request) {
link := &models.Link{ID: chi.URLParam(r, "id")}
link.Get()
userAuth, authorized, admin := util.CheckLogin(r, link.OwnerID)
if admin || authorized && link.OwnerID != "" {
link.Delete()
w.WriteHeader(http.StatusAccepted)
return
}
if link.OwnerID == "" || link.OwnerID != userAuth.UID {
w.WriteHeader(http.StatusUnauthorized)
return
}
w.WriteHeader(http.StatusUnauthorized)
}