Exemplo n.º 1
0
 def doChangeUser(self, principal_id, password):
     """Change a user's password
     """
     if self._user_passwords.get(principal_id) is None:
         raise RuntimeError("User does not exist: %s" % principal_id)
     self._user_passwords[principal_id] = AuthEncoding.pw_encrypt(password)
     notify(CredentialsUpdated(self.getUserById(principal_id), password))
Exemplo n.º 2
0
    def doChangeUser(self, principal_id, password):
        """
        Update user's password date and store passwords history.
        """
        user = api.user.get(username=principal_id)
        portal = api.portal.get()
        current_time = portal.ZopeTime()
        user.setMemberProperties({"password_date": current_time})
        self._invalidatePrincipalCache(principal_id)

        # Remember passwords here
        max_history_pws = api.portal.get_registry_record(
            "collective.pwexpiry.password_history_size"
        )

        if max_history_pws == 0:
            # disabled, return here.
            return

        enc_pw = password
        if not AuthEncoding.is_encrypted(enc_pw):
            enc_pw = AuthEncoding.pw_encrypt(enc_pw)

        pw_history = list(user.getProperty("password_history", tuple()))
        pw_history.append(enc_pw)
        if len(pw_history) > max_history_pws:
            # Truncate the history
            pw_history = pw_history[-max_history_pws:]

        user.setMemberProperties({"password_history": tuple(pw_history)})
Exemplo n.º 3
0
    def setAttempt(self, login, password):
        """
         Set counter to 1 or bump it when authentication failed, if previous failed 
         attempt was more than reset period time instead of bumping counter reset it to 1
        """

        root = self.getRootPlugin()
        count, last, IP, reference = root._login_attempts.get(
            login, (0, None, '', None))

        if reference and AuthEncoding.pw_validate(reference, password):
            # don't count repeating same password
            return
        if last:
            delta = DateTime().asdatetime() - last.asdatetime()
            if delta.seconds > self.getResetPeriod():
                # set counter to 1 instead of bumping, some sort of autoreset.
                count = 1
            else:
                count += 1

        else:
            count += 1
        IP = self.remote_ip()
        log.debug("user '%s' failed to login, attempt #%i %s last: %s", login,
                  count, IP, last)
        last = DateTime()
        reference = AuthEncoding.pw_encrypt(password)
        root._login_attempts[login] = (count, last, IP, reference)
Exemplo n.º 4
0
def _createLDAPPassword(password, encoding='SHA'):
    """ Create a password string suitable for the userPassword attribute
    """
    encoding = encoding.upper()

    if encoding in ('SSHA', 'SHA', 'CRYPT'):
        pwd_str = AuthEncoding.pw_encrypt(password, encoding)
    elif encoding == 'MD5':
        m = md5_new(password)
        pwd_str = '{MD5}' + base64.encodestring(m.digest())
    elif encoding == 'CLEAR':
        pwd_str = password
    else:
        pwd_str = AuthEncoding.pw_encrypt(password, 'SSHA')

    return pwd_str.strip()
    def doChangeUser(self, principal_id, password):
        """
        Update user's password date and store passwords history.
        """
        user = api.user.get(username=principal_id)
        portal = api.portal.get()
        current_time = portal.ZopeTime()
        user.setMemberProperties({'password_date': current_time})
        self._invalidatePrincipalCache(principal_id)

        # Remember passwords here
        max_history_pws = api.portal.get_registry_record(
            'collective.pwexpiry.password_history_size'
        )

        if max_history_pws == 0:
            # disabled, return here.
            return

        enc_pw = password
        if not AuthEncoding.is_encrypted(enc_pw):
            enc_pw = AuthEncoding.pw_encrypt(enc_pw)

        pw_history = list(user.getProperty('password_history', tuple()))
        pw_history.append(enc_pw)
        if len(pw_history) > max_history_pws:
            # Truncate the history
            pw_history = pw_history[-max_history_pws:]

        user.setMemberProperties({'password_history': tuple(pw_history)})
Exemplo n.º 6
0
def _createLDAPPassword(password, encoding='SHA'):
    """ Create a password string suitable for the userPassword attribute
    """
    encoding = encoding.upper()

    if encoding in ('SSHA', 'SHA', 'CRYPT'):
        pwd_str = AuthEncoding.pw_encrypt(password, encoding)
    elif encoding == 'MD5':
        m = md5_new(password)
        pwd_str = '{MD5}' + base64.encodestring(m.digest())
    elif encoding == 'CLEAR':
        pwd_str = password
    else:
        pwd_str = AuthEncoding.pw_encrypt(password, 'SSHA')

    return pwd_str.strip()
