-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
token_validator.go
113 lines (95 loc) · 3.07 KB
/
token_validator.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
/*
Copyright 2022 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package githubactions
import (
"context"
"fmt"
"time"
"github.com/coreos/go-oidc"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/gravitational/teleport/lib/jwt"
)
type IDTokenValidatorConfig struct {
// Clock is used by the validator when checking expiry and issuer times of
// tokens. If omitted, a real clock will be used.
Clock clockwork.Clock
// GitHubIssuerHost is the host of the Issuer for tokens issued by
// GitHub's cloud hosted version. If no GHESHost override is provided to
// the call to Validate, then this will be used as the host.
GitHubIssuerHost string
// insecure configures the validator to use HTTP rather than HTTPS. This
// is not exported as this is only used in the test for now.
insecure bool
}
type IDTokenValidator struct {
IDTokenValidatorConfig
}
func NewIDTokenValidator(cfg IDTokenValidatorConfig) *IDTokenValidator {
if cfg.GitHubIssuerHost == "" {
cfg.GitHubIssuerHost = DefaultIssuerHost
}
if cfg.Clock == nil {
cfg.Clock = clockwork.NewRealClock()
}
return &IDTokenValidator{
IDTokenValidatorConfig: cfg,
}
}
func (id *IDTokenValidator) issuerURL(
GHESHost string, enterpriseSlug string,
) string {
scheme := "https"
if id.insecure {
scheme = "http"
}
if GHESHost == "" {
url := fmt.Sprintf("%s://%s", scheme, id.GitHubIssuerHost)
// Support custom enterprise slugs, as per:
// https://docs.github.com/en/enterprise-cloud@latest/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect#customizing-the-issuer-value-for-an-enterprise
if enterpriseSlug != "" {
url = fmt.Sprintf("%s/%s", url, enterpriseSlug)
}
return url
}
return fmt.Sprintf("%s://%s/_services/token", scheme, GHESHost)
}
func (id *IDTokenValidator) Validate(
ctx context.Context, GHESHost string, enterpriseSlug string, token string,
) (*IDTokenClaims, error) {
p, err := oidc.NewProvider(
ctx,
id.issuerURL(GHESHost, enterpriseSlug),
)
if err != nil {
return nil, trace.Wrap(err)
}
verifier := p.Verifier(&oidc.Config{
ClientID: "teleport.cluster.local",
Now: id.Clock.Now,
})
idToken, err := verifier.Verify(ctx, token)
if err != nil {
return nil, trace.Wrap(err)
}
// `go-oidc` does not implement not before check, so we need to manually
// perform this
if err := jwt.CheckNotBefore(id.Clock.Now(), time.Minute*2, idToken); err != nil {
return nil, trace.Wrap(err)
}
claims := IDTokenClaims{}
if err := idToken.Claims(&claims); err != nil {
return nil, trace.Wrap(err)
}
return &claims, nil
}