public void Signature3()
        {
            // flutter: https://pub.dev/packages/fast_rsa

            var originalText = "070098c1499e4760a5c1d7888f0e4378_2021-04-28T14:38:13";
            var signature    = "Jb1fOu6tPxYPnf0vn7B4/lp17iaBTiLTExQ7O13TxqE8RywO6W1p8uQ1uAIfFIYcDFTGA06QKwk0lZyybMAjMIFtP59NsTyeicPVKnPfu+1ukTDt28yYRdUGj9ZMd1zHh5llaaH8tLHH8JG2WmyKAefuyk5vRanegftQfc5QcpDeKiobI3AVGhPUy7edOx1lMlWrIaKgR3mHc/psmDH2FVBZF5P3aOE6O6Pnn/4ZG0WzRoTTF9Btr+3qV5dry0NldlCQKSHldltEhQlBTWJwTBao24RDmgRuahR40WGn5d+5mersMsMVjP5nsI7Fp7HjEIJmvPd6nO7cYbBaTz5N2w==";
            var publicKey    = "MIIBCgKCAQEAzOmgHr6eI+2uDGCYkEg+aGGxcRwRWYL7g6ynwMxunYdPMw6KylymxP5bEGn9s7svfvQdklJNeqU/QdnyNflne70SHB4m7hNYimF8mNbJyUPGs4nIkHW2jtRmJUeWR3RYcB9upMsNWcZG2wej7oV5eDmVrF7haeMIrQKSU4/IypYgc5coZWf6EXAdjRPYddpjyS1GaatSBqVp66hlQB8GchcxogTxbWN/jcQp8VwAptK2hx5r/K9CH9DxWR0VM/m9OIbmrC5cKbksn41OtwpaMe/1KErODVbmVuYm/ol+TCO7CV2TumocF5VttjXLf59tV6ikrhMmuY8fUlnFW1ujvwIDAQAB";

            var result = MyRsa.ValidateSignature(originalText, signature, publicKey);

            Assert.IsTrue(result);
        }
        public async Task <AuthorizationResponse> RefreshSessionAsync(RefreshSessionRequest request)
        {
            using var activity = MyTelemetry.StartActivity("Refresh Session");

            if (string.IsNullOrEmpty(request.Token) || string.IsNullOrEmpty(request.SignatureBase64))
            {
                activity.AddTag("message", "bad request");
                activity.SetStatus(Status.Error);

                return(new AuthorizationResponse()
                {
                    Result = false
                });
            }

            if (DateTime.UtcNow < request.RequestTimestamp || request.RequestTimestamp < DateTime.UtcNow.AddSeconds(-_settings.RequestTimeLifeSec))
            {
                activity.AddTag("message", "request expired");
                activity.SetStatus(Status.Error);

                return(new AuthorizationResponse()
                {
                    Result = false
                });
            }

            var(result, token) = TokensManager.ParseBase64Token <JetWalletToken>(request.Token, AuthConst.GetSessionEncodingKey(), DateTime.UtcNow);

            if (result != TokenParseResult.Ok && result != TokenParseResult.Expired)
            {
                activity.AddTag("message", "wrong token");
                activity.SetStatus(Status.Error);

                return(new AuthorizationResponse()
                {
                    Result = false
                });
            }

            token.Id.AddToActivityAsTag("clientId");
            token.BrokerId.AddToActivityAsTag("brokerId");
            token.BrandId.AddToActivityAsTag("brandId");
            token.WalletId.AddToActivityAsTag("walletId");
            token.SessionRootId.AddToActivityAsTag("sessionRootId");

            var entity = await _writer.GetAsync(SpotSessionNoSql.GeneratePartitionKey(token.ClientId()), SpotSessionNoSql.GenerateRowKey(token.SessionRootId));

            if (entity == null)
            {
                activity.AddTag("message", "root session do not exist");
                activity.SetStatus(Status.Error);

                return(new AuthorizationResponse()
                {
                    Result = false
                });
            }

            if (DateTime.UtcNow >= entity.DiedDateTime)
            {
                activity.AddTag("message", "root session is died");
                activity.SetStatus(Status.Error);

                return(new AuthorizationResponse()
                {
                    Result = false
                });
            }

            if (DateTime.UtcNow <= entity.CreateDateTime.AddSeconds(_settings.TimeoutToRefreshNewSessionInSec))
            {
                activity.AddTag("message", "the session is very young, for renewal");
                activity.SetStatus(Status.Error);

                return(new AuthorizationResponse()
                {
                    Result = false
                });
            }

            var signContent     = $"{request.Token}_{request.RequestTimestamp:yyyy-MM-ddTHH:mm:ss}_{request.NewWalletId}";
            var verifySignature = MyRsa.ValidateSignature(signContent, request.SignatureBase64, entity.PublicKeyBase64);

            if (!verifySignature)
            {
                activity.AddTag("message", "wrong signature");
                activity.SetStatus(Status.Error);

                return(new AuthorizationResponse()
                {
                    Result = false
                });
            }

            var walletId = token.WalletId;

            if (!string.IsNullOrEmpty(request.NewWalletId))
            {
                var clientIdentity = new JetClientIdentity(token.BrokerId, token.BrandId, token.Id);
                var wallets        = await _clientWalletService.GetWalletsByClient(clientIdentity);

                var wallet = wallets?.Wallets?.FirstOrDefault(w => w.WalletId == request.NewWalletId);

                if (wallet == null)
                {
                    request.NewWalletId.AddToActivityAsTag("walletId");
                    _logger.LogWarning("Cannot Refresh session, NewWallet do not found. WalletId {walletId}. ClientId: {clientId}", request.NewWalletId, token.Id);
                    activity.SetStatus(Status.Error);
                    return(new AuthorizationResponse()
                    {
                        Result = false
                    });
                }

                walletId = wallet.WalletId;
                _logger.LogInformation("Client update session to new walletId. SessionRootId: {sessionRootId}; ClientId: {clientId}; WalletId: {walletId}",
                                       token.SessionRootId, token.Id, walletId);
            }

            walletId.AddToActivityAsTag("walletId");

            var newToken = new JetWalletToken()
            {
                Id            = token.Id,
                Expires       = DateTime.UtcNow.AddMinutes(_settings.SessionLifeTimeMinutes),
                SessionRootId = token.SessionRootId,
                SessionId     = Guid.NewGuid().ToString("N"),
                BrandId       = token.BrandId,
                BrokerId      = token.BrokerId,
                WalletId      = walletId
            };

            await _sessionAuditService.RefreshSessionAudit(token, newToken, request.UserAgent, request.Ip);

            _logger.LogInformation("Refresh session is success. SessionRootId: {sessionRootId}; SessionId: {sessionId}; PrevSessionId: {prevSessionId}; ClientId: {clientId}; WalletId: {walletId}",
                                   newToken.SessionRootId, newToken.SessionId, token.SessionId, newToken.ClientId(), newToken.WalletId);

            return(new AuthorizationResponse()
            {
                Token = newToken.IssueTokenAsBase64String(AuthConst.GetSessionEncodingKey()),
                Result = true
            });
        }