-
Notifications
You must be signed in to change notification settings - Fork 12
/
auth.go
72 lines (64 loc) · 1.89 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
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"errors"
"net/url"
"strings"
"github.com/xmidt-org/arrange"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/bascule/basculechecks"
"github.com/xmidt-org/bascule/basculehttp"
"github.com/xmidt-org/clortho"
"go.uber.org/fx"
)
var possiblePrefixURLs = []string{
"/" + apiBase,
"/" + prevAPIBase,
}
// JWTValidator provides a convenient way to define jwt validator through config files
type JWTValidator struct {
// Config is used to create the clortho Resolver & Refresher for JWT verification keys
Config clortho.Config
// Leeway is used to set the amount of time buffer should be given to JWT
// time values, such as nbf
Leeway bascule.Leeway
}
func provideAuthChain(configKey string) fx.Option {
return fx.Options(
basculehttp.ProvideMetrics(),
basculechecks.ProvideMetrics(),
fx.Provide(
func() basculehttp.ParseURL {
return createRemovePrefixURLFuncLegacy(possiblePrefixURLs)
},
arrange.UnmarshalKey("jwtValidator", JWTValidator{}),
func(c JWTValidator) clortho.Config {
return c.Config
},
),
basculehttp.ProvideBasicAuth(configKey),
basculehttp.ProvideBearerTokenFactory("jwtValidator", false),
basculechecks.ProvideRegexCapabilitiesValidator("capabilityCheck"),
basculehttp.ProvideBearerValidator(),
basculehttp.ProvideServerChain(),
)
}
func createRemovePrefixURLFuncLegacy(prefixes []string) basculehttp.ParseURL {
return func(u *url.URL) (*url.URL, error) {
escapedPath := u.EscapedPath()
var prefix string
for _, p := range prefixes {
if strings.HasPrefix(escapedPath, p) {
prefix = p
break
}
}
if prefix == "" {
return nil, errors.New("unexpected URL, did not start with expected prefix")
}
u.Path = escapedPath[len(prefix):]
u.RawPath = escapedPath[len(prefix):]
return u, nil
}
}