22 lines
781 B
TypeScript
22 lines
781 B
TypeScript
|
import { create_salt } from '@sapphirecode/crypto-helper';
|
||
|
import { to_b58 } from '@sapphirecode/encoding-helper';
|
||
|
|
||
|
export function generate_token_id (valid_until: Date) {
|
||
|
const salt = create_salt ();
|
||
|
return `${to_b58 (salt, 'hex')};${valid_until.toISOString ()}`;
|
||
|
}
|
||
|
|
||
|
export function parse_token_id (id: string) {
|
||
|
// eslint-disable-next-line max-len
|
||
|
const regex = /^(?<hash>[A-HJ-NP-Za-km-z1-9]+);(?<date>\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d{3}Z)$/u;
|
||
|
const result = regex.exec (id);
|
||
|
if (result === null)
|
||
|
throw new Error (`invalid token id ${id}`);
|
||
|
if (typeof result.groups === 'undefined')
|
||
|
throw new Error ('invalid state');
|
||
|
return {
|
||
|
hash: result.groups.hash as string,
|
||
|
valid_until: new Date (result.groups.date as string)
|
||
|
};
|
||
|
}
|