Skip to content

Commit 0ed9a67

Browse files
authored
Add silent mode (#27)
1 parent 677c334 commit 0ed9a67

File tree

6 files changed

+211
-66
lines changed

6 files changed

+211
-66
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The available flags are:
6464
--client-id string client identifier
6565
--client-secret string client secret
6666
--grant-type string grant type
67+
-h, --help help for oauthc
6768
--insecure allow insecure connections
6869
--no-pkce disable proof key for code exchange (PKCE)
6970
--password string resource owner password credentials grant flow password
@@ -73,6 +74,7 @@ The available flags are:
7374
--response-types strings response type
7475
--scopes strings requested scopes
7576
--signing-key string path or url to signing key in jwks format
77+
-s, --silent silent mode
7678
--tls-cert string path to tls cert pem file
7779
--tls-key string path to tls key pem file
7880
--tls-root-ca string path to tls root ca pem file
@@ -209,9 +211,24 @@ oauth2c https://oauth2c.us.authz.cloudentity.io/oauth2c/demo \
209211
--client-secret HCwQ5uuUWBRHd04ivjX5Kl0Rz8zxMOekeLtqzki0GPc \
210212
--grant-type refresh_token\
211213
--auth-method client_secret_basic \
212-
--refresh-token 1X1IvWR8p5rgKnH2YNmHGd4pZp8Dq-85xzUQuJejT_g.O_DS8Y4eiTS5jZ47_eBv3VbwP4zQUyxjNVW93AyU82k
214+
--refresh-token $REFRESH_TOKEN
213215
```
214216

217+
> **Note** In order to use this command, you must first set the REFRESH_TOKEN environment variable
218+
>
219+
> ``` sh
220+
> export REFRESH_TOKEN=`oauth2c https://oauth2c.us.authz.cloudentity.io/oauth2c/demo \
221+
> --client-id cauktionbud6q8ftlqq0 \
222+
> --client-secret HCwQ5uuUWBRHd04ivjX5Kl0Rz8zxMOekeLtqzki0GPc \
223+
> --response-types code \
224+
> --response-mode query \
225+
> --grant-type authorization_code \
226+
> --auth-method client_secret_basic \
227+
> --scopes openid,email,offline_access \
228+
> --no-pkce \
229+
> --silent | jq -r .refresh_token`
230+
> ```
231+
215232
[Learn more about the refresh token flow](https://cloudentity.com/developers/basics/oauth-grant-types/refresh-token-flow/)
216233
217234
#### Password

cmd/log.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/x509"
88
"encoding/json"
99
"encoding/pem"
10+
"strconv"
1011
"strings"
1112

1213
"github.com/cloudentity/oauth2c/internal/oauth2"
@@ -16,7 +17,110 @@ import (
1617
"github.com/tidwall/pretty"
1718
)
1819

20+
func Logln() {
21+
if silent {
22+
return
23+
}
24+
25+
pterm.Println()
26+
}
27+
28+
func Logfln(msg string, args ...interface{}) {
29+
if silent {
30+
return
31+
}
32+
33+
pterm.Printfln(msg, args...)
34+
}
35+
36+
func LogHeader(msg string) {
37+
if silent {
38+
return
39+
}
40+
41+
pterm.DefaultHeader.WithFullWidth().Println(msg)
42+
}
43+
44+
func LogSection(msg string) {
45+
if silent {
46+
return
47+
}
48+
49+
pterm.DefaultSection.Println(msg)
50+
}
51+
52+
func LogAction(msg string) func(string) {
53+
if silent {
54+
return func(string) {}
55+
}
56+
57+
done, _ := pterm.DefaultSpinner.Start(msg)
58+
return func(s string) {
59+
done.Success(s)
60+
}
61+
}
62+
63+
func LogBox(title string, msg string, args ...interface{}) {
64+
if silent {
65+
return
66+
}
67+
68+
pterm.DefaultBox.WithTitle(title).Printfln(msg, args...)
69+
}
70+
71+
func LogError(err error) {
72+
pterm.Error.PrintOnError(err)
73+
}
74+
75+
func LogWarning(msg string) {
76+
if silent {
77+
return
78+
}
79+
80+
pterm.Warning.Println(msg)
81+
}
82+
83+
func LogInputData(cc oauth2.ClientConfig) {
84+
if silent {
85+
return
86+
}
87+
88+
data := pterm.TableData{
89+
{"Issuer URL", cc.IssuerURL},
90+
{"Grant type", cc.GrantType},
91+
{"Auth method", cc.AuthMethod},
92+
{"Scopes", strings.Join(cc.Scopes, ", ")},
93+
{"Response types", strings.Join(cc.ResponseType, ", ")},
94+
{"Response mode", cc.ResponseMode},
95+
{"PKCE", strconv.FormatBool(cc.PKCE)},
96+
{"Client ID", cc.ClientID},
97+
{"Client secret", cc.ClientSecret},
98+
{"Username", cc.Username},
99+
{"Password", cc.Password},
100+
{"Refresh token", cc.RefreshToken},
101+
}
102+
103+
nonEmptyData := pterm.TableData{}
104+
105+
for _, vs := range data {
106+
if vs[1] != "" {
107+
nonEmptyData = append(nonEmptyData, vs)
108+
}
109+
}
110+
111+
if err := pterm.DefaultTable.WithData(nonEmptyData).WithBoxed().Render(); err != nil {
112+
pterm.Error.Println(err)
113+
return
114+
}
115+
116+
pterm.Println()
117+
}
118+
19119
func LogJson(value interface{}) {
120+
if silent {
121+
return
122+
}
123+
20124
output, err := json.Marshal(value)
21125

22126
if err != nil {
@@ -28,6 +132,10 @@ func LogJson(value interface{}) {
28132
}
29133

30134
func LogRequest(r oauth2.Request) {
135+
if silent {
136+
return
137+
}
138+
31139
if r.URL == nil {
32140
return
33141
}
@@ -71,17 +179,29 @@ func LogRequest(r oauth2.Request) {
71179
}
72180

73181
func LogRequestln(request oauth2.Request) {
182+
if silent {
183+
return
184+
}
185+
74186
LogRequest(request)
75187
pterm.Println()
76188
}
77189

78190
func LogRequestAndResponse(request oauth2.Request, response interface{}) {
191+
if silent {
192+
return
193+
}
194+
79195
LogRequest(request)
80196
pterm.Println(pterm.FgGray.Sprint("Response:"))
81197
LogJson(response)
82198
}
83199

84200
func LogRequestAndResponseln(request oauth2.Request, response interface{}) {
201+
if silent {
202+
return
203+
}
204+
85205
LogRequestAndResponse(request, response)
86206
pterm.Println()
87207
}
@@ -92,6 +212,10 @@ func LogTokenPayload(response oauth2.TokenResponse) {
92212
idClaims jwt.MapClaims
93213
)
94214

215+
if silent {
216+
return
217+
}
218+
95219
if response.AccessToken != "" {
96220
if _, _, err := parser.ParseUnverified(response.AccessToken, &atClaims); err != nil {
97221
pterm.Error.Println(err)
@@ -112,11 +236,19 @@ func LogTokenPayload(response oauth2.TokenResponse) {
112236
}
113237

114238
func LogTokenPayloadln(response oauth2.TokenResponse) {
239+
if silent {
240+
return
241+
}
242+
115243
LogTokenPayload(response)
116244
pterm.Println()
117245
}
118246

119247
func LogAuthMethod(config oauth2.ClientConfig) {
248+
if silent {
249+
return
250+
}
251+
120252
switch config.AuthMethod {
121253
case oauth2.ClientSecretBasicAuthMethod:
122254
pterm.DefaultBox.WithTitle("Client Secret Basic").Printfln("Authorization = Basic BASE64-ENCODE(ClientID:ClientSecret)")
@@ -132,6 +264,10 @@ func LogAssertion(request oauth2.Request, title string, name string) {
132264
err error
133265
)
134266

267+
if silent {
268+
return
269+
}
270+
135271
if assertion == "" {
136272
return
137273
}
@@ -183,3 +319,18 @@ func LogAssertion(request oauth2.Request, title string, name string) {
183319

184320
pterm.Println("")
185321
}
322+
323+
func LogResult(result interface{}) {
324+
if !silent {
325+
return
326+
}
327+
328+
output, err := json.Marshal(result)
329+
330+
if err != nil {
331+
pterm.Error.Println(err)
332+
return
333+
}
334+
335+
pterm.Println(string(output))
336+
}

cmd/oauth2.go

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ import (
88
"fmt"
99
"net/http"
1010
"os"
11-
"strconv"
1211
"strings"
1312
"time"
1413

1514
"github.com/cloudentity/oauth2c/internal/oauth2"
1615
"github.com/golang-jwt/jwt"
1716
"github.com/imdario/mergo"
18-
"github.com/pterm/pterm"
1917
"github.com/spf13/cobra"
2018
)
2119

@@ -25,6 +23,7 @@ const (
2523

2624
var (
2725
parser jwt.Parser
26+
silent bool
2827
)
2928

3029
func OAuth2Cmd() *cobra.Command {
@@ -44,12 +43,12 @@ func OAuth2Cmd() *cobra.Command {
4443

4544
if data, err = os.ReadFile(args[0]); err == nil {
4645
if err = json.Unmarshal(data, &config); err != nil {
47-
pterm.Error.PrintOnError(err)
46+
LogError(err)
4847
os.Exit(1)
4948
}
5049

5150
if err := mergo.Merge(&cconfig, config.ToClientConfig()); err != nil {
52-
pterm.Error.PrintOnError(err)
51+
LogError(err)
5352
os.Exit(1)
5453
}
5554
} else {
@@ -67,7 +66,7 @@ func OAuth2Cmd() *cobra.Command {
6766

6867
if cconfig.TLSCert != "" && cconfig.TLSKey != "" {
6968
if cert, err = oauth2.ReadKeyPair(cconfig.TLSCert, cconfig.TLSKey, hc); err != nil {
70-
pterm.Error.PrintOnError(err)
69+
LogError(err)
7170
os.Exit(1)
7271
}
7372

@@ -76,7 +75,7 @@ func OAuth2Cmd() *cobra.Command {
7675

7776
if cconfig.TLSRootCA != "" {
7877
if tr.TLSClientConfig.RootCAs, err = oauth2.ReadRootCA(cconfig.TLSRootCA, hc); err != nil {
79-
pterm.Error.PrintOnError(err)
78+
LogError(err)
8079
os.Exit(1)
8180
}
8281
}
@@ -87,11 +86,11 @@ func OAuth2Cmd() *cobra.Command {
8786
if errors.As(err, &oauth2Error) {
8887
switch oauth2Error.Hint {
8988
case "Clients must include a code_challenge when performing the authorize code flow, but it is missing.":
90-
pterm.Warning.Println("Authorization server enforces PKCE. Use --pkce flag.")
89+
LogWarning("Authorization server enforces PKCE. Use --pkce flag.")
9190
}
9291
}
9392

94-
pterm.Error.PrintOnError(err)
93+
LogError(err)
9594
os.Exit(1)
9695
}
9796
},
@@ -115,6 +114,7 @@ func OAuth2Cmd() *cobra.Command {
115114
cmd.PersistentFlags().StringVar(&cconfig.TLSKey, "tls-key", "", "path to tls key pem file")
116115
cmd.PersistentFlags().StringVar(&cconfig.TLSRootCA, "tls-root-ca", "", "path to tls root ca pem file")
117116
cmd.PersistentFlags().BoolVar(&cconfig.Insecure, "insecure", false, "allow insecure connections")
117+
cmd.PersistentFlags().BoolVarP(&silent, "silent", "s", false, "silent mode")
118118

119119
return cmd
120120
}
@@ -136,36 +136,11 @@ func Authorize(clientConfig oauth2.ClientConfig, hc *http.Client) error {
136136
return err
137137
}
138138

139-
clientConfig = PromptForClientConfig(clientConfig, serverConfig)
140-
141-
data := pterm.TableData{
142-
{"Issuer URL", clientConfig.IssuerURL},
143-
{"Grant type", clientConfig.GrantType},
144-
{"Auth method", clientConfig.AuthMethod},
145-
{"Scopes", strings.Join(clientConfig.Scopes, ", ")},
146-
{"Response types", strings.Join(clientConfig.ResponseType, ", ")},
147-
{"Response mode", clientConfig.ResponseMode},
148-
{"PKCE", strconv.FormatBool(clientConfig.PKCE)},
149-
{"Client ID", clientConfig.ClientID},
150-
{"Client secret", clientConfig.ClientSecret},
151-
{"Username", clientConfig.Username},
152-
{"Password", clientConfig.Password},
153-
{"Refresh token", clientConfig.RefreshToken},
139+
if !silent {
140+
clientConfig = PromptForClientConfig(clientConfig, serverConfig)
154141
}
155142

156-
nonEmptyData := pterm.TableData{}
157-
158-
for _, vs := range data {
159-
if vs[1] != "" {
160-
nonEmptyData = append(nonEmptyData, vs)
161-
}
162-
}
163-
164-
if err := pterm.DefaultTable.WithData(nonEmptyData).WithBoxed().Render(); err != nil {
165-
return err
166-
}
167-
168-
pterm.Println()
143+
LogInputData(clientConfig)
169144

170145
switch clientConfig.GrantType {
171146
case oauth2.AuthorizationCodeGrantType:

0 commit comments

Comments
 (0)