@casual-simulation/aux-common
Version:
Common library for AUX projects
175 lines • 8.09 kB
TypeScript
/**
* Defines an interface that represents the role that a user can have.
*
* - "none" means that the user has no special permissions.
* - "superUser" means that the user has additional permissions that only special users should have.
* - "system" means that the user is the system and is performing a system operation.
* - "moderator" means that the user is a moderator and has additional permissions to moderate content.
*/
export type UserRole = 'none' | 'superUser' | 'system' | 'moderator';
/**
* The default lifetime at which a session key should be refreshed.
*/
export declare const REFRESH_LIFETIME_MS: number;
/**
* Formats the given user ID, session ID, session secret, and expiration time into a key that is used to authenticate a user to a particular session.
* @param userId The ID of the user.
* @param sessionId The ID of the session.
* @param sessionSecret The secret for the session.
* @param expireTimeMs The unix timestamp that the key expires at.
*/
export declare function formatV1SessionKey(userId: string, sessionId: string, sessionSecret: string, expireTimeMs: number | null): string;
/**
* Parses the given session key into a user ID and session ID, and session secret array.
* Returns null if the key cannot be parsed.
* @param key The key to parse.
*/
export declare function parseSessionKey(key: string | null): [
userId: string,
sessionId: string,
sessionSecret: string,
expireTimeMs: number
];
/**
* Parses a version 1 session key into a user ID, session ID, session secret, and expiration time.
* Returns null if the key cannot be parsed or if it is not a V1 key.
* @param key The key to parse.
*/
export declare function parseV1SessionKey(key: string): [
userId: string,
sessionId: string,
sessionSecret: string,
expireTimeMs: number
];
/**
* Formats the given user ID, session ID, connection secret, and expiration time into a key that is used to generate connection tokens.
* @param userId The ID of the user.
* @param sessionId The ID of the session.
* @param sessionSecret The secret for the connections.
* @param expireTimeMs The unix timestamp that the key expires at.
*/
export declare function formatV1ConnectionKey(userId: string, sessionId: string, connectionSecret: string, expireTimeMs: number | null): string;
/**
* Parses the given connection key into a user ID and session ID, and connection secret array.
* Returns null if the key cannot be parsed.
* @param key The key to parse.
*/
export declare function parseConnectionKey(key: string | null): [
userId: string,
sessionId: string,
connectionSecret: string,
expireTimeMs: number
];
/**
* Parses a version 1 session key into a user ID, session ID, session secret, and expiration time.
* Returns null if the key cannot be parsed or if it is not a V1 key.
* @param key The key to parse.
*/
export declare function parseV1ConnectionKey(key: string): [
userId: string,
sessionId: string,
connectionSecret: string,
expireTimeMs: number
];
/**
* Formats the given OpenAI Key into a string that is detectable as an OpenAI Key.
* @param apiKey The API Key that should be formatted.
*/
export declare function formatV1OpenAiKey(apiKey: string): string;
/**
* Determines if the given string represents an OpenAI Key.
* @param apiKey The API Key.
*/
export declare function isOpenAiKey(apiKey: string): boolean;
/**
* Parses the given OpenAI Key.
* @param key The key that should be parsed.
*/
export declare function parseOpenAiKey(key: string): [key: string];
/**
* Generates a new connection token from the given key, connection ID, and device ID.
*
* Returns null if the key cannot be parsed.
* @param key The connection key that should be used to generate the token.
* @param connectionId The connection ID.
* @param deviceId The device ID.
* @param inst The ID of the instance that the connection is for.
*/
export declare function generateV1ConnectionToken(key: string, connectionId: string, recordName: string | null, inst: string): string;
/**
* Calculates the SHA-256 HMAC of the given connection ID, record name, and inst using the given connection secret.
* @param connectionSecret The connection secret.
* @param connectionId The ID of the connection.
* @param recordName The name of the record.
* @param inst The inst.
*/
export declare function v1ConnectionTokenHmac(connectionSecret: string, connectionId: string, recordName: string, inst: string): string;
/**
* Validates whether the given connection token is valid and was generated from the given connection key.
* @param connectionToken The connection token to validate.
* @param connectionSecret The secret for the connection.
*/
export declare function verifyConnectionToken(connectionToken: string, connectionSecret: string): boolean;
/**
* Determines whether the given role is a super user role.
* @param role The role to check.
*/
export declare function isSuperUserRole(role: UserRole | null | undefined): boolean;
/**
* Determines wether the given role is suitable for a package reviewer.
* @param role The role.
*/
export declare function isPackageReviewerRole(role: UserRole | null | undefined): boolean;
/**
* Determines whether the given time has expired.
* Can be used to determine wether a session keys, connection keys, etc. has expired.
*
* If the given time is null, then the key is considered to never expire.
*
* @param expirationMs The time that the key expires in miliseconds at since 1 January 1970 (Unix Epoch).
* @param nowMs The current time in milliseconds since 1 January 1970 (Unix Epoch).
*/
export declare function isExpired(expirationMs: number | null, nowMs?: number): boolean;
/**
* Determines wether the given key will expire within the next REFRESH_LIFETIME_MS.
*
* Returns true if the key has expired or will expire within the next REFRESH_LIFETIME_MS.
* Returns false if the key will not expire within the next REFRESH_LIFETIME_MS.
* @param expirationMs The time that the key expires in miliseconds at since 1 January 1970 (Unix Epoch).
* @param nowMs The current time in milliseconds since 1 January 1970 (Unix Epoch).
*/
export declare function willExpire(expirationMs: number | null, nowMs?: number): boolean;
/**
* Determines if a key with the given expiration time can expire.
* @param expirationMs The time that the key expires at in miliseconds since 1 January 1970 (Unix Epoch).
*/
export declare function canExpire(expirationMs: number | null): boolean;
/**
* Gets the amount of time in miliseconds until a token with the given expiration time should be refreshed.
*
* Returns 0 or a negative number if the key has expired or will expire within the next week (REFRESH_LIFETIME_MS).
* Returns a positive number if the key will not expire within the next week (REFRESH_LIFETIME_MS).
* Returns infinity if the key will never expire.
*
* @param expirationMs The time that the token expires in miliseconds since the Unix Epoch (1 January 1970).
* @param nowMs The current time in miliseconds since the Unix Epoch (1 January 1970).
*/
export declare function timeUntilRefresh(expirationMs: number | null, nowMs?: number): number;
/**
* Gets the amount of time until a token with the given expiration time expires.
*
* Returns 0 or a negative number if the key has expired.
* Returns infinity if the key will never expire.
* Returns some other positive number if the key will expire in the future.
*
* @param expirationMs The time that the token expires in miliseconds since the Unix Epoch (1 January 1970).
* @param nowMs The current time in miliseconds since the Unix Epoch (1 January 1970).
*/
export declare function timeUntilExpiration(expirationMs: number | null, nowMs?: number): number;
/**
* Gets the expiration time of the given session key.
* @param key The session key.
* @returns Returns the expiration time in miliseconds since the Unix Epoch (1 January 1970). Returns -1 if the key is invalid.
*/
export declare function getSessionKeyExpiration(key: string): number;
//# sourceMappingURL=AuthUtils.d.ts.map