studiocms
Version:
Astro Native CMS for AstroDB. Built from the ground up by the Astro community.
61 lines (60 loc) • 2.51 kB
TypeScript
/**
* Encodes a given string to a Base64URL format.
*
* This function converts the input string to Base64, then replaces characters
* to make the output URL-safe according to the Base64URL specification:
* - Replaces '+' with '-'
* - Replaces '/' with '_'
* - Removes any trailing '=' characters
*
* @param input - The string to encode.
* @returns The Base64URL-encoded string.
*/
export declare function base64UrlEncode(input: string): string;
/**
* Decodes a Base64URL-encoded string into its original representation.
*
* This function replaces URL-safe Base64 characters with their standard equivalents,
* adds necessary padding, and decodes the string using Node.js Buffer.
*
* @param input - The Base64URL-encoded string to decode.
* @returns The decoded string.
*/
export declare function base64UrlDecode(input: string): string;
/**
* Represents the result of verifying a JWT (JSON Web Token).
*
* @property isValid - Indicates whether the JWT is valid.
* @property userId - The user ID extracted from the token, if available. This is optional and may be undefined if the token is invalid.
*/
export interface JwtVerificationResult {
isValid: boolean;
userId?: string;
}
/**
* Generates a JSON Web Token (JWT) using the provided secret and payload.
*
* The token is signed using the HS256 algorithm. The payload must include a `userId`.
* The token's expiration (`exp`) is set to 24 hours from the current time by default,
* or 30 years from today if `noExpire` is `true`.
*
* @param secret - The secret key used to sign the JWT.
* @param payload - An object containing the JWT payload. Must include a `userId`.
* @param noExpire - If `true`, sets the token expiration to 30 years from now; otherwise, 24 hours.
* @returns The generated JWT as a string.
*/
export declare function generateJwt(secret: string, payload: {
userId: string;
}, noExpire?: boolean): string;
/**
* Verifies a JWT token using the provided secret.
*
* This function checks the token's algorithm, expiration, and signature.
* It expects the token to use the HS256 algorithm and verifies the signature
* using HMAC SHA-256 with the provided secret concatenated with itself.
*
* @param token - The JWT token string to verify.
* @param secret - The secret key used to verify the token's signature.
* @returns An object indicating whether the token is valid and, if valid, the user ID.
*/
export declare function verifyJwt(token: string, secret: string): JwtVerificationResult;