Skip to content

Add code expiry settings for validate_email #203

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Delete partial token from session if still present
- Use `userPrincipalName` to set `username` and `email` accordingly
- Send authorization headers to Kakao OAuth2, properly fill user details
- Add settings for controlling email validation code expiry

## [1.7.0](https://github.com/python-social-auth/social-core/releases/tag/1.7.0) - 2018-02-20

Expand Down
1 change: 1 addition & 0 deletions social_core/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class CodeMixin(object):
email = ''
code = ''
verified = False
timestamp = None

def verify(self):
self.verified = True
Expand Down
11 changes: 10 additions & 1 deletion social_core/strategy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import time
import random
import hashlib
from datetime import datetime, timedelta

from .utils import setting_name, module_member, PARTIAL_TOKEN_SESSION_NAME
from .store import OpenIdStore, OpenIdSessionWrapper
Expand Down Expand Up @@ -136,11 +137,19 @@ def send_email_validation(self, backend, email, partial_token=None):

def validate_email(self, email, code):
verification_code = self.storage.code.get_code(code)
allow_reuse = self.setting('EMAIL_VALIDATION_ALLOW_REUSE')
expiry = self.setting('EMAIL_VALIDATION_EXPIRED_THRESHOLD')
expired_code = expiry and verification_code.timestamp and \
((verification_code.timestamp + \
timedelta(seconds=expiry)).utctimetuple() < \
datetime.utcnow().utctimetuple())
if not verification_code or verification_code.code != code:
return False
elif verification_code.email != email:
return False
elif verification_code.verified:
elif expired_code:
return False
elif verification_code.verified and not allow_reuse:
return False
else:
verification_code.verify()
Expand Down