Skip to content

Commit

Permalink
Simplify reset_token logic and improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalmi committed Sep 21, 2021
1 parent 274f426 commit 98818d3
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions set_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,27 +377,30 @@ set_user(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(cstring_to_text("OK"));
}

if (prev_state->reset_token && !is_token)
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("reset token required but not provided")));
else if (prev_state->reset_token && is_token)
curr_state->reset_token = text_to_cstring(PG_GETARG_TEXT_PP(0));

/* Enforce token comparison if the reset_token is set */
if (prev_state->reset_token)
{
if (strcmp(old_state->reset_token, new_state->reset_token) != 0)
if (!is_token)
{
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("reset token required but not provided")));
}

pending_state->reset_token = text_to_cstring(PG_GETARG_TEXT_PP(0));
if (strcmp(prev_state->reset_token, pending_state->reset_token) != 0)
{
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("incorrect reset token provided")));
}
}

/* store old state as pending */
pending_state->userid = prev_state->userid;
pending_state->username = GETUSERNAMEFROMID(prev_state->userid);
pending_state->log_statement = prev_state->log_statement;
pending_state->log_prefix = prev_state->log_prefix;
pending_state->reset_token = NULL;
pending_state->is_superuser = superuser_arg(prev_state->userid);
}
else
Expand Down

0 comments on commit 98818d3

Please sign in to comment.