Beispiel #1
0
        public void Init()
        {
            ssa =  new SuperSimpleAuth ("testing_mtgdb.info", 
                "ae132e62-570f-4ffb-87cc-b9c087b09dfb");

            mtgdb = new Db("http://127.0.0.1:8082");

            repository =  new MongoRepository ("mongodb://localhost", mtgdb, ssa);
            admin = new Admin("http://127.0.0.1:8082");

            //Super simple auth can't delete from here. 
            try
            {
                repository.AddPlaneswalker ("mtgdb_tester", 
                    "test123", "*****@*****.**");
            }
            catch(Exception e)
            {
                System.Console.WriteLine (e.Message);
            }

            User ssaUser = ssa.Authenticate ("mtgdb_tester", 
                "test123", "127.0.0.1");

            mtgdbUser = new Planeswalker 
            {
                UserName = ssaUser.UserName,
                AuthToken = ssaUser.AuthToken,
                Email = ssaUser.Email,
                Id = ssaUser.Id,
                Claims = ssaUser.Claims,
                Roles = ssaUser.Roles,
                Profile = repository.GetProfile (ssaUser.Id)
            };
        }
Beispiel #2
0
        public void Init()
        {
            ssa = new SuperSimpleAuth("testing_mtgdb.info",
                                      "ae132e62-570f-4ffb-87cc-b9c087b09dfb");

            mtgdb = new Db("http://127.0.0.1:8082");

            repository = new MongoRepository("mongodb://localhost", mtgdb, ssa);
            admin      = new Admin("http://127.0.0.1:8082");

            //Super simple auth can't delete from here.
            try
            {
                repository.AddPlaneswalker("mtgdb_tester",
                                           "test123", "*****@*****.**");
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }

            User ssaUser = ssa.Authenticate("mtgdb_tester",
                                            "test123", "127.0.0.1");

            mtgdbUser = new Planeswalker
            {
                UserName  = ssaUser.UserName,
                AuthToken = ssaUser.AuthToken,
                Email     = ssaUser.Email,
                Id        = ssaUser.Id,
                Claims    = ssaUser.Claims,
                Roles     = ssaUser.Roles,
                Profile   = repository.GetProfile(ssaUser.Id)
            };
        }
 public void Init()
 {
     admin =     new Admin("http://127.0.0.1:8082");
     ssa =       new SuperSimpleAuth ("testing_mtgdb.info", 
         "ae132e62-570f-4ffb-87cc-b9c087b09dfb");
     ssaUser =   ssa.Authenticate ("mtgdb_tester", 
         "test123", "127.0.0.1");
 }
Beispiel #4
0
        public void Authenticate()
        {
            var api = new SuperSimpleAuth(LOCAL_KEY,
                                          LOCAL_URL);

            var created = api.CreateUser("test", "test", "*****@*****.**").Result;

            var user = api.Authenticate("test", "test").Result;

            Assert.True(!string.IsNullOrEmpty(user.Jwt));

            var valid = api.Validate(user).Result;

            Assert.True(valid);
            var u = api.Validate(user.AuthToken).Result;

            Assert.IsNotNull(u);
            u = api.Validate(user.Jwt);
            Assert.IsNotNull(u);

            user.Email = "*****@*****.**";
            user       = api.ChangeEmail(user, user.Email).Result;

            user.UserName = "******";
            user          = api.ChangeUserName(user, user.UserName).Result;

            valid = api.ChangePassword(user, "pazzwurd").Result;
            Assert.True(valid);

            var newpassword = api.Forgot(user.Email).Result;

            Assert.True(!string.IsNullOrEmpty(newpassword));

            valid = api.End(user).Result;
            Assert.True(valid);
            valid = api.Validate(user).Result;
            Assert.False(valid);

            user = api.Authenticate(user.UserName, newpassword).Result;

            Assert.IsNotNull(user);

            valid = api.Disable(user).Result;
            Assert.True(valid);
        }
Beispiel #5
0
        public void Init()
        {
            repository = new MongoRepository(connectionString);
            admin      = new Admin("http://127.0.0.1:8082");
            ssa        = new SuperSimpleAuth("testing_mtgdb.info",
                                             "ae132e62-570f-4ffb-87cc-b9c087b09dfb");

            ssaUser = ssa.Authenticate("mtgdb_tester",
                                       "test123", "127.0.0.1");
        }
