33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { eq } from "drizzle-orm";
|
|
import { db, User } from "@/lib/db";
|
|
|
|
/**
|
|
* Build the SSO ticket exactly like AtomCMS's User::ssoTicket():
|
|
* $hotelName = Str::replace(' ', '', setting('hotel_name'));
|
|
* sprintf('%s-%s', $hotelName, Str::uuid());
|
|
* i.e. the hotel name with ALL spaces removed, a dash, then a v4 UUID.
|
|
* The emulator validates this exact value when the Nitro/Flash client connects.
|
|
*/
|
|
export function generateSsoTicket(hotelName: string): string {
|
|
const normalized = hotelName.replace(/ /g, "");
|
|
return `${normalized}-${randomUUID()}`;
|
|
}
|
|
|
|
/**
|
|
* Generate a ticket and persist it like AtomCMS: writes auth_ticket AND
|
|
* ip_current on the user, then returns the ticket for the client launcher.
|
|
*/
|
|
export async function issueSsoTicket(
|
|
userId: number,
|
|
hotelName: string,
|
|
ip: string,
|
|
): Promise<string> {
|
|
const ticket = generateSsoTicket(hotelName);
|
|
await db
|
|
.update(User)
|
|
.set({ authTicket: ticket, ipCurrent: ip })
|
|
.where(eq(User.id, userId));
|
|
return ticket;
|
|
}
|