forked from sqrldev/server-go-ssp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handers.go
196 lines (174 loc) · 4.85 KB
/
handers.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
package ssp
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
qrcode "github.com/skip2/go-qrcode"
)
type nutJSON struct {
Nut Nut `json:"nut"`
Pagnut Nut `json:"pag"`
Expiration int `json:"exp"`
}
// Nut implements the /nut.sqrl endpoint
// TODO sin, ask and 1-9 params
func (api *SqrlSspAPI) Nut(w http.ResponseWriter, r *http.Request) {
hoardCache, err := api.createAndSaveNut(r)
if err != nil {
log.Print(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if r.Header.Get("Accept") == "application/json" {
w.Header().Add("Content-Type", "application/json")
respObj := &nutJSON{
Nut: hoardCache.OriginalNut,
Pagnut: hoardCache.PagNut,
Expiration: api.NutExpirationSeconds(),
}
enc, err := json.Marshal(respObj)
if err != nil {
log.Printf("Failed json encode: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(enc)
return
}
w.Header().Add("Content-Type", "application/x-www-form-urlencoded")
values := make(url.Values)
values.Add("nut", string(hoardCache.OriginalNut))
values.Add("pag", string(hoardCache.PagNut))
values.Add("exp", fmt.Sprintf("%d", api.NutExpirationSeconds()))
if referer := r.Header.Get("Referer"); referer != "" {
values.Add("can", Sqrl64.EncodeToString([]byte(referer)))
}
_, err = w.Write([]byte(values.Encode()))
if err != nil {
log.Printf("Nut response write error: %v", err)
}
}
func (api *SqrlSspAPI) createAndSaveNut(r *http.Request) (*HoardCache, error) {
nut, err := api.tree.Nut()
if err != nil {
return nil, fmt.Errorf("Failed generating nut: %v", err)
}
pagnut, err := api.tree.Nut()
if err != nil {
return nil, fmt.Errorf("Failed generating nut: %v", err)
}
hoardCache := &HoardCache{
State: "issued",
RemoteIP: api.RemoteIP(r),
OriginalNut: nut,
PagNut: pagnut,
}
// store the nut in the hoard
api.hoard.Save(nut, hoardCache, api.NutExpiration)
if err != nil {
return nil, fmt.Errorf("Failed to save a nut: %v", err)
}
log.Printf("Saved nut %v in hoard from %v", nut, hoardCache.RemoteIP)
return hoardCache, nil
}
func (api *SqrlSspAPI) getAndDelete(nut Nut) (*HoardCache, error) {
hoardCache, err := api.hoard.GetAndDelete(Nut(nut))
if err != nil {
return nil, err
}
return hoardCache, nil
}
// PNG implements the /png.sqrl endpoint
func (api *SqrlSspAPI) PNG(w http.ResponseWriter, r *http.Request) {
nut := r.URL.Query().Get("nut")
var hoardCache *HoardCache
var err error
if nut == "" {
// create a nut
hoardCache, err = api.createAndSaveNut(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
nut = string(hoardCache.OriginalNut)
}
params := make(url.Values)
params.Add("nut", nut)
sqrlURL := &url.URL{
Scheme: SqrlScheme,
Host: api.Host(r),
Path: fmt.Sprintf("%v/cli.sqrl", api.RootPath),
RawQuery: params.Encode(),
}
value := sqrlURL.String()
png, err := qrcode.Encode(value, qrcode.Medium, -5)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed create of PNG"))
return
}
if hoardCache != nil {
w.Header().Add("Sqrl-Nut", string(hoardCache.OriginalNut))
w.Header().Add("Sqrl-Pag", string(hoardCache.PagNut))
w.Header().Add("Sqrl-Exp", fmt.Sprintf("%d", api.NutExpirationSeconds()))
}
w.Header().Add("Content-Type", "image/png")
w.Write(png)
}
type pagJSON struct {
URL string `json:"url"`
}
// Pag implements the /pag.sqrl endpoint
func (api *SqrlSspAPI) Pag(w http.ResponseWriter, r *http.Request) {
nut := r.URL.Query().Get("nut")
if nut == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Missing required nut parameter"))
return
}
pagnut := r.URL.Query().Get("pag")
if pagnut == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Missing required pag parameter"))
return
}
hoardCache, err := api.getAndDelete(Nut(pagnut))
if err != nil {
if err == ErrNotFound {
w.WriteHeader(http.StatusNotFound)
return
}
log.Printf("Failed nut lookup: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed nut lookup"))
return
}
if hoardCache.OriginalNut != Nut(nut) {
log.Printf("Got query for pagnut but original nut doesn't match")
w.WriteHeader(http.StatusUnauthorized)
return
}
if hoardCache.Identity == nil {
log.Printf("Nil identity on pag hoardCache")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Missing identity"))
return
}
if r.Header.Get("Accept") == "application/json" {
w.Header().Add("Content-Type", "application/json")
respObj := &pagJSON{
URL: api.Authenticator.AuthenticateIdentity(hoardCache.Identity),
}
enc, err := json.Marshal(respObj)
if err != nil {
log.Printf("Failed json encode: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(enc)
return
}
w.Write([]byte(api.Authenticator.AuthenticateIdentity(hoardCache.Identity)))
}