Beispiel #1
0
        public async Task <JwtDTO> RefreshAccessToken(string refreshToken)
        {
            var token = await _refreshTokens.GetAsync(refreshToken);

            if (token == null)
            {
                throw new NewException("Token was not found.");
            }
            if (token.Revoked)
            {
                throw new NewException("Token was revoked.");
            }
            var user = await _userRepository.GetAsync(token.UserId);

            if (user == null)
            {
                throw new NewException("User was not found.");
            }
            var jwt    = _jwtHandler.CreateToken(user.Id);
            var jwtDto = new JwtDTO()
            {
                AccessToken = jwt.AccessToken, Expires = jwt.Expires, RefreshToken = token.Token
            };

            return(jwtDto);
        }
Beispiel #2
0
        public async Task <TokenDTO> LoginAsync(string email, string password)
        {
            const string invalidCredentials = "Invalid Credentials";
            User         user = await _userRepository.GetUserOrFailAsync(email, invalidCredentials);

            if (user.Password != new HashingProvider().ComputeHashFromStringToString(password))
            {
                throw new UserNotFoundException(invalidCredentials);
            }

            JwtDTO jwtDto = _jwtService.CreateToken(user.Id, user.Role);

            return(new TokenDTO(jwtDto.Token, jwtDto.ExpiryTime, user.Role));
        }
Beispiel #3
0
        public async Task <JwtDTO> Login(string login, string password)
        {
            var user = await _userRepository.GetAsync(login);

            if (user == null)
            {
                throw new NewException(NewCodes.UserNotFound);
            }
            var hash = _encrypter.GetHash(password, user.Salt);

            if (user.Password != hash)
            {
                throw new NewException(NewCodes.WrongCredentials);
            }
            var jwt          = _jwtHandler.CreateToken(user.Id);
            var refreshToken = await _refreshTokens.GetByUserIdAsync(user.Id);

            string token = "";

            if (refreshToken == null)
            {
                token = Guid.NewGuid().ToString()
                        .Replace("+", string.Empty)
                        .Replace("=", string.Empty)
                        .Replace("/", string.Empty);
                await _refreshTokens.AddAsync(new RefreshToken(user, token));
            }
            else
            {
                token = refreshToken.Token;
            }
            var jwtDto = new JwtDTO()
            {
                AccessToken = jwt.AccessToken, Expires = jwt.Expires, RefreshToken = token, UserId = jwt.UserId
            };

            return(jwtDto);
        }
 public static void SetJwt(this IMemoryCache cache, Guid tokenId, JwtDTO jwt)
 => cache.Set(GetJwtKey(tokenId), jwt, TimeSpan.FromSeconds(5));