From d6e543bc01d2f1aa3bb29f005171911f6f09da02 Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Mon, 15 Apr 2019 19:14:42 -0700 Subject: Move SessionAuthenticationHelper to pyramid.authentication. --- src/pyramid/authentication.py | 31 +++++++++++++++++++++++++++ src/pyramid/security.py | 31 --------------------------- tests/test_authentication.py | 50 +++++++++++++++++++++++++++++++++++++++++++ tests/test_security.py | 50 ------------------------------------------- 4 files changed, 81 insertions(+), 81 deletions(-) diff --git a/src/pyramid/authentication.py b/src/pyramid/authentication.py index 21cfc0c0e..4f8077309 100644 --- a/src/pyramid/authentication.py +++ b/src/pyramid/authentication.py @@ -1118,6 +1118,37 @@ class SessionAuthenticationPolicy(CallbackAuthenticationPolicy): return request.session.get(self.userid_key) +class SessionAuthenticationHelper: + """ A helper for use with a :term:`security policy` which stores user data + in the configured :term:`session`. + + Constructor Arguments + + ``prefix`` + + A prefix used when storing the authentication parameters in the + session. Defaults to 'auth.'. Optional. + + """ + + def __init__(self, prefix='auth.'): + self.userid_key = prefix + 'userid' + + def remember(self, request, userid, **kw): + """ Store a userid in the session.""" + request.session[self.userid_key] = userid + return [] + + def forget(self, request): + """ Remove the stored userid from the session.""" + if self.userid_key in request.session: + del request.session[self.userid_key] + return [] + + def identify(self, request): + return request.session.get(self.userid_key) + + @implementer(IAuthenticationPolicy) class BasicAuthAuthenticationPolicy(CallbackAuthenticationPolicy): """ A :app:`Pyramid` authentication policy which uses HTTP standard basic diff --git a/src/pyramid/security.py b/src/pyramid/security.py index dda61ef27..5d157d219 100644 --- a/src/pyramid/security.py +++ b/src/pyramid/security.py @@ -540,34 +540,3 @@ class ACLHelper: allowed.update(allowed_here) return allowed - - -class SessionAuthenticationHelper: - """ A helper for use with a :term:`security policy` which stores user data - in the configured :term:`session`. - - Constructor Arguments - - ``prefix`` - - A prefix used when storing the authentication parameters in the - session. Defaults to 'auth.'. Optional. - - """ - - def __init__(self, prefix='auth.'): - self.userid_key = prefix + 'userid' - - def remember(self, request, userid, **kw): - """ Store a userid in the session.""" - request.session[self.userid_key] = userid - return [] - - def forget(self, request): - """ Remove the stored userid from the session.""" - if self.userid_key in request.session: - del request.session[self.userid_key] - return [] - - def identify(self, request): - return request.session.get(self.userid_key) diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 8671eba05..710e87423 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -1693,6 +1693,56 @@ class TestSessionAuthenticationPolicy(unittest.TestCase): self.assertEqual(result, []) +class TestSessionAuthenticationHelper(unittest.TestCase): + def _makeRequest(self, session=None): + from types import SimpleNamespace + + if session is None: + session = dict() + return SimpleNamespace(session=session) + + def _makeOne(self, prefix=''): + from pyramid.authentication import SessionAuthenticationHelper + + return SessionAuthenticationHelper(prefix=prefix) + + def test_identify(self): + request = self._makeRequest({'userid': 'fred'}) + helper = self._makeOne() + self.assertEqual(helper.identify(request), 'fred') + + def test_identify_with_prefix(self): + request = self._makeRequest({'foo.userid': 'fred'}) + helper = self._makeOne(prefix='foo.') + self.assertEqual(helper.identify(request), 'fred') + + def test_identify_none(self): + request = self._makeRequest() + helper = self._makeOne() + self.assertEqual(helper.identify(request), None) + + def test_remember(self): + request = self._makeRequest() + helper = self._makeOne() + result = helper.remember(request, 'fred') + self.assertEqual(request.session.get('userid'), 'fred') + self.assertEqual(result, []) + + def test_forget(self): + request = self._makeRequest({'userid': 'fred'}) + helper = self._makeOne() + result = helper.forget(request) + self.assertEqual(request.session.get('userid'), None) + self.assertEqual(result, []) + + def test_forget_no_identity(self): + request = self._makeRequest() + helper = self._makeOne() + result = helper.forget(request) + self.assertEqual(request.session.get('userid'), None) + self.assertEqual(result, []) + + class TestBasicAuthAuthenticationPolicy(unittest.TestCase): def _getTargetClass(self): from pyramid.authentication import BasicAuthAuthenticationPolicy as cls diff --git a/tests/test_security.py b/tests/test_security.py index f14159156..ecd6a088b 100644 --- a/tests/test_security.py +++ b/tests/test_security.py @@ -887,53 +887,3 @@ GUEST_PERMS = (VIEW, COMMENT) MEMBER_PERMS = GUEST_PERMS + (EDIT, CREATE, DELETE) MODERATOR_PERMS = MEMBER_PERMS + (MODERATE,) ADMINISTRATOR_PERMS = MODERATOR_PERMS + (ADMINISTER,) - - -class TestSessionAuthenticationHelper(unittest.TestCase): - def _makeRequest(self, session=None): - from types import SimpleNamespace - - if session is None: - session = dict() - return SimpleNamespace(session=session) - - def _makeOne(self, prefix=''): - from pyramid.security import SessionAuthenticationHelper - - return SessionAuthenticationHelper(prefix=prefix) - - def test_identify(self): - request = self._makeRequest({'userid': 'fred'}) - helper = self._makeOne() - self.assertEqual(helper.identify(request), 'fred') - - def test_identify_with_prefix(self): - request = self._makeRequest({'foo.userid': 'fred'}) - helper = self._makeOne(prefix='foo.') - self.assertEqual(helper.identify(request), 'fred') - - def test_identify_none(self): - request = self._makeRequest() - helper = self._makeOne() - self.assertEqual(helper.identify(request), None) - - def test_remember(self): - request = self._makeRequest() - helper = self._makeOne() - result = helper.remember(request, 'fred') - self.assertEqual(request.session.get('userid'), 'fred') - self.assertEqual(result, []) - - def test_forget(self): - request = self._makeRequest({'userid': 'fred'}) - helper = self._makeOne() - result = helper.forget(request) - self.assertEqual(request.session.get('userid'), None) - self.assertEqual(result, []) - - def test_forget_no_identity(self): - request = self._makeRequest() - helper = self._makeOne() - result = helper.forget(request) - self.assertEqual(request.session.get('userid'), None) - self.assertEqual(result, []) -- cgit v1.2.3