示例#1
0
 def crypt_verify(self, encrypted, raw):
     '''Verify if the ``raw`` string match the ``encrypted`` string
     '''
     return self.crypt_module.verify(to_bytes(encrypted),
                                     to_bytes(raw),
                                     self.secret_key,
                                     **self.ckwargs)
示例#2
0
文件: tools.py 项目: huobao36/pulsar
 def test_to_bytes(self):
     s = to_bytes("ciao")
     self.assertTrue(isinstance(s, bytes))
     s2 = to_bytes(s)
     self.assertEqual(id(s), id(s2))
     s3 = to_bytes(s, "latin-1")
     self.assertEqual(s, s3)
     self.assertNotEqual(id(s), id(s3))
示例#3
0
 def authenticate(self, request, user, password=None):
     if password is not None:
         password = to_bytes(password, self.encoding)
         encrypted = to_bytes(user.password, self.encoding)
         if self.crypt_module.verify(password, encrypted, self.secret_key):
             return user
         else:
             raise AuthenticationError('Invalid password')
示例#4
0
 def authenticate(self, request, user, password=None):
     if password is not None:
         password = to_bytes(password, self.encoding)
         encrypted = to_bytes(user.password, self.encoding)
         if self.crypt_module.verify(password, encrypted, self.secret_key):
             return user
         else:
             raise AuthenticationError('Invalid password')
示例#5
0
文件: tools.py 项目: japaks/pulsar
 def test_to_bytes(self):
     s = to_bytes('ciao')
     self.assertTrue(isinstance(s, bytes))
     s2 = to_bytes(s)
     self.assertEqual(id(s), id(s2))
     s3 = to_bytes(s, 'latin-1')
     self.assertEqual(s, s3)
     self.assertNotEqual(id(s), id(s3))
示例#6
0
 def decript(self, password=None):
     if password:
         p = self.crypt_module.decrypt(to_bytes(password, self.encoding),
                                       self.secret_key)
         return to_string(p, self.encoding)
     else:
         return UNUSABLE_PASSWORD
示例#7
0
文件: user.py 项目: SirZazu/lux
 def decript(self, password=None):
     if password:
         p = self.crypt_module.decrypt(to_bytes(password, self.encoding),
                                       self.secret_key)
         return to_string(p, self.encoding)
     else:
         return UNUSABLE_PASSWORD
示例#8
0
    def _encode_body(self, data, files, json):
        body = None
        if isinstance(data, (str, bytes)):
            if files:
                raise ValueError('data cannot be a string or bytes when '
                                 'files are present')
            body = to_bytes(data, self.charset)
        elif data and is_streamed(data):
            if files:
                raise ValueError('data cannot be an iterator when '
                                 'files are present')
            if 'content-length' not in self.headers:
                self.headers['transfer-encoding'] = 'chunked'
            return data
        elif data or files:
            if files:
                body, content_type = self._encode_files(data, files)
            else:
                body, content_type = self._encode_params(data)
            self.headers['Content-Type'] = content_type
        elif json:
            body = _json.dumps(json).encode(self.charset)
            self.headers['Content-Type'] = 'application/json'

        if body:
            self.headers['content-length'] = str(len(body))

        return body
示例#9
0
    def _encode_body(self, data, files, json):
        body = None
        if isinstance(data, (str, bytes)):
            if files:
                raise ValueError('data cannot be a string or bytes when '
                                 'files are present')
            body = to_bytes(data, self.charset)
        elif data and is_streamed(data):
            if files:
                raise ValueError('data cannot be an iterator when '
                                 'files are present')
            if 'content-length' not in self.headers:
                self.headers['transfer-encoding'] = 'chunked'
            return data
        elif data or files:
            if files:
                body, content_type = self._encode_files(data, files)
            else:
                body, content_type = self._encode_params(data)
            self.headers['Content-Type'] = content_type
        elif json:
            body = _json.dumps(json).encode(self.charset)
            self.headers['Content-Type'] = 'application/json'

        if body:
            self.headers['content-length'] = str(len(body))

        return body
