Documentation
¶
Overview ¶
Package push delivers notifications to people's phones through Apple's push service (APNs), and seals what a phone hands its server so that only the relay that sealed it can use it.
A TeaNode server never talks to Apple for its own phones unless it is the relay: Apple takes notifications for an app only from a key of the developer account that publishes it. The relay (internal/api/v1api/apipush) holds that key; every other server sends to the relay (relay_client.go), and what it sends is already encrypted for the phone (payload.go).
Index ¶
- Constants
- Variables
- func ParseKey(keyPEM string) (*ecdsa.PrivateKey, error)
- type APNs
- type Button
- type Content
- type DeliverRequest
- type Delivery
- type ErrorResponse
- type Notification
- type RegisterRequest
- type RegisterResponse
- type Registration
- type Relay
- func (self *Relay) DeliverTo(ctx context.Context, registration *Registration, delivery *Delivery, ...) error
- func (self *Relay) Open(pushRegistration string, now time.Time) (*Registration, error)
- func (self *Relay) Register(deviceToken string, isSandbox bool, pushType string, now time.Time) (string, error)
- func (self *Relay) Send(ctx context.Context, pushRegistration string, delivery *Delivery) error
- type RelayClient
- type Sealer
- type Sender
Constants ¶
const ( ProductionHost = "https://api.push.apple.com" SandboxHost = "https://api.sandbox.push.apple.com" )
The two APNs hosts: production for App Store and TestFlight builds, the sandbox for builds installed from Xcode.
const ( PushTypeAlert = "alert" PushTypeLiveActivity = "liveactivity" )
Push types: an ordinary notification, or an update to a Live Activity on the lock screen.
const ( PathRegistrations = api.PathPushRegistrations PathNotifications = api.PathPushNotifications )
Paths of the relay's two endpoints, under the relay's address.
const ( ErrorCodeGone = "gone" ErrorCodeTooLarge = "too_large" ErrorCodeRefused = "refused" ErrorCodeLimited = "limited" ErrorCodeUnreached = "unreached" )
Error codes the relay answers with, which a sender maps back to errors.
const PhoneKeyBytes = 32
PhoneKeyBytes is the length of a phone's payload key.
Variables ¶
var ( // ErrGone is APNs saying the device token is no longer valid: the app // was deleted, or the phone gave the app a new token. ErrGone = errors.New("push: the phone is no longer registered for notifications") // ErrTooLarge is a payload over what APNs takes. ErrTooLarge = errors.New("push: the notification is too large") // ErrRefused is APNs refusing the request for a reason a retry will not // change, such as a topic the key does not cover. ErrRefused = errors.New("push: Apple refused the notification") )
What a send can come to besides success. A caller acts on these: a gone device is deleted, the others are retried later or reported.
Functions ¶
Types ¶
type APNs ¶
type APNs struct {
// contains filtered or unexported fields
}
APNs sends notifications to Apple with a token-based authentication key.
func NewAPNs ¶
NewAPNs builds a client from an APNs authentication key: its identifier, the team it belongs to, the app's bundle identifier, and the .p8 file.
type Button ¶
type Button struct {
Label string `json:"label"`
// ButtonAction is what pressing it does: approve, deny, answer:<option
// number>, reply or open.
ButtonAction string `json:"buttonAction"`
}
Button is one action on a notification.
type Content ¶
type Content struct {
// NotificationKind is what this is: alert, herdr_question,
// goal_needs_you, speak_first, approval, question, answer or test.
NotificationKind string `json:"notificationKind"`
ConversationID string `json:"conversationId,omitempty"`
RunID string `json:"runId,omitempty"`
CallID string `json:"callId,omitempty"`
Title string `json:"title"`
Body string `json:"body"`
// Buttons are what can be pressed on the notification itself.
Buttons []Button `json:"buttons,omitempty"`
// ThreadID groups notifications on the lock screen.
ThreadID string `json:"threadId,omitempty"`
// For a coding session's question: where to send the answer.
Computer string `json:"computer,omitempty"`
PaneID string `json:"paneId,omitempty"`
QuestionFingerprint string `json:"questionFingerprint,omitempty"`
}
Content is a notification as the phone shows it, before encryption.
type DeliverRequest ¶
type DeliverRequest struct {
PushRegistration string `json:"pushRegistration"`
Delivery *Delivery `json:"delivery"`
}
DeliverRequest is what a server sends the relay for one notification.
type Delivery ¶
type Delivery struct {
// Sealed is the content, encrypted for the phone.
Sealed string `json:"sealed"`
// ThreadID groups notifications on the lock screen, and CollapseID makes
// a newer notification replace an older one. Both are hashed with the
// phone's key, so Apple and the relay never see a conversation or a
// card's identifier.
ThreadID string `json:"threadId,omitempty"`
CollapseID string `json:"collapseId,omitempty"`
// ExpiresAt is when Apple gives up on a phone it cannot reach; zero is
// deliveryLifetime from now.
ExpiresAt time.Time `json:"expiresAt,omitzero"`
// ActivityState is, for a Live Activity, the one word it shows: one of
// activityStates. A Live Activity's delivery carries nothing else.
ActivityState string `json:"activityState,omitempty"`
// IsActivityEnded ends the Live Activity, showing ActivityState.
IsActivityEnded bool `json:"isActivityEnded,omitempty"`
// ActivityChangedAt is when the turn came to ActivityState, on the
// server's clock. iOS shows the update with the latest aps.timestamp, so
// it is what orders a turn's states; the relay's own clock would order
// them by when each reached it.
ActivityChangedAt time.Time `json:"activityChangedAt,omitzero"`
}
Delivery is a notification as a server hands it to the relay: the content already encrypted for the phone, and the little Apple needs to route it. The relay writes everything Apple shows itself (relay.go), so a server holding a registration can make the phone show only an encrypted notification from the app, never words of its own.
type ErrorResponse ¶
type ErrorResponse struct {
ErrorCode string `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
}
ErrorResponse is what the relay answers when it does not deliver.
type Notification ¶
type Notification struct {
// PushType is PushTypeAlert or PushTypeLiveActivity.
PushType string `json:"pushType"`
// Priority is 10 to deliver now, 5 to let the phone save power.
Priority int `json:"priority,omitempty"`
// CollapseID makes a newer notification replace an older one with the
// same identifier, such as a coding session's latest state.
CollapseID string `json:"collapseId,omitempty"`
// ExpiresAt is when APNs should give up on a phone it cannot reach.
// Zero delivers once or not at all.
ExpiresAt time.Time `json:"expiresAt,omitzero"`
// Payload is the JSON APNs delivers: the "aps" dictionary and anything
// beside it.
Payload json.RawMessage `json:"payload"`
}
Notification is what is delivered: the payload APNs passes to the phone, and how APNs should treat it.
type RegisterRequest ¶
type RegisterRequest struct {
DeviceToken string `json:"deviceToken"`
IsSandbox bool `json:"isSandbox"`
// PushType is PushTypeAlert, the phone itself, which is the default, or
// PushTypeLiveActivity, one Live Activity's push token.
PushType string `json:"pushType,omitempty"`
}
RegisterRequest is what a phone sends the relay to register.
type RegisterResponse ¶
type RegisterResponse struct {
PushRegistration string `json:"pushRegistration"`
}
RegisterResponse is the sealed registration the phone hands its server.
type Registration ¶
type Registration struct {
DeviceToken string `json:"deviceToken"`
IsSandbox bool `json:"isSandbox"`
PushType string `json:"pushType"`
CreatedAt time.Time `json:"createdAt"`
}
Registration is what a sealed registration holds.
type Relay ¶
type Relay struct {
// contains filtered or unexported fields
}
Relay is what a relay does, whether asked over HTTP by another server (internal/api/v1api/apipush) or by its own server, which sends to itself without the network.
func NewRelay ¶
NewRelay builds a relay around its APNs client, sealing registrations with a key made from the client's.
func (*Relay) DeliverTo ¶
func (self *Relay) DeliverTo(ctx context.Context, registration *Registration, delivery *Delivery, now time.Time) error
DeliverTo sends a delivery to an opened registration's phone: an alert to the phone, or a state to one Live Activity.
func (*Relay) Register ¶
func (self *Relay) Register(deviceToken string, isSandbox bool, pushType string, now time.Time) (string, error)
Register seals a phone's device token, or one Live Activity's push token, into the registration it hands its server.
A Live Activity's update is shown by iOS as it arrives, with no extension in between to decrypt it, so it crosses Apple and the relay readable. It therefore carries no words at all: the relay writes it from one of a few states (activityStates), and the app's widget says what each means.
type RelayClient ¶
type RelayClient struct {
// contains filtered or unexported fields
}
RelayClient sends notifications to a relay over HTTP.
func NewRelayClient ¶
func NewRelayClient(baseURL string) *RelayClient
NewRelayClient sends to the relay at baseURL, such as https://mail.teanode.com.
type Sealer ¶
type Sealer struct {
// contains filtered or unexported fields
}
Sealer seals and opens registrations.
func NewSealer ¶
func NewSealer(keyID string, key *ecdsa.PrivateKey) (*Sealer, error)
NewSealer builds a sealer from the relay's APNs key, the one secret only the relay has, rather than from the server secret, which an operator may rotate for reasons of their own. The key's identifier is written in front of every registration: a relay given another key answers the old registrations as gone, so servers clear them and the phones register again.