-
Notifications
You must be signed in to change notification settings - Fork 42
/
key-auth.go
117 lines (92 loc) · 2.66 KB
/
key-auth.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
106
107
108
109
110
111
112
113
114
115
116
117
package auth
import (
"fmt"
"log/slog"
"net/http"
"strings"
"github.com/direktiv/direktiv/pkg/refactor/core"
"github.com/direktiv/direktiv/pkg/refactor/gateway/consumer"
"github.com/direktiv/direktiv/pkg/refactor/gateway/plugins"
)
const (
KeyAuthPluginName = "key-auth"
KeyName = "API-Token"
)
// KeyAuthConfig configures a key-auth plugin instance.
// The plugin can be configured to set consumer information (name, groups, tags)
// and the name of the header for the api key.
type KeyAuthConfig struct {
AddUsernameHeader bool `mapstructure:"add_username_header" yaml:"add_username_header"`
AddTagsHeader bool `mapstructure:"add_tags_header" yaml:"add_tags_header"`
AddGroupsHeader bool `mapstructure:"add_groups_header" yaml:"add_groups_header"`
// KeyName defines the header for the key
KeyName string `mapstructure:"key_name" yaml:"key_name"`
}
type KeyAuthPlugin struct {
config *KeyAuthConfig
}
func ConfigureKeyAuthPlugin(config interface{}, _ string) (core.PluginInstance, error) {
keyAuthConfig := &KeyAuthConfig{
KeyName: KeyName,
}
err := plugins.ConvertConfig(config, keyAuthConfig)
if err != nil {
return nil, err
}
return &KeyAuthPlugin{
config: keyAuthConfig,
}, nil
}
func (ka *KeyAuthPlugin) ExecutePlugin(c *core.ConsumerFile,
w http.ResponseWriter, r *http.Request,
) bool {
key := r.Header.Get(ka.config.KeyName)
// no basic auth provided
if key == "" {
return true
}
gwObj := r.Context().Value(plugins.ConsumersParamCtxKey)
if gwObj == nil {
slog.Debug("no consumer list in context")
return true
}
consumerList, ok := gwObj.(*consumer.List)
if !ok {
plugins.ReportError(r.Context(), w, http.StatusInternalServerError,
"consumerlist", fmt.Errorf("wrong object in context"))
return false
}
consumer := consumerList.FindByAPIKey(key)
// no consumer with that name
if consumer == nil {
slog.Debug("no consumer configured for api key")
return true
}
if consumer.APIKey == key {
*c = *consumer
// set headers if configured.
if ka.config.AddUsernameHeader {
r.Header.Set(plugins.ConsumerUserHeader, c.Username)
}
if ka.config.AddTagsHeader && len(c.Tags) > 0 {
r.Header.Set(plugins.ConsumerTagsHeader, strings.Join(c.Tags, ","))
}
if ka.config.AddGroupsHeader && len(c.Groups) > 0 {
r.Header.Set(plugins.ConsumerGroupsHeader, strings.Join(c.Groups, ","))
}
}
return true
}
func (ka *KeyAuthPlugin) Config() interface{} {
return ka.config
}
func (ka *KeyAuthPlugin) Type() string {
return KeyAuthPluginName
}
//nolint:gochecknoinits
func init() {
plugins.AddPluginToRegistry(plugins.NewPluginBase(
KeyAuthPluginName,
plugins.AuthPluginType,
ConfigureKeyAuthPlugin))
}