private static bool ValidateToken(string token, out int?userId)
 {
     using (var db = new ScribsDbContext()) {
         userId = null;
         try {
             var             success         = true;
             var             jwtToken        = JwtManager.GetJwtToken(token);
             ClaimsPrincipal simplePrinciple = JwtManager.GetPrincipal(db, jwtToken, out Access access);
             if (simplePrinciple == null)
             {
                 success = false;
             }
             else
             {
                 var identity = simplePrinciple.Identity as ClaimsIdentity;
                 if (identity == null || !identity.IsAuthenticated)
                 {
                     success = false;
                 }
                 else
                 {
                     var agentIdClaim = identity.FindFirst(ClaimTypes.NameIdentifier);
                     if (agentIdClaim != null)
                     {
                         userId = int.Parse(agentIdClaim.Value);
                     }
                     if (!userId.HasValue)
                     {
                         success = false;
                     }
                 }
             }
             if (access != null)
             {
                 if (success)
                 {
                     access.CTime = DateTime.Now;
                 }
                 else
                 {
                     access.Status = Status.Expired;
                 }
                 access.MTime = DateTime.Now;
                 db.SaveChanges();
             }
             else
             {
                 success = false;
             }
             return(success);
         } catch (Exception) {
             return(false);
         }
     }
 }
Esempio n. 2
0
 public TokenModel SignUp(UserModel model)
 {
     using (var db = new ScribsDbContext()) {
         if (String.IsNullOrEmpty(model.Username) || String.IsNullOrEmpty(model.Password) || String.IsNullOrEmpty(model.Mail))
         {
             throw new Exception("Some fields are missing");
         }
         if (model.Username.Length > 40)
         {
             throw new Exception("This username is too long");
         }
         var user = db.Users.SingleOrDefault(o => o.Name == model.Username);
         if (user != null)
         {
             throw new Exception("This username is already taken");
         }
         user = db.Users.SingleOrDefault(o => o.Mail == model.Mail);
         if (user != null)
         {
             throw new Exception("This email is already used");
         }
         user          = Scribs.User.Factory.CreateInstance(db);
         user.Name     = model.Username;
         user.Password = model.Password;
         user.Mail     = model.Mail;
         try {
             user.CreateDirectory();
         } catch {
             throw new Exception("Some special characters in your username are not supported");
         }
         db.Users.Add(user);
         db.SaveChanges();
         var result = new TokenModel();
         var access = db.Accesses.FirstOrDefault(o => o.UserId == user.Id && o.Status == Status.Active);
         result.Token = access != null ? access.Token : JwtManager.GenerateToken(db, user, model);
         return(result);
     }
     throw new HttpResponseException(HttpStatusCode.Unauthorized);
 }