FAQ
Reviewers: adg1,

Message:
Hello adg@google.com (cc: golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/goauth2/


Description:
goauth2: Use content-type detection to select codec

Please review this at http://codereview.appspot.com/6900055/

Affected files:
M oauth/oauth.go
M oauth/oauth_test.go


Index: oauth/oauth.go
===================================================================
--- a/oauth/oauth.go
+++ b/oauth/oauth.go
@@ -39,9 +39,11 @@

import (
"encoding/json"
+ "io/ioutil"
"net/http"
"net/url"
"os"
+ "strings"
"time"
)

@@ -249,7 +251,7 @@
}

// Make the HTTP request.
- req.Header.Set("Authorization", "OAuth "+t.AccessToken)
+ req.Header.Set("Authorization", "Bearer "+t.AccessToken)
return t.transport().RoundTrip(req)
}

@@ -290,8 +292,25 @@
Refresh string `json:"refresh_token"`
ExpiresIn time.Duration `json:"expires_in"`
}
- if err = json.NewDecoder(r.Body).Decode(&b); err != nil {
- return err
+
+ content := strings.Split(r.Header.Get("Content-Type"), ";")
+ switch content[0] {
+ case "application/x-www-form-urlencoded", "text/plain":
+ body, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ return err
+ }
+ vals, err := url.ParseQuery(string(body))
+ if err != nil {
+ return err
+ }
+
+ b.Access = vals.Get("access_token")
+ b.ExpiresIn, _ = time.ParseDuration(vals.Get("expires") + "s")
+ default:
+ if err = json.NewDecoder(r.Body).Decode(&b); err != nil {
+ return err
+ }
}
tok.AccessToken = b.Access
// Don't overwrite `RefreshToken` with an empty value
Index: oauth/oauth_test.go
===================================================================
--- a/oauth/oauth_test.go
+++ b/oauth/oauth_test.go
@@ -29,7 +29,7 @@
}
`,
},
- {path: "/secure", auth: "OAuth token1", body: "first payload"},
+ {path: "/secure", auth: "Bearer token1", body: "first payload"},
{
path: "/token",

query: "grant_type=refresh_token&refresh_token=refreshtoken1&client_id=cl13nt1d",
@@ -41,13 +41,14 @@
}
`,
},
- {path: "/secure", auth: "OAuth token2", body: "second payload"},
+ {path: "/secure", auth: "Bearer token2", body: "second payload"},
}

func TestOAuth(t *testing.T) {
// Set up test server.
n := 0
handler := func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
if n >= len(requests) {
t.Errorf("too many requests: %d", n)
return

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 8, '12 at 11:21p
activeDec 10, '12 at 12:40a
posts3
users2
websitegolang.org

2 users in discussion

Surma: 2 posts Andrew Gerrand: 1 post

People

Translate

site design / logo © 2022 Grokbase