This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
80 lines (75 loc) · 2.73 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
package infopage
import (
"encoding/json"
"github.com/orange-cloudfoundry/gobis"
"github.com/orange-cloudfoundry/gobis-middlewares/utils"
"net/http"
"strings"
)
const (
GobisHeaderPrefix = "x-gobis-"
)
type InfoPageConfig struct {
InfoPage *InfoPageOptions `mapstructure:"info_page" json:"info_page" yaml:"info_page"`
}
type InfoPageOptions struct {
// enable info page middleware
Enabled bool `mapstructure:"enabled" json:"enabled" yaml:"enabled"`
// Path where the info page will be available (Default: "/gobis_info")
// Be careful it will override any existing path with this name on upstream
Path string `mapstructure:"path" json:"path" yaml:"path"`
// Show in the info page the authorization header set in request
// This permit to see, for example, token created by oauth2 middleware
// (every handler use for login should write in Authorization token/string used to connect)
ShowAuthorizationHeader bool `mapstructure:"show_authorization_header" json:"show_authorization_header" yaml:"show_authorization_header"`
// Name of the key for authorization header to use in info page (Default: authorization)
// ShowAuthorizationHeader must be true to set this value
AuthorizationKeyName string `mapstructure:"authorization_key_name" json:"authorization_key_name" yaml:"authorization_key_name"`
}
type InfoPage struct{}
func NewInfoPage() *InfoPage {
return &InfoPage{}
}
func (InfoPage) Handler(proxyRoute gobis.ProxyRoute, params interface{}, next http.Handler) (http.Handler, error) {
config := params.(InfoPageConfig)
options := config.InfoPage
if options == nil || !options.Enabled {
return next, nil
}
options.Path = utils.CondVal(options.Path, "/gobis_info").(string)
options.AuthorizationKeyName = utils.CondVal(options.AuthorizationKeyName, "authorization").(string)
return utils.PathHandler(options.Path, next, func(w http.ResponseWriter, req *http.Request) {
infoMap := make(map[string]interface{})
for name, values := range req.Header {
name = strings.ToLower(name)
if !strings.HasPrefix(name, GobisHeaderPrefix) ||
len(values) == 0 ||
gobis.IsDirtyHeader(req, name) {
continue
}
name = strings.TrimPrefix(name, GobisHeaderPrefix)
if len(values) > 1 {
infoMap[name] = values
continue
}
infoMap[name] = values[0]
}
username := gobis.Username(req)
if username != "" {
infoMap["username"] = username
}
groups := gobis.Groups(req)
if len(groups) > 0 {
infoMap["groups"] = groups
}
if options.ShowAuthorizationHeader {
infoMap[options.AuthorizationKeyName] = req.Header.Get("Authorization")
}
b, _ := json.MarshalIndent(infoMap, "", "\t")
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}), nil
}
func (InfoPage) Schema() interface{} {
return InfoPageConfig{}
}