5.9 KiB
Class: UnsecuredJWT
The UnsecuredJWT class is a utility for dealing with { "alg": "none" } Unsecured JWTs.
example
// ESM import
import UnsecuredJWT from 'jose/jwt/unsecured'
example
// CJS import
const { default: UnsecuredJWT } = require('jose/jwt/unsecured')
example
// encoding
const unsecuredJwt = new UnsecuredJWT({ 'urn:example:claim': true })
.setIssuedAt()
.setIssuer('urn:example:issuer')
.setAudience('urn:example:audience')
.setExpirationTime('2h')
.encode()
console.log(unsecuredJwt)
example
// decoding
const payload = new UnsecuredJWT.decode(jwt, {
issuer: 'urn:example:issuer',
audience: 'urn:example:audience'
})
console.log(payload)
Index
Constructors
Methods
Constructors
constructor
+ new UnsecuredJWT(payload: JWTPayload): UnsecuredJWT
Defined in src/lib/jwt_producer.ts:10
Parameters:
| Name | Type | Description |
|---|---|---|
payload |
JWTPayload | The JWT Claims Set object. |
Returns: UnsecuredJWT
Methods
encode
▸ encode(): string
Defined in src/jwt/unsecured.ts:55
Encodes the Unsecured JWT.
Returns: string
setAudience
▸ setAudience(audience: string | string[]): this
Defined in src/lib/jwt_producer.ts:47
Set "aud" (Audience) Claim.
Parameters:
| Name | Type | Description |
|---|---|---|
audience |
string | string[] | "aud" (Audience) Claim value to set on the JWT Claims Set. |
Returns: this
setExpirationTime
▸ setExpirationTime(input: number | string): this
Defined in src/lib/jwt_producer.ts:85
Set "exp" (Expiration Time) Claim.
Parameters:
| Name | Type | Description |
|---|---|---|
input |
number | string | "exp" (Expiration Time) Claim value to set on the JWT Claims Set. When number is passed that is used as a value, when string is passed it is resolved to a time span and added to the current timestamp. |
Returns: this
setIssuedAt
▸ setIssuedAt(input?: number): this
Defined in src/lib/jwt_producer.ts:100
Set "iat" (Issued At) Claim.
Parameters:
| Name | Type | Description |
|---|---|---|
input? |
number | "iat" (Issued At) Claim value to set on the JWT Claims Set. Default is current timestamp. |
Returns: this
setIssuer
▸ setIssuer(issuer: string): this
Defined in src/lib/jwt_producer.ts:27
Set "iss" (Issuer) Claim.
Parameters:
| Name | Type | Description |
|---|---|---|
issuer |
string | "Issuer" Claim value to set on the JWT Claims Set. |
Returns: this
setJti
▸ setJti(jwtId: string): this
Defined in src/lib/jwt_producer.ts:57
Set "jti" (JWT ID) Claim.
Parameters:
| Name | Type | Description |
|---|---|---|
jwtId |
string | "jti" (JWT ID) Claim value to set on the JWT Claims Set. |
Returns: this
setNotBefore
▸ setNotBefore(input: number | string): this
Defined in src/lib/jwt_producer.ts:69
Set "nbf" (Not Before) Claim.
Parameters:
| Name | Type | Description |
|---|---|---|
input |
number | string | "nbf" (Not Before) Claim value to set on the JWT Claims Set. When number is passed that is used as a value, when string is passed it is resolved to a time span and added to the current timestamp. |
Returns: this
setSubject
▸ setSubject(subject: string): this
Defined in src/lib/jwt_producer.ts:37
Set "sub" (Subject) Claim.
Parameters:
| Name | Type | Description |
|---|---|---|
subject |
string | "sub" (Subject) Claim value to set on the JWT Claims Set. |
Returns: this
decode
▸ Staticdecode(jwt: string, options?: JWTClaimVerificationOptions): object
Defined in src/jwt/unsecured.ts:77
Decodes an unsecured JWT.
example
// decoding
const { payload, header } = UnsecuredJWT.decode(unsecuredJwt)
console.log(header)
console.log(payload)
Parameters:
| Name | Type | Description |
|---|---|---|
jwt |
string | Unsecured JWT to decode the payload of. |
options? |
JWTClaimVerificationOptions | JWT Claims Set validation options. |
Returns: object
| Name | Type |
|---|---|
header |
JWSHeaderParameters |
payload |
JWTPayload |