2015-08-27 03:42:02 +03:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2015-01-17 01:43:33 +03:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package clientcredentials
|
|
|
|
|
|
|
|
import (
|
2016-08-25 01:40:36 +03:00
|
|
|
"context"
|
2019-01-15 01:52:49 +03:00
|
|
|
"io"
|
2015-01-17 01:43:33 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-02-11 01:32:30 +03:00
|
|
|
"net/url"
|
2015-01-17 01:43:33 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2017-02-11 01:32:30 +03:00
|
|
|
func newConf(serverURL string) *Config {
|
2015-01-17 01:43:33 +03:00
|
|
|
return &Config{
|
2017-02-11 01:32:30 +03:00
|
|
|
ClientID: "CLIENT_ID",
|
|
|
|
ClientSecret: "CLIENT_SECRET",
|
|
|
|
Scopes: []string{"scope1", "scope2"},
|
|
|
|
TokenURL: serverURL + "/token",
|
|
|
|
EndpointParams: url.Values{"audience": {"audience1"}},
|
2015-01-17 01:43:33 +03: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)
|
|
|
|
}
|
|
|
|
|
2019-01-30 01:49:30 +03:00
|
|
|
func TestTokenSourceGrantTypeOverride(t *testing.T) {
|
|
|
|
wantGrantType := "password"
|
|
|
|
var gotGrantType string
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ioutil.ReadAll(r.Body) == %v, %v, want _, <nil>", body, err)
|
|
|
|
}
|
|
|
|
if err := r.Body.Close(); err != nil {
|
|
|
|
t.Errorf("r.Body.Close() == %v, want <nil>", err)
|
|
|
|
}
|
|
|
|
values, err := url.ParseQuery(string(body))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("url.ParseQuery(%q) == %v, %v, want _, <nil>", body, values, err)
|
|
|
|
}
|
|
|
|
gotGrantType = values.Get("grant_type")
|
|
|
|
w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&token_type=bearer"))
|
|
|
|
}))
|
|
|
|
config := &Config{
|
|
|
|
ClientID: "CLIENT_ID",
|
|
|
|
ClientSecret: "CLIENT_SECRET",
|
|
|
|
Scopes: []string{"scope"},
|
|
|
|
TokenURL: ts.URL + "/token",
|
|
|
|
EndpointParams: url.Values{
|
|
|
|
"grant_type": {wantGrantType},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
token, err := config.TokenSource(context.Background()).Token()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("config.TokenSource(_).Token() == %v, %v, want !<nil>, <nil>", token, err)
|
|
|
|
}
|
|
|
|
if gotGrantType != wantGrantType {
|
|
|
|
t.Errorf("grant_type == %q, want %q", gotGrantType, wantGrantType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:43:33 +03:00
|
|
|
func TestTokenRequest(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.String() != "/token" {
|
|
|
|
t.Errorf("authenticate client request URL = %q; want %q", r.URL, "/token")
|
|
|
|
}
|
|
|
|
headerAuth := r.Header.Get("Authorization")
|
|
|
|
if headerAuth != "Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ=" {
|
|
|
|
t.Errorf("Unexpected authorization header, %v is found.", headerAuth)
|
|
|
|
}
|
|
|
|
if got, want := r.Header.Get("Content-Type"), "application/x-www-form-urlencoded"; got != want {
|
|
|
|
t.Errorf("Content-Type header = %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
r.Body.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed reading request body: %s.", err)
|
|
|
|
}
|
2017-02-11 01:32:30 +03:00
|
|
|
if string(body) != "audience=audience1&grant_type=client_credentials&scope=scope1+scope2" {
|
2016-06-04 07:11:54 +03:00
|
|
|
t.Errorf("payload = %q; want %q", string(body), "grant_type=client_credentials&scope=scope1+scope2")
|
2015-01-17 01:43:33 +03:00
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&token_type=bearer"))
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
conf := newConf(ts.URL)
|
2016-08-25 01:40:36 +03:00
|
|
|
tok, err := conf.Token(context.Background())
|
2015-01-17 01:43:33 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !tok.Valid() {
|
|
|
|
t.Fatalf("token invalid. got: %#v", tok)
|
|
|
|
}
|
|
|
|
if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
|
|
|
|
t.Errorf("Access token = %q; want %q", tok.AccessToken, "90d64460d14870c08c81352a05dedd3465940a7c")
|
|
|
|
}
|
|
|
|
if tok.TokenType != "bearer" {
|
|
|
|
t.Errorf("token type = %q; want %q", tok.TokenType, "bearer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTokenRefreshRequest(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.String() == "/somethingelse" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.URL.String() != "/token" {
|
2019-01-15 01:52:49 +03:00
|
|
|
t.Errorf("Unexpected token refresh request URL: %q", r.URL)
|
2015-01-17 01:43:33 +03:00
|
|
|
}
|
|
|
|
headerContentType := r.Header.Get("Content-Type")
|
2019-01-15 01:52:49 +03:00
|
|
|
if got, want := headerContentType, "application/x-www-form-urlencoded"; got != want {
|
|
|
|
t.Errorf("Content-Type = %q; want %q", got, want)
|
2015-01-17 01:43:33 +03:00
|
|
|
}
|
|
|
|
body, _ := ioutil.ReadAll(r.Body)
|
2019-01-15 01:52:49 +03:00
|
|
|
const want = "audience=audience1&grant_type=client_credentials&scope=scope1+scope2"
|
|
|
|
if string(body) != want {
|
|
|
|
t.Errorf("Unexpected refresh token payload.\n got: %s\nwant: %s\n", body, want)
|
2015-01-17 01:43:33 +03:00
|
|
|
}
|
2019-01-15 01:52:49 +03:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
io.WriteString(w, `{"access_token": "foo", "refresh_token": "bar"}`)
|
2015-01-17 01:43:33 +03:00
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
conf := newConf(ts.URL)
|
2016-08-25 01:40:36 +03:00
|
|
|
c := conf.Client(context.Background())
|
2015-01-17 01:43:33 +03:00
|
|
|
c.Get(ts.URL + "/somethingelse")
|
|
|
|
}
|