-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
84 lines (75 loc) · 1.88 KB
/
handler.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
package main
import (
"encoding/base64"
"encoding/json"
"log"
"net/http"
"strings"
"time"
)
// GET /
func handler(w http.ResponseWriter, req *http.Request) {
if accessLog {
log.Printf("%s %s %s", req.RemoteAddr, req.Method, req.RequestURI)
}
time.Sleep(responseSleep)
_, _ = w.Write(responseBody)
}
// GET /echo
func echoHandler(w http.ResponseWriter, req *http.Request) {
if accessLog {
log.Printf("%s %s %s", req.RemoteAddr, req.Method, req.RequestURI)
}
// Parse Authorization header
var authzInfo = map[string]any{}
{
authzHeader := req.Header.Get("Authorization")
if len(authzHeader) <= 7 {
// empty or not have bearer token
goto breakAuthz
}
// JWT
jwtInfo := map[string]any{}
jwtAll := strings.Split(strings.TrimPrefix(authzHeader, "Bearer "), ".")
if strings.HasPrefix(authzHeader, "Bearer ") && len(jwtAll) == 3 {
parseJWT := func(jwtBodyB64Encoded, mapKey string) error {
jwtBody := map[string]any{}
jwtBodyDecoded, err := base64.RawURLEncoding.DecodeString(jwtBodyB64Encoded)
if err != nil {
return err
}
if err := json.Unmarshal(jwtBodyDecoded, &jwtBody); err != nil {
return err
}
jwtInfo[mapKey] = jwtBody
return nil
}
if err := parseJWT(jwtAll[0], "header"); err != nil {
log.Println(err)
goto breakAuthz
}
if err := parseJWT(jwtAll[1], "payload"); err != nil {
log.Println(err)
goto breakAuthz
}
authzInfo["jwt"] = jwtInfo
}
}
breakAuthz:
respMap := map[string]interface{}{
"Header": req.Header,
"Form": req.Form,
"Proto": req.Proto,
"Method": req.Method,
"Host": req.Host,
"RequestURI": req.RequestURI,
"RemoteAddr": req.RemoteAddr,
"Authorization": authzInfo,
}
resp, err := json.MarshalIndent(respMap, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
_, _ = w.Write(resp)
}