forked from remote/oauth2
google: more nits
Change-Id: I6b655c042e1758a49727909031847a86d092e303
This commit is contained in:
@@ -68,13 +68,13 @@ type CredentialSource struct {
|
|||||||
// instance determines the type of CredentialSource needed
|
// instance determines the type of CredentialSource needed
|
||||||
func (c *Config) parse() baseCredentialSource {
|
func (c *Config) parse() baseCredentialSource {
|
||||||
if c.CredentialSource.File != "" {
|
if c.CredentialSource.File != "" {
|
||||||
return fileCredentialSource{File: c.CredentialSource.File}
|
return fileCredentialSource{File: c.CredentialSource.File, Format: c.CredentialSource.Format}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type baseCredentialSource interface {
|
type baseCredentialSource interface {
|
||||||
retrieveSubjectToken(c *Config) (string, error)
|
subjectToken() (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// tokenSource is the source that handles external credentials.
|
// tokenSource is the source that handles external credentials.
|
||||||
@@ -91,7 +91,7 @@ func (ts tokenSource) Token() (*oauth2.Token, error) {
|
|||||||
if credSource == nil {
|
if credSource == nil {
|
||||||
return nil, fmt.Errorf("oauth2/google: unable to parse credential source")
|
return nil, fmt.Errorf("oauth2/google: unable to parse credential source")
|
||||||
}
|
}
|
||||||
subjectToken, err := credSource.retrieveSubjectToken(conf)
|
subjectToken, err := credSource.subjectToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,33 +14,34 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type fileCredentialSource struct {
|
type fileCredentialSource struct {
|
||||||
File string
|
File string
|
||||||
|
Format format
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs fileCredentialSource) retrieveSubjectToken(c *Config) (string, error) {
|
func (cs fileCredentialSource) subjectToken() (string, error) {
|
||||||
tokenFile, err := os.Open(cs.File)
|
tokenFile, err := os.Open(cs.File)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("oauth2/google: failed to open credential file %q\n", cs.File)
|
return "", fmt.Errorf("oauth2/google: failed to open credential file %q", cs.File)
|
||||||
}
|
}
|
||||||
defer tokenFile.Close()
|
defer tokenFile.Close()
|
||||||
tokenBytes, err := ioutil.ReadAll(tokenFile)
|
tokenBytes, err := ioutil.ReadAll(tokenFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("oauth2/google: failed to read credential file; %q", err)
|
return "", fmt.Errorf("oauth2/google: failed to read credential file: %v", err)
|
||||||
}
|
}
|
||||||
tokenBytes = bytes.TrimSpace(tokenBytes)
|
tokenBytes = bytes.TrimSpace(tokenBytes)
|
||||||
var output string
|
var output string
|
||||||
switch c.CredentialSource.Format.Type {
|
switch cs.Format.Type {
|
||||||
case "json":
|
case "json":
|
||||||
jsonData := make(map[string]interface{})
|
jsonData := make(map[string]interface{})
|
||||||
err = json.Unmarshal(tokenBytes, &jsonData)
|
err = json.Unmarshal(tokenBytes, &jsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("oauth2/google: failed to unmarshal subject token file; %q", err)
|
return "", fmt.Errorf("oauth2/google: failed to unmarshal subject token file: %v", err)
|
||||||
}
|
}
|
||||||
if val, ok := jsonData[c.CredentialSource.Format.SubjectTokenFieldName]; !ok {
|
if val, ok := jsonData[cs.Format.SubjectTokenFieldName]; !ok {
|
||||||
return "", errors.New("oauth2/google: provided subject_token_field_name not found in credentials")
|
return "", errors.New("oauth2/google: provided subject_token_field_name not found in credentials")
|
||||||
} else {
|
} else {
|
||||||
token, ok := val.(string)
|
token, ok := val.(string)
|
||||||
if ok {
|
if !ok {
|
||||||
return "", errors.New("oauth2/google: improperly formatted subject token")
|
return "", errors.New("oauth2/google: improperly formatted subject token")
|
||||||
}
|
}
|
||||||
output = token
|
output = token
|
||||||
|
|||||||
Reference in New Issue
Block a user