This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
/
auth.go
87 lines (82 loc) · 1.77 KB
/
auth.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
package main
/*
* auth.go
* Handle authentication
* By J. Stuart McMurray
* Created 20160108
* Last Modified 20160108
*/
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
/* getToken gets the authentication token for the given username and
password */
func getToken(u, p string) (string, error) {
/* Send the request */
res, err := http.PostForm(authURL, url.Values(map[string][]string{
"email": {u},
"password": {p},
}))
if nil != err {
return "", err
}
defer res.Body.Close()
/* Read body into buffer */
buf := &bytes.Buffer{}
_, err = io.Copy(buf, res.Body)
if nil != err {
return "", err
}
/* If we're not authorized, :( */
if "Unauthorized" == buf.String() {
return "", fmt.Errorf("unauthorized")
}
/* Struct into which to decode token */
rs := struct {
Ok int
Token string
}{}
/* Unmarshal the JSON */
if err := json.Unmarshal(buf.Bytes(), &rs); nil != err {
return "", err
}
return rs.Token, nil
}
/* getUID gets the user's UID for the websocket connection given the token t */
func getUID(t string) (string, error) {
/* Roll the request */
req, err := http.NewRequest("GET", uidURL, nil)
if nil != err {
return "", err
}
req.Header.Add("X-Token", t)
req.Header.Add("X-Username", t)
/* Send the request */
res, err := http.DefaultClient.Do(req)
if nil != err {
return "", err
}
defer res.Body.Close()
/* Buffer the output */
buf := &bytes.Buffer{}
_, err = io.Copy(buf, res.Body)
/* Struct into which to unmarshal the uid */
rs := struct {
Error string
Id string `json:"_id"`
}{}
/* Unmarshal the json */
if err := json.Unmarshal(buf.Bytes(), &rs); nil != err {
return "", err
}
/* If there's an error, note it */
if "" != rs.Error {
return "", fmt.Errorf("%v", rs.Error)
}
return rs.Id, err
}