Exemplo n.º 1
0
        internal static void AdalLogin(AdalAuthentication adalAuth, SearchRequest searchRequest, bool forceLogin)
        {
            var task = adalAuth.Login(searchRequest.SharePointSiteUrl, forceLogin);

            if (!task.Wait(300000))
            {
                throw new TimeoutException("Prompt for user credentials timed out after 5 minutes.");
            }

            var token = task.Result;

            searchRequest.AuthenticationType = AuthenticationType.SPOManagement;
            searchRequest.Token = token;
        }
Exemplo n.º 2
0
        private void SetRequestParameters(SearchConnection searchConnection, SearchQueryRequest searchQueryRequest)
        {
            searchQueryRequest.SharePointSiteUrl = GetSPSite() ?? searchQueryRequest.SharePointSiteUrl;
            searchConnection.SpSiteUrl           = searchQueryRequest.SharePointSiteUrl;

            searchQueryRequest.QueryText = GetQuery() ?? searchQueryRequest.QueryText;

            searchQueryRequest.HttpMethodType = MethodType.HasValue ? MethodType.Value : searchQueryRequest.HttpMethodType;
            searchConnection.HttpMethod       = searchQueryRequest.HttpMethodType.ToString();

            searchQueryRequest.AcceptType = AcceptType.HasValue ? AcceptType.Value : searchQueryRequest.AcceptType;

            searchQueryRequest.ClientType = GetClientTypeName(searchQueryRequest);

            // This cmdlet keeps select properties as a list of strings, and SearchQueryRequest uses a string.

            // searchQueryRequest.SelectProperties is set very last thing before we execute the query in SetSelectProperties()


            searchQueryRequest.HitHighlightedProperties = new StringListArgumentParser(HithighlightedProperties).Parse() ?? searchQueryRequest.HitHighlightedProperties;


            searchQueryRequest.SortList = new SortListArgumentParser(Sort).Parse() ?? searchQueryRequest.SortList;

            // set based on switches
            bool?switchValue;

            switchValue = GetThreeWaySwitchValue(EnableStemming, DisableStemming);
            if (switchValue.HasValue)
            {
                searchQueryRequest.EnableStemming = switchValue;
            }

            switchValue = GetThreeWaySwitchValue(EnablePhonetic, DisablePhonetic);
            if (switchValue.HasValue)
            {
                searchQueryRequest.EnablePhonetic = switchValue;
            }

            switchValue = GetThreeWaySwitchValue(EnableNickNames, DisableNickNames);
            if (switchValue.HasValue)
            {
                searchQueryRequest.EnableNicknames = switchValue;
            }

            switchValue = GetThreeWaySwitchValue(EnableTrimDuplicates, DisableTrimDuplicates);
            if (switchValue.HasValue)
            {
                searchQueryRequest.TrimDuplicates = switchValue;
            }

            switchValue = GetThreeWaySwitchValue(EnableFql, DisableFql);
            if (switchValue.HasValue)
            {
                searchQueryRequest.EnableFql = switchValue;
            }

            switchValue = GetThreeWaySwitchValue(EnableQueryRules, DisableQueryRules);
            if (switchValue.HasValue)
            {
                searchQueryRequest.EnableQueryRules = switchValue;
            }

            switchValue = GetThreeWaySwitchValue(EnableProcessBestBets, DisableProcessBestBets);
            if (switchValue.HasValue)
            {
                searchQueryRequest.ProcessBestBets = switchValue;
            }

            switchValue = GetThreeWaySwitchValue(EnableByPassResultTypes, DisableByPassResultTypes);
            if (switchValue.HasValue)
            {
                searchQueryRequest.ByPassResultTypes = switchValue;
            }

            switchValue = GetThreeWaySwitchValue(EnableProcessPersonalFavorites, DisableProcessPersonalFavorites);
            if (switchValue.HasValue)
            {
                searchQueryRequest.ProcessPersonalFavorites = switchValue;
            }

            searchQueryRequest.RankingModelId = RankingModelId ?? searchQueryRequest.RankingModelId;

            if (RankingModelName.HasValue)
            {
                if (!String.IsNullOrWhiteSpace(RankingModelId))
                {
                    WriteWarning("RankingModelName overrides RankingModelId. Please use one or the other.");
                }

                searchQueryRequest.RankingModelId = RankModel.Select(RankingModelName.Value);
            }

            searchQueryRequest.SourceId = ResultSourceId ?? searchQueryRequest.SourceId;

            if (ResultSourceName.HasValue)
            {
                if (!String.IsNullOrWhiteSpace(ResultSourceId))
                {
                    WriteWarning("ResultSourceName overrides ResutSourceId. Please use one or the other.");
                }

                searchQueryRequest.SourceId = ResultSource.Select(ResultSourceName.Value);
            }

            searchQueryRequest.HiddenConstraints = HiddenConstraints ?? searchQueryRequest.HiddenConstraints;
            searchQueryRequest.QueryTemplate     = QueryTemplate ?? searchQueryRequest.QueryTemplate;

            searchQueryRequest.RowLimit = RowLimit.HasValue ? RowLimit : (searchQueryRequest.RowLimit.HasValue ? searchQueryRequest.RowLimit : rowLimitDefault);
            searchQueryRequest.StartRow = StartRow.HasValue ? StartRow : (searchQueryRequest.StartRow.HasValue ? searchQueryRequest.StartRow : startRowDefault);
            searchQueryRequest.Timeout  = Timeout.HasValue ? Timeout : (searchQueryRequest.Timeout.HasValue ? searchQueryRequest.Timeout : timeoutDefault);

            searchQueryRequest.Refiners = new StringListArgumentParser(Refiners).Parse() ?? searchQueryRequest.Refiners;


            if (Credential != null || searchQueryRequest.AuthenticationType == AuthenticationType.Windows)
            {
                if (Credential == null)
                {
                    var userName = searchQueryRequest.UserName;

                    Credential = this.Host.UI.PromptForCredential("Enter username/password", "", userName, "");
                }

                searchQueryRequest.AuthenticationType = AuthenticationType.Windows;
                searchQueryRequest.UserName           = Credential.UserName;
                searchQueryRequest.SecurePassword     = Credential.Password;
            }
            else if (searchQueryRequest.AuthenticationType == AuthenticationType.SPO)
            {
                Guid runspaceId = Guid.Empty;
                using (var ps = PowerShell.Create(RunspaceMode.CurrentRunspace))
                {
                    runspaceId = ps.Runspace.InstanceId;

                    CookieCollection cc;

                    bool found = Tokens.TryGetValue(runspaceId, out cc);

                    if (!found)
                    {
                        cc = PSWebAuthentication.GetAuthenticatedCookies(this, searchQueryRequest.SharePointSiteUrl, AuthenticationType.SPO);

                        if (cc == null)
                        {
                            throw new RuntimeException("Authentication cookie returned is null! Authentication failed. Please try again.");  // TODO find another exception
                        }
                        else
                        {
                            Tokens.Add(runspaceId, cc);
                        }
                    }

                    searchQueryRequest.AuthenticationType = AuthenticationType.SPO;
                    searchQueryRequest.Cookies            = cc;
                    //searchSuggestionsRequest.Cookies = cc;
                }
            }
            else if (AuthenticationMethod == PSAuthenticationMethod.SPOManagement || searchQueryRequest.AuthenticationType == AuthenticationType.SPOManagement)
            {
                AdalAuthentication adalAuth = new AdalAuthentication();

                var task = adalAuth.Login(searchQueryRequest.SharePointSiteUrl);

                if (!task.Wait(300000))
                {
                    throw new TimeoutException("Prompt for user credentials timed out after 5 minutes.");
                }

                var token = task.Result;

                searchQueryRequest.AuthenticationType = AuthenticationType.SPOManagement;
                searchQueryRequest.Token = token;
                //searchSuggestionsRequest.Token = token;
            }
            else
            {
                searchQueryRequest.AuthenticationType = AuthenticationType.CurrentUser;
                WindowsIdentity currentWindowsIdentity = WindowsIdentity.GetCurrent();
                searchQueryRequest.UserName = currentWindowsIdentity.Name;
            }
        }