/*
This file is part of »zeitbild«.

Copyright 2025 'kcf' <fenris@folksprak.org>

»zeitbild« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

»zeitbild« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with »zeitbild«. If not, see <http://www.gnu.org/licenses/>.
 */


namespace _zeitbild.service.auth_internal
{
	
	/**
	 */
	export function set(
		name : string,
		password : string
	) : Promise<void>
	{
		return (
			lib_plankton.bcrypt.compute(password)
			.then<boolean>(
				(password_image) => _zeitbild.repository.auth_internal.write(name, password_image)
			)
			.then<void>(
				() => Promise.resolve<void>(undefined)
			)
		);
	}
	
	
	/**
	 */
	export async function check_raw(
		password_image : string,
		password : string
	) : Promise<boolean>
	{
		return lib_plankton.bcrypt.compare(
			password_image,
			password
		);
	}
	
	
	/**
	 */
	export async function check(
		name : string,
		password : string
	) : Promise<boolean>
	{
		try {
			const password_image : string = await _zeitbild.repository.auth_internal.read(name);
			return check_raw(password_image, password);
		}
		catch (error) {
			return Promise.resolve<boolean>(false);
		}
	}
	
}