Exemplo n.º 7
0
    def updateUserPassword( self, user_id, password ):

        if self._user_passwords.get( user_id ) is None:
            raise KeyError, 'Invalid user ID: %s' % user_id

        if password:
            digested = AuthEncoding.pw_encrypt( password )
            self._user_passwords[ user_id ] = digested
Exemplo n.º 8
0
 def password(self, password):
     # When editing, the password field is empty in the browser; do
     # not do anything then.
     if password is not None:
         self.context.password = AuthEncoding.pw_encrypt(
             safe_encode(password),
             encoding='BCRYPT'
         )
Exemplo n.º 9
0
 def testBlankPassword(self):
     pw = ''
     for id in AuthEncoding.listSchemes():
         enc = AuthEncoding.pw_encrypt(pw, id)
         assert enc != pw
         assert AuthEncoding.pw_validate(enc, pw)
         assert not AuthEncoding.pw_validate(enc, enc)
         assert not AuthEncoding.pw_validate(enc, 'xxx')
Exemplo n.º 10
0
 def testBlankPassword(self):
     pw = ''
     for id in AuthEncoding.listSchemes():
         enc = AuthEncoding.pw_encrypt(pw, id)
         assert enc != pw
         assert AuthEncoding.pw_validate(enc, pw)
         assert not AuthEncoding.pw_validate(enc, enc)
         assert not AuthEncoding.pw_validate(enc, 'xxx')
Exemplo n.º 11
0
 def setPasswordForUser(self, login, password):
     """Add password to the list of previously used passwords for a user.
     """
     hashes = self._user_passwords.get(login, [])
     hash = AuthEncoding.pw_encrypt(password)
     hashes.append(hash)
     self._user_passwords[login] = hashes
     log.info("Password '%s' for user '%s' stored" % (password, login))
Exemplo n.º 12
0
    def updateUserPassword(self, user_id, password):

        if self._user_passwords.get(user_id) is None:
            raise KeyError, 'Invalid user ID: %s' % user_id

        if password:
            digested = AuthEncoding.pw_encrypt(password)
            self._user_passwords[user_id] = digested
Exemplo n.º 13
0
 def password(self, password):
     # When editing, the password field is empty in the browser; do
     # not do anything then.
     if password is not None:
         self.context.password = AuthEncoding.pw_encrypt(
             safe_encode(password),
             encoding='BCRYPT'
         )
Exemplo n.º 14
0
 def testGoodPassword(self):
     pw = 'good_password'
     assert len(AuthEncoding.listSchemes()) > 0  # At least one must exist!
     for id in AuthEncoding.listSchemes():
         enc = AuthEncoding.pw_encrypt(pw, id)
         assert enc != pw
         assert AuthEncoding.pw_validate(enc, pw)
         assert AuthEncoding.is_encrypted(enc)
         assert not AuthEncoding.is_encrypted(pw)
Exemplo n.º 15
0
 def testGoodPassword(self):
     pw = 'good_password'
     assert len(AuthEncoding.listSchemes()) > 0  # At least one must exist!
     for id in AuthEncoding.listSchemes():
         enc = AuthEncoding.pw_encrypt(pw, id)
         assert enc != pw
         assert AuthEncoding.pw_validate(enc, pw)
         assert AuthEncoding.is_encrypted(enc)
         assert not AuthEncoding.is_encrypted(pw)
    def _pw_encrypt(self, password):
        """Returns the AuthEncoding encrypted password

        If 'password' is already encrypted, it is returned
        as is and not encrypted again.
        """
        if AuthEncoding.is_encrypted(password):
            return password
        return AuthEncoding.pw_encrypt(password)
Exemplo n.º 17
0
    def _pw_encrypt(self, password):
        """Returns the AuthEncoding encrypted password

        If 'password' is already encrypted, it is returned
        as is and not encrypted again.
        """
        if AuthEncoding.is_encrypted(password):
            return password
        return AuthEncoding.pw_encrypt(password)
