-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.go
318 lines (280 loc) · 11.5 KB
/
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package authkit
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"os"
"github.com/acronis/go-appkit/httpserver/middleware"
"github.com/acronis/go-appkit/log"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"github.com/acronis/go-authkit/idptoken"
"github.com/acronis/go-authkit/internal/idputil"
"github.com/acronis/go-authkit/internal/libinfo"
"github.com/acronis/go-authkit/jwks"
"github.com/acronis/go-authkit/jwt"
)
// NewJWTParser creates a new JWTParser with the given configuration.
// If cfg.JWT.ClaimsCache.Enabled is true, then jwt.CachingParser created, otherwise - jwt.Parser.
func NewJWTParser(cfg *Config, opts ...JWTParserOption) (JWTParser, error) {
options := jwtParserOptions{loggerProvider: middleware.GetLoggerFromContext}
for _, opt := range opts {
opt(&options)
}
// Make caching JWKS client.
jwksCacheUpdateMinInterval := cfg.JWKS.Cache.UpdateMinInterval
if jwksCacheUpdateMinInterval == 0 {
jwksCacheUpdateMinInterval = jwks.DefaultCacheUpdateMinInterval
}
jwksClientOpts := jwks.CachingClientOpts{
ClientOpts: jwks.ClientOpts{
LoggerProvider: options.loggerProvider,
HTTPClient: idputil.MakeDefaultHTTPClient(cfg.HTTPClient.RequestTimeout, options.loggerProvider),
PrometheusLibInstanceLabel: options.prometheusLibInstanceLabel,
},
CacheUpdateMinInterval: jwksCacheUpdateMinInterval,
}
jwksClient := jwks.NewCachingClientWithOpts(jwksClientOpts)
// Make JWT parser.
if len(cfg.JWT.TrustedIssuers) == 0 && len(cfg.JWT.TrustedIssuerURLs) == 0 {
idputil.GetLoggerFromProvider(context.Background(), options.loggerProvider).Warn(
"list of trusted issuers is empty, jwt parsing may not work properly")
}
parserOpts := jwt.ParserOpts{
RequireAudience: cfg.JWT.RequireAudience,
ExpectedAudience: cfg.JWT.ExpectedAudience,
TrustedIssuerNotFoundFallback: options.trustedIssuerNotFoundFallback,
LoggerProvider: options.loggerProvider,
ClaimsTemplate: options.claimsTemplate,
}
if cfg.JWT.ClaimsCache.Enabled {
cachingJWTParser, err := jwt.NewCachingParserWithOpts(jwksClient, jwt.CachingParserOpts{
ParserOpts: parserOpts,
CacheMaxEntries: cfg.JWT.ClaimsCache.MaxEntries,
})
if err != nil {
return nil, fmt.Errorf("new caching JWT parser: %w", err)
}
if err = addTrustedIssuers(cachingJWTParser, cfg.JWT.TrustedIssuers, cfg.JWT.TrustedIssuerURLs); err != nil {
return nil, err
}
return cachingJWTParser, nil
}
jwtParser := jwt.NewParserWithOpts(jwksClient, parserOpts)
if err := addTrustedIssuers(jwtParser, cfg.JWT.TrustedIssuers, cfg.JWT.TrustedIssuerURLs); err != nil {
return nil, err
}
return jwtParser, nil
}
type jwtParserOptions struct {
loggerProvider func(ctx context.Context) log.FieldLogger
prometheusLibInstanceLabel string
trustedIssuerNotFoundFallback jwt.TrustedIssNotFoundFallback
claimsTemplate jwt.Claims
}
// JWTParserOption is an option for creating JWTParser.
type JWTParserOption func(options *jwtParserOptions)
// WithJWTParserLoggerProvider sets the logger provider for JWTParser.
func WithJWTParserLoggerProvider(loggerProvider func(ctx context.Context) log.FieldLogger) JWTParserOption {
return func(options *jwtParserOptions) {
options.loggerProvider = loggerProvider
}
}
// WithJWTParserPrometheusLibInstanceLabel sets the Prometheus lib instance label for JWTParser.
func WithJWTParserPrometheusLibInstanceLabel(label string) JWTParserOption {
return func(options *jwtParserOptions) {
options.prometheusLibInstanceLabel = label
}
}
// WithJWTParserTrustedIssuerNotFoundFallback sets the fallback for JWTParser when trusted issuer is not found.
func WithJWTParserTrustedIssuerNotFoundFallback(fallback jwt.TrustedIssNotFoundFallback) JWTParserOption {
return func(options *jwtParserOptions) {
options.trustedIssuerNotFoundFallback = fallback
}
}
// WithJWTParserClaimsTemplate sets the claims template for JWTParser.
func WithJWTParserClaimsTemplate(claimsTemplate jwt.Claims) JWTParserOption {
return func(options *jwtParserOptions) {
options.claimsTemplate = claimsTemplate
}
}
// NewTokenIntrospector creates a new TokenIntrospector with the given configuration, token provider and scope filter.
// If cfg.Introspection.ClaimsCache.Enabled or cfg.Introspection.NegativeCache.Enabled is true,
// then idptoken.CachingIntrospector created, otherwise - idptoken.Introspector.
// Please note that the tokenProvider should be able to provide access token with the policy for introspection.
// scopeFilter is a list of filters that will be applied to the introspected token.
func NewTokenIntrospector(
cfg *Config,
tokenProvider idptoken.IntrospectionTokenProvider,
scopeFilter jwt.ScopeFilter,
opts ...TokenIntrospectorOption,
) (*idptoken.Introspector, error) {
options := tokenIntrospectorOptions{loggerProvider: middleware.GetLoggerFromContext}
for _, opt := range opts {
opt(&options)
}
if len(cfg.JWT.TrustedIssuers) == 0 && len(cfg.JWT.TrustedIssuerURLs) == 0 {
idputil.GetLoggerFromProvider(context.Background(), options.loggerProvider).Warn(
"list of trusted issuers is empty, jwt introspection may not work properly")
}
var grpcClient *idptoken.GRPCClient
if cfg.Introspection.GRPC.Endpoint != "" {
transportCreds, err := makeGRPCTransportCredentials(cfg.Introspection.GRPC.TLS)
if err != nil {
return nil, fmt.Errorf("make grpc transport credentials: %w", err)
}
grpcClientOpts := idptoken.GRPCClientOpts{
RequestTimeout: cfg.GRPCClient.RequestTimeout,
LoggerProvider: options.loggerProvider,
UserAgent: libinfo.UserAgent(),
}
if grpcClient, err = idptoken.NewGRPCClientWithOpts(
cfg.Introspection.GRPC.Endpoint, transportCreds, grpcClientOpts,
); err != nil {
return nil, fmt.Errorf("new grpc client: %w", err)
}
}
introspectorOpts := idptoken.IntrospectorOpts{
HTTPEndpoint: cfg.Introspection.Endpoint,
GRPCClient: grpcClient,
HTTPClient: idputil.MakeDefaultHTTPClient(cfg.HTTPClient.RequestTimeout, options.loggerProvider),
AccessTokenScope: cfg.Introspection.AccessTokenScope,
LoggerProvider: options.loggerProvider,
ResultTemplate: options.resultTemplate,
ScopeFilter: scopeFilter,
TrustedIssuerNotFoundFallback: options.trustedIssuerNotFoundFallback,
PrometheusLibInstanceLabel: options.prometheusLibInstanceLabel,
ClaimsCache: idptoken.IntrospectorCacheOpts{
Enabled: cfg.Introspection.ClaimsCache.Enabled,
MaxEntries: cfg.Introspection.ClaimsCache.MaxEntries,
TTL: cfg.Introspection.ClaimsCache.TTL,
},
NegativeCache: idptoken.IntrospectorCacheOpts{
Enabled: cfg.Introspection.NegativeCache.Enabled,
MaxEntries: cfg.Introspection.NegativeCache.MaxEntries,
TTL: cfg.Introspection.NegativeCache.TTL,
},
}
introspector, err := idptoken.NewIntrospectorWithOpts(tokenProvider, introspectorOpts)
if err != nil {
return nil, err
}
if err = addTrustedIssuers(introspector, cfg.JWT.TrustedIssuers, cfg.JWT.TrustedIssuerURLs); err != nil {
return nil, err
}
return introspector, nil
}
type tokenIntrospectorOptions struct {
loggerProvider func(ctx context.Context) log.FieldLogger
prometheusLibInstanceLabel string
trustedIssuerNotFoundFallback idptoken.TrustedIssNotFoundFallback
resultTemplate idptoken.IntrospectionResult
}
// TokenIntrospectorOption is an option for creating TokenIntrospector.
type TokenIntrospectorOption func(options *tokenIntrospectorOptions)
// WithTokenIntrospectorLoggerProvider sets the logger provider for TokenIntrospector.
func WithTokenIntrospectorLoggerProvider(loggerProvider func(ctx context.Context) log.FieldLogger) TokenIntrospectorOption {
return func(options *tokenIntrospectorOptions) {
options.loggerProvider = loggerProvider
}
}
// WithTokenIntrospectorPrometheusLibInstanceLabel sets the Prometheus lib instance label for TokenIntrospector.
func WithTokenIntrospectorPrometheusLibInstanceLabel(label string) TokenIntrospectorOption {
return func(options *tokenIntrospectorOptions) {
options.prometheusLibInstanceLabel = label
}
}
// WithTokenIntrospectorTrustedIssuerNotFoundFallback sets the fallback for TokenIntrospector
// when trusted issuer is not found.
func WithTokenIntrospectorTrustedIssuerNotFoundFallback(
fallback idptoken.TrustedIssNotFoundFallback,
) TokenIntrospectorOption {
return func(options *tokenIntrospectorOptions) {
options.trustedIssuerNotFoundFallback = fallback
}
}
// WithTokenIntrospectorResultTemplate sets the result template for TokenIntrospector.
func WithTokenIntrospectorResultTemplate(resultTemplate idptoken.IntrospectionResult) TokenIntrospectorOption {
return func(options *tokenIntrospectorOptions) {
options.resultTemplate = resultTemplate
}
}
// Role is a representation of role which may be used for verifying access.
type Role struct {
Namespace string
Name string
}
// NewVerifyAccessByRolesInJWT creates a new function which may be used for verifying access by roles in JWT scope.
func NewVerifyAccessByRolesInJWT(roles ...Role) func(r *http.Request, claims jwt.Claims) bool {
return func(_ *http.Request, claims jwt.Claims) bool {
claimsScope := claims.GetScope()
for i := range roles {
for j := range claimsScope {
if roles[i].Name == claimsScope[j].Role && roles[i].Namespace == claimsScope[j].ResourceNamespace {
return true
}
}
}
return false
}
}
// NewVerifyAccessByRolesInJWTMaker creates a new function which may be used for verifying access by roles in JWT scope given a namespace.
func NewVerifyAccessByRolesInJWTMaker(namespace string) func(roleNames ...string) func(r *http.Request, claims jwt.Claims) bool {
return func(roleNames ...string) func(r *http.Request, claims jwt.Claims) bool {
roles := make([]Role, 0, len(roleNames))
for i := range roleNames {
roles = append(roles, Role{Namespace: namespace, Name: roleNames[i]})
}
return NewVerifyAccessByRolesInJWT(roles...)
}
}
// SetDefaultLogger sets the default logger for the library.
func SetDefaultLogger(logger log.FieldLogger) {
idputil.DefaultLogger = logger
}
type issuerParser interface {
AddTrustedIssuer(issName string, issURL string)
AddTrustedIssuerURL(issURL string) error
}
func addTrustedIssuers(issParser issuerParser, issuers map[string]string, issuerURLs []string) error {
for issName, issURL := range issuers {
issParser.AddTrustedIssuer(issName, issURL)
}
for _, issURL := range issuerURLs {
if err := issParser.AddTrustedIssuerURL(issURL); err != nil {
return fmt.Errorf("add trusted issuer URL: %w", err)
}
}
return nil
}
func makeGRPCTransportCredentials(tlsCfg GRPCTLSConfig) (credentials.TransportCredentials, error) {
if !tlsCfg.Enabled {
return insecure.NewCredentials(), nil
}
config := &tls.Config{} // nolint: gosec // TLS 1.2 is used by default.
if tlsCfg.CACert != "" {
caCert, err := os.ReadFile(tlsCfg.CACert)
if err != nil {
return nil, fmt.Errorf("read CA's certificate: %w", err)
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(caCert) {
return nil, fmt.Errorf("failed to add CA's certificate")
}
config.RootCAs = certPool
}
if tlsCfg.ClientCert != "" && tlsCfg.ClientKey != "" {
clientCert, err := tls.LoadX509KeyPair(tlsCfg.ClientCert, tlsCfg.ClientKey)
if err != nil {
return nil, fmt.Errorf("load client's certificate and key: %w", err)
}
config.Certificates = []tls.Certificate{clientCert}
}
return credentials.NewTLS(config), nil
}