2015-08-26 20:42:02 -04:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-05-17 11:26:57 -04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-05-13 14:06:46 -04:00
|
|
|
|
2014-05-05 17:54:23 -04:00
|
|
|
package oauth2
|
|
|
|
|
|
|
|
import (
|
2018-11-01 11:53:36 -04:00
|
|
|
"context"
|
2014-05-05 17:54:23 -04:00
|
|
|
"errors"
|
2015-03-09 17:25:31 -04:00
|
|
|
"fmt"
|
2019-01-14 17:52:49 -05:00
|
|
|
"io"
|
2014-05-05 17:54:23 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2014-09-04 16:28:18 -04:00
|
|
|
"net/http/httptest"
|
2015-09-29 20:24:17 -04:00
|
|
|
"net/url"
|
2014-05-05 17:54:23 -04:00
|
|
|
"testing"
|
2015-03-06 03:54:39 -05:00
|
|
|
"time"
|
2019-01-14 17:52:49 -05:00
|
|
|
|
|
|
|
"golang.org/x/oauth2/internal"
|
2014-05-05 17:54:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockTransport struct {
|
|
|
|
rt func(req *http.Request) (resp *http.Response, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *mockTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
|
|
|
return t.rt(req)
|
|
|
|
}
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
func newConf(url string) *Config {
|
|
|
|
return &Config{
|
|
|
|
ClientID: "CLIENT_ID",
|
|
|
|
ClientSecret: "CLIENT_SECRET",
|
|
|
|
RedirectURL: "REDIRECT_URL",
|
|
|
|
Scopes: []string{"scope1", "scope2"},
|
|
|
|
Endpoint: Endpoint{
|
|
|
|
AuthURL: url + "/auth",
|
|
|
|
TokenURL: url + "/token",
|
|
|
|
},
|
|
|
|
}
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthCodeURL(t *testing.T) {
|
2014-12-11 02:30:13 -05:00
|
|
|
conf := newConf("server")
|
|
|
|
url := conf.AuthCodeURL("foo", AccessTypeOffline, ApprovalForce)
|
2019-04-08 23:29:41 -04:00
|
|
|
const want = "server/auth?access_type=offline&client_id=CLIENT_ID&prompt=consent&redirect_uri=REDIRECT_URL&response_type=code&scope=scope1+scope2&state=foo"
|
2016-08-26 19:14:08 -04:00
|
|
|
if got := url; got != want {
|
|
|
|
t.Errorf("got auth code URL = %q; want %q", got, want)
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 00:22:14 -04:00
|
|
|
func TestAuthCodeURL_CustomParam(t *testing.T) {
|
|
|
|
conf := newConf("server")
|
2015-04-06 09:53:19 -04:00
|
|
|
param := SetAuthURLParam("foo", "bar")
|
2015-03-26 00:22:14 -04:00
|
|
|
url := conf.AuthCodeURL("baz", param)
|
2016-08-26 19:14:08 -04:00
|
|
|
const want = "server/auth?client_id=CLIENT_ID&foo=bar&redirect_uri=REDIRECT_URL&response_type=code&scope=scope1+scope2&state=baz"
|
|
|
|
if got := url; got != want {
|
|
|
|
t.Errorf("got auth code = %q; want %q", got, want)
|
2015-03-26 00:22:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 16:28:18 -04:00
|
|
|
func TestAuthCodeURL_Optional(t *testing.T) {
|
2014-12-11 02:30:13 -05:00
|
|
|
conf := &Config{
|
|
|
|
ClientID: "CLIENT_ID",
|
|
|
|
Endpoint: Endpoint{
|
|
|
|
AuthURL: "/auth-url",
|
|
|
|
TokenURL: "/token-url",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
url := conf.AuthCodeURL("")
|
2016-08-26 19:14:08 -04:00
|
|
|
const want = "/auth-url?client_id=CLIENT_ID&response_type=code"
|
|
|
|
if got := url; got != want {
|
|
|
|
t.Fatalf("got auth code = %q; want %q", got, want)
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
|
|
|
|
2017-06-22 11:12:08 -04:00
|
|
|
func TestURLUnsafeClientConfig(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, want := r.Header.Get("Authorization"), "Basic Q0xJRU5UX0lEJTNGJTNGOkNMSUVOVF9TRUNSRVQlM0YlM0Y="; got != want {
|
|
|
|
t.Errorf("Authorization header = %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 := newConf(ts.URL)
|
|
|
|
conf.ClientID = "CLIENT_ID??"
|
|
|
|
conf.ClientSecret = "CLIENT_SECRET??"
|
|
|
|
_, err := conf.Exchange(context.Background(), "exchange-code")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 16:28:18 -04:00
|
|
|
func TestExchangeRequest(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.String() != "/token" {
|
2019-01-14 17:52:49 -05:00
|
|
|
t.Errorf("Unexpected exchange request URL %q", r.URL)
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
|
|
|
headerAuth := r.Header.Get("Authorization")
|
2019-01-14 17:52:49 -05:00
|
|
|
if want := "Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ="; headerAuth != want {
|
|
|
|
t.Errorf("Unexpected authorization header %q, want %q", headerAuth, want)
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
|
|
|
headerContentType := r.Header.Get("Content-Type")
|
|
|
|
if headerContentType != "application/x-www-form-urlencoded" {
|
2019-01-14 17:52:49 -05:00
|
|
|
t.Errorf("Unexpected Content-Type header %q", headerContentType)
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
2014-09-06 21:03:51 -04:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed reading request body: %s.", err)
|
|
|
|
}
|
2016-06-04 00:11:54 -04:00
|
|
|
if string(body) != "code=exchange-code&grant_type=authorization_code&redirect_uri=REDIRECT_URL" {
|
2019-01-14 17:52:49 -05:00
|
|
|
t.Errorf("Unexpected exchange payload; got %q", body)
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
2014-09-06 21:03:51 -04:00
|
|
|
w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
|
2014-09-06 19:39:43 -04:00
|
|
|
w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&scope=user&token_type=bearer"))
|
2014-09-04 16:28:18 -04:00
|
|
|
}))
|
|
|
|
defer ts.Close()
|
2014-12-11 02:30:13 -05:00
|
|
|
conf := newConf(ts.URL)
|
2016-08-24 18:40:36 -04:00
|
|
|
tok, err := conf.Exchange(context.Background(), "exchange-code")
|
2014-09-06 19:39:43 -04:00
|
|
|
if err != nil {
|
2014-11-06 19:36:41 -05:00
|
|
|
t.Error(err)
|
2014-09-06 19:39:43 -04:00
|
|
|
}
|
2014-12-30 16:25:01 -05:00
|
|
|
if !tok.Valid() {
|
|
|
|
t.Fatalf("Token invalid. Got: %#v", tok)
|
2014-09-06 19:39:43 -04:00
|
|
|
}
|
|
|
|
if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
|
2014-10-27 20:35:35 -04:00
|
|
|
t.Errorf("Unexpected access token, %#v.", tok.AccessToken)
|
2014-09-06 19:39:43 -04:00
|
|
|
}
|
|
|
|
if tok.TokenType != "bearer" {
|
2014-10-27 20:35:35 -04:00
|
|
|
t.Errorf("Unexpected token type, %#v.", tok.TokenType)
|
|
|
|
}
|
|
|
|
scope := tok.Extra("scope")
|
|
|
|
if scope != "user" {
|
|
|
|
t.Errorf("Unexpected value for scope: %v", scope)
|
2014-09-06 19:39:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 16:45:12 -04:00
|
|
|
func TestExchangeRequest_CustomParam(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) != "code=exchange-code&foo=bar&grant_type=authorization_code&redirect_uri=REDIRECT_URL" {
|
|
|
|
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 := newConf(ts.URL)
|
|
|
|
|
|
|
|
param := SetAuthURLParam("foo", "bar")
|
|
|
|
tok, err := conf.Exchange(context.Background(), "exchange-code", param)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !tok.Valid() {
|
|
|
|
t.Fatalf("Token invalid. Got: %#v", tok)
|
|
|
|
}
|
|
|
|
if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
|
|
|
|
t.Errorf("Unexpected access token, %#v.", tok.AccessToken)
|
|
|
|
}
|
|
|
|
if tok.TokenType != "bearer" {
|
|
|
|
t.Errorf("Unexpected token type, %#v.", tok.TokenType)
|
|
|
|
}
|
|
|
|
scope := tok.Extra("scope")
|
|
|
|
if scope != "user" {
|
|
|
|
t.Errorf("Unexpected value for scope: %v", scope)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:35:35 -04:00
|
|
|
func TestExchangeRequest_JSONResponse(t *testing.T) {
|
2014-09-06 19:39:43 -04:00
|
|
|
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)
|
|
|
|
}
|
2014-09-06 21:03:51 -04:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed reading request body: %s.", err)
|
|
|
|
}
|
2016-06-04 00:11:54 -04:00
|
|
|
if string(body) != "code=exchange-code&grant_type=authorization_code&redirect_uri=REDIRECT_URL" {
|
2014-09-06 19:39:43 -04:00
|
|
|
t.Errorf("Unexpected exchange payload, %v is found.", string(body))
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2014-11-10 14:59:36 -05:00
|
|
|
w.Write([]byte(`{"access_token": "90d64460d14870c08c81352a05dedd3465940a7c", "scope": "user", "token_type": "bearer", "expires_in": 86400}`))
|
2014-09-06 19:39:43 -04:00
|
|
|
}))
|
|
|
|
defer ts.Close()
|
2014-12-11 02:30:13 -05:00
|
|
|
conf := newConf(ts.URL)
|
2016-08-24 18:40:36 -04:00
|
|
|
tok, err := conf.Exchange(context.Background(), "exchange-code")
|
2014-09-06 19:39:43 -04:00
|
|
|
if err != nil {
|
2014-11-06 19:36:41 -05:00
|
|
|
t.Error(err)
|
2014-09-06 19:39:43 -04:00
|
|
|
}
|
2014-12-30 16:25:01 -05:00
|
|
|
if !tok.Valid() {
|
|
|
|
t.Fatalf("Token invalid. Got: %#v", tok)
|
2014-09-06 19:39:43 -04:00
|
|
|
}
|
|
|
|
if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
|
2014-10-27 20:35:35 -04:00
|
|
|
t.Errorf("Unexpected access token, %#v.", tok.AccessToken)
|
2014-09-06 19:39:43 -04:00
|
|
|
}
|
|
|
|
if tok.TokenType != "bearer" {
|
2014-10-27 20:35:35 -04:00
|
|
|
t.Errorf("Unexpected token type, %#v.", tok.TokenType)
|
|
|
|
}
|
|
|
|
scope := tok.Extra("scope")
|
|
|
|
if scope != "user" {
|
|
|
|
t.Errorf("Unexpected value for scope: %v", scope)
|
|
|
|
}
|
2015-09-29 20:24:17 -04:00
|
|
|
expiresIn := tok.Extra("expires_in")
|
|
|
|
if expiresIn != float64(86400) {
|
|
|
|
t.Errorf("Unexpected non-numeric value for expires_in: %v", expiresIn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtraValueRetrieval(t *testing.T) {
|
|
|
|
values := url.Values{}
|
|
|
|
kvmap := map[string]string{
|
|
|
|
"scope": "user", "token_type": "bearer", "expires_in": "86400.92",
|
|
|
|
"server_time": "1443571905.5606415", "referer_ip": "10.0.0.1",
|
|
|
|
"etag": "\"afZYj912P4alikMz_P11982\"", "request_id": "86400",
|
|
|
|
"untrimmed": " untrimmed ",
|
|
|
|
}
|
|
|
|
for key, value := range kvmap {
|
|
|
|
values.Set(key, value)
|
|
|
|
}
|
|
|
|
|
2016-08-26 19:14:08 -04:00
|
|
|
tok := Token{raw: values}
|
2015-09-29 20:24:17 -04:00
|
|
|
scope := tok.Extra("scope")
|
2016-08-26 19:14:08 -04:00
|
|
|
if got, want := scope, "user"; got != want {
|
|
|
|
t.Errorf("got scope = %q; want %q", got, want)
|
2015-09-29 20:24:17 -04:00
|
|
|
}
|
|
|
|
serverTime := tok.Extra("server_time")
|
2016-08-26 19:14:08 -04:00
|
|
|
if got, want := serverTime, 1443571905.5606415; got != want {
|
|
|
|
t.Errorf("got server_time value = %v; want %v", got, want)
|
2015-09-29 20:24:17 -04:00
|
|
|
}
|
2016-08-26 19:14:08 -04:00
|
|
|
refererIP := tok.Extra("referer_ip")
|
|
|
|
if got, want := refererIP, "10.0.0.1"; got != want {
|
|
|
|
t.Errorf("got referer_ip value = %v, want %v", got, want)
|
2015-09-29 20:24:17 -04:00
|
|
|
}
|
2016-08-26 19:14:08 -04:00
|
|
|
expiresIn := tok.Extra("expires_in")
|
|
|
|
if got, want := expiresIn, 86400.92; got != want {
|
|
|
|
t.Errorf("got expires_in value = %v, want %v", got, want)
|
2015-09-29 20:24:17 -04:00
|
|
|
}
|
2016-08-26 19:14:08 -04:00
|
|
|
requestID := tok.Extra("request_id")
|
|
|
|
if got, want := requestID, int64(86400); got != want {
|
|
|
|
t.Errorf("got request_id value = %v, want %v", got, want)
|
2015-09-29 20:24:17 -04:00
|
|
|
}
|
|
|
|
untrimmed := tok.Extra("untrimmed")
|
2016-08-26 19:14:08 -04:00
|
|
|
if got, want := untrimmed, " untrimmed "; got != want {
|
|
|
|
t.Errorf("got untrimmed = %q; want %q", got, want)
|
2015-09-29 20:24:17 -04:00
|
|
|
}
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
|
|
|
|
2015-03-06 03:54:39 -05:00
|
|
|
const day = 24 * time.Hour
|
|
|
|
|
|
|
|
func TestExchangeRequest_JSONResponse_Expiry(t *testing.T) {
|
|
|
|
seconds := int32(day.Seconds())
|
|
|
|
for _, c := range []struct {
|
2019-03-17 20:32:44 -04:00
|
|
|
name string
|
|
|
|
expires string
|
|
|
|
want bool
|
|
|
|
nullExpires bool
|
2015-03-06 03:54:39 -05:00
|
|
|
}{
|
2019-03-17 20:32:44 -04:00
|
|
|
{"normal", fmt.Sprintf(`"expires_in": %d`, seconds), true, false},
|
|
|
|
{"paypal", fmt.Sprintf(`"expires_in": "%d"`, seconds), true, false},
|
|
|
|
{"issue_239", fmt.Sprintf(`"expires_in": null`), true, true},
|
2019-02-11 19:33:33 -05:00
|
|
|
|
2019-03-17 20:32:44 -04:00
|
|
|
{"wrong_type", `"expires_in": false`, false, false},
|
|
|
|
{"wrong_type2", `"expires_in": {}`, false, false},
|
|
|
|
{"wrong_value", `"expires_in": "zzz"`, false, false},
|
2015-03-06 03:54:39 -05:00
|
|
|
} {
|
2019-02-11 19:33:33 -05:00
|
|
|
t.Run(c.name, func(t *testing.T) {
|
2019-03-17 20:32:44 -04:00
|
|
|
testExchangeRequest_JSONResponse_expiry(t, c.expires, c.want, c.nullExpires)
|
2019-02-11 19:33:33 -05:00
|
|
|
})
|
2015-03-06 03:54:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 20:32:44 -04:00
|
|
|
func testExchangeRequest_JSONResponse_expiry(t *testing.T, exp string, want, nullExpires bool) {
|
2015-03-06 03:54:39 -05:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(fmt.Sprintf(`{"access_token": "90d", "scope": "user", "token_type": "bearer", %s}`, exp)))
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
conf := newConf(ts.URL)
|
|
|
|
t1 := time.Now().Add(day)
|
2016-08-24 18:40:36 -04:00
|
|
|
tok, err := conf.Exchange(context.Background(), "exchange-code")
|
2019-03-17 20:32:44 -04:00
|
|
|
t2 := t1.Add(day)
|
2016-10-06 17:47:20 -04:00
|
|
|
|
|
|
|
if got := (err == nil); got != want {
|
|
|
|
if want {
|
|
|
|
t.Errorf("unexpected error: got %v", err)
|
|
|
|
} else {
|
|
|
|
t.Errorf("unexpected success")
|
|
|
|
}
|
2015-04-01 11:31:48 -04:00
|
|
|
}
|
2016-10-06 17:47:20 -04:00
|
|
|
if !want {
|
2015-04-01 11:31:48 -04:00
|
|
|
return
|
2015-03-06 03:54:39 -05:00
|
|
|
}
|
|
|
|
if !tok.Valid() {
|
|
|
|
t.Fatalf("Token invalid. Got: %#v", tok)
|
|
|
|
}
|
|
|
|
expiry := tok.Expiry
|
2019-03-17 20:32:44 -04:00
|
|
|
|
|
|
|
if nullExpires && expiry.IsZero() {
|
|
|
|
return
|
|
|
|
}
|
2015-03-06 03:54:39 -05:00
|
|
|
if expiry.Before(t1) || expiry.After(t2) {
|
2019-03-17 20:32:44 -04:00
|
|
|
t.Errorf("Unexpected value for Expiry: %v (should be between %v and %v)", expiry, t1, t2)
|
2015-03-06 03:54:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:35:35 -04:00
|
|
|
func TestExchangeRequest_BadResponse(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(`{"scope": "user", "token_type": "bearer"}`))
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
2014-12-11 02:30:13 -05:00
|
|
|
conf := newConf(ts.URL)
|
2017-12-28 14:07:42 -05:00
|
|
|
_, err := conf.Exchange(context.Background(), "code")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error from missing access_token")
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExchangeRequest_BadResponseType(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(`{"access_token":123, "scope": "user", "token_type": "bearer"}`))
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
2014-12-11 02:30:13 -05:00
|
|
|
conf := newConf(ts.URL)
|
2016-08-24 18:40:36 -04:00
|
|
|
_, err := conf.Exchange(context.Background(), "exchange-code")
|
2014-12-19 00:20:30 -05:00
|
|
|
if err == nil {
|
2017-12-28 14:07:42 -05:00
|
|
|
t.Error("expected error from non-string access_token")
|
2014-09-06 19:39:43 -04:00
|
|
|
}
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
|
2014-09-04 16:28:18 -04:00
|
|
|
func TestExchangeRequest_NonBasicAuth(t *testing.T) {
|
2019-01-14 17:52:49 -05:00
|
|
|
internal.ResetAuthCache()
|
2014-09-04 16:28:18 -04:00
|
|
|
tr := &mockTransport{
|
|
|
|
rt: func(r *http.Request) (w *http.Response, err error) {
|
|
|
|
headerAuth := r.Header.Get("Authorization")
|
|
|
|
if headerAuth != "" {
|
2019-01-14 17:52:49 -05:00
|
|
|
t.Errorf("Unexpected authorization header %q", headerAuth)
|
2014-08-13 16:59:53 -04:00
|
|
|
}
|
|
|
|
return nil, errors.New("no response")
|
|
|
|
},
|
|
|
|
}
|
2014-11-06 19:36:41 -05:00
|
|
|
c := &http.Client{Transport: tr}
|
2014-12-11 02:30:13 -05:00
|
|
|
conf := &Config{
|
|
|
|
ClientID: "CLIENT_ID",
|
|
|
|
Endpoint: Endpoint{
|
2019-01-14 17:52:49 -05:00
|
|
|
AuthURL: "https://accounts.google.com/auth",
|
|
|
|
TokenURL: "https://accounts.google.com/token",
|
|
|
|
AuthStyle: AuthStyleInParams,
|
2014-12-11 02:30:13 -05:00
|
|
|
},
|
2014-11-24 20:07:50 -05:00
|
|
|
}
|
2014-12-11 02:30:13 -05:00
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), HTTPClient, c)
|
|
|
|
conf.Exchange(ctx, "code")
|
2014-08-13 16:59:53 -04:00
|
|
|
}
|
|
|
|
|
2015-02-04 03:22:21 -05:00
|
|
|
func TestPasswordCredentialsTokenRequest(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
expected := "/token"
|
|
|
|
if r.URL.String() != expected {
|
|
|
|
t.Errorf("URL = %q; want %q", r.URL, expected)
|
|
|
|
}
|
|
|
|
headerAuth := r.Header.Get("Authorization")
|
|
|
|
expected = "Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ="
|
|
|
|
if headerAuth != expected {
|
|
|
|
t.Errorf("Authorization header = %q; want %q", headerAuth, expected)
|
|
|
|
}
|
|
|
|
headerContentType := r.Header.Get("Content-Type")
|
|
|
|
expected = "application/x-www-form-urlencoded"
|
|
|
|
if headerContentType != expected {
|
|
|
|
t.Errorf("Content-Type header = %q; want %q", headerContentType, expected)
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed reading request body: %s.", err)
|
|
|
|
}
|
2016-06-04 00:11:54 -04:00
|
|
|
expected = "grant_type=password&password=password1&scope=scope1+scope2&username=user1"
|
2015-02-04 03:22:21 -05:00
|
|
|
if string(body) != expected {
|
|
|
|
t.Errorf("res.Body = %q; want %q", string(body), expected)
|
|
|
|
}
|
|
|
|
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 := newConf(ts.URL)
|
2016-08-24 18:40:36 -04:00
|
|
|
tok, err := conf.PasswordCredentialsToken(context.Background(), "user1", "password1")
|
2015-02-04 03:22:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !tok.Valid() {
|
|
|
|
t.Fatalf("Token invalid. Got: %#v", tok)
|
|
|
|
}
|
|
|
|
expected := "90d64460d14870c08c81352a05dedd3465940a7c"
|
|
|
|
if tok.AccessToken != expected {
|
|
|
|
t.Errorf("AccessToken = %q; want %q", tok.AccessToken, expected)
|
|
|
|
}
|
|
|
|
expected = "bearer"
|
|
|
|
if tok.TokenType != expected {
|
|
|
|
t.Errorf("TokenType = %q; want %q", tok.TokenType, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 16:28:18 -04:00
|
|
|
func TestTokenRefreshRequest(t *testing.T) {
|
2019-01-14 17:52:49 -05:00
|
|
|
internal.ResetAuthCache()
|
2014-09-04 16:28:18 -04:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2014-11-06 19:36:41 -05:00
|
|
|
if r.URL.String() == "/somethingelse" {
|
|
|
|
return
|
|
|
|
}
|
2014-09-04 16:28:18 -04:00
|
|
|
if r.URL.String() != "/token" {
|
2019-01-14 17:52:49 -05:00
|
|
|
t.Errorf("Unexpected token refresh request URL %q", r.URL)
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
|
|
|
headerContentType := r.Header.Get("Content-Type")
|
|
|
|
if headerContentType != "application/x-www-form-urlencoded" {
|
2019-01-14 17:52:49 -05:00
|
|
|
t.Errorf("Unexpected Content-Type header %q", headerContentType)
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
|
|
|
body, _ := ioutil.ReadAll(r.Body)
|
2016-06-04 00:11:54 -04:00
|
|
|
if string(body) != "grant_type=refresh_token&refresh_token=REFRESH_TOKEN" {
|
2019-01-14 17:52:49 -05:00
|
|
|
t.Errorf("Unexpected refresh token payload %q", body)
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
2019-01-14 17:52:49 -05:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
io.WriteString(w, `{"access_token": "foo", "refresh_token": "bar"}`)
|
2014-09-04 16:28:18 -04:00
|
|
|
}))
|
|
|
|
defer ts.Close()
|
2014-12-11 02:30:13 -05:00
|
|
|
conf := newConf(ts.URL)
|
2016-08-24 18:40:36 -04:00
|
|
|
c := conf.Client(context.Background(), &Token{RefreshToken: "REFRESH_TOKEN"})
|
2014-11-06 19:36:41 -05:00
|
|
|
c.Get(ts.URL + "/somethingelse")
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
2014-07-11 13:57:28 -04:00
|
|
|
|
2014-11-06 19:36:41 -05:00
|
|
|
func TestFetchWithNoRefreshToken(t *testing.T) {
|
2014-09-04 16:28:18 -04:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2014-11-06 19:36:41 -05:00
|
|
|
if r.URL.String() == "/somethingelse" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.URL.String() != "/token" {
|
|
|
|
t.Errorf("Unexpected token refresh request URL, %v is found.", r.URL)
|
|
|
|
}
|
|
|
|
headerContentType := r.Header.Get("Content-Type")
|
|
|
|
if headerContentType != "application/x-www-form-urlencoded" {
|
|
|
|
t.Errorf("Unexpected Content-Type header, %v is found.", headerContentType)
|
|
|
|
}
|
|
|
|
body, _ := ioutil.ReadAll(r.Body)
|
|
|
|
if string(body) != "client_id=CLIENT_ID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN" {
|
|
|
|
t.Errorf("Unexpected refresh token payload, %v is found.", string(body))
|
2014-09-04 16:28:18 -04:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
2014-12-11 02:30:13 -05:00
|
|
|
conf := newConf(ts.URL)
|
2016-08-24 18:40:36 -04:00
|
|
|
c := conf.Client(context.Background(), nil)
|
2014-11-06 19:36:41 -05:00
|
|
|
_, err := c.Get(ts.URL + "/somethingelse")
|
2014-05-05 17:54:23 -04:00
|
|
|
if err == nil {
|
2014-11-06 19:36:41 -05:00
|
|
|
t.Errorf("Fetch should return an error if no refresh token is set")
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
|
|
|
}
|
2015-02-05 14:58:01 -05:00
|
|
|
|
2017-12-14 18:38:05 -05:00
|
|
|
func TestTokenRetrieveError(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.String() != "/token" {
|
|
|
|
t.Errorf("Unexpected token refresh request URL, %v is found.", r.URL)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(`{"error": "invalid_grant"}`))
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
conf := newConf(ts.URL)
|
|
|
|
_, err := conf.Exchange(context.Background(), "exchange-code")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("got no error, expected one")
|
|
|
|
}
|
|
|
|
_, ok := err.(*RetrieveError)
|
|
|
|
if !ok {
|
2019-01-14 17:52:49 -05:00
|
|
|
t.Fatalf("got %T error, expected *RetrieveError; error was: %v", err, err)
|
2017-12-14 18:38:05 -05:00
|
|
|
}
|
|
|
|
// Test error string for backwards compatibility
|
|
|
|
expected := fmt.Sprintf("oauth2: cannot fetch token: %v\nResponse: %s", "400 Bad Request", `{"error": "invalid_grant"}`)
|
|
|
|
if errStr := err.Error(); errStr != expected {
|
|
|
|
t.Fatalf("got %#v, expected %#v", errStr, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 14:58:01 -05:00
|
|
|
func TestRefreshToken_RefreshTokenReplacement(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2017-05-17 13:14:19 -04:00
|
|
|
w.Write([]byte(`{"access_token":"ACCESS_TOKEN", "scope": "user", "token_type": "bearer", "refresh_token": "NEW_REFRESH_TOKEN"}`))
|
2015-02-05 14:58:01 -05:00
|
|
|
return
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
conf := newConf(ts.URL)
|
2017-12-27 19:42:28 -05:00
|
|
|
tkr := conf.TokenSource(context.Background(), &Token{RefreshToken: "OLD_REFRESH_TOKEN"})
|
2015-02-05 14:58:01 -05:00
|
|
|
tk, err := tkr.Token()
|
|
|
|
if err != nil {
|
2016-08-26 19:14:08 -04:00
|
|
|
t.Errorf("got err = %v; want none", err)
|
2015-02-05 14:58:01 -05:00
|
|
|
return
|
|
|
|
}
|
2017-12-27 19:42:28 -05:00
|
|
|
if want := "NEW_REFRESH_TOKEN"; tk.RefreshToken != want {
|
|
|
|
t.Errorf("RefreshToken = %q; want %q", tk.RefreshToken, want)
|
2015-02-05 14:58:01 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-09 17:25:31 -04:00
|
|
|
|
2017-05-17 13:14:19 -04:00
|
|
|
func TestRefreshToken_RefreshTokenPreservation(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(`{"access_token":"ACCESS_TOKEN", "scope": "user", "token_type": "bearer"}`))
|
|
|
|
return
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
conf := newConf(ts.URL)
|
|
|
|
const oldRefreshToken = "OLD_REFRESH_TOKEN"
|
2017-12-27 19:42:28 -05:00
|
|
|
tkr := conf.TokenSource(context.Background(), &Token{RefreshToken: oldRefreshToken})
|
|
|
|
tk, err := tkr.Token()
|
2017-05-17 13:14:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got err = %v; want none", err)
|
|
|
|
}
|
2017-12-27 19:42:28 -05:00
|
|
|
if tk.RefreshToken != oldRefreshToken {
|
|
|
|
t.Errorf("RefreshToken = %q; want %q", tk.RefreshToken, oldRefreshToken)
|
2017-05-17 13:14:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 17:25:31 -04:00
|
|
|
func TestConfigClientWithToken(t *testing.T) {
|
|
|
|
tok := &Token{
|
|
|
|
AccessToken: "abc123",
|
|
|
|
}
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, want := r.Header.Get("Authorization"), fmt.Sprintf("Bearer %s", tok.AccessToken); got != want {
|
|
|
|
t.Errorf("Authorization header = %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
conf := newConf(ts.URL)
|
|
|
|
|
2016-08-24 18:40:36 -04:00
|
|
|
c := conf.Client(context.Background(), tok)
|
2015-03-09 17:25:31 -04:00
|
|
|
req, err := http.NewRequest("GET", ts.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
_, err = c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|