forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
74 lines (67 loc) · 1.96 KB
/
view.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
package key
import (
"github.com/jchavannes/jgo/jerr"
"github.com/jchavannes/jgo/web"
"github.com/memocash/memo/app/auth"
"github.com/memocash/memo/app/db"
"github.com/memocash/memo/app/res"
"encoding/base64"
"github.com/skip2/go-qrcode"
"net/http"
)
var viewKeyRoute = web.Route{
Pattern: res.UrlKeyExport,
NeedsLogin: true,
Handler: func(r *web.Response) {
user, err := auth.GetSessionUser(r.Session.CookieId)
if err != nil {
r.Error(jerr.Get("error getting session user", err), http.StatusInternalServerError)
return
}
key, err := db.GetKeyForUser(user.Id)
if err != nil {
r.Error(jerr.Get("error getting key for user", err), http.StatusInternalServerError)
return
}
r.Helper["Key"] = key
r.RenderTemplate(res.UrlKeyExport)
},
}
var loadKeyRoute = web.Route{
Pattern: res.UrlKeyLoad,
CsrfProtect: true,
NeedsLogin: true,
Handler: func(r *web.Response) {
user, err := auth.GetSessionUser(r.Session.CookieId)
if err != nil {
r.Error(jerr.Get("error getting session user", err), http.StatusInternalServerError)
return
}
id := r.Request.GetFormValueUint("id")
password := r.Request.GetFormValue("password")
dbPrivateKey, err := db.GetKey(uint(id), user.Id)
if err != nil {
r.Error(jerr.Get("error getting key", err), http.StatusInternalServerError)
return
}
privateKey, err := dbPrivateKey.GetPrivateKey(password)
if err != nil {
r.Error(jerr.Get("error unlocking private key", err), http.StatusUnauthorized)
return
}
r.Helper["PrivateKey"] = privateKey
var qr *qrcode.QRCode
qr, err = qrcode.New(privateKey.GetBase58Compressed(), qrcode.Medium)
if err != nil {
r.Error(jerr.Get("error generating qr", err), http.StatusInternalServerError)
return
}
png, err := qr.PNG(250)
if err != nil {
r.Error(jerr.Get("error generating png", err), http.StatusInternalServerError)
return
}
r.Helper["QR"] = base64.StdEncoding.EncodeToString(png)
r.RenderTemplate(res.UrlKeyLoad)
},
}