Exemplo n.º 18
0
    def addUser( self, user_id, login_name, password ):

        if self._user_passwords.get( user_id ) is not None:
            raise KeyError, 'Duplicate user ID: %s' % user_id

        if self._login_to_userid.get( login_name ) is not None:
            raise KeyError, 'Duplicate login name: %s' % login_name

        self._user_passwords[ user_id ] = AuthEncoding.pw_encrypt( password )
        self._login_to_userid[ login_name ] = user_id
        self._userid_to_login[ user_id ] = login_name
Exemplo n.º 19
0
    def addUser(self, user_id, login_name, password):

        if self._user_passwords.get(user_id) is not None:
            raise KeyError, 'Duplicate user ID: %s' % user_id

        if self._login_to_userid.get(login_name) is not None:
            raise KeyError, 'Duplicate login name: %s' % login_name

        self._user_passwords[user_id] = AuthEncoding.pw_encrypt(password)
        self._login_to_userid[login_name] = user_id
        self._userid_to_login[user_id] = login_name
Exemplo n.º 20
0
 def testLongPassword(self):
     pw = 'Pw' * 2000
     for id in AuthEncoding.listSchemes():
         enc = AuthEncoding.pw_encrypt(pw, id)
         assert enc != pw
         assert AuthEncoding.pw_validate(enc, pw)
         assert not AuthEncoding.pw_validate(enc, enc)
         assert not AuthEncoding.pw_validate(enc, 'xxx')
         if id != 'CRYPT':
             # crypt truncates passwords and would fail these tests.
             assert not AuthEncoding.pw_validate(enc, pw[:-2])
             assert not AuthEncoding.pw_validate(enc, pw[2:])
Exemplo n.º 21
0
 def testBadPasword(self):
     pw = 'OK_pa55w0rd \n'
     for id in AuthEncoding.listSchemes():
         enc = AuthEncoding.pw_encrypt(pw, id)
         assert enc != pw
         assert not AuthEncoding.pw_validate(enc, 'xxx')
         assert not AuthEncoding.pw_validate(enc, enc)
         if id != 'CRYPT':
             # crypt truncates passwords and would fail this test.
             assert not AuthEncoding.pw_validate(enc, pw[:-1])
         assert not AuthEncoding.pw_validate(enc, pw[1:])
         assert AuthEncoding.pw_validate(enc, pw)
Exemplo n.º 22
0
 def testLongPassword(self):
     pw = 'Pw' * 2000
     for id in AuthEncoding.listSchemes():
         enc = AuthEncoding.pw_encrypt(pw, id)
         assert enc != pw
         assert AuthEncoding.pw_validate(enc, pw)
         assert not AuthEncoding.pw_validate(enc, enc)
         assert not AuthEncoding.pw_validate(enc, 'xxx')
         if id != 'CRYPT':
             # crypt truncates passwords and would fail these tests.
             assert not AuthEncoding.pw_validate(enc, pw[:-2])
             assert not AuthEncoding.pw_validate(enc, pw[2:])
Exemplo n.º 23
0
 def testBadPasword(self):
     pw = 'OK_pa55w0rd \n'
     for id in AuthEncoding.listSchemes():
         enc = AuthEncoding.pw_encrypt(pw, id)
         assert enc != pw
         assert not AuthEncoding.pw_validate(enc, 'xxx')
         assert not AuthEncoding.pw_validate(enc, enc)
         if id != 'CRYPT':
             # crypt truncates passwords and would fail this test.
             assert not AuthEncoding.pw_validate(enc, pw[:-1])
         assert not AuthEncoding.pw_validate(enc, pw[1:])
         assert AuthEncoding.pw_validate(enc, pw)
Exemplo n.º 24
0
    def updateUserPassword(self, user_id, login_name, password):

        if self._user_passwords.get(user_id) is None:
            raise KeyError, 'Invalid user ID: %s' % user_id

        old_login_name = self._userid_to_login[user_id]

        if old_login_name != login_name:
            del self._login_to_userid[old_login_name]
            self._login_to_userid[login_name] = user_id
            self._userid_to_login[user_id] = login_name

        if password:
            digested = AuthEncoding.pw_encrypt(password)
            self._user_passwords[user_id] = digested
