public async Task <IActionResult> LogIn([FromBody] Login login)
        {
            return(await Task.Run(
                       () =>
            {
                if (login == null)
                {
                    return BadRequest();
                }

                if (_userService.CurrentUserWithoutPasswords != null)
                {
                    return BadRequest(new
                    {
                        message = Resource.ErrorMessageByLogIn
                    });
                }

                UserShort user = _userService.LogIn(login.UserName, login.Password);

                if (user == null)
                {
                    return BadRequest(new
                    {
                        message = Resource.ErrorMessageByAuthenticate
                    });
                }

                IActionResult actionResult = Ok(user);

                return actionResult;
            }));
        }
        public async Task <IActionResult> Register([FromBody] Register register)
        {
            return(await Task.Run(
                       () =>
            {
                if (register == null)
                {
                    return BadRequest();
                }

                UserShort user = _userService.Register(register.UserName, register.Password);

                if (user == null)
                {
                    return BadRequest(new
                    {
                        message = Resource.ErrorMessageByRegister
                    });
                }

                IActionResult actionResult = Ok(Resource.MessageLogOutSuccess);

                return actionResult;
            }));
        }
Ejemplo n.º 3
0
        public static UserShort ShouldBeFilled(this UserShort userShort)
        {
            userShort.ShouldNotBeNull();
            userShort.uuid.ShouldNotBeNullOrWhiteSpace();
            userShort.type.ShouldNotBeNullOrWhiteSpace();
            userShort.display_name.ShouldNotBeNullOrWhiteSpace();
            userShort.links.ShouldNotBeNull();

            switch (userShort.type)
            {
            case "user":
                userShort.account_id?.ShouldNotBeNullOrWhiteSpace();     // may be null, but otherwise should be not empty.
                userShort.nickname.ShouldNotBeNullOrWhiteSpace();
                break;

            case "team":
                userShort.account_id.ShouldBeNull();
                userShort.nickname.ShouldBeNull();
#pragma warning disable 618 // that field is still valid for teams
                userShort.username.ShouldNotBeNullOrWhiteSpace();
#pragma warning restore 618
                break;

            default:
                throw new Exception($"user type {userShort.type} is not managed");
            }

            return(userShort);
        }
Ejemplo n.º 4
0
        public string GenerateJWT(UserShort userShort)
        {
            if (userShort.Email is null)
            {
                throw new ArgumentNullException();
            }
            SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Secret));
            SigningCredentials   credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha512Signature);

            SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim("Id", userShort.Id.ToString()),
                    new Claim(ClaimTypes.Email, userShort.Email),
                    new Claim("Pseudo", userShort.Pseudo),
                    new Claim(ClaimTypes.Role, userShort.IsAdmin ? "admin" : "user")
                }),
                Issuer             = Issuer,
                Audience           = Audience,
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = credentials
            };
            JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
            SecurityToken           token        = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }
Ejemplo n.º 5
0
 public void Put(int id, [FromBody] UserShort value)
 {
    var user = _context.Users.ToArray()[id];
     user.FirstName = value.FirstName;
     user.MiddleName = value.MiddleName;
     user.LastName = value.LastName;
     user.Age = value.Age;
     _context.SaveChanges();
 }
Ejemplo n.º 6
0
 public IActionResult Login([FromBody, SwaggerRequestBody("Form login", Required = true)] LoginForm login)
 {
     try
     {
         UserShort userShort = Service.Login(login.ToClient()).ToApi();
         string    Token     = TokenManager.GenerateJWT(userShort).ToString();
         return(Ok(Token));
     }
     catch (Exception)
     {
         return(new BadRequestObjectResult(new ExceptionResponse()
         {
             Status = 400, Value = "Password or Email is invalid"
         }));
     }
 }
Ejemplo n.º 7
0
 private Weight MapPostAndUser(Weight weight, UserShort createdBy)
 {
     weight.CreatedBy = createdBy;
     return(weight);
 }
Ejemplo n.º 8
0
 private Post MapPostAndUser(Post post, UserShort createdBy)
 {
     post.CreatedBy = createdBy;
     return(post);
 }
Ejemplo n.º 9
0
        protected bool IsSameAsCurrentUser(UserShort user)
        {
            var currentUser = _userContext.GetCurrentUser();

            return(currentUser != null && user.Id != currentUser.Id);
        }