Beispiel #6
0
        public LogonModule(SuperSimpleAuth ssa)
        {
            Get["/logon"] = parameters => {
                var model = new LogonModel();

                return(View["logon", model]);
            };

            Post["/logon"] = parameters => {
                var model = this.Bind <LogonModel>();
                model.Message = "Password or/and Username is incorrect.";

                User user = null;

                try
                {
                    user =
                        ssa.Authenticate(model.Username, model.Secret,
                                         this.Context.Request.UserHostAddress).Result;
                }
                catch (Exception e)
                {
                    model.Message = e.Message;
                    if (user == null)
                    {
                        return(View["logon", model]);
                    }
                }

                return(this.LoginAndRedirect(user.AuthToken,
                                             fallbackRedirectUrl: "/"));
            };

            //Get["/logoff"] = parameters => {
            //    var nuser = (NancyUserIdentity)Context.CurrentUser;
            //    ssa.End(nuser.AuthToken);

            //    return this.LogoutAndRedirect("/");
            //};

            Get ["/signup"] = parameters => {
                var signup = new SignupModel();
                return(View["signup", signup]);
            };

            Post ["/signup"] = parameters => {
                var signup = this.Bind <SignupModel>();
                var result = this.Validate(signup);
                signup.Errors = Error.GetValidationErrors(result).ToList();

                if (!result.IsValid)
                {
                    return(View["signup", signup]);
                }

                try
                {
                    ssa.CreateUser(signup.UserName, signup.Secret, signup.Email);
                }
                catch (Exception e)
                {
                    signup.Message = e.Message;
                    return(View["signup", signup]);
                }

                return(this.Response.AsRedirect("/"));
            };
        }
        public LogonModule (SuperSimpleAuth ssa)
        {
            this.ssa = ssa;

            Get["/settings"] = parameters => {
                SettingsModel model = new SettingsModel();

                if(this.Context.CurrentUser == null)
                {
                    return this.LogoutAndRedirect("/");
                }

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
               
                return View["Logon/Settings",model];
            };

            Post["/settings"] = parameters => {
                SettingsModel model = this.Bind<SettingsModel>();

                if(this.Context.CurrentUser == null)
                {
                    return this.LogoutAndRedirect("/");
                }

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
               

                if(Request.Form.Save != null)
                {
                    model.Planeswalker.Profile.Email = model.Email;
                    model.Planeswalker.Profile.Name = model.Name;

                    try
                    {
                        model.Planeswalker = repository.UpdatePlaneswalker(model.Planeswalker);
                    }
                    catch(Exception e)
                    {
                        model.Errors.Add("Could not update account");
                        model.Errors.Add(e.Message);
                    }
                }

                if(Request.Form.Delete != null)
                {
                    try
                    {
                        if(model.Yes)
                        {
                            ssa.Disable(model.Planeswalker.AuthToken);
                            repository.RemovePlaneswalker(model.Planeswalker.Id);
                            return this.LogoutAndRedirect("/");
                        }
                        else
                        {
                            model.Errors.Add("You must check, 'Yes, I know'. To delete.");
                        }
                    }
                    catch(Exception e)
                    {
                        model.Errors.Add("Account could not be deleted");
                        model.Errors.Add(e.Message);
                    }
                }

                if(Request.Form.ChangePassword != null)
                {
                    if(model.Password != null && model.ConfirmPassword != null)
                    {
                        if(model.Password == model.ConfirmPassword)
                        {
                            try
                            {
                                ssa.ChangePassword(model.Planeswalker.AuthToken, model.Password);
                                model.Messages.Add("Password successfully changed.");
                            }
                            catch(Exception e)
                            {
                                model.Errors.Add("Password cannot be changed.");
                                model.Errors.Add(e.Message);
                            }  
                        }
                        else
                        {
                            model.Errors.Add("Password and Confirmation Password do not match.");
                        }
                    }
                    else
                    {
                        model.Errors.Add("Password and Confirmation Password must not be blank.");
                    }
                }

                return View["Logon/Settings",model];
            };
           
            Get["/logon"] = parameters => {
                LogonModel model = new LogonModel();
                model.ActiveMenu = "signin";
                model.UrlRedirect = (string)Request.Query.Url;

                if(Request.Query.returnUrl != null)
                {
                    model.UrlRedirect = (string)Request.Query.returnUrl;
                }

                return View["Logon/logon",model];
            };

            Post["/logon"] = parameters => {
                LogonModel model = this.Bind<LogonModel>();
                model.ActiveMenu = "signin";
                var results = this.Validate(model);

                if(!results.IsValid)
                {
                    model.Errors = ErrorUtility.GetValidationErrors(results);
                    return View["Logon/Logon", model];
                }

                model.Errors.Add("Password or/and Username is incorrect.");

                User user = null;

                try
                {
                    user = ssa.Authenticate(model.UserName, model.Secret,
                        this.Context.Request.UserHostAddress);
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);

                    if(user == null)
                    {
                        return View["Logon/logon", model];
                    }
                }

                return this.LoginAndRedirect(user.AuthToken, 
                    fallbackRedirectUrl: model.UrlRedirect);
            };

			Get ["/register"] = parameters => {
                SignupModel model = new SignupModel();
				model.ActiveMenu = "register";
				return View["register", model];
            };

			Post ["/register"] = parameters => {
                SignupModel model = this.Bind<SignupModel>();
                var result = this.Validate(model);
				model.ActiveMenu = "register";

                if (!result.IsValid)
                {
                    model.Errors.AddRange(ErrorUtility.GetValidationErrors(result));
					return View["Register", model];
                }

                try
                {
                    repository.AddPlaneswalker(model.UserName, model.Secret, model.Email);
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
					return View["Register", model];
                }

                LogonModel logon = new LogonModel();
                logon.Messages.Add("You have successfully created an account. Please Sign In.");

                try
                {
                    Email.send("*****@*****.**", 
                        "New Planeswalker alert", model.UserName);
                }
                catch(Exception e)
                {
                    //swallow this for now
                }

                return View["Logon", logon];

            };

            Get["/logout"] = parameters => {
                Planeswalker nuser = (Planeswalker)Context.CurrentUser;
                ssa.End(nuser.AuthToken);

                return this.LogoutAndRedirect((string)Request.Query.Url);
            };

            Get ["/forgot"] = parameters => {
                ForgotModel model = new ForgotModel();
                model.ActiveMenu = "signin";
                return View["Forgot", model];
            };

            Post ["/forgot"] = parameters => {
                ForgotModel model = this.Bind<ForgotModel>();
                model.ActiveMenu = "signin";

                string subject = "MtgDb.info: Password reset request.";
                string body = "You have requested a password reset. You new password is: {0}";

                try
                {
                    string newPass = ssa.Forgot(model.Email);
                    Email.send(model.Email, subject,string.Format(body,newPass));
                    model.Messages.Add("Your new password has been successfully sent to your email.");
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                }
                    
                return View["Forgot", model];
            };
        }
