-
Notifications
You must be signed in to change notification settings - Fork 11
Verify Your Account Today! #435
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3156655
Added in initial version of gc email regex
d3f4664
added pool_pre_ping to db create engine
521ac0e
added default argument for exp time, and started work on email upon a…
9cd323f
Implemented first iteration of verification emails
3f183dc
Added email_validated field to Users table
f317198
Create email verification mutation
328d289
Fix email sending email exception raising
60dcd75
add error check to see if user does exist
30ef1fa
work on send_verification_email.py and its tests
f44776e
Fixed checks in send_verification_email.py
6968398
Moved sandbox creation to when user verifies their account
bb7f97f
Cleaned up imports
b657998
Created new hybrid method in Users.py that verifies the user's account
a4b0c40
Updated all tests with new verify account which in turn creates the u…
3cd7664
increase wait time
c2c904a
changed up user create, to create the user then attempt to send verif…
d37508c
Add tests for successful, and temporary fail
59eac87
Corrected verify account function to actually set field to true, and …
bca7e3c
Implemented new mutation for sending verification email, if failed in…
1e98919
Tests for new mutation
60c2275
Black ran on files
260be42
Added api/api.current.graphql to gitignore, and updated description f…
cdd647b
Updated schema.faker.graphql, and added new email_verified field to u…
e62e077
remove pre_pool_ping, and wait for new branch
18e6744
Moved NotificationsAPI client to be a function argument
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| platform/**/*.json | ||
| frontend/public | ||
| api/schema.json | ||
| api/api.current.graphql | ||
|
|
||
| **/node_modules | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import time | ||
|
|
||
| from flask import request | ||
| from graphql import GraphQLError | ||
| from notifications_python_client.notifications import NotificationsAPIClient | ||
| from requests import HTTPError | ||
|
|
||
| from json_web_token import tokenize | ||
| from models import Users | ||
|
|
||
|
|
||
| def send_verification_email(user: Users, client: NotificationsAPIClient): | ||
| """ | ||
| This function allows a user object to be passed in during account creation | ||
| and send an email to be used for verifying accounts | ||
| :param user: A instance of /models/User.py | ||
| :param client: An instance of NotificationsAPIClient | ||
| :return: None | ||
| """ | ||
| # Create Notify Client | ||
| notify_client = client | ||
|
|
||
| # Check to see if users preferred lang is English or French | ||
| if user.preferred_lang == "french": | ||
| email_template_id = "f2c9b64a-c754-4ffd-93e9-33fdb0b5ae0b" | ||
| else: | ||
| email_template_id = "6e3368a7-0d75-47b1-b4b2-878234e554c9" | ||
|
|
||
| # URL Generation | ||
| token = tokenize(user_id=user.user_name, exp_period=24) | ||
| url = str(request.url_root) + "validate/" + str(token) | ||
|
|
||
| # Send Email | ||
| try: | ||
| response = notify_client.send_email_notification( | ||
| email_address=user.user_name, | ||
| template_id=email_template_id, | ||
| personalisation={"user": "", "verify_email_url": url}, | ||
| ) | ||
|
|
||
| except HTTPError: | ||
| raise GraphQLError( | ||
| "Error, when sending verification email, error: {}".format(HTTPError) | ||
| ) | ||
|
|
||
| # Sleep to wait and see if email was successful | ||
| time.sleep(1.5) | ||
| email_status = notify_client.get_notification_by_id(response.get("id")).get( | ||
| "status" | ||
| ) | ||
|
|
||
| if ( | ||
| email_status == "permanent-failure" | ||
| or email_status == "temporary-failure" | ||
| or email_status == "technical-failure" | ||
| ): | ||
| return "Email Send Error: {}".format(email_status) | ||
| else: | ||
|
sleepycat marked this conversation as resolved.
Outdated
|
||
| return email_status | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """empty message | ||
|
|
||
| Revision ID: 2a25ac8483bc | ||
| Revises: c264f0905a36 | ||
| Create Date: 2020-05-20 13:38:34.078690 | ||
|
|
||
| """ | ||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "2a25ac8483bc" | ||
| down_revision = "c264f0905a36" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.add_column("users", sa.Column("email_validated", sa.Boolean(), nullable=True)) | ||
| # ### end Alembic commands ### | ||
|
|
||
|
|
||
| def downgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.drop_column("users", "email_validated") | ||
| # ### end Alembic commands ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import graphene | ||
| import jwt | ||
| import os | ||
|
|
||
| from graphql import GraphQLError | ||
|
|
||
| from app import app | ||
| from db import db_session | ||
| from functions.input_validators import cleanse_input | ||
| from models.Users import Users | ||
|
|
||
|
|
||
| class EmailVerifyAccount(graphene.Mutation): | ||
| """ | ||
| Mutation that allows the user to verify their account through a token | ||
| sent in an email | ||
| """ | ||
| class Arguments: | ||
| token_string = graphene.String( | ||
| description="Token in sent via email, and located in url", required=True | ||
| ) | ||
|
|
||
| status = graphene.Boolean( | ||
| description="Informs user if account was successfully verified." | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def mutate(self, info, **kwargs): | ||
| token_string = cleanse_input(kwargs.get("token_string")) | ||
|
|
||
| try: | ||
| payload = jwt.decode( | ||
| token_string, os.getenv("SUPER_SECRET_SALT"), algorithms=["HS256"] | ||
| ) | ||
| except jwt.ExpiredSignatureError: | ||
| raise GraphQLError("Signature expired, please login again") | ||
| except jwt.InvalidTokenError: | ||
| raise GraphQLError("Invalid token, please login again") | ||
| user_name = payload.get("user_id") | ||
|
|
||
| with app.app_context(): | ||
| # Check to see if user exists | ||
| user = db_session.query(Users).filter(Users.user_name == user_name).first() | ||
|
|
||
| if not user: | ||
| raise GraphQLError("Error, User does not exist") | ||
|
|
||
| user.verify_account() | ||
|
|
||
| try: | ||
| db_session.commit() | ||
| except Exception as e: | ||
| db_session.rollback() | ||
| db_session.flush() | ||
| raise GraphQLError("Error, unable to verify account.") | ||
|
|
||
| return EmailVerifyAccount(status=True) |
Empty file.
57 changes: 57 additions & 0 deletions
57
api/schemas/send_email_verification/send_email_verification.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import graphene | ||
| import os | ||
|
|
||
| from graphql import GraphQLError | ||
| from notifications_python_client.notifications import NotificationsAPIClient | ||
|
|
||
| from db import db_session | ||
| from functions.input_validators import cleanse_input | ||
| from functions.verification_email import send_verification_email | ||
| from models.Users import Users | ||
| from scalars.email_address import EmailAddress | ||
|
|
||
|
|
||
| class SendEmailVerification(graphene.Mutation): | ||
| """ | ||
| This mutation is used for re-sending a verification email if it failed | ||
| during user creation | ||
| """ | ||
|
|
||
| class Arguments: | ||
| user_name = EmailAddress( | ||
| description="The users email address used for sending email", required=True, | ||
| ) | ||
|
|
||
| status = graphene.Boolean( | ||
| description="If email is successfully sent status will be true" | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def mutate(self, info, **kwargs): | ||
| # Get information from mutation arguments | ||
| user_name = cleanse_input(kwargs.get("user_name")) | ||
|
|
||
| # Find user | ||
| user = db_session.query(Users).filter(Users.user_name == user_name).first() | ||
|
|
||
| # Check to see if user is found, or if they are already validated | ||
| if user is None: | ||
| raise GraphQLError("Error, cannot find user.") | ||
| elif user.email_validated: | ||
| raise GraphQLError("Error, user is already validated.") | ||
|
|
||
| # Send validation email | ||
| email_status = send_verification_email( | ||
| user=user, | ||
| client=NotificationsAPIClient( | ||
| api_key=os.getenv("NOTIFICATION_API_KEY"), | ||
| base_url=os.getenv("NOTIFICATION_API_URL"), | ||
| ) | ||
| ) | ||
|
|
||
| if email_status.__contains__("Email Send Error"): | ||
| raise GraphQLError( | ||
| "Error, when sending verification email, please try again." | ||
| ) | ||
|
|
||
| return SendEmailVerification(status=True) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.