forked from sensu/uchiwa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
150 lines (128 loc) · 3.94 KB
/
handlers.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package auth
import (
"encoding/json"
"fmt"
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/context"
"github.com/contegix/uchiwa/uchiwa/audit"
"github.com/contegix/uchiwa/uchiwa/helpers"
"github.com/contegix/uchiwa/uchiwa/logger"
"github.com/contegix/uchiwa/uchiwa/structs"
)
const jwtToken = "jwtToken"
// GetTokenFromContext retrieves the JWT Token from the request
func GetTokenFromContext(r *http.Request) *jwt.Token {
if value := context.Get(r, jwtToken); value != nil {
return value.(*jwt.Token)
}
return nil
}
// setTokenIntoContext inject the JWT Token into the request for later use
func setTokenIntoContext(r *http.Request, token *jwt.Token) {
context.Set(r, jwtToken, token)
}
// publicHandler does not enforce authentication
func publicHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}
// restrictedHandler enforce authentication by validating the JWT token
func restrictedHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token, err := jwt.ParseFromRequest(r, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", t.Header["alg"])
}
return publicKey, nil
})
if token != nil && err == nil {
if token.Valid {
setTokenIntoContext(r, token)
authorized := hasPermission(token, r)
if !authorized {
http.Error(w, "", http.StatusForbidden)
return
}
next.ServeHTTP(w, r)
context.Clear(r)
} else {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
} else {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
})
}
// Authenticate calls the proper handler based on whether authentication is enabled or not
func (a *Config) Authenticate(next http.Handler) http.Handler {
if a.DriverName == "none" {
return publicHandler(next)
}
return restrictedHandler(next)
}
// GetIdentification retrieves the user & pass from a POST and authenticates the user against the Identification driver
func (a *Config) GetIdentification() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Redirect(w, r, "/#/login", http.StatusFound)
return
}
decoder := json.NewDecoder(r.Body)
var data interface{}
err := decoder.Decode(&data)
if err != nil {
logger.Warningf("Could not decode the body: %s", err)
http.Error(w, "", http.StatusInternalServerError)
return
}
m, ok := data.(map[string]interface{})
if !ok {
logger.Warningf("Could not assert the body: %s", err)
http.Error(w, "", http.StatusInternalServerError)
return
}
u := m["user"].(string)
p := m["pass"].(string)
if u == "" || p == "" {
logger.Info("Authentication failed: user and password must not be empty")
http.Error(w, "", http.StatusUnauthorized)
return
}
// validate the user with the Login authentication driver
user, err := a.DriverFn(u, p)
if err != nil {
message := fmt.Sprintf("Authentication failed: %s", err)
// Output to stdout
logger.Info(message)
// Output to audit log
log := structs.AuditLog{Action: "loginfailure", Level: "default", Output: message}
log.RemoteAddr = helpers.GetIP(r)
audit.Log(log)
http.Error(w, "", http.StatusUnauthorized)
return
}
// obfuscate the user's salt & hash
user.PasswordHash = ""
user.PasswordSalt = ""
token, err := GetToken(&user.Role, u)
if err != nil {
logger.Warningf("Authentication failed, could not create the token: %s", err)
http.Error(w, "", http.StatusInternalServerError)
return
}
// Add token to the user struct
user.Token = token
j, err := json.Marshal(user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(j)
return
})
}