forked from motiv-labs/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
130 lines (107 loc) · 3.9 KB
/
loader.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
118
119
120
121
122
123
124
125
126
127
128
129
130
package oauth2
import (
"github.com/hellofresh/janus/pkg/proxy"
"github.com/hellofresh/janus/pkg/router"
"github.com/rs/cors"
log "github.com/sirupsen/logrus"
"github.com/ulule/limiter"
"github.com/ulule/limiter/drivers/middleware/stdlib"
storeMemory "github.com/ulule/limiter/drivers/store/memory"
)
// OAuthLoader handles the loading of the api specs
type OAuthLoader struct {
register *proxy.Register
}
// NewOAuthLoader creates a new instance of the Loader
func NewOAuthLoader(register *proxy.Register) *OAuthLoader {
return &OAuthLoader{register}
}
// LoadDefinitions loads all oauth servers from a data source
func (m *OAuthLoader) LoadDefinitions(repo Repository) {
oAuthServers := m.getOAuthServers(repo)
m.RegisterOAuthServers(oAuthServers, repo)
}
// RegisterOAuthServers register many oauth servers
func (m *OAuthLoader) RegisterOAuthServers(oauthServers []*Spec, repo Repository) {
log.Debug("Loading OAuth servers configurations")
for _, oauthServer := range oauthServers {
var mw []router.Constructor
logger := log.WithField("name", oauthServer.Name)
logger.Debug("Registering OAuth server")
corsHandler := cors.New(cors.Options{
AllowedOrigins: oauthServer.CorsMeta.Domains,
AllowedMethods: oauthServer.CorsMeta.Methods,
AllowedHeaders: oauthServer.CorsMeta.RequestHeaders,
ExposedHeaders: oauthServer.CorsMeta.ExposedHeaders,
OptionsPassthrough: oauthServer.CorsMeta.OptionsPassthrough,
AllowCredentials: true,
}).Handler
mw = append(mw, corsHandler)
if oauthServer.RateLimit.Enabled {
rate, err := limiter.NewRateFromFormatted(oauthServer.RateLimit.Limit)
if err != nil {
logger.WithError(err).Error("Not able to create rate limit")
}
limiterStore := storeMemory.NewStore()
limiterInstance := limiter.New(limiterStore, rate)
rateLimitHandler := stdlib.NewMiddleware(limiterInstance).Handler
mw = append(mw, rateLimitHandler)
}
endpoints := map[*proxy.RouterDefinition][]router.Constructor{
proxy.NewRouterDefinition(oauthServer.Endpoints.Authorize): mw,
proxy.NewRouterDefinition(oauthServer.Endpoints.Token): append(mw, NewSecretMiddleware(oauthServer).Handler),
proxy.NewRouterDefinition(oauthServer.Endpoints.Introspect): mw,
proxy.NewRouterDefinition(oauthServer.Endpoints.Revoke): mw,
proxy.NewRouterDefinition(oauthServer.ClientEndpoints.Create): mw,
proxy.NewRouterDefinition(oauthServer.ClientEndpoints.Remove): mw,
}
m.registerRoutes(endpoints)
logger.Debug("OAuth server registered")
}
log.Debug("Done loading OAuth servers configurations")
}
func (m *OAuthLoader) getOAuthServers(repo Repository) []*Spec {
oauthServers, err := repo.FindAll()
if err != nil {
log.Panic(err)
}
var specs []*Spec
for _, oauthServer := range oauthServers {
spec := new(Spec)
spec.OAuth = oauthServer
manager, err := m.getManager(oauthServer)
if nil != err {
log.WithError(err).Error("Oauth definition is not well configured, skipping...")
continue
}
spec.Manager = manager
specs = append(specs, spec)
}
return specs
}
func (m *OAuthLoader) getManager(oauthServer *OAuth) (Manager, error) {
managerType, err := ParseType(oauthServer.TokenStrategy.Name)
if nil != err {
return nil, err
}
return NewManagerFactory(oauthServer).Build(managerType)
}
func (m *OAuthLoader) registerRoutes(endpoints map[*proxy.RouterDefinition][]router.Constructor) {
for endpoint, middleware := range endpoints {
if endpoint.Definition == nil || endpoint.Definition.ListenPath == "" {
log.Debug("Endpoint not registered")
continue
}
for _, mw := range middleware {
endpoint.AddMiddleware(mw)
}
l := log.WithField("listen_path", endpoint.ListenPath)
l.Debug("Registering OAuth endpoint")
if isValid, err := endpoint.Validate(); isValid && err == nil {
m.register.Add(endpoint)
l.Debug("Endpoint registered")
} else {
l.WithError(err).Error("Error when registering endpoint")
}
}
}