Ejemplo n.º 1
0
        public Connection(string identity, SerConnection config)
        {
            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            ServicePointManager.ServerCertificateValidationCallback += (a, b, c, d) => { return(true); };

            Mode            = QlikAppMode.SERVER;
            IsSharedSession = true;
            Config          = config;
            Identity        = identity;

            var connectUrl = SwitchScheme(Config.ServerUri.AbsoluteUri, SchemeMode.WEBSOCKET);
            var appurl     = Uri.EscapeDataString(HelperUtilities.GetFullAppName(Config.App).TrimStart('/'));

            connectUrl = $"{connectUrl}/app/{appurl}";

            if (identity == null)
            {
                var newIdentity = Guid.NewGuid();
                connectUrl      = $"{connectUrl}/identity/{newIdentity}";
                IsSharedSession = false;
                GlobelIdentity  = newIdentity.ToString();
            }
            else if (!String.IsNullOrEmpty(identity))
            {
                connectUrl     = $"{connectUrl}/identity/{identity}";
                GlobelIdentity = identity;
            }

            ConnectUri = new Uri(connectUrl);
            logger.Info($"Create Qlik connection {ConnId} to {connectUrl} with app {Config.App} and identity {identity}.");
        }
Ejemplo n.º 2
0
 public string GetToken(DomainUser domainUser, SerConnection connection, TimeSpan untilValid)
 {
     try
     {
         var cert     = new X509Certificate2();
         var certPath = HelperUtilities.GetFullPathFromApp(connection.Credentials.Cert);
         logger.Debug($"CERTPATH: {certPath}");
         var privateKey = HelperUtilities.GetFullPathFromApp(connection.Credentials.PrivateKey);
         logger.Debug($"PRIVATEKEY: {privateKey}");
         cert = cert.LoadPem(certPath, privateKey);
         var claims = new[]
         {
             new Claim("UserDirectory", domainUser.UserDirectory),
             new Claim("UserId", domainUser.UserId),
             new Claim("Attributes", "[SerOnDemand]")
         }.ToList();
         return(cert.GenerateQlikJWToken(claims, untilValid));
     }
     catch (Exception ex)
     {
         logger.Error(ex, "Can´t create a jwt token.");
         return(null);
     }
 }
Ejemplo n.º 3
0
        public bool Connect(bool loadPossibleApps = false)
        {
            try
            {
                logger.Info($"Connect to: {ConnectUri.AbsoluteUri}");
                var config = new EnigmaConfigurations()
                {
                    Url          = ConnectUri.AbsoluteUri,
                    CreateSocket = async(Url) =>
                    {
                        var webSocket = new ClientWebSocket();
                        webSocket.Options.Cookies = new CookieContainer();
                        webSocket.Options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
                        var credentials = Config?.Credentials ?? null;
                        var credType    = Config?.Credentials?.Type ?? QlikCredentialType.NONE;
                        logger.Debug($"Connection type is '{credType}'");
                        var jwtSession = new JwtSessionManager();
                        switch (credType)
                        {
                        case QlikCredentialType.SESSION:
                            logger.Debug($"Session-Cookie {credentials?.Key}={credentials?.Value}.");
                            ConnectCookie = new Cookie(credentials?.Key, credentials?.Value)
                            {
                                Secure = true,
                                Domain = ConnectUri.Host,
                                Path   = "/",
                            };
                            webSocket.Options.Cookies.Add(ConnectCookie);
                            logger.Debug($"Session type: {credentials?.Type} with Session {credentials?.Value}");
                            break;

                        case QlikCredentialType.JWT:
                            logger.Debug($"Jwt type: {credentials?.Key} - {credentials?.Value}.");
                            var newCookie = jwtSession.GetJWTSession(Config.ServerUri, credentials?.Value.Replace("Bearer ", ""), "X-Qlik-Session-ser");
                            ConnectCookie = newCookie;
                            webSocket.Options.Cookies.Add(ConnectCookie);
                            break;

                        case QlikCredentialType.CLOUD:
                            logger.Debug($"Connecting to Qlik Cloud.");
                            logger.Debug($"Cloud type: {credentials?.Key} - {credentials?.Value}.");
                            webSocket.Options.SetRequestHeader(credentials?.Key, credentials?.Value);
                            break;

                        case QlikCredentialType.NEWSESSION:
                            logger.Debug($"Connecting to Qlik with a new Session.");
                            logger.Debug($"Session infos: {credentials?.Key} - {credentials?.Value}.");
                            var newSession = jwtSession.CreateNewSession(Config, new DomainUser(credentials?.Value), Config.App);
                            ConnectCookie = newSession.Cookie;
                            webSocket.Options.Cookies.Add(ConnectCookie);
                            break;

                        case QlikCredentialType.NONE:
                            // No Authentication for DESKTOP and DOCKER
                            logger.Debug($"None type: No Authentication.");
                            break;

                        default:
                            throw new Exception("Unknown Qlik connection type.");
                        }
                        webSocket.Options.KeepAliveInterval = TimeSpan.FromDays(48);
                        await webSocket.ConnectAsync(new Uri(Url), CancellationToken.None);

                        return(webSocket);
                    },
                };

                SocketSession = Enigma.Create(config);
                var globalTask = SocketSession.OpenAsync();
                globalTask.Wait(7500);
                if (!globalTask.IsCompleted)
                {
                    throw new Exception("No connection to qlik.");
                }
                IGlobal global = Impromptu.ActLike <IGlobal>(globalTask.Result);
                var     task   = global.IsDesktopModeAsync();
                task.Wait(2500);
                if (!task.IsCompleted)
                {
                    throw new Exception("No connection to qlik.");
                }
                if (task.Result)
                {
                    Mode = QlikAppMode.DESKTOP;
                }
                if (loadPossibleApps)
                {
                    lock (lockObject)
                    {
                        PossibleApps = global.GetDocListAsync().Result;
                    }
                }
                logger.Debug($"Use connection mode: {Mode}");
                if (IsSharedSession)
                {
                    try
                    {
                        CurrentApp = global.GetActiveDocAsync().Result;
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, "No existing shared session found. Please open the app in the browser.");
                        return(false);
                    }
                }
                else
                {
                    var appName = String.Empty;
                    if (Mode == QlikAppMode.DESKTOP)
                    {
                        appName = HelperUtilities.GetFullAppName(Config.App);
                    }
                    else
                    {
                        appName = GetAppId(global);
                    }
                    logger.Debug($"Connect with app name: {appName}");
                    CurrentApp = global.OpenDocAsync(appName).Result;
                }
                logger.Debug("The Connection to Qlik was successfully");
                return(true);
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"The connection to Qlik Sense with uri '{ConnectUri}' app '{Config.App}' could not be established.");
                return(false);
            }
        }