Skip to content

Commit

Permalink
Rename Terminal{,KeyStore}Authenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
dimroc committed Aug 20, 2018
1 parent 27f43ce commit 02a1f00
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
14 changes: 7 additions & 7 deletions cmd/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ type KeyStoreAuthenticator interface {
Authenticate(*store.Store, string) error
}

// TerminalAuthenticator contains fields for prompting the user and an
// TerminalKeyStoreAuthenticator contains fields for prompting the user and an
// exit code.
type TerminalAuthenticator struct {
type TerminalKeyStoreAuthenticator struct {
Prompter Prompter
}

// Authenticate checks to see if there are accounts present in
// the KeyStore, and if there are none, a new account will be created
// by prompting for a password. If there are accounts present, the
// account which is unlocked by the given password will be used.
func (auth TerminalAuthenticator) Authenticate(store *store.Store, pwd string) error {
func (auth TerminalKeyStoreAuthenticator) Authenticate(store *store.Store, pwd string) error {
if len(pwd) != 0 {
return auth.authenticateWithPwd(store, pwd)
} else if auth.Prompter.IsTerminal() {
Expand All @@ -33,14 +33,14 @@ func (auth TerminalAuthenticator) Authenticate(store *store.Store, pwd string) e
}
}

func (auth TerminalAuthenticator) authenticationPrompt(store *store.Store) error {
func (auth TerminalKeyStoreAuthenticator) authenticationPrompt(store *store.Store) error {
if store.KeyStore.HasAccounts() {
return auth.promptAndCheckPasswordLoop(store)
}
return auth.promptAndCreateAccount(store)
}

func (auth TerminalAuthenticator) authenticateWithPwd(store *store.Store, pwd string) error {
func (auth TerminalKeyStoreAuthenticator) authenticateWithPwd(store *store.Store, pwd string) error {
if !store.KeyStore.HasAccounts() {
fmt.Println("There are no accounts, creating a new account with the specified password")
return createAccount(store, pwd)
Expand All @@ -56,7 +56,7 @@ func checkPassword(store *store.Store, phrase string) error {
return nil
}

func (auth TerminalAuthenticator) promptAndCheckPasswordLoop(store *store.Store) error {
func (auth TerminalKeyStoreAuthenticator) promptAndCheckPasswordLoop(store *store.Store) error {
for {
phrase := auth.Prompter.PasswordPrompt("Enter Password:")
if checkPassword(store, phrase) == nil {
Expand All @@ -67,7 +67,7 @@ func (auth TerminalAuthenticator) promptAndCheckPasswordLoop(store *store.Store)
return nil
}

func (auth TerminalAuthenticator) promptAndCreateAccount(store *store.Store) error {
func (auth TerminalKeyStoreAuthenticator) promptAndCreateAccount(store *store.Store) error {
for {
phrase := auth.Prompter.PasswordPrompt("New Password: ")
clearLine()
Expand Down
16 changes: 8 additions & 8 deletions cmd/authenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestTerminalAuthenticatorWithNoAcctNoPwdCreatesAccount(t *testing.T) {
func TestTerminalKeyStoreAuthenticator_WithNoAcctNoPwdCreatesAccount(t *testing.T) {
t.Parallel()

app, cleanup := cltest.NewApplication()
Expand All @@ -18,28 +18,28 @@ func TestTerminalAuthenticatorWithNoAcctNoPwdCreatesAccount(t *testing.T) {
cltest.Password, "wrongconfirmation", cltest.Password, cltest.Password,
}}

auth := cmd.TerminalAuthenticator{Prompter: prompt}
auth := cmd.TerminalKeyStoreAuthenticator{Prompter: prompt}
assert.False(t, app.Store.KeyStore.HasAccounts())
assert.NoError(t, auth.Authenticate(app.Store, ""))
assert.Equal(t, 4, prompt.Count)
assert.Equal(t, 1, len(app.Store.KeyStore.Accounts()))
}

func TestTerminalAuthenticatorWithNoAcctWithInitialPwdCreatesAcct(t *testing.T) {
func TestTerminalKeyStoreAuthenticator_WithNoAcctWithInitialPwdCreatesAcct(t *testing.T) {
t.Parallel()

app, cleanup := cltest.NewApplication()
defer cleanup()

auth := cmd.TerminalAuthenticator{Prompter: &cltest.MockCountingPrompter{}}
auth := cmd.TerminalKeyStoreAuthenticator{Prompter: &cltest.MockCountingPrompter{}}

assert.Equal(t, 0, len(app.Store.KeyStore.Accounts()))
assert.NoError(t, auth.Authenticate(app.Store, "somepassword"))
assert.True(t, app.Store.KeyStore.HasAccounts())
assert.Equal(t, 1, len(app.Store.KeyStore.Accounts()))
}

func TestTerminalAuthenticatorWithAcctNoInitialPwdPromptLoop(t *testing.T) {
func TestTerminalKeyStoreAuthenticator_WithAcctNoInitialPwdPromptLoop(t *testing.T) {
t.Parallel()

app, cleanup := cltest.NewApplicationWithKeyStore()
Expand All @@ -50,12 +50,12 @@ func TestTerminalAuthenticatorWithAcctNoInitialPwdPromptLoop(t *testing.T) {
EnteredStrings: []string{"wrongpassword", cltest.Password},
}

auth := cmd.TerminalAuthenticator{Prompter: prompt}
auth := cmd.TerminalKeyStoreAuthenticator{Prompter: prompt}
assert.NoError(t, auth.Authenticate(app.Store, ""))
assert.Equal(t, 2, prompt.Count)
}

func TestTerminalAuthenticatorWithAcctAndPwd(t *testing.T) {
func TestTerminalKeyStoreAuthenticator_WithAcctAndPwd(t *testing.T) {
t.Parallel()

app, cleanup := cltest.NewApplicationWithKeyStore()
Expand All @@ -71,7 +71,7 @@ func TestTerminalAuthenticatorWithAcctAndPwd(t *testing.T) {

for _, test := range tests {
t.Run(test.password, func(t *testing.T) {
auth := cmd.TerminalAuthenticator{Prompter: &cltest.MockCountingPrompter{}}
auth := cmd.TerminalKeyStoreAuthenticator{Prompter: &cltest.MockCountingPrompter{}}
err := auth.Authenticate(app.Store, test.password)
assert.Equal(t, test.wantError, err != nil)
})
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func NewProductionClient() *cmd.Client {
Renderer: cmd.RendererTable{Writer: os.Stdout},
Config: cfg,
AppFactory: cmd.ChainlinkAppFactory{},
KeyStoreAuthenticator: cmd.TerminalAuthenticator{Prompter: prompter},
KeyStoreAuthenticator: cmd.TerminalKeyStoreAuthenticator{Prompter: prompter},
FallbackAPIInitializer: cmd.NewPromptingAPIInitializer(prompter),
Runner: cmd.ChainlinkRunner{},
HTTP: cmd.NewAuthenticatedHTTPClient(cfg, cookieAuth),
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func ExampleRun() {
Renderer: cmd.RendererTable{Writer: ioutil.Discard},
Config: tc.Config,
AppFactory: cmd.ChainlinkAppFactory{},
KeyStoreAuthenticator: cmd.TerminalAuthenticator{Prompter: &cltest.MockCountingPrompter{}},
KeyStoreAuthenticator: cmd.TerminalKeyStoreAuthenticator{Prompter: &cltest.MockCountingPrompter{}},
FallbackAPIInitializer: &cltest.MockAPIInitializer{},
Runner: cmd.ChainlinkRunner{},
HTTP: cltest.NewMockAuthenticatedHTTPClient(tc.Config),
Expand Down Expand Up @@ -64,7 +64,7 @@ func ExampleVersion() {
Renderer: cmd.RendererTable{Writer: ioutil.Discard},
Config: tc.Config,
AppFactory: cmd.ChainlinkAppFactory{},
KeyStoreAuthenticator: cmd.TerminalAuthenticator{Prompter: &cltest.MockCountingPrompter{}},
KeyStoreAuthenticator: cmd.TerminalKeyStoreAuthenticator{Prompter: &cltest.MockCountingPrompter{}},
FallbackAPIInitializer: &cltest.MockAPIInitializer{},
Runner: cmd.ChainlinkRunner{},
HTTP: cltest.NewMockAuthenticatedHTTPClient(tc.Config),
Expand Down

0 comments on commit 02a1f00

Please sign in to comment.