CherryPy中的用户管理

时间:2011-09-04 13:50:44

标签: python authentication security cherrypy

我将开发一个基于CherryPy的Web应用程序。它将是一个公共用户可以注册并将在之后登录的应用程序 - 通常的东西。其他框架如Rails和Django包含有关安全性的复杂代码:加密和盐渍密码,防止会话劫持,...... CherryPy是否已有类似的功能?到目前为止,我发现只有非常简单的解决方案!?

2 个答案:

答案 0 :(得分:1)

这有帮助吗? - CherryPy Digest and Basic Authentication Tutorial (Internet Archive copy)

我有一个使用ldap模块与ldap auth系统交互的cherypy应用程序,但似乎你的应用程序需要在其自身内管理usr / passwds。您可以将注册的用户/密码加密到数据库表中,并调整上述博客中概述的方案。

答案 1 :(得分:1)

我使用了此身份验证示例,其中包含注释中的修复程序。

http://tools.cherrypy.org/wiki/AuthenticationAndAccessRestrictions

以下是我加密的方式

import Crypto.Random
from Crypto.Cipher import AES
import hashlib

# salt size in bytes
SALT_SIZE = 16

# number of iterations in the key generation
NUMBER_OF_ITERATIONS = 20

# the size multiple required for AES
AES_MULTIPLE = 16


__all__ = ['Encryption']

class Encryption(object):
    def generate_key(self, password, salt, iterations):
        assert iterations > 0    
        key = password + salt    
        for i in range(iterations):
            key = hashlib.sha256(key).digest()  

    return key

    def pad_text(self, text, multiple):
        extra_bytes = len(text) % multiple    
        padding_size = multiple - extra_bytes    
        padding = chr(padding_size) * padding_size    
        padded_text = text + padding

        return padded_text

    def unpad_text(self, padded_text):
        padding_size = padded_text[-1]    
        text = padded_text[:-padding_size]

        return text


    def encrypt(self, plaintext, password):
        salt = Crypto.Random.get_random_bytes(SALT_SIZE)    
        key = Encryption.generate_key(self, password, salt, NUMBER_OF_ITERATIONS)    
        cipher = AES.new(key, AES.MODE_ECB)    
        padded_plaintext = Encryption.pad_text(self, plaintext, AES_MULTIPLE)    
        ciphertext = cipher.encrypt(padded_plaintext)    
        ciphertext_with_salt = salt + ciphertext

        return ciphertext_with_salt

然后调用加密函数

encryptedPassword = Encryption.encrypt(self, Password, bytes(cherrypy.request.app.config['Encryption']['Password'], 'UTF-8'))

希望这有帮助!

安德鲁