Skip to content
Open
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
64 changes: 37 additions & 27 deletions openshell/v1/oidc/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"strings"
"sync"
)

type readResult struct {
Expand Down Expand Up @@ -46,52 +47,61 @@ func keyboardFlow(ctx context.Context, authURL string, input io.Reader, output i
return result.code, nil
}

type inputRequest struct {
input io.Reader
result chan readResult
// inputDispatcher keeps one scanner per reader. A timed-out login stops
// waiting, and the line that scanner later reads stays queued for the next
// login instead of being delivered to the abandoned request.
type inputDispatcher struct {
mu sync.Mutex
sources map[io.Reader]*lineSource
}

type inputDispatcher struct {
requests chan inputRequest
type lineSource struct {
lines chan readResult
}

var keyboardInput = newInputDispatcher()

func newInputDispatcher() *inputDispatcher {
d := &inputDispatcher{requests: make(chan inputRequest)}
go d.run()
return d
return &inputDispatcher{sources: make(map[io.Reader]*lineSource)}
}

func (d *inputDispatcher) read(ctx context.Context, input io.Reader) (readResult, error) {
if err := ctx.Err(); err != nil {
return readResult{}, fmt.Errorf("%w: %v", ErrTimeout, err)
}
request := inputRequest{input: input, result: make(chan readResult, 1)}
src := d.source(input)
select {
case d.requests <- request:
case <-ctx.Done():
return readResult{}, fmt.Errorf("%w: %v", ErrTimeout, ctx.Err())
}
select {
case result := <-request.result:
case result, ok := <-src.lines:
if !ok {
return readResult{err: fmt.Errorf("%w: no authorization code received (EOF)", ErrAuthCode)}, nil
}
return result, nil
case <-ctx.Done():
return readResult{}, fmt.Errorf("%w: %v", ErrTimeout, ctx.Err())
}
}

func (d *inputDispatcher) run() {
for request := range d.requests {
scanner := bufio.NewScanner(request.input)
if scanner.Scan() {
request.result <- readResult{code: strings.TrimSpace(scanner.Text())}
continue
}
if err := scanner.Err(); err != nil {
request.result <- readResult{err: fmt.Errorf("%w: failed to read authorization code: %v", ErrAuthCode, err)}
} else {
request.result <- readResult{err: fmt.Errorf("%w: no authorization code received (EOF)", ErrAuthCode)}
}
func (d *inputDispatcher) source(input io.Reader) *lineSource {
d.mu.Lock()
defer d.mu.Unlock()
if src, ok := d.sources[input]; ok {
return src
}
src := &lineSource{lines: make(chan readResult, 1)}
d.sources[input] = src
go src.run(input)
return src
}

func (s *lineSource) run(input io.Reader) {
defer close(s.lines)
scanner := bufio.NewScanner(input)
for scanner.Scan() {
s.lines <- readResult{code: strings.TrimSpace(scanner.Text())}
}
if err := scanner.Err(); err != nil {
s.lines <- readResult{err: fmt.Errorf("%w: failed to read authorization code: %v", ErrAuthCode, err)}
return
}
s.lines <- readResult{err: fmt.Errorf("%w: no authorization code received (EOF)", ErrAuthCode)}
}
23 changes: 23 additions & 0 deletions openshell/v1/oidc/keyboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ func TestKeyboardFlow_DisplaysInstructions(t *testing.T) {
)
}

func TestKeyboardFlow_TimeoutDoesNotStealNextCode(t *testing.T) {
pr, pw := io.Pipe()
t.Cleanup(func() {
_ = pw.Close()
_ = pr.Close()
})

ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
_, err := keyboardFlow(ctx, "https://example.com/auth", pr, io.Discard)
require.Error(t, err)
assert.ErrorIs(t, err, ErrTimeout)

_, err = pw.Write([]byte("pasted-code\n"))
require.NoError(t, err)

ctx2, cancel2 := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel2()
code, err := keyboardFlow(ctx2, "https://example.com/auth", pr, io.Discard)
require.NoError(t, err)
assert.Equal(t, "pasted-code", code)
}

func TestKeyboardFlow_Timeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
Expand Down