import Column from "@entity-access/entity-access/dist/decorators/Column.js"; import Table from "@entity-access/entity-access/dist/decorators/Table.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import { RelateTo } from "@entity-access/entity-access/dist/decorators/Relate.js"; import type LoginSession from "./LoginSession.js"; import { IgnoreJsonProperty, JsonProperty } from "@entity-access/server-pages/dist/services/ModelService.js"; import type UserAuthFactor from "./UserAuthFactor.js"; import CheckConstraint from "@entity-access/entity-access/dist/decorators/CheckConstraint.js"; const verificationTypes = [ "email", "sms", "captcha", "totp", "sms-to-user"] as const; export type verificationType = typeof verificationTypes[number]; @Table("VerificationTokens") export default class VerificationToken { @Column({ dataType: "BigInt", key: true, generated: "identity"}) verificationID: number; @Column({ dataType: "Char", length: 200, nullable: true }) @IgnoreJsonProperty externalID: string; /** * This will be saved if userID exists for given email/phone if * verification was successful. */ @Column({ dataType: "BigInt", nullable: true }) userID: number; @Column({ default: () => Sql.date.now()}) dateCreated: DateTime; @Column({ default: () => Sql.date.addMinutes(Sql.date.now(), 15)}) expiry: DateTime; @Column({ dataType: "Char", length: 200, nullable: true}) email: string; @Column({ dataType: "Char", length: 20, nullable: true}) phone: string; @Column({ dataType: "Boolean", default: () => false}) verified: boolean; @Column({ dataType: "Char", length: 20}) @IgnoreJsonProperty code: string; @Column({ dataType: "Char", length: 50, default: () => Sql.cast.asText(Sql.crypto.randomUUID())}) accessCode: string; @Column({ dataType: "Char", length: 50, nullable: true }) @IgnoreJsonProperty ipAddress: string; @Column({ dataType: "Int", default: () => 0}) attempt: number; @Column({ dataType: "Char", nullable: true }) externalConfig: string; @Column({ dataType: "AsciiChar", length: 50, enum: verificationTypes }) type: verificationType; @Column({ dataType: "BigInt", nullable: true}) @IgnoreJsonProperty webSiteFolderID: number; addresses: VerificationTokenAddress[]; loginSessions: LoginSession[]; authFactors: UserAuthFactor[]; @JsonProperty({}) smsConfig: any; @JsonProperty({ help: "Verification address array as string[] containing addresses to verify for email and sms" }) verifyAddresses: string; @JsonProperty({ help: "Just a property to save code" }) verifyCode: string; @JsonProperty() captchaUrl: string; } @Table("VerificationTokenAddresses") @CheckConstraint({ name: "CC_VerificationToken_Address", filter: (x) => Sql.text.length(x.address) > 3 }) export class VerificationTokenAddress { @Column({ dataType: "BigInt", key: true }) @RelateTo(VerificationToken, { property: (x) => x.verificationToken, inverseProperty: (x) => x.addresses, foreignKeyConstraint:{ onDelete: "cascade" } }) verificationID: number; @Column({ dataType: "Char", length: 200 , key: true }) @IgnoreJsonProperty address: string; @Column({ dataType:"Char", length: 200, nullable: true}) displayAddress: string; verificationToken: VerificationToken; }