-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
29 lines (24 loc) · 838 Bytes
/
util.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
package main
import "net/http"
// CheckIfAuthorized Check if a request is authorized.
func CheckIfAuthorized(w http.ResponseWriter, r *http.Request) bool {
if r.Header.Get("Authorization") == "" {
w.WriteHeader(401)
w.Write([]byte(`{ "error": "Unauthorized. Please make sure your authorization token is set correctly." }`))
return false
} else if r.Header.Get("Authorization") != "" && !Contains(authtokens, r.Header.Get("Authorization")) {
w.WriteHeader(401)
w.Write([]byte(`{ "error": "Unauthorized. Please make sure your authorization token is set correctly." }`))
return false
}
return true
}
// Contains simple function to check if an element exists within a slice
func Contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}