-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
53 lines (42 loc) · 1.29 KB
/
request.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
package utils
import (
"errors"
"net/http"
"strconv"
"strings"
"github.com/equinor/radix-api/models"
)
// GetBearerToken Gets bearer token from request header
func getBearerTokenFromHeader(r *http.Request) (string, error) {
authorizationHeader := r.Header.Get("authorization")
authArr := strings.Split(authorizationHeader, " ")
var jwtToken string
if len(authArr) != 2 {
return "", errors.New("Authentication header is invalid: " + authorizationHeader)
}
jwtToken = authArr[1]
return jwtToken, nil
}
// GetBearerToken Gets bearer token from request header
func getImpersonationFromHeader(r *http.Request) (models.Impersonation, error) {
impersonateUser := r.Header.Get("Impersonate-User")
impersonateGroup := r.Header.Get("Impersonate-Group")
return models.NewImpersonation(impersonateUser, impersonateGroup)
}
// GetTokenFromQuery Gets token from query of the request
func GetTokenFromQuery(request *http.Request) string {
return request.URL.Query().Get("token")
}
// IsWatch Indicates if request is asking for watch
func isWatch(request *http.Request) (bool, error) {
watchArg := request.FormValue("watch")
var watch bool
if watchArg != "" {
parsedWatchArg, err := strconv.ParseBool(watchArg)
if err != nil {
return false, err
}
watch = parsedWatchArg
}
return watch, nil
}