Пример #1
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);
        }
Пример #2
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)
                {
                    //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];
            };
        }
Пример #3
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]);
            };
        }