mirror of
https://github.com/golang/oauth2.git
synced 2025-07-21 00:00:09 +08:00
oauth2/google: ConfigFromJSON should support the installed app credentials
client_credentials.json may contain credentials for the installed applications. Populate the Config depending on what's available in the JSON key. Change-Id: I47f494f1c31967a920fe557a9e8c1c4652943c4e Reviewed-on: https://go-review.googlesource.com/7250 Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
parent
5cccf1a7e7
commit
54a4310f85
@ -40,29 +40,40 @@ const JWTTokenURL = "https://accounts.google.com/o/oauth2/token"
|
|||||||
// under "APIs & Auth" > "Credentials". Download the Web application credentials in the
|
// under "APIs & Auth" > "Credentials". Download the Web application credentials in the
|
||||||
// JSON format and provide the contents of the file as jsonKey.
|
// JSON format and provide the contents of the file as jsonKey.
|
||||||
func ConfigFromJSON(jsonKey []byte, scope ...string) (*oauth2.Config, error) {
|
func ConfigFromJSON(jsonKey []byte, scope ...string) (*oauth2.Config, error) {
|
||||||
var j struct {
|
type cred struct {
|
||||||
Web struct {
|
|
||||||
ClientID string `json:"client_id"`
|
ClientID string `json:"client_id"`
|
||||||
ClientSecret string `json:"client_secret"`
|
ClientSecret string `json:"client_secret"`
|
||||||
RedirectURIs []string `json:"redirect_uris"`
|
RedirectURIs []string `json:"redirect_uris"`
|
||||||
AuthURI string `json:"auth_uri"`
|
AuthURI string `json:"auth_uri"`
|
||||||
TokenURI string `json:"token_uri"`
|
TokenURI string `json:"token_uri"`
|
||||||
} `json:"web"`
|
}
|
||||||
|
var j struct {
|
||||||
|
Web *cred `json:"web"`
|
||||||
|
Installed *cred `json:"installed"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(jsonKey, &j); err != nil {
|
if err := json.Unmarshal(jsonKey, &j); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(j.Web.RedirectURIs) < 1 {
|
var c *cred
|
||||||
|
switch {
|
||||||
|
case j.Web != nil:
|
||||||
|
c = j.Web
|
||||||
|
case j.Installed != nil:
|
||||||
|
c = j.Installed
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("oauth2/google: no credentials found")
|
||||||
|
}
|
||||||
|
if len(c.RedirectURIs) < 1 {
|
||||||
return nil, errors.New("oauth2/google: missing redirect URL in the client_credentials.json")
|
return nil, errors.New("oauth2/google: missing redirect URL in the client_credentials.json")
|
||||||
}
|
}
|
||||||
return &oauth2.Config{
|
return &oauth2.Config{
|
||||||
ClientID: j.Web.ClientID,
|
ClientID: c.ClientID,
|
||||||
ClientSecret: j.Web.ClientSecret,
|
ClientSecret: c.ClientSecret,
|
||||||
RedirectURL: j.Web.RedirectURIs[0],
|
RedirectURL: c.RedirectURIs[0],
|
||||||
Scopes: scope,
|
Scopes: scope,
|
||||||
Endpoint: oauth2.Endpoint{
|
Endpoint: oauth2.Endpoint{
|
||||||
AuthURL: j.Web.AuthURI,
|
AuthURL: c.AuthURI,
|
||||||
TokenURL: j.Web.TokenURI,
|
TokenURL: c.TokenURI,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,13 @@ var webJSONKey = []byte(`
|
|||||||
}
|
}
|
||||||
}`)
|
}`)
|
||||||
|
|
||||||
|
var installedJSONKey = []byte(`{
|
||||||
|
"installed": {
|
||||||
|
"client_id": "222-installed.apps.googleusercontent.com",
|
||||||
|
"redirect_uris": ["https://www.example.com/oauth2callback"]
|
||||||
|
}
|
||||||
|
}`)
|
||||||
|
|
||||||
func TestConfigFromJSON(t *testing.T) {
|
func TestConfigFromJSON(t *testing.T) {
|
||||||
conf, err := ConfigFromJSON(webJSONKey, "scope1", "scope2")
|
conf, err := ConfigFromJSON(webJSONKey, "scope1", "scope2")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -48,3 +55,13 @@ func TestConfigFromJSON(t *testing.T) {
|
|||||||
t.Errorf("TokenURL = %q; want %q", got, want)
|
t.Errorf("TokenURL = %q; want %q", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigFromJSON_Installed(t *testing.T) {
|
||||||
|
conf, err := ConfigFromJSON(installedJSONKey)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if got, want := conf.ClientID, "222-installed.apps.googleusercontent.com"; got != want {
|
||||||
|
t.Errorf("ClientID = %q; want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user