Skip to content

Commit b358aeb

Browse files
authored
adds auth get command (#253)
1 parent 4e3114a commit b358aeb

3 files changed

Lines changed: 161 additions & 0 deletions

File tree

pkg/cmd/auth/auth.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/algolia/cli/pkg/auth"
77
"github.com/algolia/cli/pkg/cmd/auth/crawler"
8+
"github.com/algolia/cli/pkg/cmd/auth/get"
89
"github.com/algolia/cli/pkg/cmd/auth/login"
910
"github.com/algolia/cli/pkg/cmd/auth/logout"
1011
"github.com/algolia/cli/pkg/cmd/auth/signup"
@@ -22,6 +23,7 @@ func NewAuthCmd(f *cmdutil.Factory) *cobra.Command {
2223

2324
cmd.AddCommand(login.NewLoginCmd(f))
2425
cmd.AddCommand(logout.NewLogoutCmd(f))
26+
cmd.AddCommand(get.NewGetCmd(f))
2527
cmd.AddCommand(signup.NewSignupCmd(f))
2628
cmd.AddCommand(crawler.NewCrawlerCmd(f))
2729

pkg/cmd/auth/get/get.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package get
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
"github.com/spf13/cobra"
8+
9+
"github.com/algolia/cli/pkg/auth"
10+
"github.com/algolia/cli/pkg/cmdutil"
11+
"github.com/algolia/cli/pkg/iostreams"
12+
"github.com/algolia/cli/pkg/validators"
13+
)
14+
15+
// GetOptions represents the options for the get command.
16+
type GetOptions struct {
17+
IO *iostreams.IOStreams
18+
19+
LoadToken func() *auth.StoredToken
20+
21+
PrintFlags *cmdutil.PrintFlags
22+
}
23+
24+
// Identity is the authenticated user, without any token information.
25+
type Identity struct {
26+
UserID string `json:"user_id,omitempty"`
27+
Email string `json:"email,omitempty"`
28+
Name string `json:"name,omitempty"`
29+
}
30+
31+
// NewGetCmd returns a new instance of the get command.
32+
func NewGetCmd(f *cmdutil.Factory) *cobra.Command {
33+
opts := &GetOptions{
34+
IO: f.IOStreams,
35+
LoadToken: auth.LoadToken,
36+
PrintFlags: cmdutil.NewPrintFlags().WithDefaultOutput("json"),
37+
}
38+
39+
cmd := &cobra.Command{
40+
Use: "get",
41+
Short: "Get the authenticated user",
42+
Long: heredoc.Doc(`
43+
Get the identity of the authenticated user from the OAuth
44+
credentials stored in the local keychain.
45+
`),
46+
Example: heredoc.Doc(`
47+
# Get the authenticated user
48+
$ algolia auth get
49+
`),
50+
Args: validators.NoArgs(),
51+
RunE: func(cmd *cobra.Command, args []string) error {
52+
return runGetCmd(opts)
53+
},
54+
}
55+
56+
opts.PrintFlags.AddFlags(cmd)
57+
58+
return cmd
59+
}
60+
61+
// runGetCmd runs the get command.
62+
func runGetCmd(opts *GetOptions) error {
63+
stored := opts.LoadToken()
64+
if stored == nil {
65+
return fmt.Errorf("you are not logged in — run `algolia auth login` first")
66+
}
67+
68+
if stored.IsExpired() {
69+
return fmt.Errorf("your session has expired — run `algolia auth login` again")
70+
}
71+
72+
identity := Identity{
73+
UserID: stored.UserID,
74+
Email: stored.Email,
75+
Name: stored.Name,
76+
}
77+
78+
p, err := opts.PrintFlags.ToPrinter()
79+
if err != nil {
80+
return err
81+
}
82+
83+
return p.Print(opts.IO, identity)
84+
}

pkg/cmd/auth/get/get_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package get
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
"github.com/zalando/go-keyring"
10+
11+
"github.com/algolia/cli/api/dashboard"
12+
"github.com/algolia/cli/pkg/auth"
13+
"github.com/algolia/cli/test"
14+
)
15+
16+
func TestGet_NotLoggedIn(t *testing.T) {
17+
keyring.MockInit()
18+
auth.ClearToken()
19+
20+
f, out := test.NewFactory(false, nil, nil, "")
21+
cmd := NewGetCmd(f)
22+
_, err := test.Execute(cmd, "", out)
23+
require.Error(t, err)
24+
assert.Equal(t, "you are not logged in — run `algolia auth login` first", err.Error())
25+
}
26+
27+
func TestGet_Expired(t *testing.T) {
28+
keyring.MockInit()
29+
t.Cleanup(auth.ClearToken)
30+
require.NoError(t, auth.SaveToken(&dashboard.OAuthTokenResponse{
31+
AccessToken: "secret-access",
32+
CreatedAt: time.Now().Unix() - 7200,
33+
ExpiresIn: 3600,
34+
User: &dashboard.User{
35+
ID: 42,
36+
Email: "user@example.com",
37+
Name: "Test User",
38+
},
39+
}))
40+
41+
f, out := test.NewFactory(false, nil, nil, "")
42+
cmd := NewGetCmd(f)
43+
_, err := test.Execute(cmd, "", out)
44+
require.Error(t, err)
45+
assert.Equal(t, "your session has expired — run `algolia auth login` again", err.Error())
46+
}
47+
48+
func TestGet_PrintsIdentityWithoutTokens(t *testing.T) {
49+
keyring.MockInit()
50+
t.Cleanup(auth.ClearToken)
51+
require.NoError(t, auth.SaveToken(&dashboard.OAuthTokenResponse{
52+
AccessToken: "secret-access",
53+
RefreshToken: "secret-refresh",
54+
CreatedAt: time.Now().Unix(),
55+
ExpiresIn: 3600,
56+
User: &dashboard.User{
57+
ID: 42,
58+
Email: "user@example.com",
59+
Name: "Test User",
60+
},
61+
}))
62+
63+
f, out := test.NewFactory(false, nil, nil, "")
64+
cmd := NewGetCmd(f)
65+
out, err := test.Execute(cmd, "--output ndjson", out)
66+
require.NoError(t, err)
67+
68+
assert.Contains(t, out.String(), `"user_id":"42"`)
69+
assert.Contains(t, out.String(), `"email":"user@example.com"`)
70+
assert.Contains(t, out.String(), `"name":"Test User"`)
71+
assert.NotContains(t, out.String(), "secret-access")
72+
assert.NotContains(t, out.String(), "secret-refresh")
73+
assert.NotContains(t, out.String(), "access_token")
74+
assert.NotContains(t, out.String(), "refresh_token")
75+
}

0 commit comments

Comments
 (0)