From 90de3012108b80bdcb6e073973ce86cd1cd06362 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 10 Aug 2014 10:52:19 -0700 Subject: [PATCH] Update goauth2 in third_party to 8273d5a0e11a Change-Id: Ibe23ad22887ba8ddc6c6a02e8ff81525015e96e6 --- .../code.google.com/p/goauth2/oauth/oauth.go | 22 +++++++++++- .../p/goauth2/oauth/oauth_test.go | 36 ++++++++++++++++--- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/third_party/code.google.com/p/goauth2/oauth/oauth.go b/third_party/code.google.com/p/goauth2/oauth/oauth.go index ef6deb3da..25ff2a09a 100644 --- a/third_party/code.google.com/p/goauth2/oauth/oauth.go +++ b/third_party/code.google.com/p/goauth2/oauth/oauth.go @@ -44,6 +44,7 @@ import ( "net/http" "net/url" "os" + "strings" "time" ) @@ -316,10 +317,29 @@ func (t *Transport) Refresh() error { return nil } +// AuthenticateClient gets an access Token using the client_credentials grant +// type. +func (t *Transport) AuthenticateClient() error { + if t.Config == nil { + return OAuthError{"Exchange", "no Config supplied"} + } + if t.Token == nil { + t.Token = &Token{} + } + return t.updateToken(t.Token, url.Values{"grant_type": {"client_credentials"}}) +} + func (t *Transport) updateToken(tok *Token, v url.Values) error { v.Set("client_id", t.ClientId) v.Set("client_secret", t.ClientSecret) - r, err := (&http.Client{Transport: t.transport()}).PostForm(t.TokenURL, v) + client := &http.Client{Transport: t.transport()} + req, err := http.NewRequest("POST", t.TokenURL, strings.NewReader(v.Encode())) + if err != nil { + return err + } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.SetBasicAuth(t.ClientId, t.ClientSecret) + r, err := client.Do(req) if err != nil { return err } diff --git a/third_party/code.google.com/p/goauth2/oauth/oauth_test.go b/third_party/code.google.com/p/goauth2/oauth/oauth_test.go index 69b887124..b903c1607 100644 --- a/third_party/code.google.com/p/goauth2/oauth/oauth_test.go +++ b/third_party/code.google.com/p/goauth2/oauth/oauth_test.go @@ -23,8 +23,9 @@ var requests = []struct { }{ { path: "/token", - query: "grant_type=authorization_code&code=c0d3&client_id=cl13nt1d", + query: "grant_type=authorization_code&code=c0d3&client_id=cl13nt1d&client_secret=s3cr3t", contenttype: "application/json", + auth: "Basic Y2wxM250MWQ6czNjcjN0", body: ` { "access_token":"token1", @@ -37,8 +38,9 @@ var requests = []struct { {path: "/secure", auth: "Bearer token1", body: "first payload"}, { path: "/token", - query: "grant_type=refresh_token&refresh_token=refreshtoken1&client_id=cl13nt1d", + query: "grant_type=refresh_token&refresh_token=refreshtoken1&client_id=cl13nt1d&client_secret=s3cr3t", contenttype: "application/json", + auth: "Basic Y2wxM250MWQ6czNjcjN0", body: ` { "access_token":"token2", @@ -51,11 +53,25 @@ var requests = []struct { {path: "/secure", auth: "Bearer token2", body: "second payload"}, { path: "/token", - query: "grant_type=refresh_token&refresh_token=refreshtoken2&client_id=cl13nt1d", + query: "grant_type=refresh_token&refresh_token=refreshtoken2&client_id=cl13nt1d&client_secret=s3cr3t", contenttype: "application/x-www-form-urlencoded", body: "access_token=token3&refresh_token=refreshtoken3&id_token=idtoken3&expires_in=3600", + auth: "Basic Y2wxM250MWQ6czNjcjN0", }, {path: "/secure", auth: "Bearer token3", body: "third payload"}, + { + path: "/token", + query: "grant_type=client_credentials&client_id=cl13nt1d&client_secret=s3cr3t", + contenttype: "application/json", + auth: "Basic Y2wxM250MWQ6czNjcjN0", + body: ` + { + "access_token":"token4", + "expires_in":3600 + } + `, + }, + {path: "/secure", auth: "Bearer token4", body: "fourth payload"}, } func TestOAuth(t *testing.T) { @@ -131,6 +147,18 @@ func TestOAuth(t *testing.T) { } checkBody(t, resp, "third payload") checkToken(t, transport.Token, "token3", "refreshtoken3", "idtoken3") + + transport.Token = &Token{} + err = transport.AuthenticateClient() + if err != nil { + t.Fatalf("AuthenticateClient: %v", err) + } + checkToken(t, transport.Token, "token4", "", "") + resp, err = c.Get(server.URL + "/secure") + if err != nil { + t.Fatalf("Get: %v", err) + } + checkBody(t, resp, "fourth payload") } func checkToken(t *testing.T, tok *Token, access, refresh, id string) { @@ -152,7 +180,7 @@ func checkToken(t *testing.T, tok *Token, access, refresh, id string) { func checkBody(t *testing.T, r *http.Response, body string) { b, err := ioutil.ReadAll(r.Body) if err != nil { - t.Error("reading reponse body: %v, want %q", err, body) + t.Errorf("reading reponse body: %v, want %q", err, body) } if g, w := string(b), body; g != w { t.Errorf("request body mismatch: got %q, want %q", g, w)