private RefreshedAccessToken GenerateRefreshedAccessToken(string userName)
        {
            var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:Secret"]));

            DateTime now    = DateTime.Now;
            var      claims = GetTokenClaims(userName, now);

            var accessJwt = new JwtSecurityToken(
                issuer: _configuration["JWT:ValidIssuer"],
                audience: _configuration["JWT:ValidAudience"],
                claims: claims,
                notBefore: now,
                expires: now.AddDays(1),
                signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)
                );

            var encodedAccessJwt = new JwtSecurityTokenHandler().WriteToken(accessJwt);

            var refreshedAccessToken = new RefreshedAccessToken
            {
                UserName          = userName,
                AccessToken       = encodedAccessJwt,
                AccessTokenExpiry = DateTime.Now.AddDays(1)
            };

            _authenticationRepository.UpdateAccessToken(refreshedAccessToken);

            //TODO: this should be a async operation and can be made more cross-cutting design feature rather than calling inside the actual feature.
            _logger.LoginTokenLogForRefreshToken(refreshedAccessToken);

            return(refreshedAccessToken);
        }
Exemple #2
0
        public int LoginTokenLogForRefreshToken(RefreshedAccessToken refreshedAccessToken)
        {
            using IDbConnection db = _connectionFactory.GetConnection;

            string query = @"Insert into [LoginTokenLog](UserId, AccessToken, AccessTokenExpiry, DeviceCode, DeviceName) 
                values (@UserId, @AccessToken, @AccessTokenExpiry, @DeviceCode, @DeviceName)";

            return(db.Execute(query, refreshedAccessToken));
        }
Exemple #3
0
        public int UpdateAccessToken(RefreshedAccessToken refreshedAccessToken)
        {
            using IDbConnection db = _connectionFactory.GetConnection;
            int userId = db.Query <int>(@"Select u.Id From [User] u Where u.UserName = @UserName", new { refreshedAccessToken.UserName }).FirstOrDefault();

            refreshedAccessToken.UserId = userId;

            int loginTokenUserId = db.Query <int>(@"Select userId From [LoginToken] Where  UserId = @userId", new { userId }).FirstOrDefault();

            if (loginTokenUserId > 0)
            {
                string query = @"update [LoginToken] Set 
                                UserId = @UserId,
                                AccessToken = @AccessToken,
                                AccessTokenExpiry = @AccessTokenExpiry,
                                DeviceCode = @DeviceCode,
                                DeviceName = @DeviceName
                              Where UserId = @UserId";

                return(db.Execute(query, refreshedAccessToken));
            }
            return(0);
        }
Exemple #4
0
 public async Task <int> LoginTokenLogForRefreshToken(RefreshedAccessToken refreshedAccessToken)
 {
     return(await Task.Run(() => _loggerRepository.LoginTokenLogForRefreshToken(refreshedAccessToken)));
 }