2021-06-11 14:40:45 -04:00
|
|
|
// Copyright 2021 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.
|
|
|
|
|
2021-06-10 17:11:50 -04:00
|
|
|
package downscope
|
2021-06-09 13:46:53 -04:00
|
|
|
|
|
|
|
import (
|
2021-06-09 17:25:06 -04:00
|
|
|
"context"
|
2021-06-09 13:46:53 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2021-06-11 14:40:45 -04:00
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
2021-06-09 13:46:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-06-16 15:14:24 -04:00
|
|
|
standardReqBody = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&options=%7B%22accessBoundary%22%3A%7B%22accessBoundaryRules%22%3A%5B%7B%22availableResource%22%3A%22test1%22%2C%22availablePermissions%22%3A%5B%22Perm1%22%2C%22Perm2%22%5D%7D%5D%7D%7D&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&subject_token=Mellon&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token"
|
2021-06-09 13:46:53 -04:00
|
|
|
standardRespBody = `{"access_token":"Open Sesame","expires_in":432,"issued_token_type":"urn:ietf:params:oauth:token-type:access_token","token_type":"Bearer"}`
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_DownscopedTokenSource(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
t.Errorf("Unexpected request method, %v is found", r.Method)
|
|
|
|
}
|
2021-06-09 17:25:06 -04:00
|
|
|
if r.URL.String() != "/" {
|
2021-06-09 13:46:53 -04:00
|
|
|
t.Errorf("Unexpected request URL, %v is found", r.URL)
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read request body: %v", err)
|
|
|
|
}
|
|
|
|
if got, want := string(body), standardReqBody; got != want {
|
|
|
|
t.Errorf("Unexpected exchange payload: got %v but want %v,", got, want)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(standardRespBody))
|
|
|
|
|
|
|
|
}))
|
2021-06-11 14:40:45 -04:00
|
|
|
new := []AccessBoundaryRule{
|
2021-06-17 14:06:20 -04:00
|
|
|
{
|
2021-06-11 14:40:45 -04:00
|
|
|
AvailableResource: "test1",
|
|
|
|
AvailablePermissions: []string{"Perm1", "Perm2"},
|
|
|
|
},
|
|
|
|
}
|
2021-06-09 13:46:53 -04:00
|
|
|
myTok := oauth2.Token{AccessToken: "Mellon"}
|
|
|
|
tmpSrc := oauth2.StaticTokenSource(&myTok)
|
2021-06-22 12:25:17 -04:00
|
|
|
_, err := downscopedTokenWithEndpoint(context.Background(), DownscopingConfig{tmpSrc, new}, ts.URL)
|
2021-06-09 13:46:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewDownscopedTokenSource failed with error: %v", err)
|
|
|
|
}
|
|
|
|
}
|