Skip to content

Commit 13449ad

Browse files
Aeneas Rekkas (arekkas)rakyll
Aeneas Rekkas (arekkas)
authored andcommitted
internal: urlencode client id and secret in header
As per https://tools.ietf.org/html/rfc6749#section-2.3.1 client IDs and secrets must be urlencoded in the authorization header. This patch addresses this by wrapping clientID and clientSecret with url.QueryEscape. A dedicated test for unsafe-url client IDs and secrets has been added as well. Closes golang#237 Change-Id: I1f277b52caef4932e14147be8fb1712203da51d0 Reviewed-on: https://go-review.googlesource.com/46473 Reviewed-by: JBD <[email protected]>
1 parent d89af98 commit 13449ad

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

internal/token.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func RetrieveToken(ctx context.Context, clientID, clientSecret, tokenURL string,
188188
}
189189
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
190190
if !bustedAuth {
191-
req.SetBasicAuth(clientID, clientSecret)
191+
req.SetBasicAuth(url.QueryEscape(clientID), url.QueryEscape(clientSecret))
192192
}
193193
r, err := ctxhttp.Do(ctx, hc, req)
194194
if err != nil {

oauth2_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ func TestAuthCodeURL_Optional(t *testing.T) {
7272
}
7373
}
7474

75+
func TestURLUnsafeClientConfig(t *testing.T) {
76+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
77+
if got, want := r.Header.Get("Authorization"), "Basic Q0xJRU5UX0lEJTNGJTNGOkNMSUVOVF9TRUNSRVQlM0YlM0Y="; got != want {
78+
t.Errorf("Authorization header = %q; want %q", got, want)
79+
}
80+
81+
w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
82+
w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&scope=user&token_type=bearer"))
83+
}))
84+
defer ts.Close()
85+
conf := newConf(ts.URL)
86+
conf.ClientID = "CLIENT_ID??"
87+
conf.ClientSecret = "CLIENT_SECRET??"
88+
_, err := conf.Exchange(context.Background(), "exchange-code")
89+
if err != nil {
90+
t.Error(err)
91+
}
92+
}
93+
7594
func TestExchangeRequest(t *testing.T) {
7695
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7796
if r.URL.String() != "/token" {

0 commit comments

Comments
 (0)