public async Task Execute(string accountId, string tokenId)
        {
            AccountRTokenInfo token = null;

            try
            {
                // Retrieve all tokens of provided account from db.
                token = await _accountRTokenRepo.Find(x => x.AccountId == accountId && x.TokenId == tokenId).SingleOrDefaultAsync();

                if (token == null)
                {
                    return;
                }

                // Eventing for revoked token.
                await Publish(new RefreshTokenRevokedEvent()
                {
                    CorrelationId = Guid.NewGuid().ToString("N"),
                    IssuerSystem  = "AuthServer.SecurityTokens",
                    Issuer        = "AuthServer.SecurityTokens.Services.Commands.RevokeTokenCommand",
                    EventDate     = DateTime.Now,
                    TokenId       = token.TokenId,
                    Expires       = token.ExpireDate
                });

                // Update tokens in db.
                _accountRTokenRepo.UpdateOne(x => x.TokenId == tokenId, GetUpdateDefinitions());

                // Log operation.
                _logger.LogEvent("RevokeTokenCommand.Execute", $"Refresh token revoked : {tokenId}", new { Token = token });
            }
            catch (Exception ex)
            {
                // Log error.
                _logger.LogError("RevokeAllAccountTokensCommand.Execute", "Exception was thrown", new
                {
                    TokenId   = tokenId,
                    Token     = token,
                    Exception = ex
                });

                throw;
            }
        }
Пример #2
0
        public async Task <TokenResult> Execute(string accountId, List <Claim> claims, TokenAdditionalData additionalData, DateTime?customExpireDate = null)
        {
            try
            {
                SymmetricSecurityKey key   = _symmetricKeyProvider.GetKey();
                SigningCredentials   creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                //Generate and add token Jti claim. Will be used in short tokens to identify the creator refresh token
                string tokenJti = _tokenIdGenerator.Generate();
                claims.Add(new Claim(JwtRegisteredClaimNames.Jti, tokenJti));

                //Set expire date
                DateTime expireDate;
                if (customExpireDate != null)
                {
                    expireDate = customExpireDate.Value;
                }
                else
                {
                    expireDate = DateTime.Now.AddMinutes(_rTokenConfig.ExpiresInMin);
                }

                JwtSecurityToken jwtTokenOptions = new JwtSecurityToken(
                    issuer: _rTokenConfig.ValidIssuer,
                    audience: _rTokenConfig.ValidAudience,
                    claims: claims,
                    expires: expireDate,
                    signingCredentials: creds
                    );

                //Generate token string
                string token = new JwtSecurityTokenHandler().WriteToken(jwtTokenOptions);

                //Create token db record
                AccountRTokenInfo accountRTokenInfo = new AccountRTokenInfo()
                {
                    TokenId       = tokenJti,
                    AccountId     = accountId,
                    ExpireDate    = expireDate,
                    Status        = AccountRTokenStatus.Active,
                    CreateDate    = DateTime.Now,
                    DeviceInfo    = additionalData.DeviceInfo,
                    RequesterIPv4 = additionalData.RequesterIPv4,
                    RequesterIPv6 = additionalData.RequesterIPv6
                };

                //Save token db record
                await _accountRTokenRepo.InsertOneAsync(accountRTokenInfo);

                return(new TokenResult(tokenJti, token, jwtTokenOptions.ValidTo));
            }
            catch (Exception ex)
            {
                //Log error
                _logger.LogError("GenerateRefreshTokenCommand.Execute", "Exception was thrown", new
                {
                    Exception = ex
                });

                throw;
            }
        }