142 lines
3.4 KiB
TypeScript
142 lines
3.4 KiB
TypeScript
import { hashPassword, verifyPassword } from "../utils/crypto"
|
|
|
|
interface Credentials {
|
|
username: string
|
|
password: string
|
|
}
|
|
|
|
/**
|
|
* Authentication service for managing user credentials
|
|
*/
|
|
export class AuthService {
|
|
// Default credentials
|
|
private static readonly DEFAULT_ADMIN: Credentials = {
|
|
username: "admin",
|
|
password: "K123456",
|
|
}
|
|
|
|
private static readonly DEFAULT_SUPERADMIN: Credentials = {
|
|
username: "superadmin",
|
|
password: "key123456",
|
|
}
|
|
|
|
// In-memory storage for credentials (simulating file storage)
|
|
private static adminCredentials: Credentials | null = null
|
|
private static superadminCredentials: Credentials | null = null
|
|
|
|
/**
|
|
* Initialize credentials
|
|
*/
|
|
static initialize(): void {
|
|
if (!this.adminCredentials) {
|
|
this.adminCredentials = {
|
|
username: this.DEFAULT_ADMIN.username,
|
|
password: hashPassword(this.DEFAULT_ADMIN.password),
|
|
}
|
|
}
|
|
|
|
if (!this.superadminCredentials) {
|
|
this.superadminCredentials = {
|
|
username: this.DEFAULT_SUPERADMIN.username,
|
|
password: hashPassword(this.DEFAULT_SUPERADMIN.password),
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Verify admin credentials
|
|
*/
|
|
static async verifyAdminCredentials(username: string, password: string): Promise<boolean> {
|
|
this.initialize()
|
|
|
|
if (!this.adminCredentials) {
|
|
return false
|
|
}
|
|
|
|
return username === this.adminCredentials.username && verifyPassword(password, this.adminCredentials.password)
|
|
}
|
|
|
|
/**
|
|
* Verify super admin credentials
|
|
*/
|
|
static async verifySuperAdminCredentials(username: string, password: string): Promise<boolean> {
|
|
this.initialize()
|
|
|
|
if (!this.superadminCredentials) {
|
|
return false
|
|
}
|
|
|
|
return (
|
|
username === this.superadminCredentials.username && verifyPassword(password, this.superadminCredentials.password)
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Update admin credentials
|
|
*/
|
|
static async updateAdminCredentials(
|
|
currentUsername: string,
|
|
currentPassword: string,
|
|
newUsername: string,
|
|
newPassword: string,
|
|
): Promise<boolean> {
|
|
// Verify current credentials first
|
|
const isValid = await this.verifyAdminCredentials(currentUsername, currentPassword)
|
|
if (!isValid) {
|
|
return false
|
|
}
|
|
|
|
// Update credentials
|
|
this.adminCredentials = {
|
|
username: newUsername,
|
|
password: hashPassword(newPassword),
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* Update super admin credentials
|
|
*/
|
|
static async updateSuperAdminCredentials(
|
|
currentUsername: string,
|
|
currentPassword: string,
|
|
newUsername: string,
|
|
newPassword: string,
|
|
): Promise<boolean> {
|
|
// Verify current credentials first
|
|
const isValid = await this.verifySuperAdminCredentials(currentUsername, currentPassword)
|
|
if (!isValid) {
|
|
return false
|
|
}
|
|
|
|
// Update credentials
|
|
this.superadminCredentials = {
|
|
username: newUsername,
|
|
password: hashPassword(newPassword),
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* Get current admin credentials (for display purposes)
|
|
*/
|
|
static getCurrentAdminCredentials(): { username: string } {
|
|
this.initialize()
|
|
return {
|
|
username: this.adminCredentials?.username || this.DEFAULT_ADMIN.username,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get current super admin credentials (for display purposes)
|
|
*/
|
|
static getCurrentSuperAdminCredentials(): { username: string } {
|
|
this.initialize()
|
|
return {
|
|
username: this.superadminCredentials?.username || this.DEFAULT_SUPERADMIN.username,
|
|
}
|
|
}
|
|
}
|