You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Example: Password check hook against bad password dictionary
SELECTpgtle.install_extension
(
'my_password_check_rules',
'1.0',
'Do not let users use the 10 most commonly used passwords',
$_pgtle_$
CREATESCHEMApassword_check;
REVOKE ALL ON SCHEMA password_check FROM PUBLIC;
GRANT USAGE ON SCHEMA password_check TO PUBLIC;
CREATETABLEpassword_check.bad_passwords (plaintext) ASVALUES
('123456'),
('password'),
('12345678'),
('qwerty'),
('123456789'),
('12345'),
('1234'),
('111111'),
('1234567'),
('dragon');
CREATEUNIQUE INDEXONpassword_check.bad_passwords (plaintext);
CREATEFUNCTIONpassword_check.passcheck_hook(username text, password text, password_type pgtle.password_types, valid_until timestamptz, valid_null boolean)
RETURNS void AS $$
let pws;
switch(password_type) {
case "PASSWORD_TYPE_MD5":
pws =plv8.execute(
"SELECT EXISTS(SELECT 1 FROM password_check.bad_passwords bp WHERE ('md5' || md5(bp.plaintext || $1)) = $2)",
[username, password]);
if (pws[0].exists) {
plv8.elog(ERROR, "password must not be found in a common password dictionary");
}
break;
case "PASSWORD_TYPE_PLAINTEXT":
pws =plv8.execute(
"SELECT EXISTS(SELECT 1 FROM password_check.bad_passwords bp WHERE bp.plaintext = $1)",
[password]);
if (pws[0].exists) {
plv8.elog(ERROR, "password must not be found in a common password dictionary");
}
break;
default: // for now just return if it is SCRAM.
plv8.elog(WARNING, "password check skipped. password type: "+ password_type);
}
$$ LANGUAGE plv8 SECURITY DEFINER;
GRANT EXECUTE ON FUNCTION password_check.passcheck_hook TO PUBLIC;
SELECTpgtle.register_feature('password_check.passcheck_hook', 'passcheck');
$_pgtle_$
);
CREATE EXTENSION my_password_check_rules;
ALTER SYSTEM SETpgtle.enable_password_check TO 'on';
SELECTpg_catalog.pg_reload_conf();
CREATE ROLE user_with_bad_password PASSWORD 'password';
SET password_encryption TO 'md5';
\password -- use "password"; this will fail
RESET password_encryption;
ALTER SYSTEM SETpgtle.enable_password_check TO 'off';
SELECTpg_catalog.pg_reload_conf();
DROP EXTENSION my_password_check_rules;
SELECTpgtle.uninstall_extension('my_password_check_rules');