Skip to content

Commit

Permalink
fix(GODT-1593): list of capabilities based on state. (#98)
Browse files Browse the repository at this point in the history
* fix: passphrase for demo store.
* fix(GODT-1593): list of capabilities based on state.
  • Loading branch information
cuthix authored Sep 5, 2022
1 parent 3f133c2 commit 7a5d6de
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
11 changes: 11 additions & 0 deletions imap/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ const (
UIDPLUS Capability = `UIDPLUS`
MOVE Capability = `MOVE`
)

func IsCapabilityAvailableBeforeAuth(c Capability) bool {
switch c {
case IMAP4rev1, StartTLS, IDLE:
return true
case UNSELECT, UIDPLUS, MOVE:
return false
}

return false
}
21 changes: 20 additions & 1 deletion internal/session/handle_capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,34 @@ package session
import (
"context"

"github.com/ProtonMail/gluon/imap"
"github.com/ProtonMail/gluon/internal/parser/proto"
"github.com/ProtonMail/gluon/internal/response"
)

func (s *Session) getCaps() []imap.Capability {
s.userLock.Lock()
defer s.userLock.Unlock()

if s.state != nil {
return s.caps
}

caps := []imap.Capability{}
for _, c := range s.caps {
if imap.IsCapabilityAvailableBeforeAuth(c) {
caps = append(caps, c)
}
}

return caps
}

func (s *Session) handleCapability(ctx context.Context, tag string, cmd *proto.Capability, ch chan response.Response) error {
s.capsLock.Lock()
defer s.capsLock.Unlock()

ch <- response.Capability().WithCapabilities(s.caps...)
ch <- response.Capability().WithCapabilities(s.getCaps()...)

ch <- response.Ok(tag).WithMessage("CAPABILITY")

Expand Down
5 changes: 4 additions & 1 deletion store/badger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package store

import (
"crypto/sha256"
"path/filepath"
"sync"
"time"
Expand All @@ -21,9 +22,11 @@ type badgerTransaction struct {
}

func NewBadgerStore(path string, userID string, encryptionPassphrase []byte) (*BadgerStore, error) {
encryptionKey := sha256.Sum256(encryptionPassphrase)

db, err := badger.Open(badger.DefaultOptions(filepath.Join(path, userID)).
WithLogger(logrus.StandardLogger()).
WithEncryptionKey(encryptionPassphrase).
WithEncryptionKey(encryptionKey[:]).
WithIndexCacheSize(128 * 1024 * 1024),
)
if err != nil {
Expand Down
27 changes: 11 additions & 16 deletions tests/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@ package tests

import (
"testing"

"github.com/ProtonMail/gluon/imap"
"github.com/emersion/go-imap/client"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

func TestCapability(t *testing.T) {
runOneToOneTestClient(t, defaultServerOptions(t), func(client *client.Client, s *testSession) {
capabilities, err := client.Capability()
require.NoError(t, err)
require.ElementsMatch(t, maps.Keys(capabilities), []string{
string(imap.IMAP4rev1),
string(imap.StartTLS),
string(imap.IDLE),
string(imap.UNSELECT),
string(imap.UIDPLUS),
string(imap.MOVE),
})
runOneToOneTest(t, defaultServerOptions(t), func(c *testConnection, _ *testSession) {
c.C("A001 Capability")
c.S(`* CAPABILITY IDLE IMAP4rev1 STARTTLS`)
c.S("A001 OK")

c.C(`A002 login "user" "pass"`)
c.S(`A002 OK [CAPABILITY IDLE IMAP4rev1 MOVE STARTTLS UIDPLUS UNSELECT]`)

c.C("A003 Capability")
c.S(`* CAPABILITY IDLE IMAP4rev1 MOVE STARTTLS UIDPLUS UNSELECT`)
c.S("A003 OK")
})
}

0 comments on commit 7a5d6de

Please sign in to comment.