Skip to content

Commit 9db96f9

Browse files
authored
Print authorize url in the silent and no-browser mode (#147)
1 parent c51bce1 commit 9db96f9

4 files changed

Lines changed: 20 additions & 30 deletions

File tree

cmd/log.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"crypto/x509"
88
"encoding/json"
99
"encoding/pem"
10+
"fmt"
1011
"os"
1112
"strconv"
1213
"strings"
1314

15+
"github.com/cli/browser"
1416
"github.com/go-jose/go-jose/v3"
1517
"github.com/go-jose/go-jose/v3/jwt"
1618
"github.com/go-playground/validator/v10"
@@ -148,7 +150,6 @@ func LogJson(value interface{}) {
148150
}
149151

150152
output, err := json.Marshal(value)
151-
152153
if err != nil {
153154
pterm.Error.Println(err)
154155
return
@@ -397,7 +398,6 @@ func LogKey(name string, key interface{}) {
397398
pterm.FgGray.Printfln("%s", p.String())
398399
case *ecdsa.PublicKey:
399400
b, err := x509.MarshalPKIXPublicKey(key)
400-
401401
if err != nil {
402402
pterm.Error.Println(err)
403403
}
@@ -414,7 +414,6 @@ func LogKey(name string, key interface{}) {
414414
pterm.FgGray.Printfln("%s", p.String())
415415
case *ecdsa.PrivateKey:
416416
b, err := x509.MarshalECPrivateKey(key)
417-
418417
if err != nil {
419418
pterm.Error.Println(err)
420419
}
@@ -473,3 +472,18 @@ func LogSubjectTokenAndActorToken(request oauth2.Request) {
473472
pterm.Println()
474473
}
475474
}
475+
476+
func LogAuthURL(url string, noBrowser bool) {
477+
if noBrowser && silent {
478+
fmt.Fprintln(os.Stderr, url)
479+
} else {
480+
Logfln("\nGo to the following URL:\n\n%s", url)
481+
}
482+
483+
if !noBrowser {
484+
Logfln("\nOpening browser...")
485+
if err := browser.OpenURL(url); err != nil {
486+
LogError(err)
487+
}
488+
}
489+
}

cmd/oauth2_authorize_code.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"net/http"
66

7-
"github.com/cli/browser"
87
"github.com/cloudentity/oauth2c/internal/oauth2"
98
)
109

@@ -54,14 +53,7 @@ func (c *OAuth2Cmd) AuthorizationCodeGrantFlow(clientConfig oauth2.ClientConfig,
5453
LogBox("PKCE", "code_verifier = %s\ncode_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))", codeVerifier)
5554
}
5655

57-
Logfln("\nGo to the following URL:\n\n%s", authorizeRequest.URL.String())
58-
59-
if !clientConfig.NoBrowser {
60-
Logfln("\nOpening browser...")
61-
if err = browser.OpenURL(authorizeRequest.URL.String()); err != nil {
62-
LogError(err)
63-
}
64-
}
56+
LogAuthURL(authorizeRequest.URL.String(), clientConfig.NoBrowser)
6557

6658
Logln()
6759

cmd/oauth2_device.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/http"
77
"time"
88

9-
"github.com/cli/browser"
109
"github.com/cloudentity/oauth2c/internal/oauth2"
1110
)
1211

@@ -36,14 +35,7 @@ func (c *OAuth2Cmd) DeviceGrantFlow(clientConfig oauth2.ClientConfig, serverConf
3635
verificationUri = *authorizationResponse.VerificationURIComplete
3736
}
3837

39-
Logfln("\nGo to the following URL:\n\n%s", verificationUri)
40-
41-
if !clientConfig.NoBrowser {
42-
Logfln("\nOpening browser...")
43-
if err = browser.OpenURL(verificationUri); err != nil {
44-
LogError(err)
45-
}
46-
}
38+
LogAuthURL(verificationUri, clientConfig.NoBrowser)
4739

4840
Logln()
4941

cmd/oauth2_implicit.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"net/http"
55

6-
"github.com/cli/browser"
76
"github.com/cloudentity/oauth2c/internal/oauth2"
87
)
98

@@ -25,14 +24,7 @@ func (c *OAuth2Cmd) ImplicitGrantFlow(clientConfig oauth2.ClientConfig, serverCo
2524

2625
LogRequest(authorizeRequest)
2726

28-
Logfln("\nGo to the following URL:\n\n%s", authorizeRequest.URL.String())
29-
30-
if !clientConfig.NoBrowser {
31-
Logfln("\nOpening browser...")
32-
if err = browser.OpenURL(authorizeRequest.URL.String()); err != nil {
33-
LogError(err)
34-
}
35-
}
27+
LogAuthURL(authorizeRequest.URL.String(), clientConfig.NoBrowser)
3628

3729
Logln()
3830

0 commit comments

Comments
 (0)