/
http.go
42 lines (33 loc) · 1000 Bytes
/
http.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
package util
import (
"net/http"
"net/url"
"strings"
)
func GetAccessTokenFromRequest(request *http.Request) string {
token := request.Header.Get("Authorization")
if token != "" {
if !strings.HasPrefix(token, "Bearer ") { // including space
// It's probably an X-Matrix authentication header (federation)
//logrus.Warn("Invalid Authorization header observed: expected a Bearer token, got something else")
return ""
}
return token[len("Bearer "):] // including space
}
return request.URL.Query().Get("access_token")
}
func GetAppserviceUserIdFromRequest(request *http.Request) string {
return request.URL.Query().Get("user_id")
}
func GetLogSafeQueryString(r *http.Request) string {
qs := r.URL.Query()
if qs.Get("access_token") != "" {
qs.Set("access_token", "redacted")
}
return qs.Encode()
}
func GetLogSafeUrl(r *http.Request) string {
copyUrl, _ := url.ParseRequestURI(r.URL.String())
copyUrl.RawQuery = GetLogSafeQueryString(r)
return copyUrl.String()
}