Exemplo n.º 1
0
        public async Task <IActionResult> SubmitLogin([FromForm] LoginModel model,
                                                      CancellationToken cancellationToken = default)
        {
            if (LoggedInUser <GoblinIdentityUserModel> .Current?.Data != null)
            {
                if (!string.IsNullOrWhiteSpace(model.Continue))
                {
                    return(Redirect(model.Continue));
                }

                return(RedirectToAction("Index", "Home"));
            }

            if (!ModelState.IsValid)
            {
                ViewBag.WarningMessage = Messages.InvalidData;

                return(View("Login", model));
            }

            if (string.IsNullOrWhiteSpace(model.Continue))
            {
                model.Continue = Url.AbsoluteAction("Index", "Home");
            }

            ViewBag.ContinueUrl = model.Continue;

            try
            {
                var generateAccessTokenModel = new GoblinIdentityGenerateAccessTokenModel
                {
                    UserName = model.UserName,
                    Password = model.Password
                };

                var accessToken =
                    await GoblinIdentityHelper.GenerateAccessTokenAsync(generateAccessTokenModel, cancellationToken);

                accessToken = accessToken?.Trim('"');

                CookieHelper.SetShare(HttpContext, GoblinCookieKeys.AccessToken, accessToken);

                return(View("LoggedIn"));
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;

                return(View("Login", model));
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;

                return(View("Login", model));
            }
        }
Exemplo n.º 2
0
        public async Task <string> GenerateAccessTokenAsync(GoblinIdentityGenerateAccessTokenModel model,
                                                            CancellationToken cancellationToken = default)
        {
            var userEntity = await _userRepo.Get(x => x.UserName == model.UserName)
                             .FirstOrDefaultAsync(cancellationToken).ConfigureAwait(true);

            // Check User is exist

            if (userEntity == null)
            {
                throw new GoblinException(nameof(GoblinIdentityErrorCode.UserNotFound),
                                          GoblinIdentityErrorCode.UserNotFound);
            }

            // Compare password hash from request and database

            var passwordHash = PasswordHelper.HashPassword(model.Password, userEntity.PasswordLastUpdatedTime);

            if (passwordHash != userEntity.PasswordHash)
            {
                throw new GoblinException(nameof(GoblinIdentityErrorCode.WrongPassword),
                                          GoblinIdentityErrorCode.WrongPassword);
            }

            // Generate Access Token

            var now = GoblinDateTimeHelper.SystemTimeNow;

            var accessTokenData = new TokenDataModel <AccessTokenDataModel>
            {
                ExpireTime  = now.Add(SystemSetting.Current.AccessTokenLifetime),
                CreatedTime = now,
                Data        = new AccessTokenDataModel
                {
                    UserId = userEntity.Id
                }
            };

            var accessToken = JwtHelper.Generate(accessTokenData);

            return(accessToken);
        }
Exemplo n.º 3
0
        public static async Task <string> GenerateAccessTokenAsync(GoblinIdentityGenerateAccessTokenModel model, CancellationToken cancellationToken = default)
        {
            ValidationHelper.Validate <GoblinIdentityGenerateAccessTokenModelValidator, GoblinIdentityGenerateAccessTokenModel>(model);

            try
            {
                var endpoint = GetRequest(model.LoggedInUserId).AppendPathSegment(GoblinIdentityEndpoints.GenerateAccessToken);

                var accessToken = await endpoint
                                  .PostJsonAsync(model, cancellationToken : cancellationToken)
                                  .ReceiveString()
                                  .ConfigureAwait(true);

                accessToken = accessToken?.Trim().Trim('"');

                return(accessToken);
            }
            catch (FlurlHttpException ex)
            {
                await FlurlHttpExceptionHelper.HandleErrorAsync(ex).ConfigureAwait(true);

                return(null);
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> GenerateAccessToken([FromBody] GoblinIdentityGenerateAccessTokenModel model, CancellationToken cancellationToken = default)
        {
            var accessToken = await _userService.GenerateAccessTokenAsync(model, cancellationToken);

            return(Ok(accessToken));
        }