If a session is found in the cache, check that it works by issuing
a GetIdentities request. If that request fails, invalidate the cache and
go through the authentication.
Ensure that the session is valid and explicitly fail if it is not.
Signed-off-by: Robin Jarry <robin@jarry.cc>
---
worker/jmap/connect.go | 58 +++++++++++++++++++++++-------------------
1 file changed, 32 insertions(+), 26 deletions(-)
diff --git a/worker/jmap/connect.go b/worker/jmap/connect.go
index affa9474a09e..7fe17ccc8b6b 100644
--- a/worker/jmap/connect.go
+++ b/worker/jmap/connect.go
@@ -2,9 +2,8 @@ package jmap
import (
"encoding/json"
- "fmt"
+ "errors"
"io"
- "net/url"
"strings"
"sync/atomic"
@@ -15,54 +14,61 @@ import (
)
func (w *JMAPWorker) handleConnect(msg *types.Connect) error {
- client := &jmap.Client{SessionEndpoint: w.config.endpoint}
+ w.client = &jmap.Client{SessionEndpoint: w.config.endpoint}
if w.config.oauth {
pass, _ := w.config.user.Password()
- client.WithAccessToken(pass)
+ w.client.WithAccessToken(pass)
} else {
user := w.config.user.Username()
pass, _ := w.config.user.Password()
- client.WithBasicAuth(user, pass)
+ w.client.WithBasicAuth(user, pass)
}
- if session, err := w.cache.GetSession(); err != nil {
- if err := client.Authenticate(); err != nil {
+ if session, err := w.cache.GetSession(); err == nil {
+ w.client.Session = session
+ if w.GetIdentities() != nil {
+ w.client.Session = nil
+ if err := w.cache.DeleteSession(); err != nil {
+ w.w.Warnf("DeleteSession: %s", err)
+ }
+ }
+ }
+ if w.client.Session == nil {
+ if err := w.client.Authenticate(); err != nil {
return err
}
- if err := w.cache.PutSession(client.Session); err != nil {
+ if err := w.cache.PutSession(w.client.Session); err != nil {
w.w.Warnf("PutSession: %s", err)
}
- } else {
- client.Session = session
+ }
+ if len(w.identities) == 0 {
+ if err := w.GetIdentities(); err != nil {
+ return err
+ }
}
switch {
- case client == nil:
+ case w.client == nil:
fallthrough
- case client.Session == nil:
+ case w.client.Session == nil:
fallthrough
- case client.Session.PrimaryAccounts == nil:
+ case w.client.Session.PrimaryAccounts == nil:
break
default:
- w.accountId = client.Session.PrimaryAccounts[mail.URI]
+ w.accountId = w.client.Session.PrimaryAccounts[mail.URI]
+ }
+ if w.accountId == "" {
+ if err := w.cache.DeleteSession(); err != nil {
+ w.w.Warnf("DeleteSession: %s", err)
+ }
+ return errors.New("cannot get account id from session")
}
- w.client = client
-
- return w.GetIdentities()
+ return nil
}
func (w *JMAPWorker) GetIdentities() error {
- u, err := url.Parse(w.config.account.Outgoing.Value)
- if err != nil {
- return fmt.Errorf("GetIdentities: %w", err)
- }
- if !strings.HasPrefix(u.Scheme, "jmap") {
- // no need for identities
- return nil
- }
-
var req jmap.Request
req.Invoke(&identity.Get{Account: w.accountId})
--
2.45.1