Ejemplo n.º 1
0
        /*******************************************************
         * GetAllProjectsFromTokenAsync
         * ****************************************************/
        public async Task <IList <Project> > GetAllProjectsFromTokenAsync(string token)
        {
            var validatedToken = _tokenHelperService.GetPrincipalFromToken(token);

            try
            {
                if (validatedToken is null)
                {
                    return(null);
                }

                var user = await _tokenHelperService.GetUserFromToken(token);

                IList <Project> ProjectList = (from p in _dataContext.Projects
                                               join up in _dataContext.UserProjects
                                               on p.ProjectKey equals up.ProjectKey
                                               where up.UserName == user.UserName
                                               select p).ToList();
                return(ProjectList);
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        /*******************************************************
         * ValidateProjectAccessFromToken
         * ****************************************************/
        public async Task <bool> ValidateProjectAccessFromToken(string projectKey, string token)
        {
            var validatedToken = _tokenHelperService.GetPrincipalFromToken(token);

            try
            {
                if (validatedToken is null)
                {
                    return(false);
                }

                var user = await _tokenHelperService.GetUserFromToken(token);

                return(_dataContext.UserProjects.Where(x => x.ProjectKey == projectKey && x.UserName == user.UserName).Count() > 0);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 3
0
        public async Task <AuthResult> RefreshTokenAsync(string token, string refreshToken)
        {
            var validatedToken = _tokenHelperService.GetPrincipalFromToken(token);

            if (validatedToken is null)
            {
                return(new AuthResult {
                    Success = false, Errors = new[] { "Invalid Token" }
                });
            }

            var expiryDateUnix = long.Parse(validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Exp).Value);

            var expiryDateTimeUtc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
                                    .AddSeconds(expiryDateUnix);

            if (expiryDateTimeUtc > DateTime.UtcNow)
            {
                return(new AuthResult {
                    Success = false, Errors = new[] { "This token hasn't expired yet" }
                });
            }

            var jti = validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Jti).Value;
            var storedRefreshToken = await _dataContext.RefreshTokens.SingleOrDefaultAsync(x => x.Token.ToString() == refreshToken);

            if (storedRefreshToken is null)
            {
                return(new AuthResult {
                    Success = false, Errors = new[] { "This refresh token does not exist" }
                });
            }

            if (DateTime.UtcNow > storedRefreshToken.DtExpires)
            {
                return(new AuthResult {
                    Success = false, Errors = new[] { "This refresh token has been invalidated" }
                });
            }

            if (storedRefreshToken.Used)
            {
                return(new AuthResult {
                    Success = false, Errors = new[] { "This refresh token has been used" }
                });
            }

            if (storedRefreshToken.JwtId != jti)
            {
                return(new AuthResult {
                    Success = false, Errors = new[] { "This refresh token does not match this JWT" }
                });
            }

            storedRefreshToken.Used = true;
            _dataContext.RefreshTokens.Update(storedRefreshToken);
            await _dataContext.SaveChangesAsync();

            var user = await _userManager.FindByIdAsync(validatedToken.Claims.Single(x => x.Type == "id").Value);

            return(await GeneratTokenResultForUserAsync(user));
        }