Two problems the mail screens exposed, both in the delivery path. The subject, snippet, sender name and message-id columns have existed since the first migration, but nothing ever wrote them: every row went in with those fields NULL. A message list built on the database would have shown blank subjects for every message. Delivery now parses them once per message and stores them, decoding RFC 2047 encoded words so an accented subject is readable rather than raw. Parsing failures are non-fatal: a message with unreadable headers is still delivered, just with less metadata, because refusing it would lose mail over a malformed Subject. Worse, a message the spam filter routed to Junk failed delivery outright when the mailbox had no Junk folder - the sender got 451 and retried forever against a mailbox that would never grow one on its own. Delivery now falls back to INBOX and says so in the log. Found by sending a real message through port 25 against the running stack. The SMTP handler also swallowed the cause of any unexpected failure: the peer saw "temporary error" and the operator got nothing. It is logged now, which is how the missing folder was diagnosed at all. Also adds the JWT verifier the mail API needs. The tokens are minted by the auth service and verified here under the same secret, so the test fixtures are real tokens produced by that service: if the two implementations drift, this fails before the webmail does. Challenge tokens, wrong secrets, tampered payloads, expired tokens and the other surface's audience are all rejected. The dev seed gained the standard folder set, which is what a provisioned mailbox has and what the fallback above should not have to compensate for. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
103 lines
3.2 KiB
Go
103 lines
3.2 KiB
Go
package httppresentation
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// WebSession is the claim set the auth service puts in its session tokens.
|
|
// The two services share only this shape and the signing secret; the tokens
|
|
// themselves are issued in one place and verified in both.
|
|
type WebSession struct {
|
|
Subject string `json:"sub"`
|
|
Email string `json:"email"`
|
|
Role string `json:"role"`
|
|
DomainID string `json:"domainId"`
|
|
Surface string `json:"surface"`
|
|
Audience string `json:"aud"`
|
|
MfaSatisfied bool `json:"mfaSatisfied"`
|
|
Purpose string `json:"purpose"`
|
|
IssuedAt int64 `json:"iat"`
|
|
ExpiresAt int64 `json:"exp"`
|
|
}
|
|
|
|
var (
|
|
ErrTokenMalformed = errors.New("web session: token is not a three-part JWT")
|
|
ErrBadSignature = errors.New("web session: signature does not verify")
|
|
ErrTokenExpired = errors.New("web session: token has expired")
|
|
ErrNotASession = errors.New("web session: token is not a session token")
|
|
ErrWrongAudience = errors.New("web session: token was issued for a different surface")
|
|
)
|
|
|
|
// WebSessionVerifier validates the tokens minted by the auth service.
|
|
//
|
|
// The secret is shared rather than the tokens being re-checked over HTTP: a
|
|
// round trip to the auth service on every mail request would put a second
|
|
// network hop in the read path for no extra safety, since both services
|
|
// already trust the same secret to say who the user is.
|
|
type WebSessionVerifier struct {
|
|
secret []byte
|
|
now func() time.Time
|
|
}
|
|
|
|
func NewWebSessionVerifier(secret string) *WebSessionVerifier {
|
|
return &WebSessionVerifier{secret: []byte(secret), now: time.Now}
|
|
}
|
|
|
|
// Verify checks the signature, the expiry, and that this is a real session
|
|
// rather than the short-lived challenge issued between the password and the
|
|
// second factor.
|
|
func (v *WebSessionVerifier) Verify(token string) (*WebSession, error) {
|
|
parts := strings.Split(token, ".")
|
|
if len(parts) != 3 {
|
|
return nil, ErrTokenMalformed
|
|
}
|
|
|
|
mac := hmac.New(sha256.New, v.secret)
|
|
mac.Write([]byte(parts[0] + "." + parts[1]))
|
|
expected := base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
|
|
|
// Compared with hmac.Equal so a wrong signature costs the same time as a
|
|
// right one, whatever the mismatch position.
|
|
if !hmac.Equal([]byte(parts[2]), []byte(expected)) {
|
|
return nil, ErrBadSignature
|
|
}
|
|
|
|
raw, err := base64.RawURLEncoding.DecodeString(parts[1])
|
|
if err != nil {
|
|
return nil, ErrTokenMalformed
|
|
}
|
|
|
|
var session WebSession
|
|
if err := json.Unmarshal(raw, &session); err != nil {
|
|
return nil, ErrTokenMalformed
|
|
}
|
|
|
|
if session.ExpiresAt > 0 && v.now().Unix() >= session.ExpiresAt {
|
|
return nil, ErrTokenExpired
|
|
}
|
|
if session.Purpose != "session" {
|
|
return nil, ErrNotASession
|
|
}
|
|
|
|
return &session, nil
|
|
}
|
|
|
|
// RequireSurface additionally pins the token to one surface, which is what
|
|
// keeps a /user token out of an admin-only endpoint (PLAN.md section 14.1).
|
|
func (v *WebSessionVerifier) RequireSurface(token, surface string) (*WebSession, error) {
|
|
session, err := v.Verify(token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if session.Surface != surface || session.Audience != "lambdamail:"+surface {
|
|
return nil, ErrWrongAudience
|
|
}
|
|
return session, nil
|
|
}
|