113 lines
2.9 KiB
TypeScript
113 lines
2.9 KiB
TypeScript
import z from 'zod';
|
|
|
|
// Maybe load this from .env? 24 hours in seconds.
|
|
export const DEFAULT_TOKEN_LIFETIME = 86_400;
|
|
|
|
/**
|
|
* @openapi
|
|
* components:
|
|
* schemas:
|
|
* LoginRequestDTO:
|
|
* type: object
|
|
* required:
|
|
* - name
|
|
* - password
|
|
* properties:
|
|
* name:
|
|
* type: string
|
|
* default: mail@example.com or username
|
|
* password:
|
|
* type: string
|
|
* default: sha512(Hunter2)
|
|
* ttl:
|
|
* type: number
|
|
* default: 86400 # 24 hours * 60 minutes * 60 seconds
|
|
*/
|
|
const loginRequestSchemaBody = z.object({
|
|
name: z.string({
|
|
error: (e) => e.input === undefined ? 'Name is required' : 'Name must be a string'
|
|
})
|
|
.min( 3, 'Name is too short (try something longer than 3 characters)')
|
|
.max(64, 'Name is too long (try something shorter than 64 characters)'),
|
|
password: z.hash('sha512', {
|
|
error: (e) => e.input === undefined ? 'Password is required' : 'Password must be a SHA512 hash'
|
|
}),
|
|
ttl: z.number('TTL must be a number between 120 and 2 592 000 seconds')
|
|
.min( 120, 'TTL is too short (try something longer than 120 seconds)') // 120s
|
|
.max(2_592_000, 'TTL is too long (try something shorter than 30 days)') // 30d
|
|
.optional()
|
|
});
|
|
// export type LoginRequestBodyDTO = z.TypeOf<typeof loginRequestSchemaBody>;
|
|
|
|
export const loginRequestSchema = z.object({
|
|
body: loginRequestSchemaBody
|
|
});
|
|
export type LoginRequestDTO = z.TypeOf<typeof loginRequestSchema>;
|
|
|
|
/**
|
|
* @swagger
|
|
* components:
|
|
* schemas:
|
|
* UserInfoDTO:
|
|
* type: object
|
|
* required:
|
|
* - status
|
|
* - name
|
|
* - role
|
|
* - token
|
|
* properties:
|
|
* status:
|
|
* type: string
|
|
* default: ok on success, otherwise ErrorDTO with error
|
|
* name:
|
|
* type: string
|
|
* default: username
|
|
* role:
|
|
* type: number
|
|
* default: 0 # 0 - standard user, 1 - administrator
|
|
* token:
|
|
* type: string
|
|
* default: JWT
|
|
* ttl:
|
|
* type: number
|
|
* default: 86400 # 24 hours * 60 minutes * 60 seconds
|
|
*/
|
|
export type UserInfoDTO = {
|
|
status: 'ok';
|
|
name: string;
|
|
role: number;
|
|
token: string;
|
|
ttl: number | null;
|
|
};
|
|
|
|
/**
|
|
* @swagger
|
|
* components:
|
|
* schemas:
|
|
* ShortUserInfoDTO:
|
|
* type: object
|
|
* required:
|
|
* - status
|
|
* - id
|
|
* - name
|
|
* - role
|
|
* properties:
|
|
* status:
|
|
* type: string
|
|
* default: ok on success, otherwise ErrorDTO with error
|
|
* id:
|
|
* type: number
|
|
* name:
|
|
* type: string
|
|
* default: username
|
|
* role:
|
|
* type: number
|
|
* default: 0 # 0 - standard user, 1 - administrator
|
|
*/
|
|
export type ShortUserInfoDTO = {
|
|
status: 'ok';
|
|
id: number;
|
|
name: string;
|
|
role: number;
|
|
};
|