Beispiel #1
0
        public IHttpActionResult Get()
        {
            var results = new
            {
                NumFoods = TheRepo.GetAllFoods().Count(),
                NumUsers = TheRepo.GetApiUsers().Count()
            };

            return(Ok(results));
        }
Beispiel #2
0
 public IHttpActionResult Get(string name)
 {
     if (name == "foods")
     {
         return(Ok(new { NumFoods = TheRepo.GetAllFoods().Count() }));
     }
     if (name == "users")
     {
         return(Ok(new { NumUsers = TheRepo.GetApiUsers().Count() }));
     }
     return(NotFound());
 }
Beispiel #3
0
 public IHttpActionResult Get(int id)
 {
     if (id == 1)
     {
         return(Ok(new { NumFoods = TheRepo.GetAllFoods().Count() }));
     }
     if (id == 2)
     {
         return(Ok(new { NumUsers = TheRepo.GetApiUsers().Count() }));
     }
     return(NotFound());
 }
Beispiel #4
0
        public HttpResponseMessage Post([FromBody] TokenRequestModel model)
        {
            try
            {
                var user = TheRepo.GetApiUsers().Where(u => u.AppId == model.ApiKey).FirstOrDefault();
                if (user != null)
                {
                    var secret = user.Secret;

                    var key      = Convert.FromBase64String(secret);
                    var provider = new System.Security.Cryptography.HMACSHA256(key);

                    var hash      = provider.ComputeHash(Encoding.UTF8.GetBytes(user.AppId));
                    var signature = Convert.ToBase64String(hash);

                    if (signature == model.Signature)
                    {
                        var rawTokenInfo = string.Concat(user.AppId + DateTime.UtcNow.ToString("d"));
                        var rawTokenByte = Encoding.UTF8.GetBytes(rawTokenInfo);
                        var token        = provider.ComputeHash(rawTokenByte);

                        var authToken = new AuthToken()
                        {
                            Token      = Convert.ToBase64String(token),
                            Expiration = DateTime.UtcNow.AddDays(7),
                            ApiUser    = user
                        };

                        if (TheRepo.Insert(authToken) && TheRepo.SaveAll())
                        {
                            return(Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(authToken)));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.BadRequest));
        }