示例#10
0
文件: __init__.py 项目: arhik/pulsar
 def _encode_data(self, data):
     body = None
     if self.method in ENCODE_URL_METHODS:
         self.files = None
         self._encode_url(data)
     elif isinstance(data, bytes):
         assert self.files is None, ('data cannot be bytes when files are '
                                     'present')
         body = data
     elif isinstance(data, str):
         assert self.files is None, ('data cannot be string when files are '
                                     'present')
         body = to_bytes(data, self.charset)
     elif data and is_streamed(data):
         assert self.files is None, ('data cannot be an iterator when '
                                     'files are present')
         if 'content-type' not in self.headers:
             self.headers['content-type'] = 'application/octet-stream'
         if 'content-length' not in self.headers:
             self.headers['transfer-encoding'] = 'chunked'
         return data
     elif data or self.files:
         if self.files:
             body, content_type = self._encode_files(data)
         else:
             body, content_type = self._encode_params(data)
         # set files to None, Important!
         self.files = None
         self.headers['Content-Type'] = content_type
     if body:
         self.headers['content-length'] = str(len(body))
     elif 'expect' not in self.headers:
         self.headers.pop('content-length', None)
         self.headers.pop('content-type', None)
     return body
示例#11
0
 def _encode_data(self, data):
     body = None
     if self.method in ENCODE_URL_METHODS:
         self.files = None
         self._encode_url(data)
     elif isinstance(data, bytes):
         assert self.files is None, ('data cannot be bytes when files are '
                                     'present')
         body = data
     elif isinstance(data, str):
         assert self.files is None, ('data cannot be string when files are '
                                     'present')
         body = to_bytes(data, self.charset)
     elif data and is_streamed(data):
         assert self.files is None, ('data cannot be an iterator when '
                                     'files are present')
         # if 'content-type' not in self.headers:
         #     self.headers['content-type'] = 'application/octet-stream'
         if 'content-length' not in self.headers:
             self.headers['transfer-encoding'] = 'chunked'
         return data
     elif data or self.files:
         if self.files:
             body, content_type = self._encode_files(data)
         else:
             body, content_type = self._encode_params(data)
         # set files to None, Important!
         self.files = None
         self.headers['Content-Type'] = content_type
     if body:
         self.headers['content-length'] = str(len(body))
     # elif 'expect' not in self.headers:
     #     self.headers.pop('content-length', None)
     #     self.headers.pop('content-type', None)
     return body
示例#12
0
 def _encode_data(self, data):
     body = None
     if self.method in ENCODE_URL_METHODS:
         self.files = None
         self._encode_url(data)
     elif isinstance(data, bytes):
         assert self.files is None, ('data cannot be bytes when files are '
                                     'present')
         body = data
     elif isinstance(data, str):
         assert self.files is None, ('data cannot be string when files are '
                                     'present')
         body = to_bytes(data, self.charset)
     elif data or self.files:
         if self.files:
             body, content_type = self._encode_files(data)
         else:
             body, content_type = self._encode_params(data)
         # set files to None, Important!
         self.files = None
         self.headers['Content-Type'] = content_type
     if body:
         self.headers['content-length'] = str(len(body))
     elif 'expect' not in self.headers:
         self.headers.pop('content-length', None)
         self.headers.pop('content-type', None)
     return body
示例#13
0
 def _encode_data(self, data):
     body = None
     if self.method in ENCODE_URL_METHODS:
         self.files = None
         self._encode_url(data)
     elif isinstance(data, bytes):
         assert self.files is None, ('data cannot be bytes when files are '
                                     'present')
         body = data
     elif isinstance(data, str):
         assert self.files is None, ('data cannot be string when files are '
                                     'present')
         body = to_bytes(data, self.charset)
     elif data or self.files:
         if self.files:
             body, content_type = self._encode_files(data)
         else:
             body, content_type = self._encode_params(data)
         # set files to None, Important!
         self.files = None
         self.headers['Content-Type'] = content_type
     if body:
         self.headers['content-length'] = str(len(body))
     elif 'expect' not in self.headers:
         self.headers.pop('content-length', None)
         self.headers.pop('content-type', None)
     return body
示例#14
0
 def digest_auth_header(self,
                        realm=None,
                        nonce=None,
                        qop=None,
                        opaque=None,
                        algorithm=None,
                        stale=None):
     options = {}
     if nonce is None:
         nonce = hexmd5(to_bytes('%d' % time.time()) + os.urandom(10))
         if opaque is None:
             opaque = hexmd5(os.urandom(10))
     if stale:
         options['stale'] = 'TRUE'
     if opaque is not None:
         options['opaque'] = opaque
     if algorithm is not None:
         options['algorithm'] = algorithm
     if qop is None:
         qop = ('auth', )
     return self._auth_header('digest',
                              realm=realm,
                              nonce=nonce,
                              qop=', '.join(qop),
                              **options)
示例#15
0
    def encrypt(self, string_or_bytes):
        '''Encrypt ``string_or_bytes`` using the algorithm specified
        in the :setting:`CRYPT_ALGORITHM` setting.

        Return an encrypted string
        '''
        b = to_bytes(string_or_bytes, self.encoding)
        p = self.crypt_module.encrypt(b, self.secret_key, **self.ckwargs)
        return p.decode(self.encoding)