Beispiel #8
0
        public LogonModule(SuperSimpleAuth ssa)
        {
            this.ssa = ssa;

            Get["/settings"] = parameters => {
                SettingsModel model = new SettingsModel();

                if (this.Context.CurrentUser == null)
                {
                    return(this.LogoutAndRedirect("/"));
                }

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Logon/Settings", model]);
            };

            Post["/settings"] = parameters => {
                SettingsModel model = this.Bind <SettingsModel>();

                if (this.Context.CurrentUser == null)
                {
                    return(this.LogoutAndRedirect("/"));
                }

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;


                if (Request.Form.Save != null)
                {
                    model.Planeswalker.Profile.Email = model.Email;
                    model.Planeswalker.Profile.Name  = model.Name;

                    try
                    {
                        model.Planeswalker = repository.UpdatePlaneswalker(model.Planeswalker);
                    }
                    catch (Exception e)
                    {
                        model.Errors.Add("Could not update account");
                        model.Errors.Add(e.Message);
                    }
                }

                if (Request.Form.Delete != null)
                {
                    try
                    {
                        if (model.Yes)
                        {
                            ssa.Disable(model.Planeswalker.AuthToken);
                            repository.RemovePlaneswalker(model.Planeswalker.Id);
                            return(this.LogoutAndRedirect("/"));
                        }
                        else
                        {
                            model.Errors.Add("You must check, 'Yes, I know'. To delete.");
                        }
                    }
                    catch (Exception e)
                    {
                        model.Errors.Add("Account could not be deleted");
                        model.Errors.Add(e.Message);
                    }
                }

                if (Request.Form.ChangePassword != null)
                {
                    if (model.Password != null && model.ConfirmPassword != null)
                    {
                        if (model.Password == model.ConfirmPassword)
                        {
                            try
                            {
                                ssa.ChangePassword(model.Planeswalker.AuthToken, model.Password);
                                model.Messages.Add("Password successfully changed.");
                            }
                            catch (Exception e)
                            {
                                model.Errors.Add("Password cannot be changed.");
                                model.Errors.Add(e.Message);
                            }
                        }
                        else
                        {
                            model.Errors.Add("Password and Confirmation Password do not match.");
                        }
                    }
                    else
                    {
                        model.Errors.Add("Password and Confirmation Password must not be blank.");
                    }
                }

                return(View["Logon/Settings", model]);
            };

            Get["/logon"] = parameters => {
                LogonModel model = new LogonModel();
                model.ActiveMenu  = "signin";
                model.UrlRedirect = (string)Request.Query.Url;

                if (Request.Query.returnUrl != null)
                {
                    model.UrlRedirect = (string)Request.Query.returnUrl;
                }

                return(View["Logon/logon", model]);
            };

            Post["/logon"] = parameters => {
                LogonModel model = this.Bind <LogonModel>();
                model.ActiveMenu = "signin";
                var results = this.Validate(model);

                if (!results.IsValid)
                {
                    model.Errors = ErrorUtility.GetValidationErrors(results);
                    return(View["Logon/Logon", model]);
                }

                model.Errors.Add("Password or/and Username is incorrect.");

                User user = null;

                try
                {
                    user = ssa.Authenticate(model.UserName, model.Secret,
                                            this.Context.Request.UserHostAddress);
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);

                    if (user == null)
                    {
                        return(View["Logon/logon", model]);
                    }
                }

                return(this.LoginAndRedirect(user.AuthToken,
                                             fallbackRedirectUrl: model.UrlRedirect));
            };

            Get ["/register"] = parameters => {
                SignupModel model = new SignupModel();
                model.ActiveMenu = "register";
                return(View["register", model]);
            };

            Post ["/register"] = parameters => {
                SignupModel model  = this.Bind <SignupModel>();
                var         result = this.Validate(model);
                model.ActiveMenu = "register";

                if (!result.IsValid)
                {
                    model.Errors.AddRange(ErrorUtility.GetValidationErrors(result));
                    return(View["Register", model]);
                }

                try
                {
                    repository.AddPlaneswalker(model.UserName, model.Secret, model.Email);
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                    return(View["Register", model]);
                }

                LogonModel logon = new LogonModel();
                logon.Messages.Add("You have successfully created an account. Please Sign In.");

                try
                {
                    Email.Send("*****@*****.**",
                               "New Planeswalker alert", model.UserName);
                }
                catch (Exception e)
                {
                    //return null;
                }

                return(View["Logon", logon]);
            };

            Get["/logout"] = parameters => {
                Planeswalker nuser = (Planeswalker)Context.CurrentUser;
                ssa.End(nuser.AuthToken);

                return(this.LogoutAndRedirect((string)Request.Query.Url));
            };

            Get ["/forgot"] = parameters => {
                ForgotModel model = new ForgotModel();
                model.ActiveMenu = "signin";
                return(View["Forgot", model]);
            };

            Post ["/forgot"] = parameters => {
                ForgotModel model = this.Bind <ForgotModel>();
                model.ActiveMenu = "signin";

                string subject = "MtgDb.info: Password reset request.";
                string body    = "You have requested a password reset. You new password is: {0}";

                try
                {
                    string newPass = ssa.Forgot(model.Email);
                    Email.Send(model.Email, subject, string.Format(body, newPass));
                    model.Messages.Add("Your new password has been successfully sent to your email.");
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["Forgot", model]);
            };
        }