密码验证钩子
你们公司希望在默认密码实现的要求之外提高安全性,以满足安全或合规要求。你计划跟踪密码登录尝试的状态,并在必要时通过电子邮件或对登录进行限制来采取措施。
🌐 Your company wishes to increase security beyond the requirements of the default password implementation in order to fulfill security or compliance requirements. You plan to track the status of a password sign-in attempt and take action via an email or a restriction on sign-ins where necessary.
由于这个钩子会在未认证的请求上运行,恶意用户可能会通过多次调用它来滥用。使用这个钩子时要特别小心,否则可能会无意中阻止合法用户访问你的应用。
🌐 As this hook runs on unauthenticated requests, malicious users can abuse the hook by calling it multiple times. Pay extra care when using the hook as you can unintentionally block legitimate users from accessing your application.
在采取任何额外措施确保用户合法之前,先检查密码是否有效。如果可能的话,尽量发送电子邮件或通知,而不是直接屏蔽用户。
🌐 Check if a password is valid prior to taking any additional action to ensure the user is legitimate. Where possible, send an email or notification instead of blocking the user.
输入
| 字段 | 类型 | 描述 |
|---|---|---|
user_id | string | 尝试登录的用户的唯一标识。与 auth.users 表关联。 |
valid | boolean | 密码验证尝试是否有效。 |
{ "user_id": "3919cb6e-4215-4478-a960-6d3454326cec", "valid": true}输出
只有在你的钩子处理输入没有错误时才返回这些。
🌐 Return these only if your hook processed the input without errors.
| 字段 | 类型 | 描述 |
|---|---|---|
decision | string | 决定是否允许认证继续进行。使用 reject 拒绝验证尝试并注销用户所有活动会话。使用 continue 采用默认 Supabase Auth 行为。 |
message | string | 如果决策为 reject,向用户显示的消息。 |
should_logout_user | boolean | 如果发出 reject 决策,是否注销用户。发出 continue 决策时无效。 |
{ "decision": "reject", "message": "You have exceeded maximum number of password sign-in attempts.", "should_logout_user": "false"}作为公司新安全措施的一部分,用户每10秒只能输入一次错误的密码,不能超过这个频率。你想写一个钩子来强制执行这个规则。
🌐 As part of new security measures within the company, users can only input an incorrect password every 10 seconds and not more than that. You want to write a hook to enforce this.
创建一个表格来记录每个用户最后一次密码验证失败的尝试。
🌐 Create a table to record each user's last incorrect password verification attempt.
create table public.password_failed_verification_attempts ( user_id uuid not null, last_failed_at timestamp not null default now(), primary key (user_id));创建一个钩子来读取和写入这个表的信息。例如:
🌐 Create a hook to read and write information to this table. For example:
create function public.hook_password_verification_attempt(event jsonb)returns jsonblanguage plpgsqlas $$ declare last_failed_at timestamp; begin if event->'valid' is true then -- password is valid, accept it return jsonb_build_object('decision', 'continue'); end if; select last_failed_at into last_failed_at from public.password_failed_verification_attempts where user_id = event->'user_id'; if last_failed_at is not null and now() - last_failed_at < interval '10 seconds' then -- last attempt was done too quickly return jsonb_build_object( 'error', jsonb_build_object( 'http_code', 429, 'message', 'Please wait a moment before trying again.' ) ); end if; -- record this failed attempt insert into public.password_failed_verification_attempts ( user_id, last_failed_at ) values ( event->'user_id', now() ) on conflict do update set last_failed_at = now(); -- finally let Supabase Auth do the default behavior for a failed attempt return jsonb_build_object('decision', 'continue'); end;$$;-- Assign appropriate permissionsgrant all on table public.password_failed_verification_attempts to supabase_auth_admin;revoke all on table public.password_failed_verification_attempts from authenticated, anon, public;