google-api-go-generator: allow "openid" as a scope
Updates https://github.com/googleapis/google-api-go-client/issues/446
Change-Id: Iaf8e5ead2037ebb181ab870be154735a78aa696e
Reviewed-on: https://code-review.googlesource.com/c/google-api-go-client/+/51391
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Tyler Bui-Palsulich <tbp@google.com>
diff --git a/google-api-go-generator/gen.go b/google-api-go-generator/gen.go
index e998290..1902f7b 100644
--- a/google-api-go-generator/gen.go
+++ b/google-api-go-generator/gen.go
@@ -605,7 +605,7 @@
pn(`// By default, all available scopes (see "Constants") are used to authenticate. To restrict scopes, use option.WithScopes:`)
pn("//")
// NOTE: the first scope tends to be the broadest. Use the last one to demonstrate restriction.
- pn("// %sService, err := %s.NewService(ctx, option.WithScopes(%s.%s))", pkg, pkg, pkg, scopeIdentifierFromURL(a.doc.Auth.OAuth2Scopes[len(a.doc.Auth.OAuth2Scopes)-1].URL))
+ pn("// %sService, err := %s.NewService(ctx, option.WithScopes(%s.%s))", pkg, pkg, pkg, scopeIdentifier(a.doc.Auth.OAuth2Scopes[len(a.doc.Auth.OAuth2Scopes)-1]))
pn("//")
}
pn("// To use an API key for authentication (note: some APIs do not support API keys), use option.WithAPIKey:")
@@ -683,7 +683,7 @@
if len(a.doc.Auth.OAuth2Scopes) != 0 {
pn("scopesOption := option.WithScopes(")
for _, scope := range a.doc.Auth.OAuth2Scopes {
- pn("%q,", scope.URL)
+ pn("%q,", scope.ID)
}
pn(")")
pn("// NOTE: prepend, so we don't override user-specified scopes.")
@@ -770,16 +770,21 @@
a.p("\n")
}
n++
- ident := scopeIdentifierFromURL(scope.URL)
+ ident := scopeIdentifier(scope)
if scope.Description != "" {
a.p("%s", asComment("\t", scope.Description))
}
- a.pn("\t%s = %q", ident, scope.URL)
+ a.pn("\t%s = %q", ident, scope.ID)
}
a.p(")\n\n")
}
-func scopeIdentifierFromURL(urlStr string) string {
+func scopeIdentifier(s disco.Scope) string {
+ if s.ID == "openid" {
+ return "OpenIDScope"
+ }
+
+ urlStr := s.ID
const prefix = "https://www.googleapis.com/auth/"
if !strings.HasPrefix(urlStr, prefix) {
const https = "https://"
diff --git a/google-api-go-generator/gen_test.go b/google-api-go-generator/gen_test.go
index 4a2cd92..9f530fe 100644
--- a/google-api-go-generator/gen_test.go
+++ b/google-api-go-generator/gen_test.go
@@ -13,6 +13,7 @@
"strings"
"testing"
+ "google.golang.org/api/google-api-go-generator/internal/disco"
"google.golang.org/api/internal/version"
)
@@ -99,8 +100,8 @@
},
}
for _, test := range tests {
- if got := scopeIdentifierFromURL(test[0]); got != test[1] {
- t.Errorf("scopeIdentifierFromURL(%q) got %q, want %q", test[0], got, test[1])
+ if got := scopeIdentifier(disco.Scope{ID: test[0]}); got != test[1] {
+ t.Errorf("scopeIdentifier(%q) got %q, want %q", test[0], got, test[1])
}
}
}
diff --git a/google-api-go-generator/internal/disco/disco.go b/google-api-go-generator/internal/disco/disco.go
index 5221d88..0dee96a 100644
--- a/google-api-go-generator/internal/disco/disco.go
+++ b/google-api-go-generator/internal/disco/disco.go
@@ -93,7 +93,7 @@
// A Scope is an OAuth2 scope.
type Scope struct {
- URL string
+ ID string
Description string
}
@@ -114,7 +114,7 @@
// Sort keys to provide a deterministic ordering, mainly for testing.
for _, k := range sortedKeys(m.OAuth2.Scopes) {
a.OAuth2Scopes = append(a.OAuth2Scopes, Scope{
- URL: k,
+ ID: k,
Description: m.OAuth2.Scopes[k].Description,
})
}