Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix caching of default credentials #84

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"context"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -121,6 +122,19 @@ func readJSON(file string) (string, error) {
return "", nil
}

// Attempts to read JSON credentials from default locations if unset.
// This is redundant as sgauth performs this resolution automatically, but
// necessary for proper caching in case the default location changes.
func resolveDefaultJSON(settings *sgauth.Settings) {
if settings.CredentialsJSON != "" {
return
}
credentials, err := sgauth.FindJSONCredentials(context.Background(), settings)
if err == nil {
settings.CredentialsJSON = string(credentials.JSON)
}
}

// Default 3LO authorization handler. Prints the authorization URL on stdout
// and reads the verification code from stdin.
func defaultAuthorizeFlowHandler(authorizeUrl string) (string, error) {
Expand Down Expand Up @@ -291,6 +305,7 @@ func main() {
CredentialsJSON: json,
Audience: audience,
}
resolveDefaultJSON(settings)

taskArgs := getTaskArgs(cmd, curlcli, url, format, remainingArgs...)
task(settings, taskArgs...)
Expand Down Expand Up @@ -353,6 +368,7 @@ func main() {
OAuthFlowHandler: defaultAuthorizeFlowHandler,
State: "state",
}
resolveDefaultJSON(settings)

taskArgs := getTaskArgs(cmd, curlcli, url, format, remainingArgs...)
task(settings, taskArgs...)
Expand Down