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
48 changes: 11 additions & 37 deletions internal/cli/auth/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ type ConfigDeleter interface {
Delete() error
SetAccessToken(string)
SetRefreshToken(string)
SetPublicAPIKey(string)
SetPrivateAPIKey(string)
SetProjectID(string)
SetOrgID(string)
AuthType() config.AuthMechanism
Save() error
}

Expand All @@ -65,30 +62,16 @@ func (opts *logoutOpts) initFlow() error {
}

func (opts *logoutOpts) Run(ctx context.Context) error {
authType := opts.config.AuthType()

// Handle OAuth authentication - revoke tokens
if authType == config.OAuth {
if _, err := opts.flow.RevokeToken(ctx, config.RefreshToken(), "refresh_token"); err != nil {
return err
}
// revoking a refresh token revokes the access token
if _, err := opts.flow.RevokeToken(ctx, config.RefreshToken(), "refresh_token"); err != nil {
return err
}

if !opts.keepConfig {
return opts.Delete(opts.config.Delete)
}

// Clear credentials based on auth type
switch authType {
case config.OAuth:
opts.config.SetAccessToken("")
opts.config.SetRefreshToken("")
case config.APIKeys:
opts.config.SetPublicAPIKey("")
opts.config.SetPrivateAPIKey("")
case config.NotLoggedIn:
}

opts.config.SetAccessToken("")
opts.config.SetRefreshToken("")
opts.config.SetProjectID("")
opts.config.SetOrgID("")
return opts.config.Save()
Expand All @@ -111,23 +94,14 @@ func LogoutBuilder() *cobra.Command {
return opts.initFlow()
},
RunE: func(cmd *cobra.Command, _ []string) error {
authType := config.AuthType()
var accountIdentifier string
var err error

switch authType {
case config.OAuth:
accountIdentifier, err = config.AccessTokenSubject()
if err != nil {
return err
}
case config.APIKeys:
accountIdentifier = config.PublicAPIKey()
case config.NotLoggedIn:
if config.RefreshToken() == "" {
return ErrUnauthenticated
}

opts.Entry = accountIdentifier
s, err := config.AccessTokenSubject()
if err != nil {
return err
}
opts.Entry = s
if err := opts.PromptWithMessage("Are you sure you want to log out of account %s?"); err != nil || !opts.Confirm {
return err
}
Expand Down
111 changes: 0 additions & 111 deletions internal/cli/auth/logout_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 2 additions & 96 deletions internal/cli/auth/logout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import (
"testing"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func Test_logoutOpts_Run_OAuth(t *testing.T) {
func Test_logoutOpts_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockFlow := NewMockRevoker(ctrl)
mockConfig := NewMockConfigDeleter(ctrl)
Expand All @@ -40,12 +39,6 @@ func Test_logoutOpts_Run_OAuth(t *testing.T) {
},
}
ctx := t.Context()

mockConfig.
EXPECT().
AuthType().
Return(config.OAuth).
Times(1)
mockFlow.
EXPECT().
RevokeToken(ctx, gomock.Any(), gomock.Any()).
Expand All @@ -59,38 +52,7 @@ func Test_logoutOpts_Run_OAuth(t *testing.T) {
require.NoError(t, opts.Run(ctx))
}

func Test_logoutOpts_Run_APIKeys(t *testing.T) {
ctrl := gomock.NewController(t)
mockFlow := NewMockRevoker(ctrl)
mockConfig := NewMockConfigDeleter(ctrl)

buf := new(bytes.Buffer)

opts := logoutOpts{
OutWriter: buf,
config: mockConfig,
flow: mockFlow,
DeleteOpts: &cli.DeleteOpts{
Confirm: true,
},
}
ctx := t.Context()

mockConfig.
EXPECT().
AuthType().
Return(config.APIKeys).
Times(1)
// No token revocation expected for API keys
mockConfig.
EXPECT().
Delete().
Return(nil).
Times(1)
require.NoError(t, opts.Run(ctx))
}

func Test_logoutOpts_Run_Keep_OAuth(t *testing.T) {
func Test_logoutOpts_Run_Keep(t *testing.T) {
ctrl := gomock.NewController(t)
mockFlow := NewMockRevoker(ctrl)
mockConfig := NewMockConfigDeleter(ctrl)
Expand All @@ -107,12 +69,6 @@ func Test_logoutOpts_Run_Keep_OAuth(t *testing.T) {
keepConfig: true,
}
ctx := t.Context()

mockConfig.
EXPECT().
AuthType().
Return(config.OAuth).
Times(1)
mockFlow.
EXPECT().
RevokeToken(ctx, gomock.Any(), gomock.Any()).
Expand Down Expand Up @@ -143,53 +99,3 @@ func Test_logoutOpts_Run_Keep_OAuth(t *testing.T) {

require.NoError(t, opts.Run(ctx))
}

func Test_logoutOpts_Run_Keep_APIKeys(t *testing.T) {
ctrl := gomock.NewController(t)
mockFlow := NewMockRevoker(ctrl)
mockConfig := NewMockConfigDeleter(ctrl)

buf := new(bytes.Buffer)

opts := logoutOpts{
OutWriter: buf,
config: mockConfig,
flow: mockFlow,
DeleteOpts: &cli.DeleteOpts{
Confirm: true,
},
keepConfig: true,
}
ctx := t.Context()

mockConfig.
EXPECT().
AuthType().
Return(config.APIKeys).
Times(1)
// No token revocation for API keys

mockConfig.
EXPECT().
SetPublicAPIKey("").
Times(1)
mockConfig.
EXPECT().
SetPrivateAPIKey("").
Times(1)
mockConfig.
EXPECT().
SetProjectID("").
Times(1)
mockConfig.
EXPECT().
SetOrgID("").
Times(1)
mockConfig.
EXPECT().
Save().
Return(nil).
Times(1)

require.NoError(t, opts.Run(ctx))
}
Loading