Пример #1
0
        private AuthenticationRequestParameters CreateAuthenticationParametersAndSetupMocks(
            MockHttpAndServiceBundle harness,
            int numAuthorizationPendingResults,
            out HashSet <string> expectedScopes,
            bool isAdfs = false)
        {
            var cache      = new TokenCache(harness.ServiceBundle, false);
            var parameters = harness.CreateAuthenticationRequestParameters(
                isAdfs ? TestConstants.OnPremiseAuthority : TestConstants.AuthorityHomeTenant,
                null,
                cache,
                null,
                extraQueryParameters: TestConstants.s_extraQueryParams,
                claims: TestConstants.Claims);

            if (isAdfs)
            {
                harness.HttpManager.AddMockHandler(new MockHttpMessageHandler
                {
                    ExpectedMethod  = HttpMethod.Get,
                    ResponseMessage = MockHelpers.CreateAdfsOpenIdConfigurationResponse(TestConstants.OnPremiseAuthority)
                });
            }
            else
            {
                TestCommon.MockInstanceDiscoveryAndOpenIdRequest(harness.HttpManager);
            }

            expectedScopes = new HashSet <string>();
            expectedScopes.UnionWith(TestConstants.s_scope);
            expectedScopes.Add(OAuth2Value.ScopeOfflineAccess);
            expectedScopes.Add(OAuth2Value.ScopeProfile);
            expectedScopes.Add(OAuth2Value.ScopeOpenId);

            IDictionary <string, string> extraQueryParamsAndClaims =
                TestConstants.s_extraQueryParams.ToDictionary(e => e.Key, e => e.Value);

            extraQueryParamsAndClaims.Add(OAuth2Parameter.Claims, TestConstants.Claims);

            // Mock Handler for device code request
            harness.HttpManager.AddMockHandler(
                new MockHttpMessageHandler
            {
                ExpectedMethod   = HttpMethod.Post,
                ExpectedPostData = new Dictionary <string, string>()
                {
                    { OAuth2Parameter.ClientId, TestConstants.ClientId },
                    { OAuth2Parameter.Scope, expectedScopes.AsSingleString() }
                },
                ResponseMessage     = isAdfs ? CreateAdfsDeviceCodeResponseSuccessMessage() : CreateDeviceCodeResponseSuccessMessage(),
                ExpectedQueryParams = extraQueryParamsAndClaims
            });

            for (int i = 0; i < numAuthorizationPendingResults; i++)
            {
                harness.HttpManager.AddMockHandler(
                    new MockHttpMessageHandler
                {
                    ExpectedMethod  = HttpMethod.Post,
                    ExpectedUrl     = isAdfs ? "https://fs.contoso.com/adfs/oauth2/token" : "https://login.microsoftonline.com/home/oauth2/v2.0/token",
                    ResponseMessage = MockHelpers.CreateFailureMessage(
                        HttpStatusCode.Forbidden,
                        "{\"error\":\"authorization_pending\"," +
                        "\"error_description\":\"AADSTS70016: Pending end-user authorization." +
                        "\\r\\nTrace ID: f6c2c73f-a21d-474e-a71f-d8b121a58205\\r\\nCorrelation ID: " +
                        "36fe3e82-442f-4418-b9f4-9f4b9295831d\\r\\nTimestamp: 2015-09-24 19:51:51Z\"," +
                        "\"error_codes\":[70016],\"timestamp\":\"2015-09-24 19:51:51Z\",\"trace_id\":" +
                        "\"f6c2c73f-a21d-474e-a71f-d8b121a58205\",\"correlation_id\":" +
                        "\"36fe3e82-442f-4418-b9f4-9f4b9295831d\"}")
                });
            }

            if (numAuthorizationPendingResults > 0)
            {
                // Mock Handler for devicecode->token exchange request
                harness.HttpManager.AddMockHandler(
                    new MockHttpMessageHandler
                {
                    ExpectedMethod   = HttpMethod.Post,
                    ExpectedPostData = new Dictionary <string, string>()
                    {
                        { OAuth2Parameter.ClientId, TestConstants.ClientId },
                        { OAuth2Parameter.Scope, expectedScopes.AsSingleString() }
                    },
                    ResponseMessage = isAdfs ? MockHelpers.CreateAdfsSuccessTokenResponseMessage() : MockHelpers.CreateSuccessTokenResponseMessage()
                });
            }

            return(parameters);
        }