def authenticate(self, channel, socket_id, custom_data=None): """Used to generate delegated client subscription token. :param channel: name of the channel to authorize subscription to :param socket_id: id of the socket that requires authorization :param custom_data: used on presence channels to provide user info """ channel = validate_channel(channel) if not channel_name_re.match(channel): raise ValueError('Channel should be a valid channel, got: %s' % channel) socket_id = validate_socket_id(socket_id) if custom_data: custom_data = json.dumps(custom_data, cls=self._json_encoder) string_to_sign = "%s:%s" % (socket_id, channel) if custom_data: string_to_sign += ":%s" % custom_data signature = sign(self.secret, string_to_sign) auth = "%s:%s" % (self.key, signature) result = {'auth': auth} if custom_data: result['channel_data'] = custom_data return result
def authenticate_subscription(self, channel, socket_id, custom_data=None): """Used to generate delegated client subscription token. :param channel: name of the channel to authorize subscription to :param socket_id: id of the socket that requires authorization :param custom_data: used on presence channels to provide user info """ if not isinstance(channel, six.text_type): raise TypeError('Channel should be %s' % text) if not channel_name_re.match(channel): raise ValueError('Channel should be a valid channel, got: %s' % channel) if not isinstance(socket_id, six.text_type): raise TypeError('Socket ID should %s' % text) if custom_data: custom_data = json.dumps(custom_data) string_to_sign = "%s:%s" % (socket_id, channel) if custom_data: string_to_sign += ":%s" % custom_data signature = hmac.new(self.secret.encode('utf8'), string_to_sign.encode('utf8'), hashlib.sha256).hexdigest() auth = "%s:%s" % (self.key, signature) result = {'auth': auth} if custom_data: result['channel_data'] = custom_data return result
def authenticate(self, channel, socket_id, custom_data=None): """Used to generate delegated client subscription token. :param channel: name of the channel to authorize subscription to :param socket_id: id of the socket that requires authorization :param custom_data: used on presence channels to provide user info """ channel = validate_channel(channel) if not channel_name_re.match(channel): raise ValueError('Channel should be a valid channel, got: %s' % channel) socket_id = validate_socket_id(socket_id) if custom_data: custom_data = json.dumps(custom_data, cls=self._json_encoder) string_to_sign = "%s:%s" % (socket_id, channel) if custom_data: string_to_sign += ":%s" % custom_data signature = sign(self.secret, string_to_sign) auth = "%s:%s" % (self.key, signature) response_payload = {"auth": auth} if is_encrypted_channel(channel): shared_secret = generate_shared_secret( ensure_binary(channel, "channel"), self._encryption_master_key) shared_secret_b64 = base64.b64encode(shared_secret) response_payload["shared_secret"] = shared_secret_b64 if custom_data: response_payload['channel_data'] = custom_data return response_payload