forked from Mirrors/oauth2
oauth2/google: add client_credentials.json Config constructor
Change-Id: I27969a381784f83db7cc8287f65f4ba6a1d972bb Reviewed-on: https://go-review.googlesource.com/4968 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
721c86aedb
commit
407aee3975
|
@ -34,6 +34,39 @@ var Endpoint = oauth2.Endpoint{
|
||||||
// JWTTokenURL is Google's OAuth 2.0 token URL to use with the JWT flow.
|
// JWTTokenURL is Google's OAuth 2.0 token URL to use with the JWT flow.
|
||||||
const JWTTokenURL = "https://accounts.google.com/o/oauth2/token"
|
const JWTTokenURL = "https://accounts.google.com/o/oauth2/token"
|
||||||
|
|
||||||
|
// JWTConfigFromJSON uses a Google Developers Console client_credentials.json
|
||||||
|
// file to construct a config.
|
||||||
|
// client_credentials.json can be downloadable from https://console.developers.google.com,
|
||||||
|
// under "APIs & Auth" > "Credentials". Download the Web application credentials in the
|
||||||
|
// JSON format and provide the contents of the file as jsonKey.
|
||||||
|
func ConfigFromJSON(jsonKey []byte, scope ...string) (*oauth2.Config, error) {
|
||||||
|
var j struct {
|
||||||
|
Web struct {
|
||||||
|
ClientID string `json:"client_id"`
|
||||||
|
ClientSecret string `json:"client_secret"`
|
||||||
|
RedirectURIs []string `json:"redirect_uris"`
|
||||||
|
AuthURI string `json:"auth_uri"`
|
||||||
|
TokenURI string `json:"token_uri"`
|
||||||
|
} `json:"web"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(jsonKey, &j); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(j.Web.RedirectURIs) < 1 {
|
||||||
|
return nil, errors.New("oauth2/google: missing redirect URL in the client_credentials.json")
|
||||||
|
}
|
||||||
|
return &oauth2.Config{
|
||||||
|
ClientID: j.Web.ClientID,
|
||||||
|
ClientSecret: j.Web.ClientSecret,
|
||||||
|
RedirectURL: j.Web.RedirectURIs[0],
|
||||||
|
Scopes: scope,
|
||||||
|
Endpoint: oauth2.Endpoint{
|
||||||
|
AuthURL: j.Web.AuthURI,
|
||||||
|
TokenURL: j.Web.TokenURI,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// JWTConfigFromJSON uses a Google Developers service account JSON key file to read
|
// JWTConfigFromJSON uses a Google Developers service account JSON key file to read
|
||||||
// the credentials that authorize and authenticate the requests.
|
// the credentials that authorize and authenticate the requests.
|
||||||
// Create a service account on "Credentials" page under "APIs & Auth" for your
|
// Create a service account on "Credentials" page under "APIs & Auth" for your
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
// Copyright 2015 The oauth2 Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package google
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var webJSONKey = []byte(`
|
||||||
|
{
|
||||||
|
"web": {
|
||||||
|
"auth_uri": "https://google.com/o/oauth2/auth",
|
||||||
|
"client_secret": "3Oknc4jS_wA2r9i",
|
||||||
|
"token_uri": "https://google.com/o/oauth2/token",
|
||||||
|
"client_email": "222-nprqovg5k43uum874cs9osjt2koe97g8@developer.gserviceaccount.com",
|
||||||
|
"redirect_uris": ["https://www.example.com/oauth2callback"],
|
||||||
|
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/222-nprqovg5k43uum874cs9osjt2koe97g8@developer.gserviceaccount.com",
|
||||||
|
"client_id": "222-nprqovg5k43uum874cs9osjt2koe97g8.apps.googleusercontent.com",
|
||||||
|
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||||
|
"javascript_origins": ["https://www.example.com"]
|
||||||
|
}
|
||||||
|
}`)
|
||||||
|
|
||||||
|
func TestConfigFromJSON(t *testing.T) {
|
||||||
|
conf, err := ConfigFromJSON(webJSONKey, "scope1", "scope2")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if got, want := conf.ClientID, "222-nprqovg5k43uum874cs9osjt2koe97g8.apps.googleusercontent.com"; got != want {
|
||||||
|
t.Errorf("ClientID = %q; want %q", got, want)
|
||||||
|
}
|
||||||
|
if got, want := conf.ClientSecret, "3Oknc4jS_wA2r9i"; got != want {
|
||||||
|
t.Errorf("ClientSecret = %q; want %q", got, want)
|
||||||
|
}
|
||||||
|
if got, want := conf.RedirectURL, "https://www.example.com/oauth2callback"; got != want {
|
||||||
|
t.Errorf("RedictURL = %q; want %q", got, want)
|
||||||
|
}
|
||||||
|
if got, want := strings.Join(conf.Scopes, ","), "scope1,scope2"; got != want {
|
||||||
|
t.Errorf("Scopes = %q; want %q", got, want)
|
||||||
|
}
|
||||||
|
if got, want := conf.Endpoint.AuthURL, "https://google.com/o/oauth2/auth"; got != want {
|
||||||
|
t.Errorf("AuthURL = %q; want %q", got, want)
|
||||||
|
}
|
||||||
|
if got, want := conf.Endpoint.TokenURL, "https://google.com/o/oauth2/token"; got != want {
|
||||||
|
t.Errorf("TokenURL = %q; want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue