2017-07-23 07:51:42 +00:00
|
|
|
// Copyright 2017 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2017-09-30 14:27:27 +00:00
|
|
|
"encoding/json"
|
2017-07-23 07:51:42 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-09-30 14:27:27 +00:00
|
|
|
"time"
|
2017-07-23 07:51:42 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
)
|
|
|
|
|
2017-09-30 14:27:27 +00:00
|
|
|
// Creds returns credential information obtained from DialSettings, or if none, then
|
|
|
|
// it returns default credential information.
|
|
|
|
func Creds(ctx context.Context, ds *DialSettings) (*google.DefaultCredentials, error) {
|
|
|
|
if ds.CredentialsFile != "" {
|
|
|
|
return credFileTokenSource(ctx, ds.CredentialsFile, ds.Scopes...)
|
|
|
|
}
|
|
|
|
if ds.TokenSource != nil {
|
|
|
|
return &google.DefaultCredentials{TokenSource: ds.TokenSource}, nil
|
|
|
|
}
|
|
|
|
return google.FindDefaultCredentials(ctx, ds.Scopes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// credFileTokenSource reads a refresh token file or a service account and returns
|
2017-07-23 07:51:42 +00:00
|
|
|
// a TokenSource constructed from the config.
|
2017-09-30 14:27:27 +00:00
|
|
|
func credFileTokenSource(ctx context.Context, filename string, scope ...string) (*google.DefaultCredentials, error) {
|
2017-07-23 07:51:42 +00:00
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
2017-09-30 14:27:27 +00:00
|
|
|
return nil, fmt.Errorf("cannot read credentials file: %v", err)
|
2017-07-23 07:51:42 +00:00
|
|
|
}
|
2017-09-30 14:27:27 +00:00
|
|
|
// See if it is a refresh token credentials file first.
|
|
|
|
ts, ok, err := refreshTokenTokenSource(ctx, data, scope...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
return &google.DefaultCredentials{
|
|
|
|
TokenSource: ts,
|
|
|
|
JSON: data,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not, it should be a service account.
|
2017-07-23 07:51:42 +00:00
|
|
|
cfg, err := google.JWTConfigFromJSON(data, scope...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("google.JWTConfigFromJSON: %v", err)
|
|
|
|
}
|
2017-09-30 14:27:27 +00:00
|
|
|
// jwt.Config does not expose the project ID, so re-unmarshal to get it.
|
|
|
|
var pid struct {
|
|
|
|
ProjectID string `json:"project_id"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &pid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &google.DefaultCredentials{
|
|
|
|
ProjectID: pid.ProjectID,
|
|
|
|
TokenSource: cfg.TokenSource(ctx),
|
|
|
|
JSON: data,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshTokenTokenSource(ctx context.Context, data []byte, scope ...string) (oauth2.TokenSource, bool, error) {
|
|
|
|
var c cred
|
|
|
|
if err := json.Unmarshal(data, &c); err != nil {
|
|
|
|
return nil, false, fmt.Errorf("cannot unmarshal credentials file: %v", err)
|
|
|
|
}
|
|
|
|
if c.ClientID == "" || c.ClientSecret == "" || c.RefreshToken == "" || c.Type != "authorized_user" {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
cfg := &oauth2.Config{
|
|
|
|
ClientID: c.ClientID,
|
|
|
|
ClientSecret: c.ClientSecret,
|
|
|
|
Endpoint: google.Endpoint,
|
|
|
|
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
|
|
|
|
Scopes: scope,
|
|
|
|
}
|
|
|
|
return cfg.TokenSource(ctx, &oauth2.Token{
|
|
|
|
RefreshToken: c.RefreshToken,
|
|
|
|
Expiry: time.Now(),
|
|
|
|
}), true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type cred struct {
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
ClientSecret string `json:"client_secret"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
Type string `json:"type"`
|
2017-07-23 07:51:42 +00:00
|
|
|
}
|