-
Notifications
You must be signed in to change notification settings - Fork 62
/
backend.go
198 lines (167 loc) · 4.24 KB
/
backend.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package jwtauth
import (
"context"
"errors"
"fmt"
"sync"
"github.com/hashicorp/cap/jwt"
"github.com/hashicorp/cap/oidc"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/patrickmn/go-cache"
)
const (
configPath string = "config"
rolePrefix string = "role/"
// operationPrefixJWT/JWTOIDC are used as prefixes for OpenAPI operation id's.
operationPrefixJWT = "jwt"
operationPrefixJWTOIDC = "jwt-oidc"
)
// Factory is used by framework
func Factory(ctx context.Context, c *logical.BackendConfig) (logical.Backend, error) {
b := backend()
if err := b.Setup(ctx, c); err != nil {
return nil, err
}
return b, nil
}
type jwtAuthBackend struct {
*framework.Backend
l sync.RWMutex
provider *oidc.Provider
validator *jwt.Validator
cachedConfig *jwtConfig
oidcRequests *cache.Cache
providerCtx context.Context
providerCtxCancel context.CancelFunc
}
func backend() *jwtAuthBackend {
b := new(jwtAuthBackend)
b.providerCtx, b.providerCtxCancel = context.WithCancel(context.Background())
b.oidcRequests = cache.New(oidcRequestTimeout, oidcRequestCleanupInterval)
b.Backend = &framework.Backend{
AuthRenew: b.pathLoginRenew,
BackendType: logical.TypeCredential,
Invalidate: b.invalidate,
Help: backendHelp,
PathsSpecial: &logical.Paths{
Unauthenticated: []string{
"login",
"oidc/auth_url",
"oidc/callback",
// Uncomment to mount simple UI handler for local development
// "ui",
},
SealWrapStorage: []string{
"config",
},
},
Paths: framework.PathAppend(
[]*framework.Path{
pathLogin(b),
pathRoleList(b),
pathRole(b),
pathConfig(b),
// Uncomment to mount simple UI handler for local development
// pathUI(b),
},
pathOIDC(b),
),
Clean: b.cleanup,
}
return b
}
func (b *jwtAuthBackend) cleanup(_ context.Context) {
b.l.Lock()
if b.providerCtxCancel != nil {
b.providerCtxCancel()
}
if b.provider != nil {
b.provider.Done()
}
b.l.Unlock()
}
func (b *jwtAuthBackend) invalidate(ctx context.Context, key string) {
switch key {
case "config":
b.reset()
}
}
func (b *jwtAuthBackend) reset() {
b.l.Lock()
if b.provider != nil {
b.provider.Done()
}
b.provider = nil
b.cachedConfig = nil
b.validator = nil
b.l.Unlock()
}
func (b *jwtAuthBackend) getProvider(config *jwtConfig) (*oidc.Provider, error) {
b.l.Lock()
defer b.l.Unlock()
if b.provider != nil {
return b.provider, nil
}
provider, err := b.createProvider(config)
if err != nil {
return nil, err
}
b.provider = provider
return provider, nil
}
// jwtValidator returns a new JWT validator based on the provided config.
func (b *jwtAuthBackend) jwtValidator(config *jwtConfig) (*jwt.Validator, error) {
b.l.Lock()
defer b.l.Unlock()
if b.validator != nil {
return b.validator, nil
}
var err error
var keySet jwt.KeySet
var keySets []jwt.KeySet
// Configure the key set for the validator
switch config.authType() {
case JWKS:
keySet, err = jwt.NewJSONWebKeySet(b.providerCtx, config.JWKSURL, config.JWKSCAPEM)
keySets = []jwt.KeySet{keySet}
case MultiJWKS:
pairs, pairsErr := NewJWKSPairsConfig(config)
if pairsErr != nil {
return nil, pairsErr
}
for _, p := range pairs {
keySet, keySetErr := jwt.NewJSONWebKeySet(b.providerCtx, p.JWKSUrl, p.JWKSCAPEM)
if keySetErr != nil {
err = multierror.Append(err, keySetErr)
continue
}
keySets = append(keySets, keySet)
}
case StaticKeys:
keySet, err = jwt.NewStaticKeySet(config.ParsedJWTPubKeys)
keySets = []jwt.KeySet{keySet}
case OIDCDiscovery:
keySet, err = jwt.NewOIDCDiscoveryKeySet(b.providerCtx, config.OIDCDiscoveryURL, config.OIDCDiscoveryCAPEM)
keySets = []jwt.KeySet{keySet}
default:
return nil, errors.New("unsupported config type")
}
if err != nil {
return nil, fmt.Errorf("keyset configuration error: %w", err)
}
validator, err := jwt.NewValidator(keySets...)
if err != nil {
return nil, fmt.Errorf("JWT validator configuration error: %w", err)
}
b.validator = validator
return b.validator, nil
}
const (
backendHelp = `
The JWT backend plugin allows authentication using JWTs (including OIDC).
`
)