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
| from .. import db | |
| import datetime | |
| class BlacklistToken(db.Model): | |
| """ | |
| Token Model for storing JWT tokens | |
| """ | |
| __tablename__ = 'blacklist_tokens' |
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
| def encode_auth_token(self, user_id): | |
| """ | |
| Generates the Auth Token | |
| :return: string | |
| """ | |
| try: | |
| payload = { | |
| 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1, seconds=5), | |
| 'iat': datetime.datetime.utcnow(), | |
| 'sub': user_id |
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
| @staticmethod | |
| def decode_auth_token(auth_token): | |
| """ | |
| Decodes the auth token | |
| :param auth_token: | |
| :return: integer|string | |
| """ | |
| try: | |
| payload = jwt.decode(auth_token, key) | |
| is_blacklisted_token = BlacklistToken.check_blacklist(auth_token) |
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
| import unittest | |
| import datetime | |
| from app.main import db | |
| from app.main.model.user import User | |
| from app.test.base import BaseTestCase | |
| class TestUserModel(BaseTestCase): |
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
| class AuthDto: | |
| api = Namespace('auth', description='authentication related operations') | |
| user_auth = api.model('auth_details', { | |
| 'email': fields.String(required=True, description='The email address'), | |
| 'password': fields.String(required=True, description='The user password '), | |
| }) |
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
| from app.main import db | |
| from app.main.model.blacklist import BlacklistToken | |
| def save_token(token): | |
| blacklist_token = BlacklistToken(token=token) | |
| try: | |
| # insert the token | |
| db.session.add(blacklist_token) | |
| db.session.commit() |
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
| from flask import request | |
| from flask_restplus import Resource | |
| from app.main.service.auth_helper import Auth | |
| from ..util.dto import AuthDto | |
| api = AuthDto.api | |
| user_auth = AuthDto.user_auth | |
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
| from app.main.model.user import User | |
| from ..service.blacklist_service import save_token | |
| class Auth: | |
| @staticmethod | |
| def login_user(data): | |
| try: | |
| # fetch the user data |
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
| def generate_token(user): | |
| try: | |
| # generate the auth token | |
| auth_token = user.encode_auth_token(user.id) | |
| response_object = { | |
| 'status': 'success', | |
| 'message': 'Successfully registered.', | |
| 'Authorization': auth_token.decode() | |
| } | |
| return response_object, 201 |
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
| import unittest | |
| import json | |
| from app.test.base import BaseTestCase | |
| def register_user(self): | |
| return self.client.post( | |
| '/user/', | |
| data=json.dumps(dict( | |
| email='example@gmail.com', |