Exemplo n.º 25
0
    def addUser( self, user_id, login_name, password ):

        if self._user_passwords.get( user_id ) is not None:
            raise KeyError, 'Duplicate user ID: %s' % user_id

        if self._login_to_userid.get( login_name ) is not None:
            raise KeyError, 'Duplicate login name: %s' % login_name

        self._user_passwords[ user_id ] = AuthEncoding.pw_encrypt( password )
        self._login_to_userid[ login_name ] = user_id
        self._userid_to_login[ user_id ] = login_name

        # enumerateUsers return value has changed
        view_name = createViewName('enumerateUsers')
        self.ZCacheable_invalidate(view_name=view_name)
Exemplo n.º 26
0
    def updateUserPassword( self, user_id, login_name, password ):

        if self._user_passwords.get( user_id ) is None:
            raise KeyError, 'Invalid user ID: %s' % user_id

        old_login_name = self._userid_to_login[ user_id ]

        if old_login_name != login_name:
            del self._login_to_userid[ old_login_name ]
            self._login_to_userid[ login_name ] = user_id
            self._userid_to_login[ user_id ] = login_name

        if password:
            digested = AuthEncoding.pw_encrypt( password )
            self._user_passwords[ user_id ] = digested
Exemplo n.º 27
0
    def setAttempt(self, login, password):
        "increment attempt count and record date stamp last attempt and IP"

        root = self.getRootPlugin()
        count, last, IP, reference = root._login_attempts.get(login, (0, None, '', None))

        if reference and AuthEncoding.pw_validate(reference, password):
            return  # we don't count repeating same password in case its correct
        else:
            count += 1
        IP = self.remote_ip()
        log.info("user '%s' attempt #%i %s last: %s", login, count, IP, last)
        last = DateTime()
        reference = AuthEncoding.pw_encrypt(password)
        root._login_attempts[login] = (count, last, IP, reference)
Exemplo n.º 28
0
    def addUser(self, user_id, login_name, password):

        if self._user_passwords.get(user_id) is not None:
            raise KeyError, 'Duplicate user ID: %s' % user_id

        if self._login_to_userid.get(login_name) is not None:
            raise KeyError, 'Duplicate login name: %s' % login_name

        self._user_passwords[user_id] = AuthEncoding.pw_encrypt(password)
        self._login_to_userid[login_name] = user_id
        self._userid_to_login[user_id] = login_name

        # enumerateUsers return value has changed
        view_name = createViewName('enumerateUsers')
        self.ZCacheable_invalidate(view_name=view_name)
Exemplo n.º 29
0
 def testLongPassword(self):
     pw = 'Pw' * 2000
     for id in AuthEncoding.listSchemes():
         enc = AuthEncoding.pw_encrypt(pw, id)
         assert enc != pw
         assert AuthEncoding.pw_validate(enc, pw)
         assert not AuthEncoding.pw_validate(enc, enc)
         assert not AuthEncoding.pw_validate(enc, 'xxx')
         if id not in ('CRYPT', 'BCRYPT'):
             # crypt truncates passwords and would fail these tests.
             # bcrypt works with password inputs where len(pw) <= 50
             assert not AuthEncoding.pw_validate(enc, pw[:-2]), (
                 '%r Failed: %s %s' % (id, enc, pw[:-2])
             )
             assert not AuthEncoding.pw_validate(enc, pw[2:])
Exemplo n.º 30
0
    def setAttempt(self, login, password):
        "increment attempt count and record date stamp last attempt and IP"

        root = self.getRootPlugin()
        count, last, IP, reference = root._login_attempts.get(
            login, (0, None, '', None))

        if reference and AuthEncoding.pw_validate(reference, password):
            # we don't count repeating same password in case its correct
            return
        if last and ((DateTime() - last) * 24) > self.getResetPeriod():
            # set count to 1 following login attempt after reset period
            count = 1
        else:
            count += 1
        IP = self.remote_ip()
        log.info("user '%s' attempt #%i %s last: %s", login, count, IP, last)
        last = DateTime()
        reference = AuthEncoding.pw_encrypt(password)
        root._login_attempts[login] = (count, last, IP, reference)
Exemplo n.º 31
0
    def setAttempt(self, login, password):
        "increment attempt count and record date stamp last attempt and IP"

        # TODO: why are the login attempts stored in the root? The usernames aren't unique in the root.

        root = self.getRootPlugin()
        count, last, IP, reference = root._login_attempts.get(
            login, (0, None, '', None))

        if reference and AuthEncoding.pw_validate(reference, password):
            # we don't count repeating same password in case its correct
            return
        if last and ((DateTime() - last) * 24) > self.getResetPeriod():
            # set count to 1 following login attempt after reset period
            count = 1
        else:
            count += 1
        IP = self.remote_ip()
        log.info("user '%s' attempt #%i %s last: %s", login, count, IP, last)
        last = DateTime()
        reference = AuthEncoding.pw_encrypt(password)
        root._login_attempts[login] = (count, last, IP, reference)
