Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Last active February 28, 2016 03:22
Show Gist options
  • Save brennanMKE/9240141 to your computer and use it in GitHub Desktop.
Save brennanMKE/9240141 to your computer and use it in GitHub Desktop.
Securing User Data with Postgres
-- See Magnus Hagander: Secure Your Web Webapp passwords in PostgreSQL
-- YouTube: http://youtu.be/MuLMIQBB4Ew
-- Below the SECURITY DEFINER directive makes the query run as the user which
-- created the stored procedure which is the only user which has access to the
-- user table. The app which uses this database would connect with a user which
-- does not have access to this table, preventing queries from pulling all of
-- the data in the table.
CREATE OR REPLACE FUNCTION login(_userid text,
_pwd text, OUT _email text)
RETURNS text
LANGUAGE plpgsql
SECURITY DEFINER
AS $$ BEGIN
SELECT email INTO _email FROM users
WHERE users.userid=lower(_userid)
AND pwdhash = crypt(_pwd, users.pwdhash);
END;
$$
REVOKE ALL ON users FROM public;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment