Beispiel #1
0
        public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
        {
            try
            {
                User ssaUser = ssa.Validate(identifier,
                                            context.Request.UserHostAddress);

                Planeswalker user = 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)
                };

                return(user);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
Beispiel #2
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 #3
0
        public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
        {
            var ssaUser = _ssa.Validate(identifier,
                                        context.Request.UserHostAddress).Result;

            var user = new NancyUserIdentity(ssaUser);

            return(user);
        }
Beispiel #4
0
        public Planeswalker UpdatePlaneswalker(Planeswalker planeswalker)
        {
            MongoCollection <Profile> walkers = database.GetCollection <Profile> ("profiles");
            var query = Query <Profile> .EQ(e => e.Id, planeswalker.Id);

            Profile      updateWalker = walkers.FindOne(query);
            Planeswalker user         = null;

            if (updateWalker != null)
            {
                if (updateWalker.Email.ToLower() != planeswalker.Profile.Email.ToLower())
                {
                    ssa.ChangeEmail(planeswalker.AuthToken, planeswalker.Profile.Email);
                }

                if (updateWalker.Name.ToLower() != planeswalker.Profile.Name.ToLower())
                {
                    ssa.ChangeUserName(planeswalker.AuthToken, planeswalker.Profile.Name);
                }

                updateWalker.Email      = planeswalker.Profile.Email;
                updateWalker.ModifiedAt = DateTime.Now;
                updateWalker.Name       = planeswalker.Profile.Name;

                walkers.Save(updateWalker);

                User ssaUser = ssa.Validate(planeswalker.AuthToken);

                user = new Planeswalker
                {
                    UserName  = ssaUser.UserName,
                    AuthToken = ssaUser.AuthToken,
                    Email     = ssaUser.Email,
                    Id        = ssaUser.Id,
                    Claims    = ssaUser.Claims,
                    Roles     = ssaUser.Roles,
                    Profile   = GetProfile(ssaUser.Id)
                };
            }

            return(user);
        }
        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 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");
            };
        }