...

Source file src/google.golang.org/api/idtoken/integration_test.go

Documentation: google.golang.org/api/idtoken

     1  // Copyright 2020 Google LLC.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package idtoken_test
     6  
     7  import (
     8  	"context"
     9  	"net/http"
    10  	"os"
    11  	"strings"
    12  	"testing"
    13  
    14  	"golang.org/x/oauth2/google"
    15  	"google.golang.org/api/idtoken"
    16  	"google.golang.org/api/option"
    17  )
    18  
    19  const (
    20  	envCredentialFile = "GOOGLE_APPLICATION_CREDENTIALS"
    21  
    22  	aud = "http://example.com"
    23  )
    24  
    25  func TestNewTokenSource(t *testing.T) {
    26  	if testing.Short() {
    27  		t.Skip("skipping integration test")
    28  	}
    29  	ts, err := idtoken.NewTokenSource(context.Background(), "http://example.com", option.WithCredentialsFile(os.Getenv(envCredentialFile)))
    30  	if err != nil {
    31  		t.Fatalf("unable to create TokenSource: %v", err)
    32  	}
    33  	tok, err := ts.Token()
    34  	if err != nil {
    35  		t.Fatalf("unable to retrieve Token: %v", err)
    36  	}
    37  	req := &http.Request{Header: make(http.Header)}
    38  	tok.SetAuthHeader(req)
    39  	if !strings.HasPrefix(req.Header.Get("Authorization"), "Bearer ") {
    40  		t.Fatalf("token should sign requests with Bearer Authorization header")
    41  	}
    42  	validTok, err := idtoken.Validate(context.Background(), tok.AccessToken, aud)
    43  	if err != nil {
    44  		t.Fatalf("token validation failed: %v", err)
    45  	}
    46  	if validTok.Audience != aud {
    47  		t.Fatalf("got %q, want %q", validTok.Audience, aud)
    48  	}
    49  }
    50  
    51  func TestNewTokenSource_WithCredentialJSON(t *testing.T) {
    52  	if testing.Short() {
    53  		t.Skip("skipping integration test")
    54  	}
    55  	ctx := context.Background()
    56  	creds, err := google.FindDefaultCredentials(ctx)
    57  	if err != nil {
    58  		t.Fatalf("unable to find default creds: %v", err)
    59  	}
    60  	ts, err := idtoken.NewTokenSource(ctx, aud, option.WithCredentialsJSON(creds.JSON))
    61  	if err != nil {
    62  		t.Fatalf("unable to create Client: %v", err)
    63  	}
    64  	tok, err := ts.Token()
    65  	if err != nil {
    66  		t.Fatalf("unable to retrieve Token: %v", err)
    67  	}
    68  	req := &http.Request{Header: make(http.Header)}
    69  	tok.SetAuthHeader(req)
    70  	if !strings.HasPrefix(req.Header.Get("Authorization"), "Bearer ") {
    71  		t.Fatalf("token should sign requests with Bearer Authorization header")
    72  	}
    73  	validTok, err := idtoken.Validate(context.Background(), tok.AccessToken, aud)
    74  	if err != nil {
    75  		t.Fatalf("token validation failed: %v", err)
    76  	}
    77  	if validTok.Audience != aud {
    78  		t.Fatalf("got %q, want %q", validTok.Audience, aud)
    79  	}
    80  }
    81  

View as plain text