feat(stage): add opt-in bounded CI editorial test principal

This commit is contained in:
devops authored and devops committed 2026-10-24 13:04:35 +00:00
1 parent ed77f87ab5
commit e036c5d436
15 files changed
+340 -1

No files matched your search

+32
View File
@@ -63,6 +63,7 @@ export class CouponInput {
import { ALLOWED_USER_ROLES, assertUserMutationAllowed } from "./user-role-policy";
import type { UserAdminActor } from "./user-role-policy";
import { authorizeStageSession, STAGE_TEST_ID, STAGE_TEST_EMAIL, STAGE_TEST_MOBILE } from "../auth/stage-test-principal";
@Injectable()
export class AdminService {
@@ -237,6 +238,10 @@ export class AdminService {
async createUser(dto: CreateUserDto, actor?: UserAdminActor) {
assertUserMutationAllowed(actor, undefined, dto.role);
return this.persistUser(dto);
}
private async persistUser(dto: CreateUserDto, stage?: { session: string; expiresAt: Date }) {
const mobile = normalizeMobile(dto.mobile);
const existingMobile = await this.prisma.user.findUnique({
where: { mobile },
@@ -269,6 +274,7 @@ export class AdminService {
return this.prisma.user.create({
data: {
...(stage ? { id: STAGE_TEST_ID, stageTestSession: stage.session, authExpiresAt: stage.expiresAt } : {}),
firstName: dto.firstName.trim(),
lastName: dto.lastName.trim(),
mobile,
@@ -283,6 +289,28 @@ export class AdminService {
});
}
// Explicit CI seed authority, NOT a fabricated SUPER_ADMIN actor. Not controller-exposed.
async provisionStageTestPrincipal(env: NodeJS.ProcessEnv, sha: string, password: string) {
const grant = authorizeStageSession(env, sha);
const existing = await this.prisma.user.findFirst({where:{OR:[{id:STAGE_TEST_ID},{email:STAGE_TEST_EMAIL},{mobile:STAGE_TEST_MOBILE}]}});
if (existing) {
if (existing.id !== STAGE_TEST_ID || existing.email !== STAGE_TEST_EMAIL || existing.mobile !== STAGE_TEST_MOBILE || existing.stageTestSession !== grant.session || existing.role !== 'SUPER_ADMIN' || !existing.authExpiresAt || existing.authExpiresAt.getTime() <= Date.now() || !await bcrypt.compare(password, existing.password || '')) throw new Error('Refusing existing or unowned principal');
return existing; // Never renew TTL on retry.
}
if (password.length < 32) throw new Error('Server generated secret required');
return this.persistUser({firstName:'Stage',lastName:'CI independent reviewer',email:STAGE_TEST_EMAIL,mobile:STAGE_TEST_MOBILE,role:'SUPER_ADMIN',password}, {session:grant.session,expiresAt:new Date(Date.now()+grant.ttl*1000)});
}
async cleanupStageTestPrincipal(env: NodeJS.ProcessEnv, sha: string) {
const grant = authorizeStageSession(env, sha);
const existing = await this.prisma.user.findUnique({where:{id:STAGE_TEST_ID}});
if (!existing) return;
if (existing.email !== STAGE_TEST_EMAIL || existing.mobile !== STAGE_TEST_MOBILE || existing.stageTestSession !== grant.session || !existing.authExpiresAt) throw new Error('Refusing unowned principal cleanup');
// Revoke before relational cleanup; failed cleanup cannot leave valid tokens.
await this.prisma.user.update({where:{id:STAGE_TEST_ID},data:{authExpiresAt:new Date(0),password:''}});
return this.deleteUserRecords(STAGE_TEST_ID);
}
async updateUser(id: string, dto: UpdateUserDto, actor?: UserAdminActor) {
if (id === "12345678-1234-1234-1234-123456789012") {
throw new BadRequestException(
@@ -367,6 +395,10 @@ export class AdminService {
assertUserMutationAllowed(actor, user.role);
return this.deleteUserRecords(id);
}
private async deleteUserRecords(id: string) {
return this.prisma.$transaction(async (tx) => {
await tx.userAddress.deleteMany({ where: { userId: id } });
await tx.walletTransaction.deleteMany({ where: { userId: id } });