module

Vapid::JWT

Creates and signs JWT tokens for VAPID authentication.

VAPID requires JWT tokens with specific claims:

  • aud: The origin of the push service
  • exp: Expiration time (max 24 hours from now)
  • sub: Contact information (mailto: or https: URL)

Usage

key_pair = Vapid::KeyPair.generate

token = Vapid::JWT.encode(
  audience: "https://fcm.googleapis.com",
  subject: "mailto:admin@example.com",
  private_key: key_pair,
  expiration: 12.hours
)

Constants

DEFAULT_EXPIRATION = Time::Span.new(hours: 12)

Default expiration time (12 hours)

MAX_EXPIRATION = Time::Span.new(hours: 24)

Maximum allowed expiration time per VAPID spec (24 hours)

Instance methods

decode(token : String, public_key : KeyPair) : JSON::Any

Decodes and verifies a VAPID JWT token (for testing/validation)

Returns the decoded payload as JSON::Any

Source
encode(audience : String, subject : String, private_key : KeyPair, expiration : Time::Span = DEFAULT_EXPIRATION, extra_claims : Hash(String, String | Int64) | Nil = nil) : String

Encodes and signs a VAPID JWT token

Parameters

  • audience: The origin of the push service (e.g., "https://fcm.googleapis.com")
  • subject: Contact info as mailto: or https: URL
  • private_key: The KeyPair to sign with
  • expiration: How long the token is valid (default: 12 hours, max: 24 hours)
  • extra_claims: Optional additional JWT claims

Returns the encoded JWT token string

Source