Пример #1
0
        public IActionResult Login(LoginViewModel request)
        {
            var user = _repository.GetUser(request);

            if (user != null)
            {
                var token = _jwtGenerator.GenerateJwt(user);

                return(Ok(new { AccessToken = token }));
            }

            return(Unauthorized());
        }
Пример #2
0
        public async Task <ActionResult <User> > Register([FromBody] AuthModel model)
        {
            if (ModelState.IsValid)
            {
                User user = new User {
                    UserName = model.Login
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, false);

                    return(Ok(JwtGenerator.GenerateJwt(_userManager.Users.FirstOrDefault(u => u.UserName == model.Login))));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }
            return(BadRequest(ModelState));
        }
Пример #3
0
        public async Task <IActionResult> UserAuthorize([FromBody] AuthorizationDto authorizationDto)
        {
            try
            {
                var user = await _workWithUser.CheckUserForm(authorizationDto);

                if (user == null)
                {
                    return(NotFound("Wrong password"));
                }

                var encodedJwt = JwtGenerator.GenerateJwt(user);

                var response = new
                {
                    access_token = encodedJwt
                };

                return(new JsonResult(response));
            }
            catch (ArgumentException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                return(Problem(ex.Message));
            }
        }
Пример #4
0
        public void TestPkcs8KeyGeneration()
        {
            var generator = new JwtGenerator(_mockAppId, _mockPKCS8);
            var jwt       = generator.GenerateJwt();
            var decoded   = JsonConvert.DeserializeObject <JObject>(generator.DecodeJwt(jwt));

            Assert.Equal(_mockAppId, decoded["application_id"].ToString());
        }
        public ActionResult LoginAdmin([FromBody] Principal principal)
        {
            var admin = _adminRepo.GetByEmail(principal.Email);

            if (admin == null)
            {
                return(Forbid("User does not exist"));
            }

            if (admin.Password != principal.Password)
            {
                return(Unauthorized("Password is not correct"));
            }


            return(Ok(_jwt.GenerateJwt("admin")));
        }
Пример #6
0
        public void GenerateTokenWithFullAcls()
        {
            var generator = new JwtGenerator(_mockAppId, _mockPKCS1, Acls.FullAcls());
            var expected  = @"{""paths"":{""/*/users/**"":{},""/*/conversations/**"":{},""/*/sessions/**"":{},""/*/devices/**"":{},""/*/image/**"":{},""/*/media/**"":{},""/*/applications/**"":{},""/*/push/**"":{},""/*/knocking/**"":{}}}";
            var jwt       = generator.GenerateJwt();
            var decoded   = JsonConvert.DeserializeObject <JObject>(generator.DecodeJwt(jwt));

            Assert.Equal(_mockAppId, decoded["application_id"].ToString());
            Assert.Equal(expected, Regex.Replace(decoded["acls"].ToString(), @"\s+", ""));
        }
Пример #7
0
        public void GenerateTokenNoAcls()
        {
            var    generator = new JwtGenerator(_mockAppId, _mockPKCS1);
            var    jwt       = generator.GenerateJwt();
            JToken acls;
            var    decoded = JsonConvert.DeserializeObject <JObject>(generator.DecodeJwt(jwt));

            Assert.Equal(_mockAppId, decoded["application_id"].ToString());
            Assert.False(decoded.TryGetValue("acl", out acls));
        }
        public async Task <IActionResult> Post([FromBody] AuthVm vm)
        {
            var identity = await this.GetClaimsIdentity(vm.UserName, vm.Password);

            if (identity == null)
            {
                return(this.Unauthorized());
            }

            var jwt = await JwtGenerator.GenerateJwt(identity, this.jwtFactory, vm.UserName, this.jwtOptions, new JsonSerializerSettings { Formatting = Formatting.Indented });

            return(new OkObjectResult(jwt));
        }
Пример #9
0
        public async Task <ActionResult <User> > Login([FromBody] AuthModel model)
        {
            if (ModelState.IsValid)
            {
                var result =
                    await _signInManager.PasswordSignInAsync(model.Login, model.Password, false, false);

                if (result.Succeeded)
                {
                    return(Ok(JwtGenerator.GenerateJwt(await _userManager.FindByNameAsync(model.Login))));
                }
                else
                {
                    ModelState.AddModelError("", "Неправильный логин и (или) пароль");
                }
            }
            return(BadRequest(ModelState));
        }
Пример #10
0
        public void TestSingleAcl()
        {
            var expected = @"{""paths"":{""/*/users/**"":{}}}";
            var acls     = new Acls
            {
                Paths = new List <AclPath>
                {
                    new AclPath
                    {
                        ApiVersion = "*", ResourceType = "users", Resource = "**", AccessLevels = new object()
                    }
                }
            };
            var generator = new JwtGenerator(_mockAppId, _mockPKCS1, acls);
            var jwt       = generator.GenerateJwt();
            var decoded   = JsonConvert.DeserializeObject <JObject>(generator.DecodeJwt(jwt));

            Assert.Equal(_mockAppId, decoded["application_id"].ToString());
            Assert.Equal(expected, Regex.Replace(decoded["acls"].ToString(), @"\s+", ""));
        }