Documentation
¶
Overview ¶
Package oauth signs maild in to OAuth providers (docs/design/accounts.md, Sign-in; ADR-0011): the protocol pieces (protocol.go), provider endpoints, and later the loopback flow and the token manager.
Index ¶
- Constants
- Variables
- func AuthURL(e Endpoint, clientID, redirectURI, state, challenge, loginHint string) string
- func Challenge(verifier string) string
- func GoogleScope(service api.ServiceKind) string
- func NewVerifier(r io.Reader) (string, error)
- func ParseXOAuth2Error(b []byte) error
- func ServiceScope(provider string, service api.ServiceKind) string
- func XOAuth2(user, accessToken string) []byte
- type ChallengeError
- type Endpoint
- type Manager
- func (m *Manager) AccessToken(ctx context.Context, accountID int64) (string, error)
- func (m *Manager) AccessTokenFor(ctx context.Context, accountID int64, audience string) (string, error)
- func (m *Manager) Authorize(ctx context.Context, accountID int64) (string, error)
- func (m *Manager) Finish(ctx context.Context, rawURL string) (int64, error)
- func (m *Manager) Invalidate(accountID int64)
- type Token
- type TokenError
- type XOAuth2Client
Constants ¶
const ( MicrosoftContactsScope = "https://graph.microsoft.com/Contacts.ReadWrite" MicrosoftCalendarScope = "https://graph.microsoft.com/Calendars.ReadWrite" )
Microsoft Graph's scopes for the services besides mail.
const ( MicrosoftIMAPScope = "https://outlook.office.com/IMAP.AccessAsUser.All" MicrosoftSMTPScope = "https://outlook.office.com/SMTP.Send" )
Outlook's audience: IMAP and SMTP over XOAUTH2 (probed 2026-10-15).
const ( GoogleContactsScope = "https://www.googleapis.com/auth/carddav" GoogleCalendarScope = "https://www.googleapis.com/auth/calendar" GoogleTasksScope = "https://www.googleapis.com/auth/tasks" )
The Google scopes of the services besides mail (ADR-0017).
const AudienceGraph = "graph"
AudienceGraph is Microsoft Graph's audience: contacts and calendars (ADR-0039, ADR-0040).
const MicrosoftClientID = "8007f9b9-a6b6-4029-8295-3e6af287317b"
MicrosoftClientID is Frostmail's public client in Microsoft Entra (ADR-0039): any directory and personal accounts, the redirect http://localhost, no secret.
Variables ¶
var ( // ErrNotOAuth means the account does not sign in with OAuth. ErrNotOAuth = errors.New("oauth: the account does not sign in with OAuth") // ErrNoClient means no OAuth client is set for the account's provider. ErrNoClient = errors.New("oauth: no OAuth client is set") // ErrReauth means the provider refused the stored grant, or there is // none: the user must sign in again. ErrReauth = errors.New("oauth: sign in again") // ErrNoScope means the grant covers no scope of the audience asked // for: the user signs in again with the service on. ErrNoScope = errors.New("oauth: the sign-in covers no scope for this service") // ErrNoSignIn means no waiting sign-in has the state of the address // given to Finish: it is unknown, finished or expired. ErrNoSignIn = errors.New("oauth: no sign-in waits for this address") // ErrRefused means the provider refused the sign-in: the address // carries its error value. ErrRefused = errors.New("oauth: the provider refused the sign-in") // ErrNoCode means the address carries neither a code nor an error. ErrNoCode = errors.New("oauth: the browser returned no code") )
var Google = Endpoint{ AuthURL: "https://accounts.google.com/o/oauth2/v2/auth", TokenURL: "https://oauth2.googleapis.com/token", Scopes: []string{"https://mail.google.com/"}, Params: url.Values{"access_type": {"offline"}, "prompt": {"consent"}}, }
Google is Google's endpoint for Gmail over IMAP and SMTP.
var Microsoft = Endpoint{ AuthURL: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize", TokenURL: "https://login.microsoftonline.com/common/oauth2/v2.0/token", Scopes: []string{MicrosoftIMAPScope, MicrosoftSMTPScope, "offline_access"}, Params: url.Values{"prompt": {"select_account"}}, ClientID: MicrosoftClientID, Loopback: "localhost", TokenScopes: []string{MicrosoftIMAPScope, MicrosoftSMTPScope, "offline_access"}, Audiences: map[string]string{AudienceGraph: "https://graph.microsoft.com/"}, }
Microsoft is Microsoft's endpoint for Outlook.com and Microsoft 365 mail over IMAP and SMTP, at the common authority. A sign-in asks for offline access, and every token request names Outlook's audience.
Functions ¶
func AuthURL ¶
AuthURL is the authorization URL the user opens: e.AuthURL with the authorization-code and PKCE parameters, the endpoint's scopes joined by a space, the endpoint's own parameters (Google's offline access with consent), and login_hint only when it is not empty.
func Challenge ¶
Challenge is the S256 code challenge of a verifier (RFC 7636 §4.2): the unpadded base64url of its SHA-256.
func GoogleScope ¶
func GoogleScope(service api.ServiceKind) string
GoogleScope is the Google scope a service needs.
func NewVerifier ¶
NewVerifier reads exactly 32 random bytes from r (crypto/rand.Reader in production) and returns them as a PKCE code verifier: unpadded base64url, 43 characters. A short read is an error.
func ParseXOAuth2Error ¶
ParseXOAuth2Error reads a mail server's XOAUTH2 failure details, which arrive either as JSON or as standard base64 of that JSON. Anything that does not decode, or carries no status, is a plain error.
func ServiceScope ¶
func ServiceScope(provider string, service api.ServiceKind) string
ServiceScope is the scope a provider's service needs ("" for none).
Types ¶
type ChallengeError ¶
type ChallengeError struct {
Status string // HTTP-like status, such as "401"
Schemes string
Scope string
}
ChallengeError is a mail server's XOAUTH2 failure details.
func (*ChallengeError) AuthRefused ¶ added in v0.6.0
func (e *ChallengeError) AuthRefused() bool
AuthRefused is true: the mail server refused the token (internal/activity's classifier).
func (*ChallengeError) Error ¶
func (e *ChallengeError) Error() string
type Endpoint ¶
type Endpoint struct {
AuthURL string
TokenURL string
Scopes []string
// Params are the authorization URL's parameters besides the flow's
// own: how the provider is asked for a refresh token and a prompt.
Params url.Values
// ClientID is the provider's built-in public client, used when the user
// stored none; "" means the user brings their own (ADR-0011).
ClientID string
// Loopback is the redirect's host: "127.0.0.1" ("" too), or
// "localhost" for a client registered with it (Microsoft's).
Loopback string
// TokenScopes are sent with every token request for mail. Microsoft
// grants a token for one audience, the one these scopes name
// (ADR-0039); Google needs none.
TokenScopes []string
// Audiences are the provider's audiences besides mail's, by name, with
// the prefix of their scopes: a token for one names the granted scopes
// with that prefix (Microsoft's Graph).
Audiences map[string]string
}
Endpoint is a provider's OAuth endpoints and the scopes mail needs.
type Manager ¶
type Manager struct {
DB *store.DB
Secrets secrets.Store
Log *slog.Logger
// HTTP talks to token endpoints; nil means a client with a 30 s
// timeout.
HTTP *http.Client
// Endpoints by provider; nil means Google's and Microsoft's.
Endpoints map[string]Endpoint
// SignedIn runs after a sign-in stored its token (maild reconnects the
// account).
SignedIn func(accountID int64)
// FlowTimeout bounds a sign-in [5m].
FlowTimeout time.Duration
// Refreshed, when set, learns the end of each refresh of an access
// token: nil, or the error (maild records it as activity, ADR-0055).
Refreshed func(accountID int64, err error)
// Now and Rand are the clock and randomness; nil means the real ones.
Now func() time.Time
Rand io.Reader
// contains filtered or unexported fields
}
Manager signs accounts in (the authorization-code flow with PKCE and a loopback redirect) and keeps their access tokens fresh (docs/design/accounts.md, Sign-in). Access tokens live only in memory; refresh tokens are in the secret store.
func (*Manager) AccessToken ¶
AccessToken returns a token for mail valid for at least 5 more minutes, refreshing it when needed. It returns ErrReauth when there is no grant or the provider refuses it (and marks the account so).
func (*Manager) AccessTokenFor ¶
func (m *Manager) AccessTokenFor(ctx context.Context, accountID int64, audience string) (string, error)
AccessTokenFor is AccessToken for one of the provider's audiences besides mail's (AudienceGraph), from the same refresh token; "" is mail's. A provider with one audience (Google) gives its token for any. The lock is the account's, so two audiences never redeem a rotating refresh token at once. ErrNoScope means the grant has no scope of the audience.
func (*Manager) Authorize ¶
Authorize starts a sign-in and returns the URL for the browser. maild listens on a random loopback port for the browser's return, for one request with the right state, until FlowTimeout; a new sign-in for the same account cancels the previous one. A sign-in also ends through Finish, when its browser cannot reach maild's loopback (ADR-0043).
func (*Manager) Finish ¶
Finish ends the waiting sign-in whose state is in the address that the provider sent the browser to, and stores its token as the loopback listener would (ADR-0043). The address must have the waiting sign-in's redirect as its scheme, host, port and path. Finish never logs the code or the address.
func (*Manager) Invalidate ¶
Invalidate drops an account's cached access tokens (a server refused one), so the next AccessToken or AccessTokenFor refreshes.
type Token ¶
type Token struct {
Access string
Refresh string // empty when the response carries none (refreshes)
Expiry time.Time // zero when the response gives no lifetime
Scope string // the scopes granted, space-separated; "" when the response does not say
}
Token is what a token endpoint grants.
func ParseToken ¶
ParseToken reads a token endpoint's response body with its HTTP status and the current time. A body that carries an error is a *TokenError whatever the status; any other non-200 status, an undecodable body, a missing access token or a negative lifetime is a plain error. A lifetime of zero or a missing one leaves Expiry zero.
type TokenError ¶
TokenError is a token endpoint's error response (RFC 6749 §5.2).
func (*TokenError) AuthRefused ¶ added in v0.6.0
func (e *TokenError) AuthRefused() bool
AuthRefused reports whether the token endpoint refused the grant or the client, so the account needs a new sign-in (internal/activity's classifier).
func (*TokenError) Error ¶
func (e *TokenError) Error() string
type XOAuth2Client ¶
type XOAuth2Client struct {
// contains filtered or unexported fields
}
XOAuth2Client is a sasl.Client for XOAUTH2.
func SASL ¶
func SASL(user, accessToken string) *XOAuth2Client
SASL returns a SASL XOAUTH2 client for IMAP and SMTP. When the server refuses the token it sends a challenge with the failure details; the client answers with an empty response, as the mechanism requires, and Err then reports the details.
func (*XOAuth2Client) Err ¶
func (c *XOAuth2Client) Err() error
Err is the server's failure details after a refused token, if it sent any.