2015-08-26 20:42:02 -04:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2015-01-16 17:43:33 -05:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-12-05 17:32:25 -05:00
|
|
|
"io"
|
2017-03-13 15:46:53 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2015-01-16 17:43:33 -05:00
|
|
|
"testing"
|
2017-03-13 15:46:53 -04:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2015-01-16 17:43:33 -05:00
|
|
|
)
|
|
|
|
|
2015-11-16 16:49:40 -05:00
|
|
|
func TestRegisterBrokenAuthHeaderProvider(t *testing.T) {
|
|
|
|
RegisterBrokenAuthHeaderProvider("https://aaa.com/")
|
|
|
|
tokenURL := "https://aaa.com/token"
|
|
|
|
if providerAuthHeaderWorks(tokenURL) {
|
2016-08-26 14:51:24 -04:00
|
|
|
t.Errorf("got %q as unbroken; want broken", tokenURL)
|
2015-11-16 16:49:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 15:46:53 -04:00
|
|
|
func TestRetrieveTokenBustedNoSecret(t *testing.T) {
|
|
|
|
const clientID = "client-id"
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, want := r.FormValue("client_id"), clientID; got != want {
|
|
|
|
t.Errorf("client_id = %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
if got, want := r.FormValue("client_secret"), ""; got != want {
|
|
|
|
t.Errorf("client_secret = %q; want empty", got)
|
|
|
|
}
|
2017-12-05 17:32:25 -05:00
|
|
|
io.WriteString(w, "{}") // something non-empty, required to set a Content-Type in Go 1.10
|
2017-03-13 15:46:53 -04:00
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
RegisterBrokenAuthHeaderProvider(ts.URL)
|
|
|
|
_, err := RetrieveToken(context.Background(), clientID, "", ts.URL, url.Values{})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("RetrieveToken = %v; want no error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 17:43:33 -05:00
|
|
|
func Test_providerAuthHeaderWorks(t *testing.T) {
|
|
|
|
for _, p := range brokenAuthHeaderProviders {
|
|
|
|
if providerAuthHeaderWorks(p) {
|
2016-08-26 14:51:24 -04:00
|
|
|
t.Errorf("got %q as unbroken; want broken", p)
|
2015-01-16 17:43:33 -05:00
|
|
|
}
|
|
|
|
p := fmt.Sprintf("%ssomesuffix", p)
|
|
|
|
if providerAuthHeaderWorks(p) {
|
2016-08-26 14:51:24 -04:00
|
|
|
t.Errorf("got %q as unbroken; want broken", p)
|
2015-01-16 17:43:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p := "https://api.not-in-the-list-example.com/"
|
|
|
|
if !providerAuthHeaderWorks(p) {
|
2016-08-26 14:51:24 -04:00
|
|
|
t.Errorf("got %q as unbroken; want broken", p)
|
2015-01-16 17:43:33 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-20 14:04:02 -04:00
|
|
|
|
|
|
|
func TestProviderAuthHeaderWorksDomain(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
tokenURL string
|
|
|
|
wantWorks bool
|
|
|
|
}{
|
|
|
|
{"https://dev-12345.okta.com/token-url", false},
|
|
|
|
{"https://dev-12345.oktapreview.com/token-url", false},
|
|
|
|
{"https://dev-12345.okta.org/token-url", true},
|
|
|
|
{"https://foo.bar.force.com/token-url", false},
|
|
|
|
{"https://foo.force.com/token-url", false},
|
|
|
|
{"https://force.com/token-url", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
got := providerAuthHeaderWorks(test.tokenURL)
|
|
|
|
if got != test.wantWorks {
|
|
|
|
t.Errorf("providerAuthHeaderWorks(%q) = %v; want %v", test.tokenURL, got, test.wantWorks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 04:57:20 -04:00
|
|
|
|
|
|
|
func TestRetrieveTokenWithContexts(t *testing.T) {
|
|
|
|
const clientID = "client-id"
|
|
|
|
|
2017-12-05 17:32:25 -05:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
io.WriteString(w, "{}") // something non-empty, required to set a Content-Type in Go 1.10
|
|
|
|
}))
|
2017-06-28 04:57:20 -04:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
_, err := RetrieveToken(context.Background(), clientID, "", ts.URL, url.Values{})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("RetrieveToken (with background context) = %v; want no error", err)
|
|
|
|
}
|
|
|
|
|
2017-12-21 16:15:27 -05:00
|
|
|
retrieved := make(chan struct{})
|
2017-06-28 04:57:20 -04:00
|
|
|
cancellingts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-12-21 16:15:27 -05:00
|
|
|
<-retrieved
|
2017-06-28 04:57:20 -04:00
|
|
|
}))
|
|
|
|
defer cancellingts.Close()
|
|
|
|
|
2017-12-21 16:15:27 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
2017-06-28 04:57:20 -04:00
|
|
|
_, err = RetrieveToken(ctx, clientID, "", cancellingts.URL, url.Values{})
|
2017-12-21 16:15:27 -05:00
|
|
|
close(retrieved)
|
2017-06-28 04:57:20 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("RetrieveToken (with cancelled context) = nil; want error")
|
|
|
|
}
|
|
|
|
}
|