This repository has been archived by the owner on Jan 23, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
parser.go
105 lines (93 loc) · 2.55 KB
/
parser.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
package portal
import (
"encoding/base64"
"fmt"
"net/http"
"strings"
)
func parseCredentials(r *http.Request) (map[string]string, error) {
if r.Method == "POST" {
return parseAuthForm(r)
}
if r.Method == "GET" {
return parseAuthRequest(r)
}
return nil, fmt.Errorf("Request method %s is unsupported", r.Method)
}
func parseAuthForm(r *http.Request) (map[string]string, error) {
var reqFields []string
kv := make(map[string]string)
var maxBytesLimit int64 = 1000
var minBytesLimit int64 = 15
if r.ContentLength > maxBytesLimit {
return nil, fmt.Errorf("Request payload exceeded the limit of %d bytes: %d", maxBytesLimit, r.ContentLength)
}
if r.ContentLength < minBytesLimit {
return nil, fmt.Errorf("Request payload is too small: %d", r.ContentLength)
}
contentType := r.Header.Get("Content-Type")
if contentType != "application/x-www-form-urlencoded" {
return nil, fmt.Errorf("Request content type is not application/x-www-form-urlencoded")
}
rq := r.FormValue("activity")
if rq == "" {
rq = "login"
}
switch rq {
case "login":
reqFields = []string{"username", "password", "realm"}
default:
return nil, fmt.Errorf("request type is unsupported")
}
for _, k := range reqFields {
if v := r.FormValue(k); v != "" {
kv[k] = v
}
}
if _, exists := kv["realm"]; !exists {
kv["realm"] = "local"
}
return kv, nil
}
func parseAuthRequest(r *http.Request) (map[string]string, error) {
kv := make(map[string]string)
authzHeaderStr := r.Header.Get("Authorization")
if authzHeaderStr == "" {
return nil, nil
}
authzHeaderParts := strings.Split(authzHeaderStr, ",")
if len(authzHeaderParts) == 0 {
return nil, nil
}
authzStrParts := strings.Split(authzHeaderParts[0], " ")
if len(authzStrParts) != 2 {
return nil, nil
}
authzType := authzStrParts[0]
if authzType != "Basic" {
return nil, nil
}
authzStr, err := base64.StdEncoding.DecodeString(authzStrParts[1])
if err != nil {
return nil, fmt.Errorf("decoding error: %s", err)
}
authzArr := strings.SplitN(string(authzStr), ":", 2)
if len(authzArr) != 2 {
return nil, fmt.Errorf("parsing error: %s", err)
}
kv["username"] = authzArr[0]
kv["password"] = authzArr[1]
if len(authzHeaderParts) == 1 {
kv["realm"] = "local"
return kv, nil
}
realmHeaderParts := strings.Split(authzHeaderParts[1], "=")
if len(realmHeaderParts) != 2 {
return nil, fmt.Errorf("realm parsing failed for %s", realmHeaderParts)
}
if realmHeaderParts[0] != "realm" {
return nil, fmt.Errorf("realm not found in %s", realmHeaderParts)
}
kv["realm"] = realmHeaderParts[1]
return kv, nil
}