oauth2, oauth2/google: add, use ReuseTokenSource

Token caching is now done whenever you make a Client, and
ReuseTokenSource is exported from the oauth2 package and used by the
Google TokenSources (Compute and App Engine).

Token.Expired is now Token.Valid, and works on nil receivers.

Some other wording cleanups in the process.

All tests pass. App Engine should pass, but is untested.

Change-Id: Ibe1d2599ac3ccfe9b399b1672f74bb24cfc8d311
Reviewed-on: https://go-review.googlesource.com/2195
Reviewed-by: Burcu Dogan <jbd@google.com>
This commit is contained in:
Brad Fitzpatrick
2014-12-30 13:25:01 -08:00
parent e5909d4679
commit a379e41d44
10 changed files with 97 additions and 68 deletions

View File

@@ -55,12 +55,12 @@ func TestJWTFetch_JSONResponse(t *testing.T) {
PrivateKey: dummyPrivateKey,
TokenURL: ts.URL,
}
tok, err := conf.TokenSource(NoContext, nil).Token()
tok, err := conf.TokenSource(NoContext).Token()
if err != nil {
t.Fatal(err)
}
if tok.Expired() {
t.Errorf("Token shouldn't be expired")
if !tok.Valid() {
t.Errorf("Token invalid")
}
if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
t.Errorf("Unexpected access token, %#v", tok.AccessToken)
@@ -89,19 +89,25 @@ func TestJWTFetch_BadResponse(t *testing.T) {
PrivateKey: dummyPrivateKey,
TokenURL: ts.URL,
}
tok, err := conf.TokenSource(NoContext, nil).Token()
tok, err := conf.TokenSource(NoContext).Token()
if err != nil {
t.Fatal(err)
}
if tok.AccessToken != "" {
t.Errorf("Unexpected access token, %#v.", tok.AccessToken)
if tok == nil {
t.Fatalf("token is nil")
}
if tok.TokenType != "bearer" {
t.Errorf("Unexpected token type, %#v.", tok.TokenType)
if tok.Valid() {
t.Errorf("token is valid. want invalid.")
}
if tok.AccessToken != "" {
t.Errorf("Unexpected non-empty access token %q.", tok.AccessToken)
}
if want := "bearer"; tok.TokenType != want {
t.Errorf("TokenType = %q; want %q", tok.TokenType, want)
}
scope := tok.Extra("scope")
if scope != "user" {
t.Errorf("Unexpected value for scope: %v", scope)
if want := "user"; scope != want {
t.Errorf("token scope = %q; want %q", scope, want)
}
}
@@ -116,7 +122,7 @@ func TestJWTFetch_BadResponseType(t *testing.T) {
PrivateKey: dummyPrivateKey,
TokenURL: ts.URL,
}
tok, err := conf.TokenSource(NoContext, nil).Token()
tok, err := conf.TokenSource(NoContext).Token()
if err == nil {
t.Error("got a token; expected error")
if tok.AccessToken != "" {