-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
integration_test.go
150 lines (141 loc) · 4.13 KB
/
integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2023 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package idtoken_test
import (
"context"
"encoding/json"
"net/http"
"os"
"strings"
"testing"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/idtoken"
"google.golang.org/api/option"
)
const (
envCredentialFile = "GOOGLE_APPLICATION_CREDENTIALS"
aud = "http://example.com"
)
func TestNewTokenSource(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ts, err := idtoken.NewTokenSource(context.Background(), "http://example.com", option.WithCredentialsFile(os.Getenv(envCredentialFile)))
if err != nil {
t.Fatalf("unable to create TokenSource: %v", err)
}
tok, err := ts.Token()
if err != nil {
t.Fatalf("unable to retrieve Token: %v", err)
}
req := &http.Request{Header: make(http.Header)}
tok.SetAuthHeader(req)
if !strings.HasPrefix(req.Header.Get("Authorization"), "Bearer ") {
t.Fatalf("token should sign requests with Bearer Authorization header")
}
validTok, err := idtoken.Validate(context.Background(), tok.AccessToken, aud)
if err != nil {
t.Fatalf("token validation failed: %v", err)
}
if validTok.Audience != aud {
t.Fatalf("got %q, want %q", validTok.Audience, aud)
}
}
func TestNewTokenSource_WithImpersonatedServiceAccountCreds(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
type SourceCreds struct {
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RefreshToken string `json:"refresh_token"`
Type string `json:"type"`
}
type Creds struct {
Type string `json:"type"`
ImpersonationUrl string `json:"service_account_impersonation_url"`
SourceCredentials SourceCreds `json:"source_credentials"`
}
sc := SourceCreds{
"",
"",
"",
"",
}
c := Creds{
"",
"",
sc,
}
creds, err := json.Marshal(c)
if err != nil {
t.Fatalf("unable to marshal credentials: %v", err)
}
ts, err := idtoken.NewTokenSource(context.Background(), "http://example.com", option.WithCredentialsJSON(creds))
if err != nil {
t.Fatalf("unable to create TokenSource: %v", err)
}
tok, err := ts.Token()
if err != nil {
t.Fatalf("unable to retrieve Token: %v", err)
}
req := &http.Request{Header: make(http.Header)}
tok.SetAuthHeader(req)
if !strings.HasPrefix(req.Header.Get("Authorization"), "Bearer ") {
t.Fatalf("token should sign requests with Bearer Authorization header")
}
validTok, err := idtoken.Validate(context.Background(), tok.AccessToken, aud)
if err != nil {
t.Fatalf("token validation failed: %v", err)
}
if validTok.Audience != aud {
t.Fatalf("got %q, want %q", validTok.Audience, aud)
}
}
func TestNewClient_WithCredentialFile(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
client, err := idtoken.NewClient(context.Background(), aud, option.WithCredentialsFile(os.Getenv(envCredentialFile)))
if err != nil {
t.Fatalf("unable to create Client: %v", err)
}
tok, err := client.Transport.(*oauth2.Transport).Source.Token()
if err != nil {
t.Fatalf("unable to retrieve Token: %v", err)
}
validTok, err := idtoken.Validate(context.Background(), tok.AccessToken, aud)
if err != nil {
t.Fatalf("token validation failed: %v", err)
}
if validTok.Audience != aud {
t.Fatalf("got %q, want %q", validTok.Audience, aud)
}
}
func TestNewClient_WithCredentialJSON(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
creds, err := google.FindDefaultCredentials(ctx)
if err != nil {
t.Fatalf("unable to find default creds: %v", err)
}
client, err := idtoken.NewClient(ctx, aud, option.WithCredentialsJSON(creds.JSON))
if err != nil {
t.Fatalf("unable to create Client: %v", err)
}
tok, err := client.Transport.(*oauth2.Transport).Source.Token()
if err != nil {
t.Fatalf("unable to retrieve Token: %v", err)
}
validTok, err := idtoken.Validate(context.Background(), tok.AccessToken, aud)
if err != nil {
t.Fatalf("token validation failed: %v", err)
}
if validTok.Audience != aud {
t.Fatalf("got %q, want %q", validTok.Audience, aud)
}
}