Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cmd/src/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Usage:

The commands are:

token prints the current authentication token
token prints the current authentication token or Authorization header

Use "src auth [command] -h" for more information about a command.
`
Expand Down
18 changes: 17 additions & 1 deletion cmd/src/auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ type oauthTokenRefresher interface {

func init() {
flagSet := flag.NewFlagSet("token", flag.ExitOnError)
header := flagSet.Bool("header", false, "print the token as an Authorization header")
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src auth token':\n")
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src auth token':\n\n")
fmt.Fprintf(flag.CommandLine.Output(), "Print the current authentication token.\n")
fmt.Fprintf(flag.CommandLine.Output(), "Use --header to print a complete Authorization header instead.\n\n")
flagSet.PrintDefaults()
}

Expand All @@ -38,6 +41,7 @@ func init() {
return err
}

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

return token.AccessToken, nil
}

func formatAuthTokenOutput(token string, mode AuthMode, header bool) string {
if !header {
return token
}

if mode == AuthModeAccessToken {
return fmt.Sprintf("Authorization: token %s", token)
}

return fmt.Sprintf("Authorization: Bearer %s", token)
}
47 changes: 47 additions & 0 deletions cmd/src/auth_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,53 @@ func TestResolveAuthToken(t *testing.T) {
})
}

func TestFormatAuthTokenOutput(t *testing.T) {
tests := []struct {
name string
token string
mode AuthMode
header bool
want string
}{
{
name: "raw access token",
token: "access-token",
mode: AuthModeAccessToken,
header: false,
want: "access-token",
},
{
name: "raw oauth token",
token: "oauth-token",
mode: AuthModeOAuth,
header: false,
want: "oauth-token",
},
{
name: "authorization header for access token",
token: "access-token",
mode: AuthModeAccessToken,
header: true,
want: "Authorization: token access-token",
},
{
name: "authorization header for oauth token",
token: "oauth-token",
mode: AuthModeOAuth,
header: true,
want: "Authorization: Bearer oauth-token",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := formatAuthTokenOutput(test.token, test.mode, test.header); got != test.want {
t.Fatalf("formatAuthTokenOutput(%q, %v, %v) = %q, want %q", test.token, test.mode, test.header, got, test.want)
}
})
}
}

func stubAuthTokenDependencies(t *testing.T) func() {
t.Helper()

Expand Down
Loading