예제 #1
0
        public async Task <bool> Handle(RefreshTokenRequest message, IOutputPort <RefreshTokenResponce> outputPort)
        {
            var principals = _jwtValidator.GetPrincipalsFromToken(message.AccessToken, message.SigningKey);

            if (principals != null)
            {
                var id   = principals.Claims.First(c => c.Type == "id");
                var user = await _userReposytory.FindOneBySpec(new UserSpecification(id.Value));

                if (user.HasValidRefreshTokens(message.RefreshToken))
                {
                    var jwtToken = await _jwtFactory.GenerateEncodedToken(user.IdentityId, user.UserName);

                    var refreshToken = _tokenFactory.GenerateToken();
                    user.RemoveRefreshToken(message.RefreshToken);
                    user.AddRefreshToken(refreshToken, user.Id, "");

                    await _userReposytory.Update(user);

                    outputPort.Handle(new RefreshTokenResponce(jwtToken, refreshToken, true));
                    return(true);
                }
            }

            outputPort.Handle(new RefreshTokenResponce(false, "Invalid Refresh token"));
            return(false);
        }
예제 #2
0
        public async Task <bool> Handle(LoginRequest message, IOutputPort <LoginResponce> outputPort)
        {
            if (!string.IsNullOrEmpty(message.UserName) || !string.IsNullOrEmpty(message.Password))
            {
                var user = await _userReposytory.FindByName(message.UserName);

                if (user != null)
                {
                    if (await _userReposytory.CheckPassword(user, message.Password))
                    {
                        var jwtToken = await _jwtFactory.GenerateEncodedToken(user.IdentityId, message.UserName);

                        var refreshToken = _tokenFactory.GenerateToken();
                        user.AddRefreshToken(refreshToken, user.Id, message.RemoteIpAddress);
                        await _userReposytory.Update(user);

                        outputPort.Handle(new LoginResponce(jwtToken, refreshToken, true));
                        return(true);
                    }
                }
            }

            outputPort.Handle(new LoginResponce(new[] { new Error("login_failure", "Wrong Username of Password") }));
            return(false);
        }