forked from MrMenezes/work-at-olist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changing where auth functions are, and creating another namespace for it
-Changing where auth functions are, and creating another namespace for it -Creating a standard dto model for responses (status and message) and pagination
- Loading branch information
1 parent
c9ec10e
commit d19875a
Showing
9 changed files
with
191 additions
and
169 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from flask import current_app | ||
from flask_restplus import Resource | ||
from flask_restplus import reqparse | ||
from flask_jwt_extended import jwt_refresh_token_required, get_raw_jwt | ||
|
||
from call_records.controller import user_required | ||
from call_records.dto.auth import AuthDto | ||
from call_records.service.auth import login_user, get_refresh_token, logout_user | ||
|
||
ns = AuthDto.ns | ||
authLogInDtoModel = AuthDto.authLogIn | ||
authRefreshDtoModel = AuthDto.authRefresh | ||
authResponsesDtoModel = AuthDto.authResponses | ||
|
||
|
||
def get_login_parser(): | ||
parser = reqparse.RequestParser() | ||
parser.add_argument('username', type=str, required=True) | ||
parser.add_argument('password', type=str, required=True) | ||
return parser | ||
|
||
@ns.route('/login') | ||
class UserLogin(Resource): | ||
@ns.expect(get_login_parser(), validate=True) | ||
@ns.marshal_with(authLogInDtoModel, skip_none=True) | ||
@ns.doc(responses={ | ||
200: 'Success', | ||
401: 'Username or password does not match' | ||
}) | ||
@ns.doc(security=[]) | ||
def post(self): | ||
"""To Login in""" | ||
parser = get_login_parser() | ||
data = parser.parse_args() | ||
|
||
return login_user(data=data) | ||
|
||
@ns.route('/refresh') | ||
class UserLoginRefresh(Resource): | ||
@user_required | ||
@ns.marshal_with(authRefreshDtoModel, skip_none=True) | ||
#@jwt_refresh_token_required | ||
def post(self): | ||
"""To get a refresh token""" | ||
return get_refresh_token() | ||
|
||
@ns.route('/logout') | ||
class UserLogout(Resource): | ||
@user_required | ||
@ns.marshal_with(authResponsesDtoModel, skip_none=True) | ||
@ns.doc(responses={ | ||
200: 'Logout Successfully' | ||
}) | ||
def post(self): | ||
"""To logout an user""" | ||
return logout_user() |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from flask_restplus import fields | ||
from flask_restplus import Model | ||
from flask import current_app | ||
|
||
standardResponseDtoModel = Model('standardResponse', { | ||
'status': fields.String(required=True, enum=('success', 'fail'), description='Status of operation'), | ||
'message': fields.String(required=True, description='Message describing the success of the operation or the reason for an error') | ||
}) | ||
|
||
standardPaginationDtoModel = Model('standardPagination', { | ||
'start': fields.Integer(required=True, description='It is the position from which we want the data to be returned'), | ||
'limit': fields.Integer(required=True, description='It is the max number of items to return from that position'), | ||
'next': fields.String(required=False, description='It is the url for the next page of the query assuming current value of limit'), | ||
'previous': fields.String(required=False, description='It is the url for the previous page of the query assuming current value of limit') | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from flask_restplus import Namespace, fields | ||
from call_records.dto import standardResponseDtoModel | ||
|
||
|
||
class AuthDto: | ||
ns = Namespace('auth', description='Auth') | ||
authLogIn = ns.clone('authLogIn', standardResponseDtoModel, { | ||
'access_token': fields.String(required=False, description='Token used to access protected resources', example='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NDQ3MjE2MDAsIm5iZiI6MTU0NDcyMTYwMCwianRpIjoiZjQ3NTBmMjEtMWExMS00YWExLWI2YTYtMz.argP9jSHhZ7xWSzFAf1hWHIq6MjpoZb_hTDzUl5uGUc') | ||
}) | ||
authRefresh = ns.model('authRefresh', { | ||
'status': fields.String(required=True, enum=('success', 'fail'), description='Status of refresh token operation'), | ||
'access_token': fields.String(required=False, description='Token used to access protected resources', example='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NDQ3MjE2MDAsIm5iZiI6MTU0NDcyMTYwMCwianRpIjoiZjQ3NTBmMjEtMWExMS00YWExLWI2YTYtMz.argP9jSHhZ7xWSzFAf1hWHIq6MjpoZb_hTDzUl5uGUc') | ||
}) | ||
authResponses = ns.clone('authResponses', standardResponseDtoModel, {}) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
def login_user(data): | ||
from call_records.model.user import User | ||
from call_records.service.tokenblacklist import add_token_to_database | ||
from flask import current_app | ||
from flask_jwt_extended import ( | ||
JWTManager, jwt_required, create_access_token, | ||
get_jwt_identity | ||
) | ||
|
||
try: | ||
user = User.query.filter_by(username=data.get('username')).first() | ||
if user and user.verify_password(password=data.get('password')): | ||
access_token = create_access_token(identity=user) | ||
add_token_to_database(access_token, current_app.config['JWT_IDENTITY_CLAIM']) | ||
response_object = { | ||
'status': 'success', | ||
'message': 'Successfully logged in', | ||
'access_token': access_token | ||
} | ||
return response_object, 200 | ||
else: | ||
response_object = { | ||
'status': 'fail', | ||
'message': 'Username or password does not match.' | ||
} | ||
return response_object, 401 | ||
except Exception as e: | ||
current_app.logger.error('login_user %s', e) | ||
response_object = { | ||
'status': 'fail', | ||
'message': 'Try again' | ||
} | ||
return response_object, 500 | ||
|
||
def get_refresh_token(): | ||
from flask_jwt_extended import create_access_token, get_raw_jwt | ||
from flask import current_app | ||
from call_records.model.user import User | ||
from call_records.service.tokenblacklist import add_token_to_database, revoke_token | ||
|
||
try: | ||
#Revoke Current Token | ||
current_claims = get_raw_jwt() | ||
revoke_token(current_claims.get('jti'), current_claims.get('identity')) | ||
current_user = current_claims.get('identity') | ||
user = User.query.filter_by(username=current_user).first() | ||
if user: | ||
new_access_token = create_access_token(identity=user) | ||
add_token_to_database(new_access_token, current_app.config['JWT_IDENTITY_CLAIM']) | ||
response_object = { | ||
'status': 'success', | ||
'access_token': new_access_token | ||
} | ||
return response_object, 200 | ||
else: | ||
response_object = { | ||
'status': 'fail', | ||
'message': 'User does not match.' | ||
} | ||
return response_object, 401 | ||
except Exception as e: | ||
current_app.logger.error('get_refresh_token %s', e) | ||
response_object = { | ||
'status': 'fail', | ||
'message': 'Try again' | ||
} | ||
return response_object, 500 | ||
|
||
def logout_user(): | ||
from call_records.service.tokenblacklist import revoke_token_user | ||
from flask import current_app | ||
from flask_jwt_extended import get_raw_jwt | ||
try: | ||
#Revoke Current Token | ||
current_claims = get_raw_jwt() | ||
revoke_token_user(current_claims.get('identity')) | ||
response_object = { | ||
'status': 'success', | ||
'message': 'Logout Successfully' | ||
} | ||
return response_object, 200 | ||
except Exception as e: | ||
current_app.logger.error('logout_user %s', e) | ||
response_object = { | ||
'status': 'fail', | ||
'message': 'Try again' | ||
} | ||
return response_object, 500 |
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
Oops, something went wrong.