Skip to content

Commit

Permalink
Use set_config() to set 'uniphant.user_id' to the authenticated user
Browse files Browse the repository at this point in the history
  • Loading branch information
joelonsql committed Jan 4, 2021
1 parent 41d23f1 commit da12465
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
17 changes: 17 additions & 0 deletions FUNCTIONS/auth.sql
Expand Up @@ -6,7 +6,24 @@ SET search_path TO public, pg_temp
AS $$
DECLARE
_resource_id integer;
_user_id bigint;
BEGIN

SELECT access_tokens.user_id
INTO _user_id
FROM access_tokens
WHERE access_tokens.access_token = NULLIF(current_setting('request.cookie.access_token', TRUE),'')::uuid
AND (access_tokens.expire_at > now()) IS NOT FALSE;
IF NOT FOUND THEN
_user_id := 1; -- anonymous
END IF;

IF set_config('uniphant.user_id',_user_id::text,TRUE) = _user_id::text THEN
-- Assert OK
ELSE
RAISE EXCEPTION 'Bug! set_config() did not return the value';
END IF;

SELECT resource_id
INTO _resource_id
FROM resources
Expand Down
8 changes: 1 addition & 7 deletions FUNCTIONS/user_id.sql
Expand Up @@ -5,11 +5,5 @@ LANGUAGE sql
SECURITY DEFINER
SET search_path TO public, pg_temp
AS $$
SELECT COALESCE(
(SELECT access_tokens.user_id
FROM access_tokens
WHERE access_tokens.access_token = NULLIF(current_setting('request.cookie.access_token', TRUE),'')::uuid
AND (access_tokens.expire_at > now()) IS NOT FALSE),
1 -- anonymous
)
SELECT current_setting('uniphant.user_id',FALSE)::bigint
$$;
6 changes: 3 additions & 3 deletions demo/index.html
Expand Up @@ -392,12 +392,12 @@ <h1 class="pre">🦄🐘uniphant</h1>
}

function generate_key_from_challenge() {
if (document.getElementById("challenge").value === "") {
let challenge = document.getElementById("challenge");
if (challenge.value === "") {
document.getElementById("log").innerHTML += "please enter the challenge\n";
document.getElementById("challenge").focus();
challenge.focus();
return;
}
let challenge = document.getElementById("challenge");
get_credential_creation_options(challenge.value);
challenge.value = "";
refresh();
Expand Down

0 comments on commit da12465

Please sign in to comment.