Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
body11 = "hi #11"
body12 = "hi #12"
body21 = "hi #21"
body22 = "hi #22"
bodyU1 = "We do not want to see this! #1"
bodyU2 = "We do not want to see this! #2"
m11 = Message.objects.create(sender=self.participant1, thread=self.thread1, body=body11)
m12 = Message.objects.create(sender=self.participant2, thread=self.thread1, body=body12)
m21 = Message.objects.create(sender=self.participant3, thread=self.thread2, body=body21)
m22 = Message.objects.create(sender=self.participant1, thread=self.thread2, body=body22)
mU1 = Message.objects.create(sender=self.participant2, thread=self.thread_unrelated, body=bodyU1)
mU2 = Message.objects.create(sender=self.participant3, thread=self.thread_unrelated, body=bodyU2)
# the channels are private
# this means that Centrifugo will check the users ids to know if the user may connect
# if we query a private channel the user does not belong to, we never see the message
client = Client("{0}api/".format(getattr(settings, "CENTRIFUGE_ADDRESS")), getattr(settings, "CENTRIFUGE_SECRET"))
forbidden_message = "Message forbidden"
client.publish(
build_channel(namespace=settings.CENTRIFUGO_MESSAGE_NAMESPACE, name=self.thread_unrelated.id, user_ids=[p.id for p in self.thread_unrelated.participants.all()]),
forbidden_message
)
# we wait a little bit
time.sleep(4)
# now the messages should be displayed
m11 = self.selenium.find_element_by_id('message__{0}'.format(m11.id))
m12 = self.selenium.find_element_by_id('message__{0}'.format(m12.id))
m21 = self.selenium.find_element_by_id('message__{0}'.format(m21.id))
m22 = self.selenium.find_element_by_id('message__{0}'.format(m22.id))
self.assertTrue(body11 in m11.text)
self.assertTrue(body12 in m12.text)
self.assertTrue(body21 in m21.text)
self.assertTrue(body22 in m22.text)
def get_connection_parameters(user, info=''):
timestamp = str(int(time.time()))
user_pk = str(user.pk) if _is_authenticated(user) else ""
token = generate_token(
settings.CENTRIFUGE_SECRET,
user_pk,
timestamp,
info=info
)
return {
'sockjs_endpoint': settings.CENTRIFUGE_ADDRESS + '/connection',
'ws_endpoint': settings.CENTRIFUGE_ADDRESS + '/connection/websocket',
'user': user_pk,
'timestamp': timestamp,
'token': token,
'info': info
}
def get_auth_data():
user = USER_ID
now = str(int(time.time()))
token = generate_token(options.project_secret, options.project_key, user, now, info=INFO)
auth_data = {
'token': token,
'user': user,
'project': options.project_key,
'timestamp': now,
'info': INFO
}
return auth_data
def post(self):
#raise tornado.web.HTTPError(403)
logging.info("client wants to refresh its connection parameters")
user = USER_ID
now = str(int(time.time()))
token = generate_token(options.project_secret, options.project_key, user, now, info=INFO)
to_return = {
'token': token,
'user': user,
'project': options.project_key,
'timestamp': now,
'info': INFO
}
self.set_header('Content-Type', 'application/json; charset="utf-8"')
self.write(json.dumps(to_return))
def mq_generate_token(user, timestamp, info=""):
token = generate_token(SECRET_KEY, user, timestamp, info)
return token
@register.simple_tag
def mq_generate_token(user, timestamp, info=""):
token = generate_token(SECRET_KEY, user, timestamp, info)
if DEBUG is True:
print "Generating token:"
print "Key: "+SECRET_KEY
print "User: "+user
print "Timestamp: "+timestamp
print "Generated token for user "+user+" at "+timestamp+": "+token
return token
def post(self, request, *args, **kwargs):
"""
Returns a token identifying the user in Centrifugo.
"""
current_timestamp = "%.0f" % time.time()
user_id_str = u"{0}".format(request.user.id)
token = generate_token(settings.CENTRIFUGE_SECRET, user_id_str, "{0}".format(current_timestamp), info="")
# we get all the channels to which the user can subscribe
participant = Participant.objects.get(id=request.user.id)
# we use the threads as channels ids
channels = []
for thread in Thread.managers.get_threads_where_participant_is_active(participant_id=participant.id):
channels.append(
build_channel(settings.CENTRIFUGO_MESSAGE_NAMESPACE, thread.id, thread.participants.all())
)
# we also have a channel to alert us about new threads
threads_channel = build_channel(settings.CENTRIFUGO_THREAD_NAMESPACE, request.user.id, [request.user.id]) # he is the only one to have access to the channel
channels.append(threads_channel)
# we return the information
def __init__(self, address=None, key=None, secret=None, timeout=10, json_encoder=None):
self.address = address or settings.CENTRIFUGE_ADDRESS
self.project_key = key or settings.CENTRIFUGE_PROJECT_KEY
self.project_secret = secret or settings.CENTRIFUGE_PROJECT_SECRET
self.timeout = timeout or settings.CENTRIFUGE_TIMEOUT
self.api_address = self.address
self._client = RawClient(
self.api_address, self.project_key, self.project_secret,
timeout=self.timeout, json_encoder=json_encoder
)
def publish_participation_to_thread(sender, instance, created, **kwargs):
""" Warns users everytime a thread including them is published. This is done via channel subscription. """
if kwargs.get('created_and_add_participants') is True:
request_participant_id = kwargs.get('request_participant_id')
if request_participant_id is not None:
client = Client("{0}api/".format(getattr(settings, "CENTRIFUGE_ADDRESS")), getattr(settings, "CENTRIFUGE_SECRET"))
active_participants = [participation.participant for participation in Participation.objects.filter(thread=instance, date_left__isnull=True).select_related('participant')]
for participant in active_participants:
client.publish(
build_channel(settings.CENTRIFUGO_THREAD_NAMESPACE, participant.id, [participant.id]),
{
"message_channel_to_connect_to": build_channel(settings.CENTRIFUGO_MESSAGE_NAMESPACE, instance.id, [p.id for p in active_participants])
}
def publish_message_to_centrifugo(sender, instance, created, **kwargs):
""" Publishes each saved message to Centrifugo. """
if created is True:
client = Client("{0}api/".format(getattr(settings, "CENTRIFUGE_ADDRESS")), getattr(settings, "CENTRIFUGE_SECRET"))
# we ensure the client is still in the thread (he may have left or have been removed)
active_participants = [participation.participant.id for participation in Participation.objects.filter(thread=instance.thread, date_left__isnull=True).select_related('participant')]
client.publish(
build_channel(settings.CENTRIFUGO_MESSAGE_NAMESPACE, instance.thread.id, active_participants),
{
"id": instance.id,
"body": instance.body,
"sender": instance.sender.id,
"thread": instance.thread.id,
"sent_at": str(instance.sent_at),
"is_notification": True, # ATTENTION: check against sender too to be sure to not notify him his message
}