-
Notifications
You must be signed in to change notification settings - Fork 25
/
authentication.go
88 lines (71 loc) · 1.85 KB
/
authentication.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
// Package gpaccount handles the logic of the gPanel account server
package gpaccount
import (
"net/http"
"strings"
"encoding/base64"
"encoding/json"
"github.com/kentonh/gPanel/pkg/api/user"
jwt "github.com/dgrijalva/jwt-go"
)
// reqAuth function checks to see if the given path requires authentication.
func reqAuth(path string) bool {
path = strings.ToLower(path)
dismissibleTypes := []string{".css", ".js", ".jpg", ".png", ".ico"}
for _, t := range dismissibleTypes {
if strings.HasSuffix(path, t) {
return false
}
}
dismissibleFiles := []string{
"index.html",
"api/user/auth",
"api/user/register",
"api/user/logout",
}
for _, f := range dismissibleFiles {
if strings.HasSuffix(path, f) {
return false
}
}
return true
}
// checkAuth function returns a boolean based on whether or not the current
// caller is authenticated based off of encrypted sessions using JWT values.
func (con *Controller) checkAuth(res http.ResponseWriter, req *http.Request) bool {
c, err := req.Cookie("gpanel-account-user-auth")
if err != nil {
return false
}
data, err := base64.StdEncoding.DecodeString(c.Value)
if err != nil {
return false
}
var sessionData struct {
Username string `json:"Username"`
Token string `json:"Token"`
}
err = json.Unmarshal(data, &sessionData)
if err != nil {
return false
}
storedSecret, err := user.GetSecret(sessionData.Username, con.Directory)
if storedSecret == "" || err != nil {
return false
}
keyfunc := func(t *jwt.Token) (interface{}, error) {
return []byte(storedSecret), nil
}
p := jwt.Parser{
ValidMethods: []string{"HS256", "HS384", "HS512"},
}
t, err := p.ParseWithClaims(sessionData.Token, &jwt.StandardClaims{}, keyfunc)
if err != nil {
return false
}
claims := t.Claims.(*jwt.StandardClaims)
if claims.Subject != sessionData.Username {
return false
}
return true
}