Skip to content

Commit

Permalink
1128 - introduce Prompter.Display method
Browse files Browse the repository at this point in the history
  • Loading branch information
eliac committed Nov 15, 2023
1 parent d381c43 commit bc4d7ee
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 11 deletions.
26 changes: 24 additions & 2 deletions mocks/Prompter.go

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

5 changes: 4 additions & 1 deletion pkg/prompter/pinentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func (p *PinentryPrompter) String(pr string, defaultValue string) string {
func (p *PinentryPrompter) Password(pr string) string {
return p.DefaultPrompter.Password(pr)
}

Check failure on line 83 in pkg/prompter/pinentry.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)

// Display is runniner the default Cli Display
func (p *PinentryPrompter) Display(pr string) {
p.DefaultPrompter.Display(pr)
}
// Run wraps a pinentry run. It sends the query to pinentry via stdin and
// reads its stdout to determine the user PIN.
// Pinentry uses an Assuan protocol
Expand Down
8 changes: 7 additions & 1 deletion pkg/prompter/pinentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type FakeDefaultPrompter struct {
CalledString bool
CalledStringRequired bool
CalledPassword bool
CalledDisplay bool
}

// all the functions to implement the Prompter interface
Expand Down Expand Up @@ -63,7 +64,9 @@ func (f *FakeDefaultPrompter) Password(p string) string {
f.CalledPassword = true
return ""
}

func (f *FakeDefaultPrompter) Display(p string) {
f.CalledDisplay = true
}
func TestValidateAndSetPrompterShouldFailWithWrongInput(t *testing.T) {

// backing up the current prompters for the other tests
Expand Down Expand Up @@ -125,6 +128,9 @@ func TestChecksPinentryPrompterDefault(t *testing.T) {

_ = p.Password("random")
assert.True(t, fakeDefaultPrompter.CalledPassword)

p.Display("random")
assert.True(t, fakeDefaultPrompter.CalledDisplay)
}

func TestChecksPinentryPrompterCallsPinentryForRequestSecurityCode(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/prompter/prompter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Prompter interface {
StringRequired(string) string
String(string, string) string
Password(string) string
Display(string)
}

// SetPrompter configure an aternate prompter to the default one
Expand Down Expand Up @@ -79,3 +80,8 @@ func String(pr string, defaultValue string) string {
func Password(pr string) string {
return ActivePrompter.Password(pr)
}

// Display prompt, no user input required
func Display(pr string) {
ActivePrompter.Display(pr)
}
4 changes: 4 additions & 0 deletions pkg/prompter/survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ func (cli *CliPrompter) Password(pr string) string {
_ = survey.AskOne(prompt, &val, stdioOption())
return val
}

func (cli *CliPrompter) Display(pr string) {
_, _ = os.Stderr.WriteString(pr + "\n")
}
9 changes: 2 additions & 7 deletions pkg/provider/aad/aad.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"

Expand Down Expand Up @@ -464,12 +462,9 @@ func (ac *Client) processMfa(mfas []userProof, convergedResponse *ConvergedRespo
}
if mfaReq.AuthMethodID == "PhoneAppNotification" && i == 0 {
if mfaResp.Entropy == 0 {
log.Println("Phone approval required.")
prompter.Display("Phone approval required.")
} else {
prevWriter := log.Writer()
log.SetOutput(os.Stderr)
log.Printf("Phone approval required. Entropy is: %d", mfaResp.Entropy)
log.SetOutput(prevWriter)
prompter.Display(fmt.Sprintf("Phone approval required. Entropy is: %d", mfaResp.Entropy))
}
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/provider/aad/aad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/stretchr/testify/mock"

Check failure on line 11 in pkg/provider/aad/aad_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"io"
"math"
mrand "math/rand"
Expand Down Expand Up @@ -412,6 +413,9 @@ func Test_Authenticate(t *testing.T) {
}))
defer ts.Close()

pr := prompter.NewCli()
prompter.SetPrompter(pr)

ac, loginDetails := setupTestClient(t, ts)

orig := os.Stderr
Expand Down Expand Up @@ -481,6 +485,7 @@ func Test_Authenticate(t *testing.T) {
pr := &mocks.Prompter{}
prompter.SetPrompter(pr)
pr.Mock.On("StringRequired", "Enter verification code").Return("000000")
pr.Mock.On("Display", mock.Anything).Return()

ac, loginDetails := setupTestClient(t, ts)
got, err := ac.Authenticate(loginDetails)
Expand Down

0 comments on commit bc4d7ee

Please sign in to comment.