...

Source file src/cloud.google.com/go/auth/credentials/idtoken/idtoken_test.go

Documentation: cloud.google.com/go/auth/credentials/idtoken

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package idtoken
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"fmt"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"os"
    24  	"testing"
    25  
    26  	"cloud.google.com/go/auth/internal"
    27  	"cloud.google.com/go/auth/internal/credsfile"
    28  )
    29  
    30  func TestNewCredentials_ServiceAccount(t *testing.T) {
    31  	wantTok, _ := createRS256JWT(t)
    32  	b, err := os.ReadFile("../../internal/testdata/sa.json")
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	f, err := credsfile.ParseServiceAccount(b)
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    41  		w.Header().Set("Content-Type", "application/json")
    42  		w.Write([]byte(fmt.Sprintf(`{"id_token": "%s"}`, wantTok)))
    43  	}))
    44  	defer ts.Close()
    45  	f.TokenURL = ts.URL
    46  	b, err = json.Marshal(f)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	creds, err := NewCredentials(&Options{
    52  		Audience:        "aud",
    53  		CredentialsJSON: b,
    54  		CustomClaims: map[string]interface{}{
    55  			"foo": "bar",
    56  		},
    57  	})
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	tok, err := creds.Token(context.Background())
    62  	if err != nil {
    63  		t.Fatalf("tp.Token() = %v", err)
    64  	}
    65  	if tok.Value != wantTok {
    66  		t.Errorf("got %q, want %q", tok.Value, wantTok)
    67  	}
    68  }
    69  
    70  type mockTransport struct {
    71  	handler http.HandlerFunc
    72  }
    73  
    74  func (m mockTransport) RoundTrip(r *http.Request) (*http.Response, error) {
    75  	rw := httptest.NewRecorder()
    76  	m.handler(rw, r)
    77  	return rw.Result(), nil
    78  }
    79  
    80  func TestNewCredentials_ImpersonatedServiceAccount(t *testing.T) {
    81  	wantTok, _ := createRS256JWT(t)
    82  	client := internal.CloneDefaultClient()
    83  	client.Transport = mockTransport{
    84  		handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    85  			w.Write([]byte(fmt.Sprintf(`{"token": %q}`, wantTok)))
    86  		}),
    87  	}
    88  	creds, err := NewCredentials(&Options{
    89  		Audience:        "aud",
    90  		CredentialsFile: "../../internal/testdata/imp.json",
    91  		CustomClaims: map[string]interface{}{
    92  			"foo": "bar",
    93  		},
    94  		Client: client,
    95  	})
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	tok, err := creds.Token(context.Background())
   100  	if err != nil {
   101  		t.Fatalf("tp.Token() = %v", err)
   102  	}
   103  	if tok.Value != wantTok {
   104  		t.Errorf("got %q, want %q", tok.Value, wantTok)
   105  	}
   106  }
   107  

View as plain text