Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Volatile functions for signup and change_password? #5

Open
richcorbs opened this issue Aug 24, 2019 · 12 comments
Open

Volatile functions for signup and change_password? #5

richcorbs opened this issue Aug 24, 2019 · 12 comments

Comments

@richcorbs
Copy link

This is an awesome approach. I love it and wish it were part of Hasura main.

When VOLATILE stored procedures are available (soon I hope) will you add support for signup and change_password functions?

@richcorbs
Copy link
Author

Wow, now I see the encrypt password trigger! Clever.

This is even more amazing than I thought. Thank you for this!

@sanderhahn
Copy link
Collaborator

sanderhahn commented Aug 24, 2019

Thank you for your kind words :) Of course its the flexible design of Hasura and PostgreSQL that makes it possible! Will see if i can update the code to support password change once volatile is supported. hasura/graphql-engine#1514

@sanderhahn sanderhahn reopened this Aug 24, 2019
@richcorbs
Copy link
Author

Is the "before save trigger" a workaround until volatile functions are available?

@sanderhahn
Copy link
Collaborator

Think that at the moment the limitation is: generating a random password on the server and returning that value while also storing it encrypted. This is also necessary to make something like access_tokens work, have not figured out how to do this properly.

Think that for both signup and password reset you will need some external service to present a form and send a mail with a verification token. Once you are already using an external service it might be more convenient to have that also perform these manipulations using direct sql connection or a special Hasura role.

@sanderhahn
Copy link
Collaborator

Signup and password reset flow could be something like this:

  • signup(email, password) -> sends verification_link with verification token in email
  • verify(verification_token) -> enables user on system
  • request_password_reset(email) -> sends password_reset_link with verification token in email
  • password_reset(verification_token, new_password) -> changes password of user on system
  • invite(email) -> sends a signup link in email

However think that it requires an email service and a website that hosts the signup form/change password form and verification actions.

@richcorbs
Copy link
Author

richcorbs commented Aug 25, 2019 via email

@sanderhahn
Copy link
Collaborator

At the moment its possible for users to update their own email/password using:

mutation {
  update_hasura_user(_set: {cleartext_password: "changed_password"}, where: {}) {
    returning {
      id
    }
  }
}

Tested this using the metadata from the Quickstart, that allows updating the email and the password for the currently active user.

User update permissions 2019-08-27 12-08-07

@cpursley
Copy link

cpursley commented Dec 18, 2019

As @richcorbs mentioned, this is an awesome approach.

Any thoughts on how to do account verification?

My immediate thought was on the user table, add two columns: verification_token and verified. The verification_token can be a uuid that gets generation on record creation and verified a boolean that by default is false.

Then have a verify_hasura_user Postgres function and corresponding Hasura mutation that looks up the user by the verification_token and then sets the verified column to true. Modify the hasura_auth function to fail if verified field has false value.

A url containing the GraphQL mutation along with the verification_token would be sent to the user to click in an email.

Thoughts?

@richcorbs
Copy link
Author

Seems legit to me. I use a similar method for password resets.

@cpursley
Copy link

@richcorbs ~ how did you get around the VOLATILE restriction?

@richcorbs
Copy link
Author

richcorbs commented Dec 18, 2019 via email

@cpursley
Copy link

cpursley commented Dec 19, 2019

I ended up with something like this:

create or replace function account_verify() returns trigger as $$
begin
    if old.verified is not true and
       new.verify_token is not null and
       new.verify_token = old.verification_token
       then
		new.verify_token = '';
		new.verified = true;
    end if;
	new.verify_token = '';
    return new;
end;
$$ language 'plpgsql';

create trigger account_verify_trigger
before update of verify_token on user_account
for each row execute procedure account_verify();

Where the hasura update account function (same as used for changing password) takes the verify_token param to match against the verification_token param via trigger. I'm also now checking if verified = true in the hasura_auth function to prevent unverified users from getting a jwt token.

Any ideas on how to beef this up ~ make it more secure / reliable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants