2020-11-03 18:37:42 -05:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
2020-11-03 16:44:17 -05:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-10-13 15:02:46 -04:00
|
|
|
package externalaccount
|
|
|
|
|
|
|
|
import (
|
2020-11-03 16:44:17 -05:00
|
|
|
"context"
|
2020-10-19 18:17:49 -04:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-10-19 17:35:44 -04:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2020-10-13 15:02:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var auth = ClientAuthentication{
|
2020-10-19 17:39:14 -04:00
|
|
|
AuthStyle: oauth2.AuthStyleInHeader,
|
|
|
|
ClientID: clientID,
|
2020-10-13 15:02:46 -04:00
|
|
|
ClientSecret: clientSecret,
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokenRequest = STSTokenExchangeRequest{
|
|
|
|
ActingParty: struct {
|
|
|
|
ActorToken string
|
|
|
|
ActorTokenType string
|
|
|
|
}{},
|
|
|
|
GrantType: "urn:ietf:params:oauth:grant-type:token-exchange",
|
|
|
|
Resource: "",
|
2020-10-19 17:39:14 -04:00
|
|
|
Audience: "32555940559.apps.googleusercontent.com", //TODO: Make sure audience is correct in this test (might be mismatched)
|
2020-10-13 15:02:46 -04:00
|
|
|
Scope: []string{"https://www.googleapis.com/auth/devstorage.full_control"},
|
|
|
|
RequestedTokenType: "urn:ietf:params:oauth:token-type:access_token",
|
2020-10-19 18:17:49 -04:00
|
|
|
SubjectToken: "Sample.Subject.Token",
|
2020-10-13 15:02:46 -04:00
|
|
|
SubjectTokenType: "urn:ietf:params:oauth:token-type:jwt",
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:59:17 -05:00
|
|
|
var requestbody = "audience=32555940559.apps.googleusercontent.com&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&options=null&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdevstorage.full_control&subject_token=Sample.Subject.Token&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Ajwt"
|
2020-10-19 18:17:49 -04:00
|
|
|
var responseBody = `{"access_token":"Sample.Access.Token","issued_token_type":"urn:ietf:params:oauth:token-type:access_token","token_type":"Bearer","expires_in":3600,"scope":"https://www.googleapis.com/auth/cloud-platform"}`
|
2020-10-19 17:35:44 -04:00
|
|
|
var expectedToken = STSTokenExchangeResponse{
|
2020-10-19 18:17:49 -04:00
|
|
|
AccessToken: "Sample.Access.Token",
|
2020-10-19 17:35:44 -04:00
|
|
|
IssuedTokenType: "urn:ietf:params:oauth:token-type:access_token",
|
2020-10-19 17:39:14 -04:00
|
|
|
TokenType: "Bearer",
|
|
|
|
ExpiresIn: 3600,
|
|
|
|
Scope: "https://www.googleapis.com/auth/cloud-platform",
|
|
|
|
RefreshToken: "",
|
2020-10-13 15:02:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExchangeToken(t *testing.T) {
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-11-10 14:59:17 -05:00
|
|
|
if r.Method != "POST" {
|
|
|
|
t.Errorf("Unexpected request method, %v is found", r.Method)
|
|
|
|
}
|
2020-10-19 17:35:44 -04:00
|
|
|
if r.URL.String() != "/" {
|
2020-11-10 14:59:17 -05:00
|
|
|
t.Errorf("Unexpected request URL, %v is found", r.URL)
|
2020-10-13 15:02:46 -04:00
|
|
|
}
|
2020-10-19 17:35:44 -04:00
|
|
|
headerAuth := r.Header.Get("Authorization")
|
|
|
|
if headerAuth != "Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ=" {
|
2020-11-10 14:59:17 -05:00
|
|
|
t.Errorf("Unexpected autohrization header, %v is found", headerAuth)
|
2020-10-19 17:35:44 -04:00
|
|
|
}
|
|
|
|
headerContentType := r.Header.Get("Content-Type")
|
2020-10-19 18:17:49 -04:00
|
|
|
if headerContentType != "application/x-www-form-urlencoded" {
|
2020-11-10 14:59:17 -05:00
|
|
|
t.Errorf("Unexpected Content-Type header, %v is found", headerContentType)
|
2020-10-19 17:35:44 -04:00
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2020-11-10 14:59:17 -05:00
|
|
|
t.Errorf("Failed reading request body: %v.", err)
|
2020-10-19 17:35:44 -04:00
|
|
|
}
|
|
|
|
if string(body) != requestbody {
|
2020-11-10 14:59:17 -05:00
|
|
|
t.Errorf("Unexpected exchange payload, %v is found", string(body))
|
2020-10-19 17:35:44 -04:00
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(responseBody))
|
2020-10-13 15:02:46 -04:00
|
|
|
}))
|
|
|
|
|
2020-11-10 14:59:17 -05:00
|
|
|
headers := http.Header{}
|
|
|
|
headers.Add("Content-Type", "application/x-www-form-urlencoded")
|
2020-10-13 15:02:46 -04:00
|
|
|
|
2020-11-03 16:44:17 -05:00
|
|
|
resp, err := ExchangeToken(context.Background(), ts.URL, &tokenRequest, auth, headers, nil)
|
2020-10-13 15:02:46 -04:00
|
|
|
if err != nil {
|
2020-11-10 14:59:17 -05:00
|
|
|
t.Fatalf("ExchangeToken failed with error: %v", err)
|
2020-10-13 15:02:46 -04:00
|
|
|
}
|
2020-10-19 17:35:44 -04:00
|
|
|
|
2020-11-12 17:21:10 -05:00
|
|
|
if expectedToken != *resp {
|
|
|
|
t.Errorf("mismatched messages received by mock server. \nWant: \n%v\n\nGot:\n%v", expectedToken, *resp)
|
2020-10-19 17:35:44 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 17:39:14 -04:00
|
|
|
}
|
2020-10-29 15:28:45 -04:00
|
|
|
|
|
|
|
func TestExchangeToken_Err(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("what's wrong with this response?"))
|
|
|
|
}))
|
2020-11-03 16:44:17 -05:00
|
|
|
|
2020-11-10 14:59:17 -05:00
|
|
|
headers := http.Header{}
|
|
|
|
headers.Add("Content-Type", "application/x-www-form-urlencoded")
|
2020-11-03 16:44:17 -05:00
|
|
|
_, err := ExchangeToken(context.Background(), ts.URL, &tokenRequest, auth, headers, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected handled error; instead got nil.")
|
|
|
|
}
|
2020-11-10 14:59:17 -05:00
|
|
|
}
|