From aea98511168006fbd007409f91245a775423e91c Mon Sep 17 00:00:00 2001 From: Chris Veilleux Date: Mon, 4 Feb 2019 15:04:22 -0600 Subject: [PATCH] moved the hook to add the cookies to the response into the base class to avoid re-coding it in every place it is needed. --- api/sso/sso_api/endpoints/authenticate_internal.py | 13 +------------ shared/selene/api/base_endpoint.py | 9 +++++++-- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/api/sso/sso_api/endpoints/authenticate_internal.py b/api/sso/sso_api/endpoints/authenticate_internal.py index 4596f329..a8fa9aa2 100644 --- a/api/sso/sso_api/endpoints/authenticate_internal.py +++ b/api/sso/sso_api/endpoints/authenticate_internal.py @@ -8,8 +8,6 @@ authentication, which uses a 3rd party authentication, like Google. from binascii import a2b_base64 from http import HTTPStatus -from flask import after_this_request - from selene.account import Account, AccountRepository, RefreshTokenRepository from selene.api import SeleneEndpoint from selene.util.auth import AuthenticationError @@ -30,21 +28,12 @@ class AuthenticateInternalEndpoint(SeleneEndpoint): self._authenticate_credentials() access_token, refresh_token = self._generate_tokens() self._add_refresh_token_to_db(refresh_token) - cookies = self._generate_token_cookies(access_token, refresh_token) + self._generate_token_cookies(access_token, refresh_token) except AuthenticationError as ae: - cookies = None self.response = (str(ae), HTTPStatus.UNAUTHORIZED) else: self._build_response() - @after_this_request - def set_cookies(response): - if cookies is not None: - access_token_cookie, refresh_token_cookie = cookies - response.set_cookie(**access_token_cookie) - response.set_cookie(**refresh_token_cookie) - return response - return self.response def _authenticate_credentials(self): diff --git a/shared/selene/api/base_endpoint.py b/shared/selene/api/base_endpoint.py index c382f078..ea6b5089 100644 --- a/shared/selene/api/base_endpoint.py +++ b/shared/selene/api/base_endpoint.py @@ -2,7 +2,7 @@ from http import HTTPStatus -from flask import request, current_app +from flask import after_this_request, current_app, request from flask_restful import Resource from selene.account import Account, AccountRepository, RefreshTokenRepository @@ -138,7 +138,12 @@ class SeleneEndpoint(Resource): httponly=True ) - return access_token_cookie, refresh_token_cookie + @after_this_request + def set_cookies(response): + response.set_cookie(**access_token_cookie) + response.set_cookie(**refresh_token_cookie) + + return response def _update_refresh_token_on_db(self, new_refresh_token): old_refresh_token = self.request.cookies['seleneRefresh']