From 6aba89d19cc384021864d3b83d53082f56c3f419 Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Sat, 30 Mar 2019 10:31:17 -0700 Subject: Add SessionAuthenticationHelper. --- src/pyramid/security.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src') diff --git a/src/pyramid/security.py b/src/pyramid/security.py index bfd505a98..671cd3569 100644 --- a/src/pyramid/security.py +++ b/src/pyramid/security.py @@ -540,3 +540,34 @@ 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) -- cgit v1.2.3