Skip to content

Commit 21fa132

Browse files
authored
add --header flag to src auth token to print auth header (#1276)
* add --header flag to src auth token to print auth header - for access token print `Authorization: token <token>` - for oauth token print `Authorization: Bearer <token>` * update help
1 parent d206288 commit 21fa132

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

cmd/src/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Usage:
1616
1717
The commands are:
1818
19-
token prints the current authentication token
19+
token prints the current authentication token or Authorization header
2020
2121
Use "src auth [command] -h" for more information about a command.
2222
`

cmd/src/auth_token.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ type oauthTokenRefresher interface {
2323

2424
func init() {
2525
flagSet := flag.NewFlagSet("token", flag.ExitOnError)
26+
header := flagSet.Bool("header", false, "print the token as an Authorization header")
2627
usageFunc := func() {
27-
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src auth token':\n")
28+
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src auth token':\n\n")
29+
fmt.Fprintf(flag.CommandLine.Output(), "Print the current authentication token.\n")
30+
fmt.Fprintf(flag.CommandLine.Output(), "Use --header to print a complete Authorization header instead.\n\n")
2831
flagSet.PrintDefaults()
2932
}
3033

@@ -38,6 +41,7 @@ func init() {
3841
return err
3942
}
4043

44+
token = formatAuthTokenOutput(token, cfg.AuthMode(), *header)
4145
fmt.Println(token)
4246
return nil
4347
}
@@ -66,3 +70,15 @@ func resolveAuthToken(ctx context.Context, cfg *config) (string, error) {
6670

6771
return token.AccessToken, nil
6872
}
73+
74+
func formatAuthTokenOutput(token string, mode AuthMode, header bool) string {
75+
if !header {
76+
return token
77+
}
78+
79+
if mode == AuthModeAccessToken {
80+
return fmt.Sprintf("Authorization: token %s", token)
81+
}
82+
83+
return fmt.Sprintf("Authorization: Bearer %s", token)
84+
}

cmd/src/auth_token_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,53 @@ func TestResolveAuthToken(t *testing.T) {
103103
})
104104
}
105105

106+
func TestFormatAuthTokenOutput(t *testing.T) {
107+
tests := []struct {
108+
name string
109+
token string
110+
mode AuthMode
111+
header bool
112+
want string
113+
}{
114+
{
115+
name: "raw access token",
116+
token: "access-token",
117+
mode: AuthModeAccessToken,
118+
header: false,
119+
want: "access-token",
120+
},
121+
{
122+
name: "raw oauth token",
123+
token: "oauth-token",
124+
mode: AuthModeOAuth,
125+
header: false,
126+
want: "oauth-token",
127+
},
128+
{
129+
name: "authorization header for access token",
130+
token: "access-token",
131+
mode: AuthModeAccessToken,
132+
header: true,
133+
want: "Authorization: token access-token",
134+
},
135+
{
136+
name: "authorization header for oauth token",
137+
token: "oauth-token",
138+
mode: AuthModeOAuth,
139+
header: true,
140+
want: "Authorization: Bearer oauth-token",
141+
},
142+
}
143+
144+
for _, test := range tests {
145+
t.Run(test.name, func(t *testing.T) {
146+
if got := formatAuthTokenOutput(test.token, test.mode, test.header); got != test.want {
147+
t.Fatalf("formatAuthTokenOutput(%q, %v, %v) = %q, want %q", test.token, test.mode, test.header, got, test.want)
148+
}
149+
})
150+
}
151+
}
152+
106153
func stubAuthTokenDependencies(t *testing.T) func() {
107154
t.Helper()
108155

0 commit comments

Comments
 (0)