2021-06-14 16:51:56 -04:00
|
|
|
package downscope_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google/downscope"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleNewTokenSource() {
|
|
|
|
ctx := context.Background()
|
|
|
|
// Initializes an accessBoundary with one Rule
|
|
|
|
accessBoundary := []downscope.AccessBoundaryRule{
|
|
|
|
downscope.AccessBoundaryRule{
|
|
|
|
AvailableResource: "//storage.googleapis.com/projects/_/buckets/foo",
|
|
|
|
AvailablePermissions: []string{"inRole:roles/storage.objectViewer"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootSource oauth2.TokenSource
|
2021-06-17 17:58:26 -04:00
|
|
|
// This Source can be initialized in multiple ways; the following example uses
|
|
|
|
// Application Default Credentials.
|
2021-06-14 16:51:56 -04:00
|
|
|
|
|
|
|
// rootSource, err := google.DefaultTokenSource(ctx, "https://www.googleapis.com/auth/cloud-platform")
|
|
|
|
|
2021-06-17 17:58:26 -04:00
|
|
|
dts := downscope.DownscopingTokenSource{ctx, downscope.DownscopingConfig{RootSource: rootSource, Rules: accessBoundary}}
|
|
|
|
_ = dts
|
2021-06-14 16:51:56 -04:00
|
|
|
// You can now use the token held in myTokenSource to make
|
|
|
|
// Google Cloud Storage calls, as follows:
|
|
|
|
|
|
|
|
// storageClient, err := storage.NewClient(ctx, option.WithTokenSource(myTokenSource))
|
|
|
|
}
|