2020-10-29 14:22:53 -04:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-10-06 04:05:26 -04:00
|
|
|
package externalaccount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var clientID = "rbrgnognrhongo3bi4gb9ghg9g"
|
|
|
|
var clientSecret = "notsosecret"
|
|
|
|
|
2020-10-29 15:22:08 -04:00
|
|
|
var audience = []string{"32555940559.apps.googleusercontent.com"}
|
|
|
|
var grantType = []string{"urn:ietf:params:oauth:grant-type:token-exchange"}
|
|
|
|
var requestedTokenType = []string{"urn:ietf:params:oauth:token-type:access_token"}
|
|
|
|
var subjectTokenType = []string{"urn:ietf:params:oauth:token-type:jwt"}
|
|
|
|
var subjectToken = []string{"eyJhbGciOiJSUzI1NiIsImtpZCI6IjJjNmZhNmY1OTUwYTdjZTQ2NWZjZjI0N2FhMGIwOTQ4MjhhYzk1MmMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiIzMjU1NTk0MDU1OS5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImF1ZCI6IjMyNTU1OTQwNTU5LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTEzMzE4NTQxMDA5MDU3Mzc4MzI4IiwiaGQiOiJnb29nbGUuY29tIiwiZW1haWwiOiJpdGh1cmllbEBnb29nbGUuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF0X2hhc2giOiI5OVJVYVFrRHJsVDFZOUV0SzdiYXJnIiwiaWF0IjoxNjAxNTgxMzQ5LCJleHAiOjE2MDE1ODQ5NDl9.SZ-4DyDcogDh_CDUKHqPCiT8AKLg4zLMpPhGQzmcmHQ6cJiV0WRVMf5Lq911qsvuekgxfQpIdKNXlD6yk3FqvC2rjBbuEztMF-OD_2B8CEIYFlMLGuTQimJlUQksLKM-3B2ITRDCxnyEdaZik0OVssiy1CBTsllS5MgTFqic7w8w0Cd6diqNkfPFZRWyRYsrRDRlHHbH5_TUnv2wnLVHBHlNvU4wU2yyjDIoqOvTRp8jtXdq7K31CDhXd47-hXsVFQn2ZgzuUEAkH2Q6NIXACcVyZOrjBcZiOQI9IRWz-g03LzbzPSecO7I8dDrhqUSqMrdNUz_f8Kr8JFhuVMfVug"}
|
|
|
|
var scope = []string{"https://www.googleapis.com/auth/devstorage.full_control"}
|
2020-10-06 04:05:26 -04:00
|
|
|
|
2020-10-29 15:22:08 -04:00
|
|
|
var ContentType = []string{"application/x-www-form-urlencoded"}
|
2020-10-06 04:05:26 -04:00
|
|
|
|
|
|
|
func TestClientAuthentication_InjectHeaderAuthentication(t *testing.T) {
|
2020-10-29 14:22:53 -04:00
|
|
|
valuesH := url.Values{
|
2020-10-29 15:22:08 -04:00
|
|
|
"audience": audience,
|
|
|
|
"grant_type": grantType,
|
|
|
|
"requested_token_type": requestedTokenType,
|
|
|
|
"subject_token_type": subjectTokenType,
|
|
|
|
"subject_token": subjectToken,
|
|
|
|
"scope": scope,
|
2020-10-29 14:22:53 -04:00
|
|
|
}
|
|
|
|
headerH := http.Header{
|
2020-10-29 15:22:08 -04:00
|
|
|
"Content-Type": ContentType,
|
2020-10-29 14:22:53 -04:00
|
|
|
}
|
|
|
|
|
2020-10-06 04:05:26 -04:00
|
|
|
headerAuthentication := ClientAuthentication{
|
2020-10-06 04:18:45 -04:00
|
|
|
AuthStyle: oauth2.AuthStyleInHeader,
|
|
|
|
ClientID: clientID,
|
2020-10-06 04:05:26 -04:00
|
|
|
ClientSecret: clientSecret,
|
|
|
|
}
|
2020-10-26 16:44:32 -04:00
|
|
|
headerAuthentication.InjectAuthentication(valuesH, headerH)
|
2020-10-06 04:05:26 -04:00
|
|
|
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesH["audience"], audience; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("audience = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesH["grant_type"], grantType; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("grant_type = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesH["requested_token_type"], requestedTokenType; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("requested_token_type = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesH["subject_token_type"], subjectTokenType; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("subject_token_type = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesH["subject_token"], subjectToken; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("subject_token = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesH["scope"], scope; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("scope = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-06 04:18:45 -04:00
|
|
|
if got, want := headerH["Authorization"], []string{"Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ="}; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("Authorization in header = %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientAuthentication_ParamsAuthentication(t *testing.T) {
|
2020-10-29 14:22:53 -04:00
|
|
|
valuesP := url.Values{
|
2020-10-29 15:22:08 -04:00
|
|
|
"audience": audience,
|
|
|
|
"grant_type": grantType,
|
|
|
|
"requested_token_type": requestedTokenType,
|
|
|
|
"subject_token_type": subjectTokenType,
|
|
|
|
"subject_token": subjectToken,
|
|
|
|
"scope": scope,
|
2020-10-29 14:22:53 -04:00
|
|
|
}
|
|
|
|
headerP := http.Header{
|
2020-10-29 15:22:08 -04:00
|
|
|
"Content-Type": ContentType,
|
2020-10-29 14:22:53 -04:00
|
|
|
}
|
2020-10-06 04:05:26 -04:00
|
|
|
paramsAuthentication := ClientAuthentication{
|
2020-10-06 04:18:45 -04:00
|
|
|
AuthStyle: oauth2.AuthStyleInParams,
|
|
|
|
ClientID: clientID,
|
2020-10-06 04:05:26 -04:00
|
|
|
ClientSecret: clientSecret,
|
|
|
|
}
|
2020-10-26 16:44:32 -04:00
|
|
|
paramsAuthentication.InjectAuthentication(valuesP, headerP)
|
2020-10-06 04:05:26 -04:00
|
|
|
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesP["audience"], audience; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("audience = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesP["grant_type"], grantType; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("grant_type = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesP["requested_token_type"], requestedTokenType; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("requested_token_type = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesP["subject_token_type"], subjectTokenType; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("subject_token_type = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesP["subject_token"], subjectToken; !reflect.DeepEqual(got, want) {
|
2020-10-06 04:05:26 -04:00
|
|
|
t.Errorf("subject_token = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-29 15:22:08 -04:00
|
|
|
if got, want := valuesP["scope"], scope; !reflect.DeepEqual(got, want) {
|
2020-10-29 14:22:53 -04:00
|
|
|
t.Errorf("scope = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-06 04:05:26 -04:00
|
|
|
if got, want := valuesP["client_id"], []string{clientID}; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("client_id = %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
if got, want := valuesP["client_secret"], []string{clientSecret}; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("client_secret = %q, want %q", got, want)
|
|
|
|
}
|
2020-10-06 04:18:45 -04:00
|
|
|
}
|