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"); }
public MongoRepository (string connection, Db mtgdb, SuperSimpleAuth auth) { Connection = connection; magicdb = mtgdb; //magicdb = new Db (ConfigurationManager.AppSettings.Get("api")); client = new MongoClient(connection); server = client.GetServer(); database = server.GetDatabase("mtgdb_info"); ssa = auth; CreateUserCardIndexes (); }
protected override void ConfigureApplicationContainer(TinyIoCContainer container) { base.ConfigureApplicationContainer(container); //this must be the same key also in MonoRepository.cs SuperSimpleAuth ssa = new SuperSimpleAuth (ConfigurationManager.AppSettings.Get("domain"), ConfigurationManager.AppSettings.Get("domain_key")); container.Register<SuperSimpleAuth>(ssa); }
public MongoRepository (string connection) { ssa = new SuperSimpleAuth ( ConfigurationManager.AppSettings.Get("domain"), ConfigurationManager.AppSettings.Get("domain_key")); magicdb = new Db (ConfigurationManager.AppSettings.Get("api")); Connection = connection; client = new MongoClient(connection); server = client.GetServer(); database = server.GetDatabase("mtgdb_info"); CreateUserCardIndexes (); }
protected override void ConfigureApplicationContainer(TinyIoCContainer container) { base.ConfigureApplicationContainer(container); //some encryption on the keys could be used. var ssa = new SuperSimpleAuth(_domainCode, _url); container.Register <SuperSimpleAuth>(ssa); }
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); }
public NancyUserMapper(SuperSimpleAuth ssa) { this.ssa = ssa; }
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 WriteModule(IWriteRepository repository) { if (bool.Parse(ConfigurationManager.AppSettings.Get("https"))) { this.RequiresHttps(); } this.repository = repository; ssa = new SuperSimpleAuth(ConfigurationManager.AppSettings.Get("domain"), ConfigurationManager.AppSettings.Get("key")); Before += ctx => { try { Guid authKey = Guid.Parse(Request.Form["AuthToken"]); User user = ssa.Validate(authKey, this.Context.Request.UserHostAddress); if (user == null || !user.InRole("admin")) { return(HttpStatusCode.Forbidden); } } catch (Exception e) { return(HttpStatusCode.ProxyAuthenticationRequired); } return(null); }; Post["/sets", true] = async(parameters, ct) => { CardSet model = this.Bind <CardSet>(); CardSet set = await repository.AddCardSet(model); if (set == null) { return(Response.AsJson("false", Nancy.HttpStatusCode.UnprocessableEntity)); } //return Response.AsJson(card); return(Response.AsJson(true)); }; Post ["/sets/{id}", true] = async(parameters, ct) => { UpdateCardModel model = this.Bind <UpdateCardModel>(); string setId = (string)parameters.id; try { switch (Helper.GetSetFieldType(model.Field)) { case "bool": await repository.UpdateCardSet <bool>(setId, model.Field, bool.Parse(model.Value ?? "false")); break; case "int": await repository.UpdateCardSet <int>(setId, model.Field, int.Parse(model.Value ?? "0")); break; case "string": await repository.UpdateCardSet <string>(setId, model.Field, model.Value ?? ""); break; case "string[]": await repository.UpdateCardSet <string[]>(setId, model.Field, model.Value.Split(',')); break; case "DateTime": await repository.UpdateCardSet <DateTime>(setId, model.Field, DateTime.Parse(model.Value)); break; default: return(Response.AsJson("false", Nancy.HttpStatusCode.UnprocessableEntity)); } } catch (Exception e) { throw e; // return Response.AsJson(false, // Nancy.HttpStatusCode.UnprocessableEntity); } return(Response.AsJson(true, Nancy.HttpStatusCode.OK)); }; Post["/cards", true] = async(parameters, ct) => { Card model = this.Bind <Card>(); Card card = await repository.AddCard(model); if (card == null) { return(Response.AsJson("false", Nancy.HttpStatusCode.UnprocessableEntity)); } //return Response.AsJson(card); return(Response.AsJson(true)); }; Post ["/cards/{id}", true] = async(parameters, ct) => { UpdateCardModel model = this.Bind <UpdateCardModel>(); int mvid = (int)parameters.id; try { switch (Helper.GetCardFieldType(model.Field)) { case "bool": await repository.UpdateCardField <bool>(mvid, model.Field, bool.Parse(model.Value ?? "false")); break; case "int": await repository.UpdateCardField <int>(mvid, model.Field, int.Parse(model.Value ?? "0")); break; case "string": await repository.UpdateCardField <string>(mvid, model.Field, model.Value ?? ""); break; case "string[]": await repository.UpdateCardField <string[]>(mvid, model.Field, model.Value.Split(',')); break; case "DateTime": await repository.UpdateCardField <DateTime>(mvid, model.Field, DateTime.Parse(model.Value)); break; default: return(Response.AsJson("false", Nancy.HttpStatusCode.UnprocessableEntity)); } } catch (Exception e) { throw e; // return Response.AsJson(false, // Nancy.HttpStatusCode.UnprocessableEntity); } return(Response.AsJson(true, Nancy.HttpStatusCode.OK)); }; Post ["/cards/{id}/rulings", true] = async(parameters, ct) => { List <Ruling> rulings = this.Bind <List <Ruling> >(); int mvid = (int)parameters.id; try { await repository.UpdateCardRulings(mvid, rulings.ToArray()); } catch (Exception e) { throw e; } return(Response.AsJson("true")); }; Post ["/cards/{id}/formats", true] = async(parameters, ct) => { List <Format> formats = this.Bind <List <Format> >(); int mvid = (int)parameters.id; try { await repository.UpdateCardFormats(mvid, formats.ToArray()); } catch (Exception e) { throw e; } return(Response.AsJson("true")); }; }
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]; }; }
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]); }; }
public WriteModule (IRepository repository) { //this.RequiresHttps(); this.repository = repository; ssa = new SuperSimpleAuth (ConfigurationManager.AppSettings.Get ("domain"), ConfigurationManager.AppSettings.Get ("key")); Before += ctx => { try { Guid authKey = Guid.Parse(Request.Form["AuthToken"]); User user = ssa.Validate(authKey,this.Context.Request.UserHostAddress); if(user == null || !user.InRole("admin")) { return HttpStatusCode.Forbidden; } } catch(Exception e) { return HttpStatusCode.ProxyAuthenticationRequired; } return null; }; Post ["/cards/{id}"] = parameters => { UpdateCardModel model = this.Bind<UpdateCardModel>(); int mvid = (int)parameters.id; try { switch(Helper.GetCardFieldType(model.Field)) { case "int": repository.UpdateCardField<int>(mvid,model.Field,int.Parse(model.Value ?? "0")); break; case "string": repository.UpdateCardField<string>(mvid,model.Field, model.Value ?? ""); break; case "string[]": repository.UpdateCardField<string[]>(mvid,model.Field, model.Value.Split(',')); break; case "DateTime": repository.UpdateCardField<DateTime>(mvid,model.Field, DateTime.Parse(model.Value)); break; default: return Response.AsJson("false", Nancy.HttpStatusCode.UnprocessableEntity); } } catch(Exception e) { throw e; // return Response.AsJson(false, // Nancy.HttpStatusCode.UnprocessableEntity); } return Response.AsJson(true, Nancy.HttpStatusCode.OK); }; Post ["/cards/{id}/rulings"] = parameters => { List<Ruling> rulings = this.Bind<List<Ruling>>(); int mvid = (int)parameters.id; try { repository.UpdateCardRulings(mvid,rulings.ToArray()); } catch(Exception e) { throw e; } return Response.AsJson("true"); }; Post ["/cards/{id}/formats"] = parameters => { List<Format> formats = this.Bind<List<Format>>(); int mvid = (int)parameters.id; try { repository.UpdateCardFormats(mvid,formats.ToArray()); } catch(Exception e) { throw e; } return Response.AsJson("true"); }; }