2014-05-17 11:26:57 -04:00
|
|
|
// Copyright 2014 The oauth2 Authors. All rights reserved.
|
|
|
|
// 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 (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2014-09-04 16:28:18 -04:00
|
|
|
"net/http/httptest"
|
2014-05-05 17:54:23 -04:00
|
|
|
"testing"
|
2014-12-11 02:30:13 -05:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
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-11-12 23:41:14 -05:00
|
|
|
type mockCache struct {
|
|
|
|
token *Token
|
|
|
|
readErr error
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:07:50 -05:00
|
|
|
func (c *mockCache) ReadToken() (*Token, error) {
|
2014-11-12 23:41:14 -05:00
|
|
|
return c.token, c.readErr
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:07:50 -05:00
|
|
|
func (c *mockCache) WriteToken(*Token) {
|
2014-11-12 23:41:14 -05:00
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
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)
|
2014-09-04 16:28:18 -04:00
|
|
|
if url != "server/auth?access_type=offline&approval_prompt=force&client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&scope=scope1+scope2&state=foo" {
|
2014-11-06 19:36:41 -05:00
|
|
|
t.Errorf("Auth code URL doesn't match the expected, found: %v", url)
|
2014-05-05 17:54:23 -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("")
|
|
|
|
if url != "/auth-url?client_id=CLIENT_ID&response_type=code" {
|
2014-09-04 16:28:18 -04:00
|
|
|
t.Fatalf("Auth code URL doesn't match the expected, found: %v", url)
|
2014-05-05 17:54:23 -04:00
|
|
|
}
|
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" {
|
|
|
|
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)
|
|
|
|
}
|
2014-09-04 16:28:18 -04:00
|
|
|
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))
|
|
|
|
}
|
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)
|
|
|
|
tok, err := conf.Exchange(NoContext, "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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2014-09-06 19:39:43 -04:00
|
|
|
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")
|
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)
|
|
|
|
tok, err := conf.Exchange(NoContext, "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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
tok, err := conf.Exchange(NoContext, "code")
|
2014-10-27 20:35:35 -04:00
|
|
|
if err != nil {
|
2014-12-11 02:30:13 -05:00
|
|
|
t.Fatal(err)
|
2014-10-27 20:35:35 -04:00
|
|
|
}
|
|
|
|
if tok.AccessToken != "" {
|
|
|
|
t.Errorf("Unexpected access token, %#v.", tok.AccessToken)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2014-12-19 00:20:30 -05:00
|
|
|
_, err := conf.Exchange(NoContext, "exchange-code")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error from invalid access_token type")
|
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) {
|
|
|
|
tr := &mockTransport{
|
|
|
|
rt: func(r *http.Request) (w *http.Response, err error) {
|
|
|
|
headerAuth := r.Header.Get("Authorization")
|
|
|
|
if headerAuth != "" {
|
|
|
|
t.Errorf("Unexpected authorization header, %v is found.", 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{
|
|
|
|
AuthURL: "https://accounts.google.com/auth",
|
|
|
|
TokenURL: "https://accounts.google.com/token",
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
|
2014-09-04 16:28:18 -04:00
|
|
|
func TestTokenRefreshRequest(t *testing.T) {
|
|
|
|
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" {
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
2014-12-11 02:30:13 -05:00
|
|
|
conf := newConf(ts.URL)
|
|
|
|
c := conf.Client(NoContext, &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)
|
|
|
|
c := conf.Client(NoContext, 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
|
|
|
}
|
|
|
|
}
|