-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
63 lines (54 loc) · 1.74 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
package middleware
import (
"encoding/base64"
"encoding/json"
"strings"
scrypt "github.com/elithrar/simple-scrypt"
"github.com/mikeStr8s/simple_weapons_api/util"
"github.com/valyala/fasthttp"
)
// Auth is a handler middleware that checks that the user has an auth token
// in their header. If so it will then check to see that the auth
// is valid before sending the user to the desired end handler
func Auth(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
authToken := ctx.Request.Header.Peek("Simple-Weapons")
if authToken != nil {
username, password, hasAuth := checkTokenValid(string(authToken))
if hasAuth {
err := scrypt.CompareHashAndPassword([]byte(getPasswordHash(username)), []byte(password))
if err != nil {
ctx.Error(fasthttp.StatusMessage(fasthttp.StatusInternalServerError), fasthttp.StatusInternalServerError)
} else {
handler(ctx)
return
}
}
}
ctx.Error(fasthttp.StatusMessage(fasthttp.StatusUnauthorized), fasthttp.StatusUnauthorized)
})
}
func checkTokenValid(token string) (username, password string, ok bool) {
tokenBytes, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return
}
userpass := string(tokenBytes)
sepIdx := strings.IndexByte(userpass, ':')
if sepIdx < 0 {
return
}
userByteData := util.ReadJSONFile("user")
var userData map[string]string
json.Unmarshal(userByteData, &userData)
if _, ok := userData[userpass[:sepIdx]]; ok {
return userpass[:sepIdx], userpass[sepIdx+1:], true
}
return
}
func getPasswordHash(username string) string {
userByteData := util.ReadJSONFile("user")
var userData map[string]string
json.Unmarshal(userByteData, &userData)
return userData[username]
}