From e296c42d1289ca37dc4bd477b8c48add0b048be0 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Fri, 8 May 2015 14:14:37 -0700 Subject: [PATCH] oauth2: add StaticTokenSource to return static tokens Fixes #120 Change-Id: I2ef0cbf87c7124b89a68b5db0080f916c630072d Reviewed-on: https://go-review.googlesource.com/9895 Reviewed-by: Burcu Dogan --- oauth2.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/oauth2.go b/oauth2.go index b879351..cca8b18 100644 --- a/oauth2.go +++ b/oauth2.go @@ -253,6 +253,22 @@ func (s *reuseTokenSource) Token() (*Token, error) { return t, nil } +// StaticTokenSource returns a TokenSource that always returns the same token. +// Because the provided token t is never refreshed, StaticTokenSource is only +// useful for tokens that never expire. +func StaticTokenSource(t *Token) TokenSource { + return staticTokenSource{t} +} + +// staticTokenSource is a TokenSource that always returns the same Token. +type staticTokenSource struct { + t *Token +} + +func (s staticTokenSource) Token() (*Token, error) { + return s.t, nil +} + // HTTPClient is the context key to use with golang.org/x/net/context's // WithValue function to associate an *http.Client value with a context. var HTTPClient internal.ContextKey