Ejemplo n.º 1
0
Archivo: auth.py Proyecto: cespare/prat
def create_or_login(resp):
  user = None
  session["email"] = resp.email
  user = db.users.find_one({ "email" : resp.email })
  if user is not None:
    g.user = user
  else:
    default_channels = current_app.config["DEFAULT_CHANNELS"]
    gravatar_url = "//www.gravatar.com/avatar/" + md5(resp.email.lower()).hexdigest() + "?"
    gravatar_url += urllib.urlencode({ 's':str(18) })

    user_object = {
        "openid": resp.identity_url,
        "name": resp.fullname or resp.nickname,
        "email": resp.email,
        "gravatar": gravatar_url,
        "last_selected_channel": default_channels[0],
        "channels": default_channels,
        "api_key": str(uuid.uuid4()),
        "secret": str(uuid.uuid4()),
    }
    db.users.save(user_object)
    push_socket = zmq_context.socket(zmq.PUSH)
    push_socket.connect(current_app.config["PUSH_ADDRESS"])
    for channel in default_channels:
      add_user_to_channel(user_object, channel)
      send_join_channel(channel, user_object, push_socket)
    push_socket.close()
    g.user = user_object
  flash(u'Successfully logged in')
  return redirect(oid.get_next_url())
Ejemplo n.º 2
0
def create_or_login(resp):
    user = None
    session["email"] = resp.email
    user = db.users.find_one({"email": resp.email})
    if user is not None:
        g.user = user
    else:
        default_channels = current_app.config["DEFAULT_CHANNELS"]
        gravatar_url = "http://www.gravatar.com/avatar/" + md5(
            resp.email.lower()).hexdigest() + "?"
        gravatar_url += urllib.urlencode({'s': str(18)})

        user_object = {
            "openid": resp.identity_url,
            "name": resp.fullname or resp.nickname,
            "email": resp.email,
            "gravatar": gravatar_url,
            "last_selected_channel": default_channels[0],
            "channels": default_channels,
            "api_key": str(uuid.uuid4()),
            "secret": str(uuid.uuid4()),
        }
        db.users.save(user_object)
        push_socket = zmq_context.socket(zmq.PUSH)
        push_socket.connect(current_app.config["PUSH_ADDRESS"])
        for channel in default_channels:
            add_user_to_channel(user_object, channel)
            send_join_channel(channel, user_object, push_socket)
        push_socket.close()
        g.user = user_object
    flash(u'Successfully logged in')
    return redirect(oid.get_next_url())
Ejemplo n.º 3
0
def eventhub_client():
  websocket = request.environ.get('wsgi.websocket')
  if not websocket:
    return
  push_socket = zmq_context.socket(zmq.PUSH)
  push_socket.connect(current_app.config["PUSH_ADDRESS"])
  subscribe_socket = zmq_context.socket(zmq.SUB)
  client_id = str(uuid.uuid4())

  subscribe_socket.connect(current_app.config["SUBSCRIBE_ADDRESS"])

  # add yourself to your current pool of open clients
  add_to_user_clients(g.user, client_id)

  # listen for messages that happen on channels the user is subscribed to
  for channel in g.user["channels"]:
    channel_id = zmq_channel_key(channel)
    subscribe_socket.setsockopt(zmq.SUBSCRIBE, channel_id)

    # if redis was cleared, we'll need to resend the join channel event to populate the user status of open
    # clients
    channel_status = get_user_channel_status(g.user, channel)
    if channel_status is None:
      send_join_channel(channel, g.user, push_socket)

    set_user_channel_status(g.user, channel, "active")
    send_user_status_update(g.user, channel, push_socket, "active")

  # subscribe to events the user triggered that could affect the user's other open clients
  subscribe_socket.setsockopt(zmq.SUBSCRIBE, str(g.user["email"]))

  poller = zmq.Poller()
  poller.register(subscribe_socket, zmq.POLLIN)
  poller.register(websocket.socket, zmq.POLLIN)

  try:
    message = None
    while True:
      events = dict(poller.poll())

      # Server -> Client
      if subscribe_socket in events:
        message = subscribe_socket.recv()

        # the message is prepended by the channel_id (for PUB/SUB reasons)
        channel_id, packed = message.split(" ", 1)

        unpacked = json.loads(packed)

        action = unpacked["action"]
        if action in ["publish_message", "join_channel", "leave_channel", "user_active", "user_offline"]:
          websocket.send(packed)
        elif action in ["self_join_channel", "self_leave_channel"]:
          event_type = action.split("_")[1]
          handle_self_channel_event(client_id, websocket, subscribe_socket, unpacked["data"], event_type)

      # Client -> Server
      if websocket.socket.fileno() in events:
        socket_data = websocket.receive()
        if socket_data is None:
          break
        socket_data = json.loads(socket_data)
        action = socket_data["action"]
        data = socket_data["data"]
        if action == "switch_channel":
          handle_switch_channel(data["channel"])
        elif action == "publish_message":
          handle_publish_message(data, push_socket)
        elif action == "preview_message":
          handle_preview_message(data, websocket)
        elif action == "join_channel":
          handle_join_channel(data["channel"], subscribe_socket, push_socket, client_id)
        elif action == "leave_channel":
          handle_leave_channel(data["channel"], subscribe_socket, push_socket, client_id)
        elif action == "reorder_channels":
          handle_reorder_channels(data["channels"], push_socket, client_id)
        elif action == "ping":
          handle_ping(websocket, push_socket, client_id)
  except geventwebsocket.WebSocketError, e:
    print "{0} {1}".format(e.__class__.__name__, e)
