示例#1
0
        /* Internal API */
        private void DoAuth(MemoryStream ms)
        {
            //Get the length of the token.
            int tokenLength = (int)GetByte(ms);
            //Get this many bytes and convert it into the string.
            string token = Encoding.ASCII.GetString(GetBytes(ms, tokenLength));
            //Use the RPWS Oauth to get data.
            var user = Oauth.RpwsAuth.AuthenticateUser(token);

            //Create a reply.
            if (user == null)
            {
                SendData(new byte[] { 0x09, 0x01 });
            }
            //Accept and set up.
            user_uuid     = user.uuid;
            authenticated = true;
            Console.WriteLine("User with UUID " + user_uuid + " connected.");
            //Accept.
            SendData(new byte[] { 0x09, 0x00 });
            //Add myself to the list of clients.
            if (WebPebble.WebSockets.WebSocketServer.connectedClients.ContainsKey(user_uuid))
            {
                //Replace old phone user, if any.
                WebPebble.WebSockets.WebSocketServer.connectedClients[user_uuid].phone = this;
                pair = WebPebble.WebSockets.WebSocketServer.connectedClients[user_uuid];
                //If the web is connected, tell it we have connected.

                try
                {
                    pair.web.SetStatus(true);

                    pair.connected = true;
                }
                catch
                {
                    pair.connected = false;
                }

                SetStatus(pair.connected);
            }
            else
            {
                //Add ourself.
                WebSocketPair pair = new WebSocketPair
                {
                    phone = this
                };
                WebPebble.WebSockets.WebSocketServer.connectedClients.Add(user_uuid, pair);
                pair = WebPebble.WebSockets.WebSocketServer.connectedClients[user_uuid];
            }
            //Now, WebPebble will deal with it.
        }
示例#2
0
        /* Api */
        private void DoAuth(WebPebbleRequest req)
        {
            //Get the token that was offered. We get this from the HTTP request.
            string token = http.Request.Cookies["access-token"];
            //Check this against the RPWS server.
            E_RPWS_User user = Oauth.RpwsAuth.AuthenticateUser(token);

            if (user == null)
            {
                //Respond with failure.
                QuickReply(req.requestid, WebPebbleRequestType.Auth, new Dictionary <string, object>()
                {
                    { "ok", "false" }
                });
            }
            else
            {
                user_uuid     = user.uuid;
                authenticated = true;
                //Respond with ok.
                QuickReply(req.requestid, WebPebbleRequestType.Auth, new Dictionary <string, object>()
                {
                    { "ok", "true" }
                });
                //Add myself to the list of clients.
                if (WebPebble.WebSockets.WebSocketServer.connectedClients.ContainsKey(user_uuid))
                {
                    pair = WebPebble.WebSockets.WebSocketServer.connectedClients[user_uuid];
                    //If the phone is connected, tell it we have connected.
                    try
                    {
                        pair.phone.SetStatus(true);

                        pair.connected = true;
                    } catch
                    {
                        //Remain in "disconnected" state.
                        pair.connected = false;
                    }
                    //If another WebPebble session is connected, boot them.
                    try
                    {
                        pair.web.QuickReply(-1, WebPebbleRequestType.CloseOldClient, new Dictionary <string, object>()
                        {
                        });
                    } catch
                    {
                    }
                    //Replace old WebPebble user, if any.
                    WebPebble.WebSockets.WebSocketServer.connectedClients[user_uuid].web = this;
                    //Set our connection status.
                    SetStatus(pair.connected);
                }
                else
                {
                    //Add ourself.
                    WebSocketPair new_pair = new WebSocketPair
                    {
                        web = this
                    };
                    WebPebble.WebSockets.WebSocketServer.connectedClients.Add(user_uuid, new_pair);
                    this.pair = WebPebble.WebSockets.WebSocketServer.connectedClients[user_uuid];
                }
            }
        }