2015-08-27 03:42:02 +03:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-05-17 19:26:57 +04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-05-13 22:06:46 +04:00
|
|
|
|
2015-02-26 08:39:41 +03:00
|
|
|
// Package google provides support for making OAuth2 authorized and
|
|
|
|
// authenticated HTTP requests to Google APIs.
|
|
|
|
// It supports the Web server flow, client-side credentials, service accounts,
|
|
|
|
// Google Compute Engine service accounts, and Google App Engine service
|
|
|
|
// accounts.
|
2014-05-06 01:54:23 +04:00
|
|
|
//
|
|
|
|
// For more information, please read
|
2015-02-26 08:39:41 +03:00
|
|
|
// https://developers.google.com/accounts/docs/OAuth2
|
|
|
|
// and
|
2015-04-05 04:18:45 +03:00
|
|
|
// https://developers.google.com/accounts/docs/application-default-credentials.
|
2014-11-26 22:44:45 +03:00
|
|
|
package google // import "golang.org/x/oauth2/google"
|
2014-05-06 01:54:23 +04:00
|
|
|
|
|
|
|
import (
|
2014-05-11 19:06:03 +04:00
|
|
|
"encoding/json"
|
2015-01-06 22:34:34 +03:00
|
|
|
"errors"
|
2014-10-23 21:06:00 +04:00
|
|
|
"fmt"
|
2015-01-06 22:34:34 +03:00
|
|
|
"strings"
|
2014-05-11 19:06:03 +04:00
|
|
|
"time"
|
|
|
|
|
2014-11-26 22:44:45 +03:00
|
|
|
"golang.org/x/oauth2"
|
2014-12-31 02:11:02 +03:00
|
|
|
"golang.org/x/oauth2/jwt"
|
2015-01-06 22:34:34 +03:00
|
|
|
"google.golang.org/cloud/compute/metadata"
|
2014-05-06 01:54:23 +04:00
|
|
|
)
|
|
|
|
|
2014-12-11 10:30:13 +03:00
|
|
|
// Endpoint is Google's OAuth 2.0 endpoint.
|
|
|
|
var Endpoint = oauth2.Endpoint{
|
|
|
|
AuthURL: "https://accounts.google.com/o/oauth2/auth",
|
|
|
|
TokenURL: "https://accounts.google.com/o/oauth2/token",
|
|
|
|
}
|
|
|
|
|
|
|
|
// JWTTokenURL is Google's OAuth 2.0 token URL to use with the JWT flow.
|
|
|
|
const JWTTokenURL = "https://accounts.google.com/o/oauth2/token"
|
|
|
|
|
2015-02-26 08:39:41 +03:00
|
|
|
// ConfigFromJSON uses a Google Developers Console client_credentials.json
|
2015-02-17 23:48:56 +03:00
|
|
|
// 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) {
|
2015-03-10 02:33:51 +03:00
|
|
|
type cred 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"`
|
|
|
|
}
|
2015-02-17 23:48:56 +03:00
|
|
|
var j struct {
|
2015-03-10 02:33:51 +03:00
|
|
|
Web *cred `json:"web"`
|
|
|
|
Installed *cred `json:"installed"`
|
2015-02-17 23:48:56 +03:00
|
|
|
}
|
|
|
|
if err := json.Unmarshal(jsonKey, &j); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-10 02:33:51 +03:00
|
|
|
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 {
|
2015-02-17 23:48:56 +03:00
|
|
|
return nil, errors.New("oauth2/google: missing redirect URL in the client_credentials.json")
|
|
|
|
}
|
|
|
|
return &oauth2.Config{
|
2015-03-10 02:33:51 +03:00
|
|
|
ClientID: c.ClientID,
|
|
|
|
ClientSecret: c.ClientSecret,
|
|
|
|
RedirectURL: c.RedirectURIs[0],
|
2015-02-17 23:48:56 +03:00
|
|
|
Scopes: scope,
|
|
|
|
Endpoint: oauth2.Endpoint{
|
2015-03-10 02:33:51 +03:00
|
|
|
AuthURL: c.AuthURI,
|
|
|
|
TokenURL: c.TokenURI,
|
2015-02-17 23:48:56 +03:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-12-11 10:30:13 +03:00
|
|
|
// JWTConfigFromJSON uses a Google Developers service account JSON key file to read
|
|
|
|
// the credentials that authorize and authenticate the requests.
|
|
|
|
// Create a service account on "Credentials" page under "APIs & Auth" for your
|
|
|
|
// project at https://console.developers.google.com to download a JSON key file.
|
2015-01-14 02:27:09 +03:00
|
|
|
func JWTConfigFromJSON(jsonKey []byte, scope ...string) (*jwt.Config, error) {
|
2014-12-11 10:30:13 +03:00
|
|
|
var key struct {
|
|
|
|
Email string `json:"client_email"`
|
|
|
|
PrivateKey string `json:"private_key"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(jsonKey, &key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-31 02:11:02 +03:00
|
|
|
return &jwt.Config{
|
2014-12-11 10:30:13 +03:00
|
|
|
Email: key.Email,
|
|
|
|
PrivateKey: []byte(key.PrivateKey),
|
|
|
|
Scopes: scope,
|
|
|
|
TokenURL: JWTTokenURL,
|
|
|
|
}, nil
|
|
|
|
}
|
2014-05-06 01:54:23 +04:00
|
|
|
|
2014-12-11 10:30:13 +03:00
|
|
|
// ComputeTokenSource returns a token source that fetches access tokens
|
|
|
|
// from Google Compute Engine (GCE)'s metadata server. It's only valid to use
|
|
|
|
// this token source if your program is running on a GCE instance.
|
|
|
|
// If no account is specified, "default" is used.
|
|
|
|
// Further information about retrieving access tokens from the GCE metadata
|
|
|
|
// server can be found at https://cloud.google.com/compute/docs/authentication.
|
|
|
|
func ComputeTokenSource(account string) oauth2.TokenSource {
|
2015-01-06 22:34:34 +03:00
|
|
|
return oauth2.ReuseTokenSource(nil, computeSource{account: account})
|
2014-09-03 22:40:47 +04:00
|
|
|
}
|
|
|
|
|
2014-12-11 10:30:13 +03:00
|
|
|
type computeSource struct {
|
|
|
|
account string
|
2014-05-10 11:41:39 +04:00
|
|
|
}
|
|
|
|
|
2015-01-06 22:34:34 +03:00
|
|
|
func (cs computeSource) Token() (*oauth2.Token, error) {
|
|
|
|
if !metadata.OnGCE() {
|
|
|
|
return nil, errors.New("oauth2/google: can't get a token from the metadata service; not running on GCE")
|
|
|
|
}
|
2014-12-11 10:30:13 +03:00
|
|
|
acct := cs.account
|
|
|
|
if acct == "" {
|
|
|
|
acct = "default"
|
2014-09-03 01:06:51 +04:00
|
|
|
}
|
2015-01-06 22:34:34 +03:00
|
|
|
tokenJSON, err := metadata.Get("instance/service-accounts/" + acct + "/token")
|
2014-12-11 10:30:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-06 22:34:34 +03:00
|
|
|
var res struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
ExpiresInSec int `json:"expires_in"`
|
|
|
|
TokenType string `json:"token_type"`
|
2014-12-11 10:30:13 +03:00
|
|
|
}
|
2015-01-06 22:34:34 +03:00
|
|
|
err = json.NewDecoder(strings.NewReader(tokenJSON)).Decode(&res)
|
2014-12-11 10:30:13 +03:00
|
|
|
if err != nil {
|
2015-01-06 22:34:34 +03:00
|
|
|
return nil, fmt.Errorf("oauth2/google: invalid token JSON from metadata: %v", err)
|
|
|
|
}
|
|
|
|
if res.ExpiresInSec == 0 || res.AccessToken == "" {
|
|
|
|
return nil, fmt.Errorf("oauth2/google: incomplete token received from metadata")
|
2014-09-03 01:06:51 +04:00
|
|
|
}
|
2014-12-11 10:30:13 +03:00
|
|
|
return &oauth2.Token{
|
2015-01-06 22:34:34 +03:00
|
|
|
AccessToken: res.AccessToken,
|
|
|
|
TokenType: res.TokenType,
|
|
|
|
Expiry: time.Now().Add(time.Duration(res.ExpiresInSec) * time.Second),
|
2014-12-11 10:30:13 +03:00
|
|
|
}, nil
|
2014-09-03 01:06:51 +04:00
|
|
|
}
|