pyramid_cubicweb/auth.py
changeset 11533 4ced3782b90f
child 11537 caf268942436
equal deleted inserted replaced
11532:6a1d0aa3ac85 11533:4ced3782b90f
       
     1 import datetime
       
     2 import logging
       
     3 import warnings
       
     4 
       
     5 from pyramid.authorization import ACLAuthorizationPolicy
       
     6 from pyramid_cubicweb.core import get_principals
       
     7 
       
     8 from pyramid.authentication import AuthTktAuthenticationPolicy
       
     9 
       
    10 log = logging.getLogger(__name__)
       
    11 
       
    12 
       
    13 class CubicWebAuthTktAuthenticationPolicy(AuthTktAuthenticationPolicy):
       
    14     """An authentication policy that update the user last_login_time.
       
    15 
       
    16     The update is done in the 'remember' method, which is called on login,
       
    17     and each time the authentication ticket is reissued.
       
    18 
       
    19     Meaning, the last_login_time is updated reissue_time seconds (maximum)
       
    20     before the last request by the user.
       
    21     """
       
    22 
       
    23     def remember(self, request, principal, **kw):
       
    24         headers = super(CubicWebAuthTktAuthenticationPolicy, self).remember(
       
    25             request, principal, **kw)
       
    26         try:
       
    27             repo = request.registry['cubicweb.repository']
       
    28             with repo.internal_cnx() as cnx:
       
    29                 cnx.execute(
       
    30                     "SET U last_login_time %(now)s WHERE U eid %(user)s", {
       
    31                         'now': datetime.datetime.now(),
       
    32                         'user': principal})
       
    33                 cnx.commit()
       
    34         except:
       
    35             log.exception("Failed to update last_login_time")
       
    36         return headers
       
    37 
       
    38 
       
    39 def includeme(config):
       
    40     secret = config.registry['cubicweb.config']['pyramid-auth-secret']
       
    41 
       
    42     if not secret:
       
    43         secret = 'notsosecret'
       
    44         warnings.warn('''
       
    45 
       
    46             !! WARNING !! !! WARNING !!
       
    47 
       
    48             The authentication cookies are signed with a static secret key.
       
    49             To put your own secret key, edit your all-in-one.conf file
       
    50             and set the 'pyramid-auth-secret' key.
       
    51 
       
    52             YOU SHOULD STOP THIS INSTANCE unless your really know what you
       
    53             are doing !!
       
    54 
       
    55         ''')
       
    56 
       
    57     config.set_authentication_policy(
       
    58         CubicWebAuthTktAuthenticationPolicy(
       
    59             secret, callback=get_principals, hashalg='sha512',
       
    60             reissue_time=3600))
       
    61     config.set_authorization_policy(ACLAuthorizationPolicy())