Skip to content

Commit

Permalink
Fix new login, logout method
Browse files Browse the repository at this point in the history
  • Loading branch information
jssuzanne committed Feb 6, 2018
1 parent a3c3dd3 commit 985e55a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 58 deletions.
33 changes: 33 additions & 0 deletions anyblok_pyramid/bloks/auth/views.py
@@ -0,0 +1,33 @@
# This file is a part of the AnyBlok / Pyramid project
#
# Copyright (C) 2018 Jean-Sebastien SUZANNE <jssuzanne@anybox.fr>
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file,You can
# obtain one at http://mozilla.org/MPL/2.0/.
from pyramid.httpexceptions import HTTPUnauthorized, HTTPFound
from pyramid.security import remember, forget
from logging import getLogger

logger = getLogger(__name__)


def login(request):
User = request.anyblok.registry.User
params = User.format_login_params(request)
if User.check_login(**params):
login = params['login']
headers = remember(request, login)
logger.info('%s is logged in', login)
location = User.get_login_location_to(login, request)
return HTTPFound(location=location, headers=headers)

return HTTPUnauthorized(comment='Login failed')


def logout(request):
logger.info('%r is logged out', request.authenticated_userid)
headers = forget(request)
User = request.anyblok.registry.User
location = User.get_logout_location_to(request)
return HTTPFound(location=location, headers=headers)
7 changes: 0 additions & 7 deletions anyblok_pyramid/bloks/auth/views/__init__.py

This file was deleted.

41 changes: 0 additions & 41 deletions anyblok_pyramid/bloks/auth/views/auth.py

This file was deleted.

8 changes: 0 additions & 8 deletions anyblok_pyramid/bloks/password/tests/test_credential.py
Expand Up @@ -27,11 +27,3 @@ def test_check_login_ok(self):
def test_check_login_ko(self):
with self.assertRaises(HTTPUnauthorized):
self.User.check_login(login="user.1", password="P2")

def test_view_login_ok(self):
self.webserver.post_json(
'/login', dict(login="user.1", password="P1"), status=302)

def test_view_login_ko(self):
self.webserver.post_json(
'/login', dict(login="user.1", password="P2"), status=401)
4 changes: 2 additions & 2 deletions anyblok_pyramid/test_bloks/test2/__init__.py
Expand Up @@ -31,7 +31,7 @@ def pyramid_load_config(cls, config):
config.add_route('blok', '/blok/{name}',
factory=AnyBlokResourceFactory('Model.System.Blok'))
config.add_route('login', '/login', request_method='POST')
config.add_view(view=login, name='login', renderer="JSON")
config.add_view(view=login, route_name='login', renderer="JSON")
config.add_route('logout', '/logout', request_method='POST')
config.add_view(view=logout, name='logout')
config.add_view(view=logout, route_name='logout')
config.scan(cls.__module__ + '.views')

0 comments on commit 985e55a

Please sign in to comment.