@@ -15,6 +15,7 @@ import (
15
15
"os"
16
16
"time"
17
17
18
+ "cloud.google.com/go/auth"
18
19
"cloud.google.com/go/auth/credentials"
19
20
"cloud.google.com/go/auth/oauth2adapt"
20
21
"golang.org/x/oauth2"
@@ -30,7 +31,7 @@ const quotaProjectEnvVar = "GOOGLE_CLOUD_QUOTA_PROJECT"
30
31
// it returns default credential information.
31
32
func Creds (ctx context.Context , ds * DialSettings ) (* google.Credentials , error ) {
32
33
if ds .IsNewAuthLibraryEnabled () {
33
- return credsNewAuth (ctx , ds )
34
+ return credsNewAuth (ds )
34
35
}
35
36
creds , err := baseCreds (ctx , ds )
36
37
if err != nil {
@@ -42,6 +43,30 @@ func Creds(ctx context.Context, ds *DialSettings) (*google.Credentials, error) {
42
43
return creds , nil
43
44
}
44
45
46
+ // AuthCreds returns [cloud.google.com/go/auth.Credentials] based on credentials
47
+ // options provided via [option.ClientOption], including legacy oauth2/google
48
+ // options. If there are no applicable options, then it returns the result of
49
+ // [cloud.google.com/go/auth/credentials.DetectDefault].
50
+ func AuthCreds (ctx context.Context , settings * DialSettings ) (* auth.Credentials , error ) {
51
+ if settings .AuthCredentials != nil {
52
+ return settings .AuthCredentials , nil
53
+ }
54
+ // Support oauth2/google options
55
+ var oauth2Creds * google.Credentials
56
+ if settings .InternalCredentials != nil {
57
+ oauth2Creds = settings .InternalCredentials
58
+ } else if settings .Credentials != nil {
59
+ oauth2Creds = settings .Credentials
60
+ } else if settings .TokenSource != nil {
61
+ oauth2Creds = & google.Credentials {TokenSource : settings .TokenSource }
62
+ }
63
+ if oauth2Creds != nil {
64
+ return oauth2adapt .AuthCredentialsFromOauth2Credentials (oauth2Creds ), nil
65
+ }
66
+
67
+ return detectDefaultFromDialSettings (settings )
68
+ }
69
+
45
70
// GetOAuth2Configuration determines configurations for the OAuth2 transport, which is separate from the API transport.
46
71
// The OAuth2 transport and endpoint will be configured for mTLS if applicable.
47
72
func GetOAuth2Configuration (ctx context.Context , settings * DialSettings ) (string , * http.Client , error ) {
@@ -62,7 +87,7 @@ func GetOAuth2Configuration(ctx context.Context, settings *DialSettings) (string
62
87
return tokenURL , oauth2Client , nil
63
88
}
64
89
65
- func credsNewAuth (ctx context. Context , settings * DialSettings ) (* google.Credentials , error ) {
90
+ func credsNewAuth (settings * DialSettings ) (* google.Credentials , error ) {
66
91
// Preserve old options behavior
67
92
if settings .InternalCredentials != nil {
68
93
return settings .InternalCredentials , nil
@@ -76,6 +101,14 @@ func credsNewAuth(ctx context.Context, settings *DialSettings) (*google.Credenti
76
101
return oauth2adapt .Oauth2CredentialsFromAuthCredentials (settings .AuthCredentials ), nil
77
102
}
78
103
104
+ creds , err := detectDefaultFromDialSettings (settings )
105
+ if err != nil {
106
+ return nil , err
107
+ }
108
+ return oauth2adapt .Oauth2CredentialsFromAuthCredentials (creds ), nil
109
+ }
110
+
111
+ func detectDefaultFromDialSettings (settings * DialSettings ) (* auth.Credentials , error ) {
79
112
var useSelfSignedJWT bool
80
113
var aud string
81
114
var scopes []string
@@ -100,18 +133,13 @@ func credsNewAuth(ctx context.Context, settings *DialSettings) (*google.Credenti
100
133
aud = settings .DefaultAudience
101
134
}
102
135
103
- creds , err := credentials .DetectDefault (& credentials.DetectOptions {
136
+ return credentials .DetectDefault (& credentials.DetectOptions {
104
137
Scopes : scopes ,
105
138
Audience : aud ,
106
139
CredentialsFile : settings .CredentialsFile ,
107
140
CredentialsJSON : settings .CredentialsJSON ,
108
141
UseSelfSignedJWT : useSelfSignedJWT ,
109
142
})
110
- if err != nil {
111
- return nil , err
112
- }
113
-
114
- return oauth2adapt .Oauth2CredentialsFromAuthCredentials (creds ), nil
115
143
}
116
144
117
145
func baseCreds (ctx context.Context , ds * DialSettings ) (* google.Credentials , error ) {
0 commit comments