Last active
April 6, 2016 06:14
-
-
Save pminkov/e53d90c348f1dc47553408666431d2a2 to your computer and use it in GitHub Desktop.
This file contains 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
from datetime import datetime, timedelta | |
import cryptography | |
from cryptography.fernet import Fernet | |
class ExpiringTokenGenerator: | |
FERNET_KEY = 'H-gvBa31So7ZWRlIleY7q5xYPIytGnRHRcBpRbASyao=' | |
fernet = Fernet(FERNET_KEY) | |
DATE_FORMAT = '%Y-%m-%d %H-%M-%S' | |
EXPIRATION_DAYS = 3 | |
def _get_time(self): | |
"""Returns a string with the current UTC time""" | |
return datetime.utcnow().strftime(self.DATE_FORMAT) | |
def _parse_time(self, d): | |
"""Parses a string produced by _get_time and returns a datetime object""" | |
return datetime.strptime(d, self.DATE_FORMAT) | |
def generate_token(self, text): | |
"""Generates an encrypted token""" | |
full_text = text + '|' + self._get_time() | |
token = self.fernet.encrypt(bytes(full_text)) | |
return token | |
def get_token_value(self, token): | |
"""Gets a value from an encrypted token. | |
Returns None if the token is invalid or has expired. | |
""" | |
try: | |
value = self.fernet.decrypt(bytes(token)) | |
separator_pos = value.rfind('|') | |
text = value[: separator_pos] | |
token_time = self._parse_time(value[separator_pos + 1: ]) | |
if token_time + timedelta(self.EXPIRATION_DAYS) < datetime.utcnow(): | |
return None | |
except cryptography.fernet.InvalidToken: | |
return None | |
return text | |
def is_valid_token(self, token): | |
return self.get_token_value(token) != None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment