public RODResponseMessage Post(handle login)
        {
            RODResponseMessage msg = new RODResponseMessage();

            if (login.publicKey.Length <= 4)
            {
                msg.result = 0;
                msg.message = "Unable to login. Please re-type your private key (case sensitive.)";
                return msg;
            }

            handle og = (from m in db.handles
                         where m.name == login.name.ToLower() && m.active == 1 && m.privateKey.StartsWith(login.publicKey)
                         select m).FirstOrDefault();

            if(og != null && Membership.ValidateUser(og.publicKey, og.privateKey)) {
                FormsAuthentication.SetAuthCookie(og.publicKey, true);
                msg.result = 1;
                msg.message = og.privateKey;
            } else {
                msg.result = 0;
                msg.message = "Unable to login. Please re-type your private key (case sensitive.)";
            }

            return msg;
        }
Example #2
0
        //
        // GET: /Aes/
        public RODResponseMessage Get()
        {
            string Plain_Text;
            string Decrypted;
            string Encrypted_Text;
            byte[] Encrypted_Bytes;

            string TestKey = "SETHAwQFBgcICQoLDA0ODw==";

            //This class here the Rijndael is what will have most all of the methods we need to do aes encryption.
            //When this is called it will create both a key and Initialization Vector to use.
            RijndaelManaged Crypto = new RijndaelManaged();

            //This is just here to convert the Encrypted byte array to a string for viewing purposes.
            System.Text.UTF8Encoding UTF = new System.Text.UTF8Encoding();

            Crypto.Key = Convert.FromBase64String(TestKey);

            Console.WriteLine("Current Key: " + System.Text.Encoding.UTF8.GetString(Crypto.Key));
            Console.WriteLine("Please put in the text to be encrypted.");
            Plain_Text = "hi from seth";

            Encrypted_Bytes = encrypt_function(Plain_Text, Crypto.Key, Crypto.IV);
            Encrypted_Text = UTF.GetString(Encrypted_Bytes);
            Decrypted = decrypt_function(Encrypted_Bytes, Crypto.Key, Crypto.IV);

            RODResponseMessage m = new RODResponseMessage();
            m.message = Encrypted_Text;
            m.result = 1;

            return m;
        }
        // return login status
        // GET api/login
        public RODResponseMessage Get()
        {
            RODResponseMessage msg = new RODResponseMessage();
            if (User.Identity.IsAuthenticated)
            {
                msg.result = 1;
                msg.message = User.Identity.Name;
            }
            else
            {
                msg.result = 0;
                msg.message = "Anon";
            }

            return msg;
        }
        public RODResponseMessage Delete([FromBody]string s)
        {
            RODResponseMessage result = new RODResponseMessage();
            handle to_handle = (from m in db.handles where m.name == s && m.active == 1 select m).FirstOrDefault();

            int removeResult = removeContact(to_handle.id);

            if (removeResult == 0)
            {
                result.result = 0;
                result.message = "Contact not found.";
            }
            else
            {
                result.message = "Contact removed.";
                result.result = 1;
            }

            return result;
        }
        public async Task<RODResponseMessage> Get(string id)
        {
            handle logged_in = (from handle r in db.handles where r.userGuid.Equals(User.Identity.Name) select r).FirstOrDefault();
            thread selected_thread = (from thread r in db.threads where r.groupKey == id select r).FirstOrDefault();

            RODResponseMessage msg = new RODResponseMessage();

            msg.result = 0;
            msg.message = "Error.";

            if (selected_thread != null && selected_thread.fromHandle.id == logged_in.id)
            {

                ////string alert_message = logged_in.name + " sent you a snap";
                // post the message to urbanairship now
                //AirshipChatNotificationRESTService service = new AirshipChatNotificationRESTService();
                //AirshipResponse arg = await service.SendChat(selected_thread.toHandle.publicKey, alert_message, id, logged_in.publicKey, -1);

                //msg.result = 1;
                //msg.message = arg.message;
            }

            return msg;
        }
        public RODResponseMessage Post([FromBody]string s)
        {
            RODResponseMessage result = new RODResponseMessage();

            string user_id = User.Identity.Name;
            handle logged_in = (from handle r in db.handles where r.userGuid.Equals(User.Identity.Name) select r).FirstOrDefault();

            // the person posting to this method has just had
            // an authorization request sent to THEM, so they need
            // to get the handle of the sender (From handle)

            handle from_handle = (from m in db.handles where m.publicKey == s && m.active == 1 select m).FirstOrDefault();
            if (from_handle == null)
            {
                result.result = 0;
                result.message = "Handle not found.";
            }
            else
            {

                follower added = (from m in db.followers where
                                  m.followerHandle.id.Equals(from_handle.id) &&
                                  m.followeeHandle.publicKey.Equals(s) &&
                                  m.active.Equals(1) select m).FirstOrDefault();

                if (added != null)
                {
                    result.message = "Person is already a contact.";
                    result.result = 0;
                    return result;
                }

                follower f = new follower();
                f.followeeId = logged_in.id;
                f.followerId = from_handle.id;
                f.active = 1;
                db.followers.Add(f);

                // now make it reciprocal too
                follower g = new follower();
                g.followeeId = from_handle.id;
                g.followerId = logged_in.id;
                g.active = 1;
                db.followers.Add(g);

                // trash the old message

                thread request = (from m in db.threads
                                  where m.toHandleId.Equals(logged_in.id) &&
                                      m.fromHandleId == from_handle.id &&
                                      m.active == 1 &&
                                      m.authorizeRequest == 1
                                  select m).FirstOrDefault();

                if (request != null)
                {
                    // it really has to exist, otherwise something
                    // sketchy is going on
                    request.active = 0;

                    db.threads.Attach(request);
                    var updated_request = db.Entry(request);

                    updated_request.Property(e => e.active).IsModified = true;
                }

                db.SaveChanges();

                result.message = "Successful add.";
                result.result = 1;
            }

            return result;
        }
        public RODResponseMessage Post([FromBody]string s)
        {
            RODResponseMessage result = new RODResponseMessage();

            string user_id = User.Identity.Name;
            handle from_handle = (from handle r in db.handles where r.userGuid.Equals(User.Identity.Name) select r).FirstOrDefault();

            handle to_handle = (from m in db.handles where m.name == s && m.active == 1 select m).FirstOrDefault();
            if (to_handle == null)
            {
                result.result = 0;
                result.message = "Handle not found.";
            }
            else
            {

                // check to make sure that this person is not blocked

                block blocked = (from m in db.blocks where m.blockedByHandleId == to_handle.id
                                 && m.blockedHandleId == from_handle.id && m.active == 1 select m).FirstOrDefault();

                if (blocked != null)
                {
                    result.message = "Unable to send request.";
                    result.result = 0;
                    return result;
                }

                follower currently_added = (from m in db.followers where m.followerHandle.id.Equals(from_handle.id) && m.followeeHandle.name.Equals(s) && m.active.Equals(1) select m).FirstOrDefault();

                if (currently_added != null)
                {
                    result.message = "Person is already a contact.";
                    result.result = 0;
                    return result;
                }

                // DISABLED --> for now ^^
                //follower f = new follower();
                //f.followeeId = to_handle.id;
                //f.followerId = from_handle.id;
                //f.active = 1;
                //db.followers.Add(f);
                //db.SaveChanges();

                result.message = to_handle.publicKey;
                result.result = 1;
            }

            return result;
        }
        public async Task<RODResponseMessage> Post(Chat msg)
        {
            RODResponseMessage response = new RODResponseMessage();

            string user_id = User.Identity.Name;
            handle logged_in = (from handle r in db.handles where r.userGuid.Equals(User.Identity.Name) select r).FirstOrDefault();

            // now loop through all of the clients,
            // and only send the message to the appropriate one...
            thread selected_thread = (from thread m in db.threads where m.groupKey == msg.groupKey select m).FirstOrDefault();

            // how to determine who this should be sent to:
            // -- find the thread with the group key
            // -- check to see if fromHandle is same as chatter,
                  // if it is, then we are to the other person

            string notify_public_key = "";
            if (selected_thread.fromHandle.id == logged_in.id)
            {
                notify_public_key = selected_thread.toHandle.publicKey;
            }
            else
            {
                notify_public_key = selected_thread.fromHandle.publicKey;
            }


            // toId refers to the opposite of the thread starter.
            // the thread starter can have several convos open,
            // and toId is how we distinguish which one is which.


            //int toId = 1;
            //if (chatter.handle.id == selected_thread.fromHandle.id)
            //{
            //    // chat is from the user who started the thread,
            //    // so how can i get the other person's id...

            //    // it's either the toHandleId...
            //    toId = selected_thread.toHandle.id;
            //}
            //else
            //{
            //    // chat is from user who did not start the thread,
            //    // so the toId is their chatter.handle.id,
            //    toId = chatter.handle.id;
            //}

            handle toHandle = (from m in db.handles where m.publicKey == msg.toKey select m).FirstOrDefault();


            // make sure that the user i not blocked
            handle msg_sent_to_user = (from m in db.handles where m.publicKey == notify_public_key select m).FirstOrDefault();
            block blocked = (from m in db.blocks
                             where m.blockedByHandleId == msg_sent_to_user.id
                                 && m.blockedHandleId == logged_in.id && m.active == 1
                             select m).FirstOrDefault();
            if (blocked != null)
            {
                // bail out, don't send the message
                return new RODResponseMessage() { message = "Blocked.", result = 0 };
            }

            // i really should get a reference to the hub from here,
            // and use that to send out the push message... but since the hub
            // isn't even working yet, we can put that off for now
            // Clients.Client(a.connectionId).addMessage(name, message, groupKey);

            message clean_message = new message();
            clean_message.fromHandleId = logged_in.id;
            clean_message.sentDate = DateTime.UtcNow;
            clean_message.messageText = msg.message;
            clean_message.threadId = selected_thread.id;
            clean_message.toHandleId = toHandle.id;

            db.messages.Add(clean_message);
            db.SaveChanges();

            // send a notification
            string alert_message = logged_in.name + " said, '" + msg.message +"'";
            AirshipChatNotificationRESTService service = new AirshipChatNotificationRESTService();
            AirshipResponse arg = await service.SendChat(notify_public_key, alert_message, msg.message, selected_thread.groupKey, logged_in.publicKey, clean_message.id);

            response = new RODResponseMessage() { message = "Success.", result = clean_message.id };

            return response;
        }
        public RODResponseMessage Delete([FromBody]string s)
        {
            RODResponseMessage result = new RODResponseMessage();

            thread selected = (from m in db.threads where m.groupKey.Equals(s) select m).FirstOrDefault();

            string user_id = User.Identity.Name;
            handle from_handle = (from handle r in db.handles where r.userGuid.Equals(User.Identity.Name) select r).FirstOrDefault();

            // only thread starter can delete for now
            if (selected.fromHandle.id.Equals(from_handle.id) || selected.toHandle.id.Equals(from_handle.id))
            {

                selected.active = 0;

                db.threads.Attach(selected);
                var updated_thread = db.Entry(selected);

                updated_thread.Property(e => e.active).IsModified = true;
                db.SaveChanges();

                result.result = 1;
                result.message = "Success.";

            }
            else
            {
                result.result = 0;
                result.message = "Unauthorized.";
            }

            return result;

        }
        public async Task<RODResponseMessage> Post(Snap _snap)
        {

            RODResponseMessage msg = new RODResponseMessage();

            if (_snap == null)
            {
                msg.result = -1;
                msg.message = "Snap was null.";
                return msg;
            }

            string user_id = User.Identity.Name;
            handle logged_in = (from handle r in db.handles where r.userGuid.Equals(User.Identity.Name) select r).FirstOrDefault();
            handle to_handle = (from handle r in db.handles where r.publicKey == _snap.toGuid select r).FirstOrDefault();


            // check to make sure that the thread sender 
            // is not blocked by the recipent
            block blocked = (from m in db.blocks
                             where m.blockedByHandleId == to_handle.id
                                 && m.blockedHandleId == logged_in.id && m.active == 1
                             select m).FirstOrDefault();
            if (blocked != null)
            {
                // bail out, don't send the message
                msg.result = -1;
                msg.message = "Unable to send message.";
                return msg;
            }


            thread clean_thread = new thread();
            clean_thread.active = 1;
            clean_thread.startDate = DateTime.UtcNow;
            clean_thread.fromHandleId = logged_in.id;
            clean_thread.caption = _snap.caption;
            clean_thread.hearts = 0;
            clean_thread.toHandleId = to_handle.id;
            clean_thread.location = _snap.location;
            clean_thread.font = _snap.font;
            clean_thread.textColor = _snap.textColor;

            //
            // check to see if the to_handle is authorized
            // to make the send, if they are not, then this is
            // an authorization request. 
            //
            clean_thread.authorizeRequest = 0;

            //
            // we don't need to do this check if this is for
            // toHandleId = 1 or toHandleId = 2 (snaps sent
            // to profile and the daily)
            //

            if (clean_thread.toHandleId != 1 && clean_thread.toHandleId != 2)
            {

                follower confirmed_to_handle_follower = (from m in db.followers
                                                         where m.followeeId == to_handle.id &&
                                                         m.followerId == logged_in.id &&
                                                         m.active == 1
                                                         select m).FirstOrDefault();

                if (confirmed_to_handle_follower == null)
                {
                    clean_thread.authorizeRequest = 1;
                }

            }

            // should it all be organized around this group key then?
            // add new threads and messages at the same with the same
            // group key generated on the client (like a guid)...

            clean_thread.groupKey = _snap.groupKey;
            db.threads.Add(clean_thread);
            db.SaveChanges();

            if (_snap.caption != null)
            {

                if (_snap.caption.Length > 0)
                {
                    message chat_message = new message();
                    chat_message.threadId = clean_thread.id;
                    chat_message.messageText = clean_thread.caption;
                    chat_message.fromHandleId = clean_thread.fromHandleId;
                    chat_message.sentDate = DateTime.UtcNow;
                    chat_message.toHandleId = to_handle.id;
                    db.messages.Add(chat_message);
                    db.SaveChanges();
                }
            }

            msg.message = clean_thread.id.ToString();
            msg.result = 1;


            return msg;
        }