libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-jwt-auth.h File Reference

Go to the source code of this file.

Data Structures

struct  lws_jwt_auth_info

Macros

#define LWS_SSO_MAX_COOKIE   4096
#define LWS_JWT_AUTH_STATE_REAUTH   1
#define LWS_JWT_AUTH_STATE_EXPIRED   2
#define LWS_JWT_AUTH_PURPOSE_SSO   "sso"

Typedefs

typedef int(* lws_jwt_auth_cb_t) (struct lws_jwt_auth *ja, int state, void *user)

Functions

LWS_VISIBLE LWS_EXTERN struct lws_jwt_auth * lws_jwt_auth_create_via_info (struct lws *wsi, const struct lws_jwt_auth_info *i, const char **reason)
LWS_VISIBLE LWS_EXTERN struct lws_jwt_auth * lws_jwt_auth_create (struct lws *wsi, struct lws_jwk *jwk, const char *cookie_name, lws_jwt_auth_cb_t cb, void *user, const char **reason)
LWS_VISIBLE LWS_EXTERN int lws_jwt_auth_query_grant (struct lws_jwt_auth *ja, const char *service_name)
LWS_VISIBLE LWS_EXTERN const char * lws_jwt_auth_get_sub (struct lws_jwt_auth *ja)
LWS_VISIBLE LWS_EXTERN const char * lws_jwt_auth_get_did (struct lws_jwt_auth *ja)
LWS_VISIBLE LWS_EXTERN uint32_t lws_jwt_auth_get_uid (struct lws_jwt_auth *ja)
LWS_VISIBLE LWS_EXTERN uint64_t lws_jwt_auth_get_exp (struct lws_jwt_auth *ja)
LWS_VISIBLE LWS_EXTERN uint32_t lws_jwt_auth_get_sec (struct lws_jwt_auth *ja)
LWS_VISIBLE LWS_EXTERN uint32_t lws_jwt_auth_count_grants (struct lws_jwt_auth *ja)
LWS_VISIBLE LWS_EXTERN int lws_jwt_auth_update (struct lws_jwt_auth *ja, const char *jwt, const char **reason)
LWS_VISIBLE LWS_EXTERN void lws_jwt_auth_destroy (struct lws_jwt_auth **ja)

Data Structure Documentation

◆ lws_jwt_auth_info

struct lws_jwt_auth_info

struct lws_jwt_auth_info - what lws_jwt_auth_create_via_info() requires

The strings are copied into the object, so they need not outlive the call; lws_jwt_auth_update() on the object later applies the same requirements.

Definition at line 44 of file lws-jwt-auth.h.

Collaboration diagram for lws_jwt_auth_info:
Data Fields
struct lws_jwk * jwk

the key the issuer's signature must verify with

const char * cookie_name

exactly the name of the cookie to look for, eg "__Host-x"

const char * iss

NULL, or the issuer the token's "iss" must be exactly (< 128)

const char * aud

NULL, or the audience the token's "aud" must be exactly, as a string (< 128)

const char * purpose

NULL, or the purpose the token's "pur" must be exactly (< 32). When NULL, a token that has a "pur" claim at all is refused: it was minted for something else. The signature covers the claims but not the cookie name, which the client chooses, so a jwk that signs more than one kind of token is only safe when every kind has its own purpose and every check asks for the one it wants.

lws_jwt_auth_cb_t cb

NULL, or the reauth / expiry callback

void * user

opaque pointer passed to cb

Macro Definition Documentation

◆ LWS_SSO_MAX_COOKIE

#define LWS_SSO_MAX_COOKIE   4096

Definition at line 20 of file lws-jwt-auth.h.

◆ LWS_JWT_AUTH_STATE_REAUTH

#define LWS_JWT_AUTH_STATE_REAUTH   1

Definition at line 24 of file lws-jwt-auth.h.

◆ LWS_JWT_AUTH_STATE_EXPIRED

#define LWS_JWT_AUTH_STATE_EXPIRED   2

Definition at line 25 of file lws-jwt-auth.h.

◆ LWS_JWT_AUTH_PURPOSE_SSO

#define LWS_JWT_AUTH_PURPOSE_SSO   "sso"

Definition at line 36 of file lws-jwt-auth.h.

Typedef Documentation

◆ lws_jwt_auth_cb_t

typedef int(* lws_jwt_auth_cb_t) (struct lws_jwt_auth *ja, int state, void *user)

Definition at line 27 of file lws-jwt-auth.h.

Function Documentation

◆ lws_jwt_auth_create_via_info()

LWS_VISIBLE LWS_EXTERN struct lws_jwt_auth * lws_jwt_auth_create_via_info ( struct lws * wsi,
const struct lws_jwt_auth_info * i,
const char ** reason )

lws_jwt_auth_create_via_info() - lws_jwt_auth_create() with claim requirements

Parameters
wsiThe connection to extract the HTTP cookie from
ithe key, cookie name, required claims and callback
reasonNULL, or set to why on failure

As lws_jwt_auth_create(), but each occurrence of the cookie must also carry exactly the "iss", "aud" and "pur" claims i requires (see struct lws_jwt_auth_info), each once, at the top level of the claims. An occurrence that doesn't is treated as one that failed verification.

Returns NULL if no occurrence verified, or if i asks for an empty or over-long claim, otherwise the allocated opaque object.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_jwt_auth_create()

