Documentation
¶
Overview ¶
Package webpush owns browser Web Push encryption, VAPID identity, subscriptions, one-time notification actions and asynchronous event dispatch.
Index ¶
- Constants
- Variables
- type ActionClaims
- type Jobs
- type NotificationAction
- type Options
- type PushPayload
- type Service
- func (s *Service) Act(token, option string) (job.Interaction, error)
- func (s *Service) Close()
- func (s *Service) DeleteSubscription(callerID, endpoint string) (bool, error)
- func (s *Service) ListSubscriptions(callerID string) ([]jobstore.PushSubscription, error)
- func (s *Service) NewActionToken(callerID, jobID, interactionID string, options []string) (string, error)
- func (s *Service) NotifyWaiting(n waitnotice.Notice)
- func (s *Service) ObserveEvent(scope, eventType string, detail map[string]any)
- func (s *Service) PublicKey() (string, error)
- func (s *Service) QueueTestIn(lang i18n.Lang, callerID string) (int, error)
- func (s *Service) Subscribe(callerID string, input SubscriptionInput, userAgent string) (jobstore.PushSubscription, error)
- type SubscriptionInput
- type SubscriptionKeys
Constants ¶
const (
DefaultVAPIDSubject = "mailto:gofer@localhost"
)
Variables ¶
var ( ErrActionExpired = errors.New("webpush: action token expired") ErrActionUsed = errors.New("webpush: action token already used") ErrActionOption = errors.New("webpush: action option is not allowed") )
var ErrActionForbidden = errors.New("webpush: caller may not act on an interaction")
ErrActionForbidden: the notification's caller may not answer an interaction right now — it does not hold the answer permission (design §6「动作按钮在签发和兑现时都检查权限」). Minting a button and redeeming its token both answer it.
var ErrNoSubscriptions = errors.New("webpush: caller has no subscriptions")
Functions ¶
This section is empty.
Types ¶
type ActionClaims ¶
type ActionClaims struct {
CallerID string `json:"caller"`
JobID string `json:"job"`
InteractionID string `json:"interaction"`
Options []string `json:"options"`
ExpiresAt int64 `json:"exp"`
Nonce string `json:"nonce"`
}
ActionClaims is the authenticated, self-contained part of an OS notification action. The nonce registry supplies the one-time property; the HMAC supplies integrity and process identity.
type Jobs ¶
type Jobs interface {
Get(id string) (job.JobResult, bool)
GetInteractions(jobID string) ([]job.Interaction, error)
AnswerInteractionByPush(jobID, interactionID, answer string) (job.Interaction, error)
}
Jobs is the authoritative interaction/job seam used by dispatch and actions.
type NotificationAction ¶
type NotificationAction struct {
Action string `json:"action"`
Title string `json:"title"`
Option string `json:"option"`
}
NotificationAction maps one OS notification button to the exact interaction option value bound into its action token.
type Options ¶
type Options struct {
Store *jobstore.Store
Jobs Jobs
ConfigDir string
Subject string
HTTPClient *http.Client
Now func() time.Time
Random io.Reader
UserCallers func() []string
Visible func(callerID, projectKey string) bool
// MayAct is the permission check behind a notification's ACTION BUTTONS (the
// `interaction.answer` permission of the assembly's model): may callerID answer an
// interaction right now? The service asks it when a token is MINTED and again when the
// token is REDEEMED, because a permission can be revoked between the two (design §6).
// A nil MayAct means "no permission plumbing", which fails CLOSED: no action button is
// ever minted and no token is ever redeemed.
MayAct func(callerID string) bool
// LanguageFor picks the language of a push for a project (config.LanguageFor); nil =
// the default language. A push belongs to a job, so it follows the job's project.
LanguageFor func(project string) i18n.Lang
AllowInsecureLoopback bool
}
Options contains assembly-owned dependencies. Time/random/HTTP are injectable boundaries; production leaves them nil for secure defaults.
type PushPayload ¶
type PushPayload struct {
Title string `json:"title"`
Body string `json:"body,omitempty"`
ThreadID string `json:"thread_id,omitempty"`
URL string `json:"url"`
Tag string `json:"tag,omitempty"`
ActionToken string `json:"action_token,omitempty"`
Actions []NotificationAction `json:"actions,omitempty"`
}
PushPayload is intentionally small: no logs, full prompts or secrets.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service owns process-lifetime transient state. Subscription rows and job interactions remain authoritative in their existing stores.
func NewService ¶
func (*Service) DeleteSubscription ¶
func (*Service) ListSubscriptions ¶
func (s *Service) ListSubscriptions(callerID string) ([]jobstore.PushSubscription, error)
func (*Service) NewActionToken ¶
func (s *Service) NewActionToken(callerID, jobID, interactionID string, options []string) (string, error)
NewActionToken mints the one-time token behind one action button of an interaction notification. The caller must hold the answer permission RIGHT NOW (design §6: 动作按钮在 签发和兑现时都检查) — refusing here keeps a button the caller could not use out of the notification altogether.
func (*Service) NotifyWaiting ¶ added in v0.217.0
func (s *Service) NotifyWaiting(n waitnotice.Notice)
NotifyWaiting queues the Web Push half of a 「等我」 notice (the desktop / webhook half is job.Service.NotifyWaiting); the waiting-notice scan calls both from one place. The push carries the title, the subject key as its tag (a later push about the same subject replaces the earlier one on the device) and a link to the subject's workbench thread; it never carries the text of a turn. It never blocks.
func (*Service) ObserveEvent ¶
ObserveEvent implements job.JobEventObserver. It never blocks the job event path and never performs crypto/network I/O inline.
func (*Service) QueueTestIn ¶ added in v0.207.0
QueueTestIn enqueues one non-action test notification, in lang (the requester's language; "" = the server's), to the caller's current subscriptions. The return value is the number of endpoints accepted for async processing, not a delivery receipt.
func (*Service) Subscribe ¶
func (s *Service) Subscribe(callerID string, input SubscriptionInput, userAgent string) (jobstore.PushSubscription, error)
type SubscriptionInput ¶
type SubscriptionInput struct {
Endpoint string `json:"endpoint"`
Keys SubscriptionKeys `json:"keys"`
}
SubscriptionInput is the browser PushSubscription shape accepted by HTTP.