~rjarry/aerc-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH aerc] jmap: do not keep invalid sessions in cache

Details
Message ID
<20240605155820.124347-2-robin@jarry.cc>
DKIM signature
pass
Download raw message
Patch: +32 -26
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

[aerc/patches] build success

builds.sr.ht <builds@sr.ht>
Details
Message ID
<D1S7JEUXSH64.3S5WFCZIVUGJS@fra02>
In-Reply-To
<20240605155820.124347-2-robin@jarry.cc> (view parent)
DKIM signature
missing
Download raw message
aerc/patches: SUCCESS in 2m12s

[jmap: do not keep invalid sessions in cache][0] from [Robin Jarry][1]

[0]: https://lists.sr.ht/~rjarry/aerc-devel/patches/53146
[1]: robin@jarry.cc

✓ #1243143 SUCCESS aerc/patches/openbsd.yml     https://builds.sr.ht/~rjarry/job/1243143
✓ #1243142 SUCCESS aerc/patches/alpine-edge.yml https://builds.sr.ht/~rjarry/job/1243142
Reply to thread Export thread (mbox)