示例#16
0
    def encrypt(self, string_or_bytes):
        '''Encrypt ``string_or_bytes`` using the algorithm specified
        in the :setting:`CRYPT_ALGORITHM` setting.

        Return an encrypted string
        '''
        b = to_bytes(string_or_bytes, self.encoding)
        p = self.crypt_module.encrypt(b, self.secret_key, **self.ckwargs)
        return p.decode(self.encoding)
示例#17
0
文件: auth.py 项目: wilddom/pulsar
 def digest_auth_header(self, realm=None, nonce=None, qop=None, opaque=None,
                        algorithm=None, stale=None):
     options = {}
     if nonce is None:
         nonce = hexmd5(to_bytes('%d' % time.time()) + os.urandom(10))
         if opaque is None:
             opaque = hexmd5(os.urandom(10))
     if stale:
         options['stale'] = 'TRUE'
     if opaque is not None:
         options['opaque'] = opaque
     if algorithm is not None:
         options['algorithm'] = algorithm
     if qop is None:
         qop = ('auth',)
     return self._auth_header('digest', realm=realm, nonce=nonce,
                              qop=', '.join(qop), **options)
示例#18
0
 def _create_session(self, request, user=None):
     #Create new session and added it to the environment.
     old = request.cache.session
     if isinstance(old, Session):
         old.expiry = datetime.now()
         yield old.save()
     if not user:
         user = yield self._anonymous_user(request)
     expiry = datetime.now() + timedelta(seconds=self.session_expiry)
     pid = os.getpid()
     sa = os.urandom(self.salt_size)
     val = to_bytes("%s%s" % (pid, time.time())) + sa + self.secret_key
     id = sha1(val).hexdigest()
     #session is a reserved attribute in router, use dict access
     session = yield request.models[Session].new(id=id, expiry=expiry,
                                                 user=user)
     request.cache.user = session.user
     coroutine_return(session)
示例#19
0
    def _encode_params(self, params):
        content_type = self.headers.get('content-type')
        # No content type given, chose one
        if not content_type:
            content_type = FORM_URL_ENCODED

        if hasattr(params, 'read'):
            params = params.read()

        if content_type in JSON_CONTENT_TYPES:
            body = _json.dumps(params)
        elif content_type == FORM_URL_ENCODED:
            body = urlencode(tuple(split_url_params(params)))
        elif content_type == MULTIPART_FORM_DATA:
            body, content_type = encode_multipart_formdata(
                params, charset=self.charset)
        else:
            body = params
        return to_bytes(body, self.charset), content_type
示例#20
0
 def _create_session(self, request, user=None):
     #Create new session and added it to the environment.
     old = request.cache.session
     if isinstance(old, Session):
         old.expiry = datetime.now()
         yield old.save()
     if not user:
         user = yield self._anonymous_user(request)
     expiry = datetime.now() + timedelta(seconds=self.session_expiry)
     pid = os.getpid()
     sa = os.urandom(self.salt_size)
     val = to_bytes("%s%s" % (pid, time.time())) + sa + self.secret_key
     id = sha1(val).hexdigest()
     #session is a reserved attribute in router, use dict access
     session = yield request.models[Session].new(id=id,
                                                 expiry=expiry,
                                                 user=user)
     request.cache.user = session.user
     coroutine_return(session)
示例#21
0
    def _encode_params(self, params):
        content_type = self.headers.get('content-type')
        # No content type given, chose one
        if not content_type:
            content_type = FORM_URL_ENCODED

        if hasattr(params, 'read'):
            params = params.read()

        if content_type in JSON_CONTENT_TYPES:
            body = _json.dumps(params)
        elif content_type == FORM_URL_ENCODED:
            body = urlencode(tuple(split_url_params(params)))
        elif content_type == MULTIPART_FORM_DATA:
            body, content_type = encode_multipart_formdata(
                params, charset=self.charset)
        else:
            body = params
        return to_bytes(body, self.charset), content_type
示例#22
0
文件: __init__.py 项目: japaks/pulsar
    def encode_body(self):
        '''Encode body or url if the :attr:`method` does not have body.

        Called by the :meth:`encode` method.
        '''
        body = None
        if self.method in ENCODE_URL_METHODS:
            self.files = None
            self._encode_url(self.data)
        elif isinstance(self.data, bytes):
            assert self.files is None, ('data cannot be bytes when files are '
                                        'present')
            body = self.data
        elif is_string(self.data):
            assert self.files is None, ('data cannot be string when files are '
                                        'present')
            body = to_bytes(self.data, self.charset)
        elif self.data or self.files:
            if self.files:
                body, content_type = self._encode_files()
            else:
                body, content_type = self._encode_params()
            self.headers['Content-Type'] = content_type
        return body
