From 0b2492c5312a1bbd71085236e5af682b04a8f86c Mon Sep 17 00:00:00 2001 From: JP Robinson Date: Mon, 5 Nov 2018 13:50:16 -0500 Subject: [PATCH] adding identity helper func --- auth/gcp/identity.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/auth/gcp/identity.go b/auth/gcp/identity.go index 5f1c5704e..998c28df2 100644 --- a/auth/gcp/identity.go +++ b/auth/gcp/identity.go @@ -31,6 +31,28 @@ type idKeySource struct { cfg IdentityConfig } +// NewDefaultIdentityVerifier will verify tokens that have the same default service +// account as the server running this verifier. +func NewDefaultIdentityVerifier(ctx context.Context, cfg IdentityConfig) (*auth.Verifier, error) { + if cfg.Client == nil { + cfg.Client = &http.Client{Timeout: 2 * time.Second} + } + + ks, err := NewIdentityPublicKeySource(ctx, cfg) + if err != nil { + return nil, err + } + + eml, err := GetDefaultEmail(ctx, "", cfg.Client) + if err != nil { + return nil, errors.Wrap(err, "unable to get default email") + } + + return auth.NewVerifier(ks, + IdentityClaimsDecoderFunc, + VerifyIdentityEmails(ctx, []string{eml}, cfg.Audience)), nil +} + // NewIdentityPublicKeySource fetches Google's public oauth2 certificates to be used with // the auth.Verifier tool. func NewIdentityPublicKeySource(ctx context.Context, cfg IdentityConfig) (auth.PublicKeySource, error) {