Ejemplo n.º 4
0
def eventhub_client():
  if not request.environ.get('wsgi.websocket'):
    return ""
  websocket = request.environ['wsgi.websocket']
  push_socket = zmq_context.socket(zmq.PUSH)
  push_socket.connect(current_app.config["PUSH_ADDRESS"])
  subscribe_socket = zmq_context.socket(zmq.SUB)
  client_id = str(uuid.uuid4())

  subscribe_socket.connect(current_app.config["SUBSCRIBE_ADDRESS"])

  # add yourself to your current pool of open clients
  add_to_user_clients(g.user, client_id)

  # listen for messages that happen on channels the user is subscribed to
  for channel in g.user["channels"]:
    channel_id = zmq_channel_key(channel)
    subscribe_socket.setsockopt(zmq.SUBSCRIBE, channel_id)

    # if redis was cleared, we'll need to resend the join channel event to populate the user status of open
    # clients
    channel_status = get_user_channel_status(g.user, channel)
    if channel_status is None:
      send_join_channel(channel, g.user, push_socket)

    set_user_channel_status(g.user, channel, "active")
    send_user_status_update(g.user, channel, push_socket, "active")

  # subscribe to events the user triggered that could affect the user's other open clients
  subscribe_socket.setsockopt(zmq.SUBSCRIBE, str(g.user["email"]))

  poller = zmq.Poller()
  poller.register(subscribe_socket, zmq.POLLIN)
  poller.register(websocket.socket, zmq.POLLIN)

  try:
    message = None
    while True:
      events = dict(poller.poll())

      # Server -> Client
      if subscribe_socket in events:
        message = subscribe_socket.recv()

        # the message is prepended by the channel_id (for PUB/SUB reasons)
        channel_id, packed = message.split(" ", 1)
        g.msg_unpacker.feed(packed)
        unpacked = g.msg_unpacker.unpack()
        action = unpacked["action"]
        if action in ["publish_message", "join_channel", "leave_channel", "user_active", "user_offline"]:
          websocket.send(json.dumps(unpacked))
        elif action in ["self_join_channel", "self_leave_channel", "self_reorder_channels"]:
          event_type = action.split("_")[1]
          handle_self_channel_event(client_id, websocket, subscribe_socket, unpacked["data"], event_type)

      # Client -> Server
      if websocket.socket.fileno() in events:
        socket_data = websocket.receive()
        if socket_data is None:
          break
        socket_data = json.loads(socket_data)
        action = socket_data["action"]
        data = socket_data["data"]
        if action == "switch_channel":
          handle_switch_channel(data["channel"])
        elif action == "publish_message":
          handle_publish_message(data, push_socket)
        elif action == "preview_message":
          handle_preview_message(data, websocket)
        elif action == "join_channel":
          handle_join_channel(data["channel"], subscribe_socket, push_socket, client_id)
        elif action == "leave_channel":
          handle_leave_channel(data["channel"], subscribe_socket, push_socket, client_id)
        elif action == "reorder_channels":
          handle_reorder_channels(data["channels"], push_socket, client_id)
  except geventwebsocket.WebSocketError, e:
    print "{0} {1}".format(e.__class__.__name__, e)