Skip to content

Commit

Permalink
Support output format option (#49)
Browse files Browse the repository at this point in the history
Support bear/pretty/json/json_compact/header
  • Loading branch information
shinfan authored Sep 7, 2018
1 parent 3148ca0 commit b1025ee
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 27 deletions.
17 changes: 10 additions & 7 deletions go/oauth2l/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
var (
// Common prefix for google oauth scope
scopePrefix = "https://www.googleapis.com/auth/"
cmds = []string{"fetch", "header", "info", "test"}
cmds = []string{"fetch", "header", "info", "test"}
)

func help() {
Expand Down Expand Up @@ -79,6 +79,9 @@ func main() {
helpFlag := flagSet.Bool("help", false, "Print help message.")
flagSet.BoolVar(helpFlag, "h", false, "")
jsonFile := flagSet.String("json", "", "Path to secret json file.")
format := flagSet.String("credentials_format", "bare", "Output format. The supported formats are: "+
"bare(default), header, json, json_compact, pretty")
flagSet.StringVar(format, "f", "bare", "")
jwtFlag := flagSet.Bool("jwt", false, "Use JWT auth flow")
ssoFlag := flagSet.Bool("sso", false, "Use SSO auth flow")
ssocli := flagSet.String("ssocli", "", "Path to SSO CLI")
Expand All @@ -93,14 +96,14 @@ func main() {
cmd := os.Args[1]

// Tasks that fetch the access token.
fetchTasks := map[string]func(*sgauth.Settings){
fetchTasks := map[string]func(*sgauth.Settings, string){
"fetch": util.Fetch,
"header": util.Header,
}

// Tasks that verify the existing token.
infoTasks := map[string]func(string){
"info": util.Info,
"info": util.Info,
"test": util.Test,
}

Expand All @@ -116,9 +119,9 @@ func main() {

settings := &sgauth.Settings{
CredentialsJSON: json,
Audience: flagSet.Args()[len(flagSet.Args()) - 1],
Audience: flagSet.Args()[len(flagSet.Args())-1],
}
task(settings)
task(settings, *format)
} else if *ssoFlag {
// SSO flow
util.SSOFetch(flagSet.Args()[0], *ssocli, cmd,
Expand All @@ -140,10 +143,10 @@ func main() {
OAuthFlowHandler: defaultAuthorizeFlowHandler,
State: "state",
}
task(settings)
task(settings, *format)
}
} else if task, ok := infoTasks[cmd]; ok {
task(flagSet.Args()[len(flagSet.Args()) - 1])
task(flagSet.Args()[len(flagSet.Args())-1])
} else if cmd == "reset" {
util.Reset()
} else {
Expand Down
6 changes: 5 additions & 1 deletion go/oauth2l/util/sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ func SSOFetch(email string, cli string, task string, scope string) {
if err != nil {
fmt.Println(err)
}
PrintToken("Bearer", out.String(), task == "header")
if task == "header" {
printHeader("Bearer", out.String())
} else {
println(out.String())
}
}
75 changes: 59 additions & 16 deletions go/oauth2l/util/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,34 @@ import (
"errors"
"github.com/google/oauth2l/go/sgauth"
"context"
"encoding/json"
"log"
)

const (
// Base URL to fetch the token info
googleTokenInfoURLPrefix =
"https://www.googleapis.com/oauth2/v3/tokeninfo/?access_token="
googleTokenInfoURLPrefix = "https://www.googleapis.com/oauth2/v3/tokeninfo/?access_token="
)

// Prints the token in either plain or header format
func PrintToken(tokenType string, token string, headerFormat bool) {
if headerFormat {
fmt.Printf("Authorization: %s %s\n", tokenType, token)
} else {
fmt.Println(token)
}
}
// Supported output formats
const (
formatJson = "json"
formatJsonCompact = "json_compact"
formatPretty = "pretty"
formatHeader = "header"
formatBare = "bare"
)

// Fetches and prints the token in plain text with the given settings
// using Google Authenticator.
func Fetch(settings *sgauth.Settings) {
token := fetchToken(settings)
PrintToken(token.TokenType, token.AccessToken, false)
func Fetch(settings *sgauth.Settings, format string) {
printToken(fetchToken(settings), format, getCredentialType(settings))
}

// Fetches and prints the token in header format with the given settings
// using Google Authenticator.
func Header(settings *sgauth.Settings) {
token := fetchToken(settings)
PrintToken(token.TokenType, token.AccessToken, true)
func Header(settings *sgauth.Settings, format string) {
Fetch(settings, formatHeader)
}

// Fetch the information of the given token.
Expand Down Expand Up @@ -109,3 +108,47 @@ func fetchToken(settings *sgauth.Settings) (*sgauth.Token) {
}
return token
}

func getCredentialType(settings *sgauth.Settings) (string) {
cred, err := sgauth.FindJSONCredentials(context.Background(), settings)
if err != nil {
return ""
}
return cred.Type
}

// Prints the token with the specified format
func printToken(token *sgauth.Token, format string, credType string) {
switch format {
case formatBare:
fmt.Println(token.AccessToken)
case formatHeader:
printHeader(token.TokenType, token.AccessToken)
case formatJson:
json, err := json.MarshalIndent(token.Raw, "", " ")
if err != nil {
log.Fatal(err.Error())
return
}
fmt.Println(string(json))
case formatJsonCompact:
json, err := json.Marshal(token.Raw)
if err != nil {
log.Fatal(err.Error())
return
}
fmt.Println(string(json))
case formatPretty:
fmt.Printf("Fetched credentials of type:\n %s\n"+
"Access Token:\n %s\n",
credType, token.AccessToken)
default:
log.Fatalf("Invalid choice: '%s' "+
"(choose from 'bare', 'header', 'json', 'json_compact', 'pretty')",
format)
}
}

func printHeader(tokenType string, token string) {
fmt.Printf("Authorization: %s %s\n", tokenType, token)
}
3 changes: 3 additions & 0 deletions go/sgauth/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ type Credentials struct {
// environment and not with a credentials file, e.g. when code is
// running on Google Cloud Platform.
JSON []byte

// The type of the credentials. e.g. service account.
Type string
}
7 changes: 4 additions & 3 deletions go/sgauth/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func DefaultTokenSource(ctx context.Context, scope string) (internal.TokenSource
}

func OAuthJSONTokenSource(ctx context.Context, settings *Settings) (internal.TokenSource, error) {
creds, err := findJSONCredentials(ctx, settings)
creds, err := FindJSONCredentials(ctx, settings)
if err != nil {
return nil, err
}
Expand All @@ -50,15 +50,15 @@ func OAuthJSONTokenSource(ctx context.Context, settings *Settings) (internal.Tok
}

func JWTTokenSource(ctx context.Context, settings *Settings) (internal.TokenSource, error) {
creds, err := findJSONCredentials(ctx, settings)
creds, err := FindJSONCredentials(ctx, settings)
if err != nil {
return nil, err
}
ts, err := credentials.JWTAccessTokenSourceFromJSON(creds.JSON, settings.Audience)
return ts, err
}

func findJSONCredentials(ctx context.Context, settings *Settings) (*credentials.Credentials, error) {
func FindJSONCredentials(ctx context.Context, settings *Settings) (*credentials.Credentials, error) {
if settings.CredentialsJSON != "" {
return credentialsFromJSON(ctx, []byte(settings.CredentialsJSON),
strings.Split(settings.Scope, " "), settings.OAuthFlowHandler, settings.State)
Expand Down Expand Up @@ -131,6 +131,7 @@ func credentialsFromJSON(ctx context.Context, jsonData []byte, scopes []string,
ProjectID: f.ProjectID,
TokenSource: ts,
JSON: jsonData,
Type: f.CredentialsType(),
}, nil
}

Expand Down

0 comments on commit b1025ee

Please sign in to comment.