Files
inbuxa-installer/internal/jmap/frontends.go
T
jcoffey-dev a6f3e63da5 Pin the releases with files, and server 2026.10.12.1
console v2026.10.12.2 and webmail inbuxa-v2026.10.12-g6e1a1cb are the
first releases to carry their tarballs; their checksums are pinned, from
the releases' SHA256SUMS and checked against downloads (the webmail's two
architectures are identical inside, with no native code). The console's
and the webmail's images follow the same constants, so a container and a
host install of either are the same release.

Server 2026.10.12.1 (upstream 0.16.26, with inbuxa:FrontEnds) replaces
2026.10.12, checksums pinned the same way; still glibc 2.39. join's
refusal of an older server, and the README, name it.

The interface's components screen now also asks the whole plan, so a
choice the machine could make but the other choices rule out -- the
console as files on loopback, with no web server to serve them -- is
refused there, not first at the plan.

All e2e cases on the real downloads, no overrides: webmail-host 18/18,
all-host 17/17, join 24/24 (against the released server), install-host
43/43, install-local 26/26, install-public 20/20, proxy-snippets 13/13,
interface 22/22, status-export 16/16, topology 28/28.
2026-10-12 11:53:38 -07:00

112 lines
3.7 KiB
Go

// SPDX-FileCopyrightText: 2026 Coffey Labs LLC
// SPDX-License-Identifier: AGPL-3.0-or-later
package jmap
// inbuxa:FrontEnds (contract C-4): where a server's console and webmail are.
// Setting it registers their OAuth clients and allows their origins on every
// node, which is all a front end on another machine needs from the server --
// and why `inbuxa join` needs nothing from that machine but an API key.
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
var usingInbuxa = []string{"urn:ietf:params:jmap:core", "urn:inbuxa:jmap"}
// FrontEnds is the server's record of its front ends, and what its
// environment says beside it.
type FrontEnds struct {
AdminURL *string `json:"adminUrl"`
WebmailURL *string `json:"webmailUrl"`
WebmailClientSecretSet bool `json:"webmailClientSecretSet"`
ExtraOrigins []string `json:"extraOrigins"`
EnvironmentAdminURL *string `json:"environmentAdminUrl"`
EnvironmentWebmailURL *string `json:"environmentWebmailUrl"`
EnvironmentWebmailClientSecret bool `json:"environmentWebmailClientSecret"`
}
// ErrNoFrontEnds is a server too old to have inbuxa:FrontEnds.
var ErrNoFrontEnds = errors.New("this server has no inbuxa:FrontEnds; it needs release 2026.10.12.1 or newer")
// AccountID is the signed-in account's id, which inbuxa: methods name.
func (c *Client) AccountID(ctx context.Context) (string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.BaseURL+"/jmap/session", nil)
if err != nil {
return "", err
}
if c.Token != "" {
req.Header.Set("Authorization", "Bearer "+c.Token)
} else {
req.SetBasicAuth(c.Username, c.Password)
}
res, err := c.httpClient().Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return "", &HTTPError{Status: res.StatusCode, Body: http.StatusText(res.StatusCode)}
}
var session struct {
Capabilities map[string]json.RawMessage `json:"capabilities"`
PrimaryAccounts map[string]string `json:"primaryAccounts"`
}
if err := json.NewDecoder(res.Body).Decode(&session); err != nil {
return "", fmt.Errorf("not a JMAP session: %w", err)
}
if _, ok := session.Capabilities["urn:inbuxa:jmap"]; !ok {
return "", errors.New("this is not an inbuxa server: its session has no urn:inbuxa:jmap")
}
for _, capability := range []string{"urn:inbuxa:jmap", "urn:ietf:params:jmap:core"} {
if id := session.PrimaryAccounts[capability]; id != "" {
return id, nil
}
}
for _, id := range session.PrimaryAccounts {
return id, nil
}
return "", errors.New("the session names no account")
}
// GetFrontEnds reads inbuxa:FrontEnds.
func (c *Client) GetFrontEnds(ctx context.Context, accountID string) (FrontEnds, error) {
var fe FrontEnds
rs, err := c.do(ctx, usingInbuxa, Call{"inbuxa:FrontEnds/get", map[string]any{"accountId": accountID}, "0"})
var me *MethodError
if errors.As(err, &me) && me.Type == "unknownMethod" {
return fe, ErrNoFrontEnds
}
if err != nil {
return fe, err
}
var got struct {
List []FrontEnds `json:"list"`
}
if err := json.Unmarshal(rs[0].Args, &got); err != nil {
return fe, err
}
if len(got.List) != 1 {
return fe, errors.New("inbuxa:FrontEnds/get returned nothing: is this account a server administrator?")
}
return got.List[0], nil
}
// SetFrontEnds updates inbuxa:FrontEnds with the properties given; null
// clears one.
func (c *Client) SetFrontEnds(ctx context.Context, accountID string, update map[string]any) error {
rs, err := c.do(ctx, usingInbuxa, Call{"inbuxa:FrontEnds/set", map[string]any{
"accountId": accountID,
"update": map[string]any{"singleton": update},
}, "0"})
if err != nil {
return err
}
_, err = DecodeSet(rs[0])
return err
}