/
auth.go
131 lines (98 loc) · 3.28 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
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
package handlers
import (
// "fmt"
"errors"
"strings"
"encoding/base64"
"net/http"
_ "github.com/lib/pq"
// "github.com/go-gorp/gorp"
"gopkg.in/gorp.v1"
"github.com/misrab/bookshare-backend-api/models"
)
const (
TOKEN = "lalala"
)
type handler func(res http.ResponseWriter, req *http.Request, dbmap *gorp.DbMap)
func DecodeAuthHeader(req *http.Request) (string, string, int) {
header := req.Header.Get("Authorization") //req.Header["Authorization"]
if header == "" {
return "", "", http.StatusUnauthorized
}
auth := strings.SplitN(header, " ", 2)
if len(auth) != 2 || auth[0] != "Basic" {
println("auth funny")
return "", "", http.StatusUnauthorized
}
payload, _ := base64.StdEncoding.DecodeString(auth[1])
pair := strings.SplitN(string(payload), ":", 2)
// fmt.Printf("Pair is %v\n", pair)
// TODO check pair len = 2 ?
if len(pair) < 2 {
println("pair funny")
return "", "", http.StatusUnauthorized
}
return pair[0], pair[1], http.StatusOK
// return pair[0], pair[1], http.StatusOK
}
func Respond401(res http.ResponseWriter) {
err := errors.New("bad syntax")
SetHeaders(res, http.StatusUnauthorized)
Respond(nil, err, res)
}
func BasicAuth(pass handler) handler {
return func(res http.ResponseWriter, req *http.Request, dbmap *gorp.DbMap) {
// TEMP
// pass(res, req, dbmap)
// return
// token bypass
token := req.FormValue("token")
// println("found token: " + token)
if token == TOKEN {
pass(res, req, dbmap)
return
}
// header := req.Header.Get("Authorization") //req.Header["Authorization"]
// if header == "" {
// http.Error(res, "bad syntax", http.StatusBadRequest)
// return
// }
// auth := strings.SplitN(header, " ", 2)
// if len(auth) != 2 || auth[0] != "Basic" {
// http.Error(res, "bad syntax", http.StatusBadRequest)
// return
// }
// payload, _ := base64.StdEncoding.DecodeString(auth[1])
// pair := strings.SplitN(string(payload), ":", 2)
username, hash, errCode := DecodeAuthHeader(req)
if errCode != http.StatusOK {
Respond401(res)
// SetHeaders(res, http.StatusUnauthorized)
// http.Error(res, "bad syntax", errCode)
return
}
// get the user
item := new(models.User)
err := dbmap.SelectOne(&item, "select * from users where email=$1", username)
if err != nil || item.Hash != hash {
Respond401(res)
// SetHeaders(res, http.StatusUnauthorized)
// http.Error(res, "bad syntax", errCode)
return
}
// if hash !=
// if !Validate(username, password) {
// println("username password did not validate")
// SetHeaders(res, http.StatusUnauthorized)
// http.Error(res, "authorization failed", http.StatusUnauthorized)
// return
// }
pass(res, req, dbmap)
}
}
// func Validate(username, password string) bool {
// if username == "username" && password == "password" {
// return true
// }
// return false
// }