12 lines
579 B
TypeScript
12 lines
579 B
TypeScript
|
|
import { createHash } from "crypto";
|
||
|
|
import { KEYCLOAK_USER_ID_PREFIX } from "../auth";
|
||
|
|
|
||
|
|
const LEGACY_PUBLIC_ID_PREFIX = "legacy:";
|
||
|
|
|
||
|
|
// Ids of legacy userKey users are the secret they log in with, so anything saved or listed publicly uses a hash
|
||
|
|
// instead. Keycloak ids are not secret and stay as they are.
|
||
|
|
export function publicUserId(userId: string): string {
|
||
|
|
if (userId.startsWith(KEYCLOAK_USER_ID_PREFIX) || userId.startsWith(LEGACY_PUBLIC_ID_PREFIX)) return userId;
|
||
|
|
return LEGACY_PUBLIC_ID_PREFIX + createHash("sha256").update(userId).digest("hex").slice(0, 20);
|
||
|
|
}
|