예제 #1
0
        public void ProcessRequest(HttpContext context)
        {
            try{
                IBabyDataSource Sql = new SqliteWrapper (ConfigurationManager.ConnectionStrings["baby_data"].ConnectionString);
                AuthMethod Authentication = new HttpBasic(Sql);

                //set headers
                context.Response.ContentType = "application/json";
                context.Response.ContentEncoding = System.Text.Encoding.GetEncoding ("UTF-8");

                //get teh user?
                User LoggedIn =null;
                try{
                    LoggedIn = Authentication.Login (context.Request);
                }
                catch(AuthException ae){
                    Authentication.HandleFailure (context.Response, ae);
                }

                //if the user cannot login exit.
                //They will have been redirected or tnotified by the auth system.
                if (LoggedIn != null) {

                    //What is the user trying to do?
                    string key = context.Request ["type"];

                    //is that supported? Can we do that?
                    if (Responders.ContainsKey (key)){
                        if(Responders[key].HasPermision(LoggedIn, context.Request,Sql)) {
                            Responders [key].RespondToRequest (LoggedIn, context.Request, context.Response, Sql);
                        }
                        else{
                            throw new AuthException("Access was Denied, Chief.");
                        }
                    }
                    else{
                        throw new NotSupportedException(" Your request is not supported, yet:(");
                    }
                }

            }
            catch(Exception ex){
                context.Response.StatusCode = 500;
                context.Response.Write(String.Format(
                    @"{{""server_error"":{{""message"":""{0}"",""type"":""{1}""}}}}",
                    ex.Message, ex.GetType()));
            }
        }
예제 #2
0
        public void ProcessRequest(HttpContext context)
        {
            try{
                IBabyDataSource ds = new SqliteWrapper (ConfigurationManager.ConnectionStrings["baby_data"].ConnectionString);
                List<string> errors = new List<string> ();

                string username = context.Request["user"];
                string password = context.Request["pass"];
                string email = context.Request["mail"];

                if(String.IsNullOrEmpty(username)){
                    errors.Add("Must specify Username");
                }
                if(String.IsNullOrEmpty(password)){
                    errors.Add("Must specify Password");
                }
                if(String.IsNullOrEmpty(email)){
                    errors.Add("Must specify email");
                }

                User u=null;
                if(errors.Count == 0){
                    //confirm that there is no user with that username
                    u = ds.ReadUser(username);

                    if (u != null && u.Username == username) {
                        errors.Add ("Username in use");
                    }

                    //confirm password meets requirements
                    if (password.Length < MIN_PW_LENGTH) {
                        errors.Add ("Password must be at least " + MIN_PW_LENGTH +
                            " characters long");
                    }

                    if (!email.Contains ("@")) {
                        errors.Add ("Email addresses must contain @");
                    }
                }
                //set headers
                context.Response.ContentType = "application/json";
                context.Response.ContentEncoding = System.Text.Encoding.GetEncoding ("UTF-8");

                //create a new user
                if(errors.Count ==0 ){
                    u = new User();
                    u.Username = username;
                    u.Email = email;
                    u.Hash = u.BuildHash (password);

                    u = ds.CreateUser (u, u);
                    context.Response.Write(String.Format(
                        @"{{success:{{registered:{0} }} }}", u.ToJSON()));
                }
                else{
                    context.Response.Write(String.Format(
                    @"{{errors:[""{0}""]}}", String.Join("\",\"",errors)));
                }
            }
            catch(Exception ex){
                context.Response.StatusCode = 500;
                context.Response.Write(String.Format(
                    @"{{server_error:{{""message"":""{0}"",""type"":""{1}""}}}}",
                    ex.Message, ex.GetType()));
            }
        }