-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathverifier.go
47 lines (37 loc) · 1.25 KB
/
verifier.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
// Package cloudidentity provides functionality for dealing with software
// identities in cloud environments, such as OpenID Connect ID token
// acquirement and authorization.
package cloudidentity
import (
"context"
oidc "github.com/coreos/go-oidc"
)
// IDTokenVerifier provides a method for verifying an OpenID Connect ID token.
// Internally it caches the public key set used for the verification so
// that the operation is as efficient as possible.
type IDTokenVerifier struct {
oidcVerifier *oidc.IDTokenVerifier
}
// VerifyIDToken verifies an ID token.
// The parameter token is the JWT string.
// Returns IDToken and nil error when verify succeeds.
func (v *IDTokenVerifier) VerifyIDToken(ctx context.Context,
token string) (*oidc.IDToken, error) {
return v.oidcVerifier.Verify(ctx, token)
}
// NewVerifier creates a new IDTokenVerifier that internally caches the
// remote key set used for ID token verification.
func NewVerifier(ctx context.Context,
issuerURL, aud string) (*IDTokenVerifier, error) {
provider, err := oidc.NewProvider(ctx, issuerURL)
if err != nil {
return nil, err
}
var config = &oidc.Config{
ClientID: aud,
}
idTokenVerifier := &IDTokenVerifier{
oidcVerifier: provider.Verifier(config),
}
return idTokenVerifier, nil
}