Skip to content

Commit

Permalink
feat(GODT-2201): IMAP Login Command
Browse files Browse the repository at this point in the history
  • Loading branch information
LBeernaertProton committed Feb 14, 2023
1 parent 3c0f769 commit 2238d80
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
49 changes: 49 additions & 0 deletions imap/command/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package command

import (
"fmt"
"github.com/ProtonMail/gluon/imap/parser"
)

type LoginCommand struct {
UserID string
Password string
}

func (l LoginCommand) String() string {
return fmt.Sprintf("LOGIN '%v' '%v'", l.UserID, l.Password)
}

func (l LoginCommand) SanitizedString() string {
return fmt.Sprintf("LOGIN '%v' <PASSWORD>", sanitizeString(l.UserID))
}

type LoginCommandParser struct{}

func (LoginCommandParser) FromParser(p *parser.Parser) (Payload, error) {
// login = "LOGIN" SP userid SP password
// userid = astring
// password = astring
if err := p.Consume(parser.TokenTypeSP, "expected space after command"); err != nil {
return nil, err
}

user, err := p.ParseAString()
if err != nil {
return nil, err
}

if err := p.Consume(parser.TokenTypeSP, "expected space after userid"); err != nil {
return nil, err
}

password, err := p.ParseAString()
if err != nil {
return nil, err
}

return &LoginCommand{
UserID: user,
Password: password,
}, nil
}
25 changes: 25 additions & 0 deletions imap/command/login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package command

import (
"bytes"
"github.com/ProtonMail/gluon/imap/parser"
"github.com/stretchr/testify/require"
"testing"
)

func TestParser_LoginCommandQuoted(t *testing.T) {
input := toIMAPLine(`tag LOGIN "foo" "bar"`)
s := parser.NewScanner(bytes.NewReader(input))
p := NewParser(s)

expected := Command{Tag: "tag", Payload: &LoginCommand{
UserID: "foo",
Password: "bar",
}}

cmd, err := p.Parse()
require.NoError(t, err)
require.Equal(t, expected, cmd)
require.Equal(t, "login", p.LastParsedCommand())
require.Equal(t, "tag", p.LastParsedTag())
}
1 change: 1 addition & 0 deletions imap/command/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func NewParserWithLiteralContinuationCb(s *parser.Scanner, cb func() error) *Par
"unsubscribe": &UnsubscribeCommandParser{},
"rename": &RenameCommandParser{},
"lsub": &LSubCommandParser{},
"login": &LoginCommandParser{},
},
}
}
Expand Down

0 comments on commit 2238d80

Please sign in to comment.