示例#23
0
    def encode_body(self):
        '''Encode body or url if the :attr:`method` does not have body.

        Called by the :meth:`encode` method.
        '''
        body = None
        if self.method in ENCODE_URL_METHODS:
            self.files = None
            self._encode_url(self.data)
        elif isinstance(self.data, bytes):
            assert self.files is None, ('data cannot be bytes when files are '
                                        'present')
            body = self.data
        elif is_string(self.data):
            assert self.files is None, ('data cannot be string when files are '
                                        'present')
            body = to_bytes(self.data, self.charset)
        elif self.data or self.files:
            if self.files:
                body, content_type = self._encode_files()
            else:
                body, content_type = self._encode_params()
            self.headers['Content-Type'] = content_type
        return body
示例#24
0
 def decrypt(self, string_or_bytes):
     b = to_bytes(string_or_bytes, self.encoding)
     p = self.crypt_module.decrypt(b, self.secret_key)
     return p.decode(self.encoding)
示例#25
0
文件: test_tx.py 项目: robgil/pulsar
 def __call__(self, msg):
     id = to_bytes(gen_unique_id()[:8])
     self.requests[id] = d = Deferred()
     self.transport.write(id + to_bytes(msg) + self.separator)
     return d
示例#26
0
 def crypt_verify(self, encrypted, raw):
     '''Verify if the ``raw`` string match the ``encrypted`` string
     '''
     return self.crypt_module.verify(to_bytes(encrypted), to_bytes(raw),
                                     self.secret_key, **self.ckwargs)
示例#27
0
文件: tx.py 项目: japaks/pulsar
 def send_message(self, msg):
     id = to_bytes(gen_unique_id()[:8])
     self.requests[id] = Deferred()
     self.transport.write(id + to_bytes(msg) + self.separator)
     return self.requests[id]
示例#28
0
 def error_reply(self, error_string, prefix=None):
     prefix = (prefix or 'err').lower()
     return {prefix: to_bytes(error_string)}
示例#29
0
文件: user.py 项目: SirZazu/lux
 def encript(self, password):
     p = self.crypt_module.encrypt(to_bytes(password, self.encoding),
                                   self.secret_key, self.salt_size)
     return to_string(p, self.encoding)
示例#30
0
 def _encript(self, password):
     p = self.crypt_module.encrypt(to_bytes(password, self.encoding),
                                   self.secret_key, self.salt_size)
     return to_string(p, self.encoding)
示例#31
0
 def send(self, message):
     assert self._waiting is None
     self._waiting = d = Future(loop=self._loop)
     self._transport.sendto(to_bytes(message) + self.separator)
     return d
示例#32
0
 def send(self, message):
     assert self._waiting is None
     self._waiting = d = Future(loop=self._loop)
     self._transport.sendto(to_bytes(message)+self.separator)
     return d
示例#33
0
文件: client.py 项目: LJS109/pulsar
 def status_reply(self, status_string):
     return {'ok': to_bytes(status_string)}
示例#34
0
文件: tx.py 项目: BazookaShao/pulsar
 def send_message(self, msg):
     id = to_bytes(gen_unique_id()[:8])
     self.requests[id] = Deferred()
     self.transport.write(id + to_bytes(msg) + self.separator)
     return self.requests[id]
示例#35
0
 def challenge_response(self, key):
     sha1 = hashlib.sha1(to_bytes(key + WEBSOCKET_GUID))
     return native_str(base64.b64encode(sha1.digest()))
示例#36
0
 def status_reply(self, status_string):
     return {'ok': to_bytes(status_string)}
示例#37
0
 def decrypt(self, string_or_bytes):
     b = to_bytes(string_or_bytes, self.encoding)
     p = self.crypt_module.decrypt(b, self.secret_key)
     return p.decode(self.encoding)
示例#38
0
文件: client.py 项目: LJS109/pulsar
 def error_reply(self, error_string, prefix=None):
     prefix = (prefix or 'err').lower()
     return {prefix: to_bytes(error_string)}
示例#39
0
 def challenge_response(self, key):
     sha1 = hashlib.sha1(to_bytes(key+WEBSOCKET_GUID))
     return native_str(base64.b64encode(sha1.digest()))