Documentation
¶
Index ¶
Constants ¶
const ( ClaimIssuer = "iss" ClaimSubject = "sub" ClaimAudience = "aud" ClaimIssuedAt = "iat" ClaimExpiration = "exp" ClaimNotBefore = "nbf" )
Registered claim names as per NWT specification.
const Kind = 27519
Kind is the Nostr event kind for Nostr Web Tokens (NWT).
const MaxClaims = 512
MaxClaims defines the maximum number of claims allowed in a NWT, to prevent abuse.
Variables ¶
var ( ErrMissingHeader = errors.New("missing Authorization header") ErrInvalidHeader = errors.New("invalid Authorization header format") ErrInvalidEventJSON = errors.New("invalid event JSON") )
Parsing errors
var ( ErrInvalidEventKind = fmt.Errorf("event kind must be %d", Kind) ErrInvalidEventCreatedAt = errors.New("invalid event created at") ErrInvalidEventID = errors.New("invalid event ID") ErrInvalidEventSignature = errors.New("invalid event signature") )
Event validation errors
var ( // MinTime represents the minimum valid time for NWT claims, corresponding to the 0 Unix epoch. MinTime = time.Unix(0, 0).UTC() // MaxTime represents the maximum valid time for NWT claims, set to December 31, 9999. MaxTime = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC) )
var ( ErrEmptyID = errors.New("token ID is empty") ErrInvalidIssuedAt = errors.New("issued at claim is invalid") ErrInvalidExpiration = errors.New("expiration claim is invalid") ErrInvalidNotBefore = errors.New("not before claim is invalid") ErrInvalidTimeWindow = errors.New("not before is after expiration") ErrInvalidAudience = errors.New("audience claim is invalid") ErrNotYetValid = errors.New("token not yet valid (before NotBefore)") ErrExpired = errors.New("token expired (after Expiration)") )
Token validation errors
Functions ¶
func ExtractEventHTTP ¶
ExtractEventHTTP extracts the Nostr event from the Authorization header of the HTTP request without performing any validation.
func SetAuth ¶ added in v0.4.1
SetAuth validates the Nostr event and sets the Authorization header on the request.
func ValidateEvent ¶
ValidateEvent checks whether the given Nostr event is a valid NWT event. It verifies the event kind, created at, ID, and signature but doesn't validate the token claims themselves.
Types ¶
type StrictValidator ¶
StrictValidator performs validation on the Token claims. It checks time-based claims with a configurable clock skew tolerance and verifies that the Audience claim contains an exact match of the specified identifier.
func (StrictValidator) Validate ¶
func (v StrictValidator) Validate(t Token) error
type Token ¶
type Token struct {
ID string // The ID of the Nostr event
Signer string // The pubkey that signed the Nostr event
Issuer string
Subject string
Audience []string
IssuedAt time.Time
Expiration time.Time
NotBefore time.Time
// Additional custom claims, preserved as raw tags for roundtrip compatibility.
Extra nostr.Tags
}
Token represents a parsed Nostr Web Token (NWT) from a Nostr event. It includes registered claims as well as any additional claims found in the event tags. Learn more about NWTs at: https://github.com/pippellia-btc/nostr-web-tokens
func Parse ¶ added in v0.2.0
Parse a Token from the event found in the Authorization header of the request. It validates the event, but doesn't validate the token's claims. To validate the token, use a Validator.
func ParseToken ¶
ParseToken parses the Nostr event into a Token struct, without performing any validation. To validate the token, use a Validator.
func (Token) IsActive ¶
IsActive checks whether the token is currently active. It's a shorthand for Token.IsActiveAt(time.Now(), skew).
func (Token) IsActiveAt ¶
IsActiveAt checks whether the token is active at the specified time, which happens iff
NotBefore - skew <= now <= Expiration + skew
Skew is used to account for clock differences between systems, and is typically a small duration like 60s.
type Validator ¶
Validator wraps the Validate method for validating Tokens. The token is considered valid iff Validate returns nil.
Implementations may enforce different policies for what constitutes a valid token, but are generally expected to at least validate the time-based claims with ValidateTimeClaims.
As an example, check out StrictValidator.