Comparing version 0.20.0 to 0.21.0
import type { TimeSpan } from "../index.js"; | ||
export type JWTAlgorithm = "HS256" | "HS384" | "HS512" | "RS256" | "RS384" | "RS512" | "ES256" | "ES384" | "ES512" | "PS256" | "PS384" | "PS512"; | ||
export declare function createJWT(algorithm: JWTAlgorithm, key: ArrayBuffer, payload: Record<any, any>, options?: { | ||
export declare function createJWT(algorithm: JWTAlgorithm, key: ArrayBuffer, payloadClaims: Record<any, any>, options?: { | ||
headers?: Record<any, any>; | ||
@@ -12,4 +12,4 @@ expiresIn?: TimeSpan; | ||
jwtId?: string; | ||
}): Promise<JWT>; | ||
export declare function validateJWT(algorithm: JWTAlgorithm, key: ArrayBuffer, jwt: string | JWT): Promise<JWT>; | ||
}): Promise<string>; | ||
export declare function validateJWT(algorithm: JWTAlgorithm, key: ArrayBuffer, jwt: string): Promise<JWT>; | ||
export declare function parseJWT(jwt: string): JWT | null; | ||
@@ -28,20 +28,6 @@ interface JWTProperties { | ||
value: string; | ||
header: JWTHeader; | ||
payload: JWTPayload; | ||
header: object; | ||
payload: object; | ||
parts: [header: string, payload: string, signature: string]; | ||
} | ||
interface JWTHeader { | ||
typ: "JWT"; | ||
alg: JWTAlgorithm; | ||
} | ||
interface JWTPayload { | ||
exp?: number; | ||
iss?: string; | ||
aud?: string; | ||
jti?: string; | ||
nbf?: number; | ||
sub?: string; | ||
iat?: number; | ||
[claim: string]: unknown; | ||
} | ||
export {}; |
import { ECDSA, HMAC, RSASSAPKCS1v1_5, RSASSAPSS } from "../crypto/index.js"; | ||
import { decodeBase64url, encodeBase64url } from "../encoding/index.js"; | ||
import { isWithinExpirationDate } from "../index.js"; | ||
export async function createJWT(algorithm, key, payload, options) { | ||
export async function createJWT(algorithm, key, payloadClaims, options) { | ||
const header = { | ||
@@ -10,27 +10,29 @@ alg: algorithm, | ||
}; | ||
const payloadWithClaims = payload; | ||
const payload = { | ||
...payloadClaims | ||
}; | ||
if (options?.audience !== undefined) { | ||
payloadWithClaims.aud = options.audience; | ||
payload.aud = options.audience; | ||
} | ||
if (options?.subject !== undefined) { | ||
payloadWithClaims.sub = options.subject; | ||
payload.sub = options.subject; | ||
} | ||
if (options?.issuer !== undefined) { | ||
payloadWithClaims.iss = options.issuer; | ||
payload.iss = options.issuer; | ||
} | ||
if (options?.jwtId !== undefined) { | ||
payloadWithClaims.jti = options.jwtId; | ||
payload.jti = options.jwtId; | ||
} | ||
if (options?.expiresIn !== undefined) { | ||
payloadWithClaims.exp = Math.floor(Date.now() / 1000) + options.expiresIn.seconds(); | ||
payload.exp = Math.floor(Date.now() / 1000) + options.expiresIn.seconds(); | ||
} | ||
if (options?.notBefore !== undefined) { | ||
payloadWithClaims.nbf = Math.floor(options.notBefore.getTime() / 1000); | ||
payload.nbf = Math.floor(options.notBefore.getTime() / 1000); | ||
} | ||
if (options?.includeIssuedTimestamp === true) { | ||
payloadWithClaims.iat = Math.floor(Date.now() / 1000); | ||
payload.iat = Math.floor(Date.now() / 1000); | ||
} | ||
const textEncoder = new TextEncoder(); | ||
const headerPart = encodeBase64url(textEncoder.encode(JSON.stringify(header))); | ||
const payloadPart = encodeBase64url(textEncoder.encode(JSON.stringify(payloadWithClaims))); | ||
const payloadPart = encodeBase64url(textEncoder.encode(JSON.stringify(payload))); | ||
const data = textEncoder.encode([headerPart, payloadPart].join(".")); | ||
@@ -40,19 +42,6 @@ const signature = await getAlgorithm(algorithm).sign(key, data); | ||
const value = [headerPart, payloadPart, signaturePart].join("."); | ||
return { | ||
value, | ||
header, | ||
payload: payloadWithClaims, | ||
parts: [headerPart, payloadPart, signaturePart], | ||
algorithm: header.alg, | ||
expiresAt: payloadWithClaims.exp ? new Date(payloadWithClaims.exp * 1000) : null, | ||
subject: options?.subject ?? null, | ||
issuedAt: payloadWithClaims.iat ? new Date(payloadWithClaims.iat * 1000) : null, | ||
issuer: options?.issuer ?? null, | ||
jwtId: options?.jwtId ?? null, | ||
audience: options?.audience ?? null, | ||
notBefore: payloadWithClaims.nbf ? new Date(payloadWithClaims.nbf * 1000) : null | ||
}; | ||
return value; | ||
} | ||
export async function validateJWT(algorithm, key, jwt) { | ||
const parsedJWT = typeof jwt === "string" ? parseJWT(jwt) : jwt; | ||
const parsedJWT = parseJWT(jwt); | ||
if (!parsedJWT) { | ||
@@ -59,0 +48,0 @@ throw new Error("Invalid JWT"); |
{ | ||
"name": "oslo", | ||
"type": "module", | ||
"version": "0.20.0", | ||
"version": "0.21.0", | ||
"description": "A collection of auth-related utilities", | ||
@@ -6,0 +6,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58794
1537