Skip to content
This repository has been archived by the owner on Jan 2, 2020. It is now read-only.

Commit

Permalink
[#935] Initiates user session in Pixelated after successful bonafide …
Browse files Browse the repository at this point in the history
…authentication
  • Loading branch information
Sriram Viswanathan committed Apr 13, 2017
1 parent 3e0d2b5 commit 60f9cfb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion service/pixelated/account_recovery_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _auth_error(self):
def _bonafide_auth(self, credentials):
srp_provider = Api(self._leap_provider.api_uri)
self.bonafide_session = Session(credentials, srp_provider, self._leap_provider.local_ca_crt)
yield self.bonafide_session.authenticate_with_recovery_code()
yield self.bonafide_session.authenticate(recovery=True)
returnValue(Authentication(credentials.username,
self.bonafide_session.token,
self.bonafide_session.uuid,
Expand Down
40 changes: 37 additions & 3 deletions service/pixelated/resources/account_recovery_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
from twisted.logger import Logger
from twisted.cred.error import UnauthorizedLogin

from pixelated.resources import BaseResource
from pixelated.resources import BaseResource, IPixelatedSession
from pixelated.resources import get_public_static_folder
from pixelated.account_recovery_authenticator import AccountRecoveryAuthenticator
from pixelated.config.leap import BootstrapUserServices

log = Logger()

Expand All @@ -40,6 +41,15 @@ class EmptyFieldsError(Exception):
pass


def parse_accept_language(all_headers):
accepted_languages = ['pt-BR', 'en-US']
languages = all_headers.get('accept-language', '').split(';')[0]
for language in accepted_languages:
if language in languages:
return language
return 'pt-BR'


class AccountRecoveryPage(Element):
loader = XMLFile(FilePath(os.path.join(get_public_static_folder(), 'account_recovery.html')))

Expand All @@ -54,6 +64,7 @@ class AccountRecoveryResource(BaseResource):
def __init__(self, services_factory, provider):
BaseResource.__init__(self, services_factory)
self._authenticator = AccountRecoveryAuthenticator(provider)
self._bootstrap_user_services = BootstrapUserServices(services_factory, provider)

def render_GET(self, request):
request.setResponseCode(OK)
Expand All @@ -64,9 +75,10 @@ def _render_template(self, request):
return renderElement(request, site)

def render_POST(self, request):
def success_response(response):
def success_response(auth_result):
request.setResponseCode(OK)
request.finish()
self._complete_bootstrap(auth_result, request)

def error_response(failure):
log.warn(failure)
Expand All @@ -82,6 +94,27 @@ def error_response(failure):
d.addCallbacks(success_response, error_response)
return NOT_DONE_YET

def _complete_bootstrap(self, auth_result, request):
user_auth = auth_result['user_auth']
user_code = auth_result['user_code'].encode("utf-8")
log.info('**** user code ****')
log.info(user_code)

def login_error(error, session):
log.error('Login error during %s services setup: %s \n %s' % (user_auth.username, error.getErrorMessage(), error.getTraceback()))
session.login_error()

def login_successful(_, session):
session.login_successful(user_auth.uuid)

language = parse_accept_language(request.getAllHeaders())
session = IPixelatedSession(request.getSession())
session.login_started()

d = self._bootstrap_user_services.setup(user_auth, user_code, language)
d.addCallback(login_successful, session)
d.addErrback(login_error, session)

def _get_post_form(self, request):
return json.loads(request.content.getvalue())

Expand All @@ -105,4 +138,5 @@ def _handle_post(self, request):
self._validate_password(password, confirm_password)

user_auth = yield self._authenticator.authenticate(username, user_code)
defer.returnValue(user_auth)
auth_result = {'user_auth': user_auth, 'user_code': user_code}
defer.returnValue(auth_result)

0 comments on commit 60f9cfb

Please sign in to comment.