pgjwt: JSON Web Tokens
The pgjwt (PostgreSQL JSON Web Token) extension allows you to create and parse JSON Web Tokens (JWTs) within a PostgreSQL database. JWTs are commonly used for authentication and authorization in web applications and services.
Enable the extension#
- Go to the Database page in the Dashboard.
- Click on Extensions in the sidebar.
- Search for "pgjwt" and enable the extension.
API#
sign(payload json, secret text, algorithm text default 'HSA256')
: Signs a JWT containing payload with secret using algorithm.verify(token text, secret text, algorithm text default 'HSA256')
: Decodes a JWT token that was signed with secret using algorithm.
Where:
payload
is an encrypted JWT represented as a string.secret
is the private/secret passcode which is used to sign the JWT and verify its integrity.algorithm
is the method used to sign the JWT using the secret.token
is an encrypted JWT represented as a string.
Usage#
Once the extension is installed, you can use its functions to create and parse JWTs. Here's an example of how you can use the sign
function to create a JWT:
The pgjwt_encode function returns a string that represents the JWT, which can then be safely transmitted between parties.
To parse a JWT and extract its claims, you can use the verify
function. Here's an example:
Which returns the decoded contents and some associated metadata.
Resources#
- Official
pgjwt
documentation