LWS_VISIBLE LWS_EXTERN struct lws_jwt_auth * lws_jwt_auth_create ( struct lws * wsi,
struct lws_jwk * jwk,
const char * cookie_name,
lws_jwt_auth_cb_t cb,
void * user,
const char ** reason )

lws_jwt_auth_create() - Instantiates an opaque heap allocation from an incoming HTTP request

Parameters
wsiThe connection to extract the HTTP cookie from
jwkThe public JSON Web Key used to verify the issuer's signature
cookie_nameNatively searches WSI_TOKEN_HTTP_COOKIE for this payload. Exactly this name is looked for, with no __Host- / __Secure- aliasing: give the full name the cookie was minted under, eg, "__Host-x" for a cookie set by lws_jwt_sign_token_set_http_cookie() with a cookie_name of "x".
cbReauth/Expiry callback handler
userOpaque context passed cleanly to the callback

Scans the WSI for the designated cookie, validates cryptographic signatures natively, executes lightweight lejp JSON parsing to extract the exp timestamp and ANY custom grants dictionaries, allocates the tracking object, and registers the proactive SUL timer natively.

Browsers can present several same-named cookies at once (host-only alongside Domain=, or a leftover from an earlier cookie scope), oldest first. Every occurrence is tried: of those that verify and are unexpired, the one that expires last wins, so a just-renewed token is not shadowed by an older one still alive in the other scope. If none is live, the first that verified is returned so the caller decides what an expired token means (check lws_jwt_auth_get_exp()).

It requires no iss or aud, and no purpose: so a token that carries a "pur" claim, ie, one minted for a specific purpose, is refused. To accept tokens of one purpose, or to require an issuer or audience, use lws_jwt_auth_create_via_info().

Returns NULL if no occurrence verified, or the allocated opaque object.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_jwt_auth_query_grant()

LWS_VISIBLE LWS_EXTERN int lws_jwt_auth_query_grant ( struct lws_jwt_auth * ja,
const char * service_name )

lws_jwt_auth_query_grant() - Extract a dynamic grant level

Parameters
jaThe opaque helper object
service_nameThe target category string (e.g. "git-server")

Evaluates the internally parsed grants array recursively. Returns the integer level (e.g. 1, 2) if authorized, or -1 if unauthorized or missing.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_jwt_auth_get_sub()

LWS_VISIBLE LWS_EXTERN const char * lws_jwt_auth_get_sub ( struct lws_jwt_auth * ja)

lws_jwt_auth_get_sub() - Extract the native subject (identity) string

Parameters
jaThe opaque helper object

Returns a pointer to the extracted "sub" or "email" string literal on the object. Returns NULL if no identity claim was parsed naturally.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_jwt_auth_get_did()

LWS_VISIBLE LWS_EXTERN const char * lws_jwt_auth_get_did ( struct lws_jwt_auth * ja)

lws_jwt_auth_get_did() - get the device id associated with the jwt (if any)

Parameters
jathe jwt auth object

Returns the device ID ("did" claim) from the jwt, or NULL.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_jwt_auth_get_uid()

LWS_VISIBLE LWS_EXTERN uint32_t lws_jwt_auth_get_uid ( struct lws_jwt_auth * ja)

lws_jwt_auth_get_uid() - Extract the native uid integer

Parameters
jaThe opaque helper object
Returns
the integer UID natively parsed out of the token, or 0 if missing.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_jwt_auth_get_exp()

LWS_VISIBLE LWS_EXTERN uint64_t lws_jwt_auth_get_exp ( struct lws_jwt_auth * ja)

lws_jwt_auth_get_exp() - Extract the expiration timestamp

Parameters
jaThe opaque helper object
Returns
the uint64_t expiration unix timestamp, or 0 if missing.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_jwt_auth_get_sec()

LWS_VISIBLE LWS_EXTERN uint32_t lws_jwt_auth_get_sec ( struct lws_jwt_auth * ja)

lws_jwt_auth_get_sec() - Extract the session epoch

Parameters
jaThe opaque helper object
Returns
the session epoch parsed out of the token, or 0 if missing.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_jwt_auth_count_grants()

LWS_VISIBLE LWS_EXTERN uint32_t lws_jwt_auth_count_grants ( struct lws_jwt_auth * ja)

lws_jwt_auth_count_grants() - Return the scalar count of active parsed grants

Parameters
jaThe opaque helper object

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_jwt_auth_update()

LWS_VISIBLE LWS_EXTERN int lws_jwt_auth_update ( struct lws_jwt_auth * ja,
const char * jwt,
const char ** reason )

lws_jwt_auth_update() - Applies a refreshed JWT to the existing structure

Parameters
jaThe opaque helper object
jwtThe raw refreshed JWT signed blob string

Processes the updated JWT cryptographically. Re-evaluates all grant strings, recalculates exp, and safely shifts the SUL timer natively. The new JWT must meet the same iss / aud / purpose requirements the object was created with.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_jwt_auth_destroy()

LWS_VISIBLE LWS_EXTERN void lws_jwt_auth_destroy ( struct lws_jwt_auth ** ja)

lws_jwt_auth_destroy() - Gracefully cancels SUL instances and frees the allocation

Parameters
jaDouble-pointer to the object to cleanly wipe