mirror of
https://github.com/golang/oauth2.git
synced 2025-07-21 00:00:09 +08:00
google: make JWTConfigFromJSON set TokenURL from the JSON's token_uri
Fixes golang/oauth2#199. Change-Id: I534def935c7143e4276b5d880127b0af35409f9a Reviewed-on: https://go-review.googlesource.com/28411 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
4d549c893b
commit
2d2b68866f
@ -89,6 +89,7 @@ func JWTConfigFromJSON(jsonKey []byte, scope ...string) (*jwt.Config, error) {
|
|||||||
Email string `json:"client_email"`
|
Email string `json:"client_email"`
|
||||||
PrivateKey string `json:"private_key"`
|
PrivateKey string `json:"private_key"`
|
||||||
PrivateKeyID string `json:"private_key_id"`
|
PrivateKeyID string `json:"private_key_id"`
|
||||||
|
TokenURL string `json:"token_uri"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(jsonKey, &key); err != nil {
|
if err := json.Unmarshal(jsonKey, &key); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -98,7 +99,10 @@ func JWTConfigFromJSON(jsonKey []byte, scope ...string) (*jwt.Config, error) {
|
|||||||
PrivateKey: []byte(key.PrivateKey),
|
PrivateKey: []byte(key.PrivateKey),
|
||||||
PrivateKeyID: key.PrivateKeyID,
|
PrivateKeyID: key.PrivateKeyID,
|
||||||
Scopes: scope,
|
Scopes: scope,
|
||||||
TokenURL: JWTTokenURL,
|
TokenURL: key.TokenURL,
|
||||||
|
}
|
||||||
|
if config.TokenURL == "" {
|
||||||
|
config.TokenURL = JWTTokenURL
|
||||||
}
|
}
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,15 @@ var installedJSONKey = []byte(`{
|
|||||||
}`)
|
}`)
|
||||||
|
|
||||||
var jwtJSONKey = []byte(`{
|
var jwtJSONKey = []byte(`{
|
||||||
|
"private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
|
||||||
|
"private_key": "super secret key",
|
||||||
|
"client_email": "gopher@developer.gserviceaccount.com",
|
||||||
|
"client_id": "gopher.apps.googleusercontent.com",
|
||||||
|
"token_uri": "https://accounts.google.com/o/gophers/token",
|
||||||
|
"type": "service_account"
|
||||||
|
}`)
|
||||||
|
|
||||||
|
var jwtJSONKeyNoTokenURL = []byte(`{
|
||||||
"private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
|
"private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
|
||||||
"private_key": "super secret key",
|
"private_key": "super secret key",
|
||||||
"client_email": "gopher@developer.gserviceaccount.com",
|
"client_email": "gopher@developer.gserviceaccount.com",
|
||||||
@ -91,6 +100,16 @@ func TestJWTConfigFromJSON(t *testing.T) {
|
|||||||
if got, want := strings.Join(conf.Scopes, ","), "scope1,scope2"; got != want {
|
if got, want := strings.Join(conf.Scopes, ","), "scope1,scope2"; got != want {
|
||||||
t.Errorf("Scopes = %q; want %q", got, want)
|
t.Errorf("Scopes = %q; want %q", got, want)
|
||||||
}
|
}
|
||||||
|
if got, want := conf.TokenURL, "https://accounts.google.com/o/gophers/token"; got != want {
|
||||||
|
t.Errorf("TokenURL = %q; want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJWTConfigFromJSONNoTokenURL(t *testing.T) {
|
||||||
|
conf, err := JWTConfigFromJSON(jwtJSONKeyNoTokenURL, "scope1", "scope2")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
if got, want := conf.TokenURL, "https://accounts.google.com/o/oauth2/token"; got != want {
|
if got, want := conf.TokenURL, "https://accounts.google.com/o/oauth2/token"; got != want {
|
||||||
t.Errorf("TokenURL = %q; want %q", got, want)
|
t.Errorf("TokenURL = %q; want %q", got, want)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user