Skip to content

Commit

Permalink
Print debug info to stderr (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbilski authored Sep 29, 2023
1 parent 0af68f7 commit 404f381
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ The available flags are:
--idp-hint string identity provider hint
--insecure allow insecure connections
--login-hint string user identifier hint
--no-prompt disable prompt
--par enable pushed authorization requests (PAR)
--password string resource owner password credentials grant flow password
--pkce enable proof key for code exchange (PKCE)
Expand Down
6 changes: 6 additions & 0 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/x509"
"encoding/json"
"encoding/pem"
"os"
"strconv"
"strings"

Expand All @@ -19,6 +20,10 @@ import (
"github.com/tidwall/pretty"
)

func init() {
pterm.SetDefaultOutput(os.Stderr)
}

func Logln() {
if silent {
return
Expand Down Expand Up @@ -59,6 +64,7 @@ func LogAction(msg string) func(string) {
done, _ := pterm.DefaultSpinner.Start(msg)
return func(s string) {
done.Success(s)
pterm.Println()
}
}

Expand Down
12 changes: 6 additions & 6 deletions cmd/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
)

var (
silent bool
silent bool
noPrompt bool
)

type OAuth2Cmd struct {
Expand Down Expand Up @@ -75,6 +76,7 @@ func NewOAuth2Cmd() (cmd *OAuth2Cmd) {
cmd.PersistentFlags().DurationVar(&cconfig.BrowserTimeout, "browser-timeout", 10*time.Minute, "browser timeout")
cmd.PersistentFlags().BoolVar(&cconfig.Insecure, "insecure", false, "allow insecure connections")
cmd.PersistentFlags().BoolVarP(&silent, "silent", "s", false, "silent mode")
cmd.PersistentFlags().BoolVar(&noPrompt, "no-prompt", false, "disable prompt")
cmd.PersistentFlags().BoolVar(&cconfig.DPoP, "dpop", false, "use DPoP")
cmd.PersistentFlags().StringVar(&cconfig.Claims, "claims", "", "use claims")
cmd.PersistentFlags().StringVar(&cconfig.RAR, "rar", "", "use rich authorization request (RAR)")
Expand Down Expand Up @@ -108,6 +110,8 @@ func (c *OAuth2Cmd) Run(cconfig *oauth2.ClientConfig) func(cmd *cobra.Command, a

if silent {
browser.Stdout = io.Discard
} else {
browser.Stdout = os.Stderr
}

tr := &http.Transport{
Expand Down Expand Up @@ -169,7 +173,7 @@ func (c *OAuth2Cmd) Authorize(clientConfig oauth2.ClientConfig, hc *http.Client)
return err
}

if !silent {
if !silent && !noPrompt {
clientConfig = PromptForClientConfig(clientConfig, serverConfig)
}

Expand Down Expand Up @@ -198,10 +202,6 @@ func (c *OAuth2Cmd) Authorize(clientConfig oauth2.ClientConfig, hc *http.Client)
}

func (c *OAuth2Cmd) PrintResult(result interface{}) {
if !silent {
return
}

output, err := json.Marshal(result)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/oauth2_authorize_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ func (c *OAuth2Cmd) AuthorizationCodeGrantFlow(clientConfig oauth2.ClientConfig,
LogRequestAndResponse(tokenRequest, tokenResponse)
LogTokenPayloadln(tokenResponse)

c.PrintResult(tokenResponse)

exchangeStatus("Exchanged authorization code for access token")

c.PrintResult(tokenResponse)

return nil
}
4 changes: 2 additions & 2 deletions cmd/oauth2_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func (c *OAuth2Cmd) DeviceGrantFlow(clientConfig oauth2.ClientConfig, serverConf
LogRequestAndResponse(tokenRequest, tokenResponse)
LogTokenPayloadln(tokenResponse)

c.PrintResult(tokenResponse)

tokenStatus("Obtained token")

c.PrintResult(tokenResponse)

return nil
}
4 changes: 2 additions & 2 deletions cmd/oauth2_implicit.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (c *OAuth2Cmd) ImplicitGrantFlow(clientConfig oauth2.ClientConfig, serverCo
LogTokenPayloadln(tokenResponse)
Logln()

c.PrintResult(tokenResponse)

callbackStatus("Obtained authorization")

c.PrintResult(tokenResponse)

return nil
}
4 changes: 2 additions & 2 deletions cmd/oauth2_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func (c *OAuth2Cmd) tokenEndpointFlow(
LogRequestAndResponse(tokenRequest, tokenResponse)
LogTokenPayloadln(tokenResponse)

c.PrintResult(tokenResponse)

authorizationStatus("Authorization completed")

c.PrintResult(tokenResponse)

return nil
}

0 comments on commit 404f381

Please sign in to comment.