-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
57 lines (45 loc) · 1.23 KB
/
server.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
package main
import (
"encoding/base64"
"net/http"
"strconv"
"strings"
"time"
)
func (db *Db) handler(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
otp := r.URL.Query().Get("otp")
nonce := r.URL.Query().Get("nonce")
if id == "" || otp == "" || nonce == "" {
db.reply(w, MISSING_PARAMETER, "", "", "")
return
}
err := db.eatToken(otp)
if err != nil {
db.reply(w, err.Error(), id, otp, nonce)
return
}
db.reply(w, OK, id, otp, nonce)
}
func (db *Db) reply(w http.ResponseWriter, status, id, otp, nonce string) {
var keyring *Keyring
params := []string{}
if status != MISSING_PARAMETER {
if id64, err := strconv.ParseInt(id, 10, 64); err == nil {
if keyring, err = db.getKeyring(id64); err == nil {
params = append(params, "nonce="+nonce)
params = append(params, "otp="+otp)
} else {
status = NO_SUCH_CLIENT
}
} else {
status = NO_SUCH_CLIENT
}
}
params = append(params, "status="+status)
params = append(params, "t="+time.Now().Format(time.RFC3339))
if status != MISSING_PARAMETER && status != NO_SUCH_CLIENT {
params = append(params, "h="+base64.StdEncoding.EncodeToString(hmacSign(params, keyring.ApiKey)))
}
w.Write([]byte(strings.Join(params, "\n")))
}