Exemplo n.º 1
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);
            }
        }