Exemplo n.º 32
0
    def addUser(self, user_id, login_name, password):
        """Original ZODBUserManager.addUser, modified to check if
        incoming password is already encypted.

        This support clean migration from default user source.
        Should go into PAS.
        """
        if self._user_passwords.get(user_id) is not None:
            raise KeyError('Duplicate user ID: %s' % user_id)

        if self._login_to_userid.get(login_name) is not None:
            raise KeyError('Duplicate login name: %s' % login_name)

        if not AuthEncoding.is_encrypted(password):
            password = AuthEncoding.pw_encrypt(password)
        self._user_passwords[user_id] = password
        self._login_to_userid[login_name] = user_id
        self._userid_to_login[user_id] = login_name

        # enumerateUsers return value has changed
        view_name = createViewName('enumerateUsers')
        self.ZCacheable_invalidate(view_name=view_name)
Exemplo n.º 33
0
    def addUser(self, user_id, login_name, password):
        """Original ZODBUserManager.addUser, modified to check if
        incoming password is already encypted.

        This support clean migration from default user source.
        Should go into PAS.
        """
        if self._user_passwords.get(user_id) is not None:
            raise KeyError, 'Duplicate user ID: %s' % user_id

        if self._login_to_userid.get(login_name) is not None:
            raise KeyError, 'Duplicate login name: %s' % login_name

        if not AuthEncoding.is_encrypted(password):
            password = AuthEncoding.pw_encrypt(password)
        self._user_passwords[ user_id ] = password
        self._login_to_userid[ login_name ] = user_id
        self._userid_to_login[ user_id ] = login_name

        # enumerateUsers return value has changed
        view_name = createViewName('enumerateUsers')
        self.ZCacheable_invalidate(view_name=view_name)
Exemplo n.º 34
0
 def _legacy_set_password(self, member, password):
     from AccessControl import AuthEncoding
     # Default AuthEncoding 'encryption' uses SSHA
     member.password = AuthEncoding.pw_encrypt(password)
     self.layer['portal'].membrane_tool.reindexObject(member)
 def _encryptPassword(self, pw):
     # we need to override the default, because if we encrypt with SSHA
     # we have trouble when we do the wire protocol
     upw = to_unicode_or_bust(pw)
     utf8pw = upw.encode('utf-8', 'ignore')
     return AuthEncoding.pw_encrypt(utf8pw, 'SHA')
Exemplo n.º 36
0
 def _legacy_set_password(self, member, password):
     from AccessControl import AuthEncoding
     # Default AuthEncoding 'encryption' uses SSHA
     member.password = AuthEncoding.pw_encrypt(password)
     self.layer['portal'].membrane_tool.reindexObject(member)
Exemplo n.º 37
0
 def _encryptPassword(self, pw):
     return AuthEncoding.pw_encrypt(pw, 'SSHA')
Exemplo n.º 38
0
 def _encryptPassword(self, pw):
     # we need to override the default, because if we encrypt with SSHA
     # we have trouble when we do the wire protocol
     upw = to_unicode_or_bust(pw)
     utf8pw = upw.encode('utf-8', 'ignore')
     return AuthEncoding.pw_encrypt(utf8pw, 'SHA')
Exemplo n.º 39
0
 def test_argon_is_used_by_default(self):
     encrypted = AuthEncoding.pw_encrypt('foobar')
     self.assertTrue('{argon2}' in encrypted)
     self.assertTrue(AuthEncoding.pw_validate(encrypted, 'foobar'))
Exemplo n.º 40
0
 def doChangeUser(self, principal_id, password):
     """Change a user's password
     """
     if self._user_passwords.get(principal_id) is None:
         raise RuntimeError, "User does not exist: %s" % principal_id
     self._user_passwords[principal_id] = AuthEncoding.pw_encrypt(password)
Exemplo n.º 41
0
 def _encryptPassword(self, pw):
     return AuthEncoding.pw_encrypt(pw, 'SSHA')