예제 #1
0
        public async Task AutoLaunch_Sets_Correct_RedirectUri()
        {
            // Arrange mocks
            var autoLaunchOptions = new BankIdLoginOptions(new List <string>(), null, false, true, false);
            var mockProtector     = new Mock <IBankIdLoginOptionsProtector>();

            mockProtector
            .Setup(protector => protector.Unprotect(It.IsAny <string>()))
            .Returns(autoLaunchOptions);

            var client = CreateServer(
                o =>
            {
                o.AuthenticationBuilder.Services.TryAddTransient <IBankIdLauncher, TestBankIdLauncher>();
                o.UseSimulatedEnvironment().AddSameDevice();
            },
                DefaultAppConfiguration(async context =>
            {
                await context.ChallengeAsync(BankIdAuthenticationDefaults.SameDeviceAuthenticationScheme);
            }),
                services =>
            {
                services.AddTransient(s => mockProtector.Object);
            })
                         .CreateClient();

            // Arrange csrf info
            var loginResponse = await client.GetAsync("/BankIdAuthentication/Login?returnUrl=%2F&loginOptions=X&orderRef=Y");

            var loginCookies = loginResponse.Headers.GetValues("set-cookie");
            var loginContent = await loginResponse.Content.ReadAsStringAsync();

            var csrfToken = TokenExtractor.ExtractRequestVerificationTokenFromForm(loginContent);

            // Arrange acting request
            var testReturnUrl     = "/TestReturnUrl";
            var testOptions       = "TestOptions";
            var initializeRequest = new JsonContent(new  { returnUrl = testReturnUrl, loginOptions = testOptions });

            initializeRequest.Headers.Add("Cookie", loginCookies);
            initializeRequest.Headers.Add("RequestVerificationToken", csrfToken);

            // Act
            var transaction = await client.PostAsync("/BankIdAuthentication/Api/Initialize", initializeRequest);

            // Assert
            Assert.Equal(HttpStatusCode.OK, transaction.StatusCode);

            var responseContent = await transaction.Content.ReadAsStringAsync();

            var responseObject = JsonConvert.DeserializeAnonymousType(responseContent, new { RedirectUri = "", OrderRef = "", IsAutoLaunch = false });

            Assert.True(responseObject.IsAutoLaunch);

            var encodedReturnParam = UrlEncoder.Default.Encode(testReturnUrl);
            var expectedUrl        = $"http://localhost/BankIdAuthentication/Login?returnUrl={encodedReturnParam}&loginOptions={testOptions}";

            Assert.Equal(expectedUrl, responseObject.RedirectUri);
        }