Merge pull request #36 from jtolds/master

fix token exchange
This commit is contained in:
Burcu Dogan 2014-09-08 09:58:20 -07:00
commit 16c7b8dd8a
2 changed files with 60 additions and 7 deletions

View File

@ -215,10 +215,6 @@ func (c *Config) retrieveToken(v url.Values) (*Token, error) {
content, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
switch content {
case "application/x-www-form-urlencoded", "text/plain":
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
vals, err := url.ParseQuery(string(body))
if err != nil {
return nil, err
@ -229,7 +225,7 @@ func (c *Config) retrieveToken(v url.Values) (*Token, error) {
resp.ExpiresIn, _ = strconv.ParseInt(vals.Get("expires_in"), 10, 64)
resp.IdToken = vals.Get("id_token")
default:
if err = json.NewDecoder(r.Body).Decode(&resp); err != nil {
if err = json.Unmarshal(body, &resp); err != nil {
return nil, err
}
}

View File

@ -64,14 +64,71 @@ func TestExchangeRequest(t *testing.T) {
if headerContentType != "application/x-www-form-urlencoded" {
t.Errorf("Unexpected Content-Type header, %v is found.", headerContentType)
}
body, _ := ioutil.ReadAll(r.Body)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Failed reading request body: %s.", err)
}
if string(body) != "client_id=CLIENT_ID&code=exchange-code&grant_type=authorization_code&redirect_uri=REDIRECT_URL&scope=scope1+scope2" {
t.Errorf("Unexpected exchange payload, %v is found.", string(body))
}
w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&scope=user&token_type=bearer"))
}))
defer ts.Close()
conf := newTestConf(ts.URL)
conf.Exchange("exchange-code")
tok, err := conf.Exchange("exchange-code")
if err != nil {
t.Errorf("Failed retrieving token: %s.", err)
}
if tok.Expired() {
t.Errorf("Token shouldn't be expired.")
}
if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
t.Errorf("Wrong access token, %#v.", tok.AccessToken)
}
if tok.TokenType != "bearer" {
t.Errorf("Wrong token type, %#v.", tok.TokenType)
}
}
func TestExchangeRequest_JsonResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.String() != "/token" {
t.Errorf("Unexpected exchange request URL, %v is found.", r.URL)
}
headerAuth := r.Header.Get("Authorization")
if headerAuth != "Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ=" {
t.Errorf("Unexpected authorization header, %v is found.", headerAuth)
}
headerContentType := r.Header.Get("Content-Type")
if headerContentType != "application/x-www-form-urlencoded" {
t.Errorf("Unexpected Content-Type header, %v is found.", headerContentType)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Failed reading request body: %s.", err)
}
if string(body) != "client_id=CLIENT_ID&code=exchange-code&grant_type=authorization_code&redirect_uri=REDIRECT_URL&scope=scope1+scope2" {
t.Errorf("Unexpected exchange payload, %v is found.", string(body))
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"access_token": "90d64460d14870c08c81352a05dedd3465940a7c", "scope": "user", "token_type": "bearer"}`))
}))
defer ts.Close()
conf := newTestConf(ts.URL)
tok, err := conf.Exchange("exchange-code")
if err != nil {
t.Errorf("Failed retrieving token: %s.", err)
}
if tok.Expired() {
t.Errorf("Token shouldn't be expired.")
}
if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
t.Errorf("Wrong access token, %#v.", tok.AccessToken)
}
if tok.TokenType != "bearer" {
t.Errorf("Wrong token type, %#v.", tok.TokenType)
}
}
func TestExchangeRequest_NonBasicAuth(t *testing.T) {