forked from kentonh/gPanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
88 lines (71 loc) · 1.82 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 webhost handles the logic of the webhosting panel
package webhost
import (
"net/http"
"strings"
"github.com/Ennovar/gPanel/pkg/api/user"
"github.com/Ennovar/gPanel/pkg/networking"
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"}
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 checkAuth(res http.ResponseWriter, req *http.Request) bool {
store := networking.GetStore(networking.COOKIES_USER_AUTH)
session_value, err := store.Read(res, req, "user")
if err != nil || session_value == nil {
return false
}
username, ok := session_value.(string)
if !ok {
return false
}
stored_secret, err := user.GetSecret(username)
if stored_secret == "" {
return false
}
session_value, err = store.Read(res, req, "token")
if err != nil || session_value == nil {
return false
}
tokenString, ok := session_value.(string)
if !ok {
return false
}
keyfunc := func(t *jwt.Token) (interface{}, error) {
return []byte(stored_secret), nil
}
p := jwt.Parser{
ValidMethods: []string{"HS256", "HS384", "HS512"},
}
t, err := p.ParseWithClaims(tokenString, &jwt.StandardClaims{}, keyfunc)
if err != nil {
return false
}
claims := t.Claims.(*jwt.StandardClaims)
if claims.Subject != username {
return false
}
return true
}