diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/AuthenticationPropertiesFormaterKeyValue.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/AuthenticationPropertiesFormaterKeyValue.cs
deleted file mode 100644
index 1be4b80bca..0000000000
--- a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/AuthenticationPropertiesFormaterKeyValue.cs
+++ /dev/null
@@ -1,70 +0,0 @@
-// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
-// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-
-using System;
-using System.Text;
-using System.Text.Encodings.Web;
-using Microsoft.AspNetCore.Http.Authentication;
-
-namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
-{
- ///
- /// This formatter creates an easy to read string of the format: "'key1' 'value1' ..."
- ///
- public class AuthenticationPropertiesFormaterKeyValue : ISecureDataFormat
- {
- string _protectedString = Guid.NewGuid().ToString();
-
- public string Protect(AuthenticationProperties data)
- {
- if (data == null || data.Items.Count == 0)
- {
- return "null";
- }
-
- var sb = new StringBuilder();
- foreach(var item in data.Items)
- {
- sb.Append(Uri.EscapeDataString(item.Key) + " " + Uri.EscapeDataString(item.Value) + " ");
- }
-
- return sb.ToString();
- }
- public string Protect(AuthenticationProperties data, string purpose)
- {
- return Protect(data);
- }
-
- public AuthenticationProperties Unprotect(string protectedText)
- {
- if (string.IsNullOrEmpty(protectedText))
- {
- return null;
- }
-
- if (protectedText == "null")
- {
- return new AuthenticationProperties();
- }
-
- string[] items = protectedText.Split(' ');
- if (items.Length % 2 != 0)
- {
- return null;
- }
-
- var propeties = new AuthenticationProperties();
- for (int i = 0; i < items.Length - 1; i+=2)
- {
- propeties.Items.Add(items[i], items[i + 1]);
- }
-
- return propeties;
- }
-
- public AuthenticationProperties Unprotect(string protectedText, string purpose)
- {
- return Unprotect(protectedText);
- }
- }
-}
diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/ExpectedQueryValues.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/ExpectedQueryValues.cs
deleted file mode 100644
index 98df02ee61..0000000000
--- a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/ExpectedQueryValues.cs
+++ /dev/null
@@ -1,175 +0,0 @@
-// Copyright (c) .NET Foundation. All rights reserved.
-// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Text;
-using System.Text.Encodings.Web;
-using Microsoft.IdentityModel.Protocols.OpenIdConnect;
-using Xunit;
-
-namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
-{
- ///
- /// This helper class is used to check that query string parameters are as expected.
- ///
- public class ExpectedQueryValues
- {
- public ExpectedQueryValues(string authority, OpenIdConnectConfiguration configuration = null)
- {
- Authority = authority;
- Configuration = configuration ?? TestUtilities.DefaultOpenIdConnectConfiguration;
- }
-
- public static ExpectedQueryValues Defaults(string authority)
- {
- var result = new ExpectedQueryValues(authority);
- result.Scope = OpenIdConnectScope.OpenIdProfile;
- result.ResponseType = OpenIdConnectResponseType.CodeIdToken;
- return result;
- }
-
- public void CheckValues(string query, IEnumerable parameters)
- {
- var errors = new List();
- if (!query.StartsWith(ExpectedAuthority))
- {
- errors.Add("ExpectedAuthority: " + ExpectedAuthority);
- }
-
- foreach(var str in parameters)
- {
- if (str == OpenIdConnectParameterNames.ClientId)
- {
- if (!query.Contains(ExpectedClientId))
- errors.Add("ExpectedClientId: " + ExpectedClientId);
-
- continue;
- }
-
- if (str == OpenIdConnectParameterNames.RedirectUri)
- {
- if(!query.Contains(ExpectedRedirectUri))
- errors.Add("ExpectedRedirectUri: " + ExpectedRedirectUri);
-
- continue;
- }
-
- if (str == OpenIdConnectParameterNames.Resource)
- {
- if(!query.Contains(ExpectedResource))
- errors.Add("ExpectedResource: " + ExpectedResource);
-
- continue;
- }
-
- if (str == OpenIdConnectParameterNames.ResponseMode)
- {
- if(!query.Contains(ExpectedResponseMode))
- errors.Add("ExpectedResponseMode: " + ExpectedResponseMode);
-
- continue;
- }
-
- if (str == OpenIdConnectParameterNames.Scope)
- {
- if (!query.Contains(ExpectedScope))
- errors.Add("ExpectedScope: " + ExpectedScope);
-
- continue;
- }
-
- if (str == OpenIdConnectParameterNames.State)
- {
- if (!query.Contains(ExpectedState))
- errors.Add("ExpectedState: " + ExpectedState);
-
- continue;
- }
- }
-
- if (errors.Count > 0)
- {
- var sb = new StringBuilder();
- sb.AppendLine("query string not as expected: " + Environment.NewLine + query + Environment.NewLine);
- foreach (var str in errors)
- {
- sb.AppendLine(str);
- }
-
- Debug.WriteLine(sb.ToString());
- Assert.True(false, sb.ToString());
- }
- }
-
- public UrlEncoder Encoder { get; set; } = UrlEncoder.Default;
-
- public string Authority { get; set; }
-
- public string ClientId { get; set; } = Guid.NewGuid().ToString();
-
- public string RedirectUri { get; set; } = Guid.NewGuid().ToString();
-
- public OpenIdConnectRequestType RequestType { get; set; } = OpenIdConnectRequestType.Authentication;
-
- public string Resource { get; set; } = Guid.NewGuid().ToString();
-
- public string ResponseMode { get; set; } = OpenIdConnectResponseMode.FormPost;
-
- public string ResponseType { get; set; } = Guid.NewGuid().ToString();
-
- public string Scope { get; set; } = Guid.NewGuid().ToString();
-
- public string State { get; set; } = Guid.NewGuid().ToString();
-
- public string ExpectedAuthority
- {
- get
- {
- if (RequestType == OpenIdConnectRequestType.Token)
- {
- return Configuration?.EndSessionEndpoint ?? Authority + @"/oauth2/token";
- }
- else if (RequestType == OpenIdConnectRequestType.Logout)
- {
- return Configuration?.TokenEndpoint ?? Authority + @"/oauth2/logout";
- }
-
- return Configuration?.AuthorizationEndpoint ?? Authority + (@"/oauth2/authorize");
- }
- }
-
- public OpenIdConnectConfiguration Configuration { get; set; }
-
- public string ExpectedClientId
- {
- get { return OpenIdConnectParameterNames.ClientId + "=" + Encoder.Encode(ClientId); }
- }
-
- public string ExpectedRedirectUri
- {
- get { return OpenIdConnectParameterNames.RedirectUri + "=" + Encoder.Encode(RedirectUri); }
- }
-
- public string ExpectedResource
- {
- get { return OpenIdConnectParameterNames.Resource + "=" + Encoder.Encode(Resource); }
- }
-
- public string ExpectedResponseMode
- {
- get { return OpenIdConnectParameterNames.ResponseMode + "=" + Encoder.Encode(ResponseMode); }
- }
-
- public string ExpectedScope
- {
- get { return OpenIdConnectParameterNames.Scope + "=" + Encoder.Encode(Scope); }
- }
-
- public string ExpectedState
- {
- get { return Encoder.Encode(State); }
- }
- }
-}
diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/Infrastructure/TestDefaultValues.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/Infrastructure/TestDefaultValues.cs
deleted file mode 100644
index de921bb96a..0000000000
--- a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/Infrastructure/TestDefaultValues.cs
+++ /dev/null
@@ -1,10 +0,0 @@
-// Copyright (c) .NET Foundation. All rights reserved.
-// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-
-namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect.Infrastructre
-{
- internal class TestDefaultValues
- {
- public static readonly string DefaultAuthority = @"https://example.com/common";
- }
-}
\ No newline at end of file
diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/MockOpenIdConnectMessage.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/MockOpenIdConnectMessage.cs
new file mode 100644
index 0000000000..432980f771
--- /dev/null
+++ b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/MockOpenIdConnectMessage.cs
@@ -0,0 +1,21 @@
+using Microsoft.IdentityModel.Protocols.OpenIdConnect;
+
+namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
+{
+ internal class MockOpenIdConnectMessage : OpenIdConnectMessage
+ {
+ public string TestAuthorizeEndpoint { get; set; }
+
+ public string TestLogoutRequest { get; set; }
+
+ public override string CreateAuthenticationRequestUrl()
+ {
+ return TestAuthorizeEndpoint ?? base.CreateAuthenticationRequestUrl();
+ }
+
+ public override string CreateLogoutRequestUrl()
+ {
+ return TestLogoutRequest ?? base.CreateLogoutRequestUrl();
+ }
+ }
+}
\ No newline at end of file
diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectChallengeTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectChallengeTests.cs
new file mode 100644
index 0000000000..b2e2514d61
--- /dev/null
+++ b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectChallengeTests.cs
@@ -0,0 +1,322 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Authentication.OpenIdConnect;
+using Microsoft.AspNetCore.Http.Authentication;
+using Microsoft.IdentityModel.Protocols.OpenIdConnect;
+using Xunit;
+
+namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
+{
+ public class OpenIdConnectChallengeTests
+ {
+ [Fact]
+ public async Task ChallengeIsIssuedCorrectly()
+ {
+ var settings = new TestSettings(
+ opt => opt.AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet);
+
+ var server = settings.CreateTestServer();
+ var transaction = await TestTransaction.SendAsync(server, ChallengeEndpoint);
+
+ var res = transaction.Response;
+ Assert.Equal(HttpStatusCode.Redirect, res.StatusCode);
+ Assert.NotNull(res.Headers.Location);
+
+ settings.ValidateChallengeRedirect(
+ res.Headers.Location,
+ OpenIdConnectParameterNames.ClientId,
+ OpenIdConnectParameterNames.ResponseType,
+ OpenIdConnectParameterNames.ResponseMode,
+ OpenIdConnectParameterNames.Scope,
+ OpenIdConnectParameterNames.RedirectUri);
+ }
+
+ /*
+ Example of a form post
+
+
+
+
+ */
+ [Fact]
+ public async Task ChallengeIssueedCorrectlyForFormPost()
+ {
+ var settings = new TestSettings(
+ opt => opt.AuthenticationMethod = OpenIdConnectRedirectBehavior.FormPost);
+
+ var server = settings.CreateTestServer();
+ var transaction = await TestTransaction.SendAsync(server, ChallengeEndpoint);
+
+ var res = transaction.Response;
+ Assert.Equal(HttpStatusCode.OK, res.StatusCode);
+ Assert.Equal("text/html", transaction.Response.Content.Headers.ContentType.MediaType);
+
+ var body = await res.Content.ReadAsStringAsync();
+ settings.ValidateChallengeFormPost(
+ body,
+ OpenIdConnectParameterNames.ClientId,
+ OpenIdConnectParameterNames.ResponseType,
+ OpenIdConnectParameterNames.ResponseMode,
+ OpenIdConnectParameterNames.Scope,
+ OpenIdConnectParameterNames.RedirectUri);
+ }
+
+ [Theory]
+ [InlineData("sample_user_state")]
+ [InlineData(null)]
+ public async Task ChallengeCanSetUserStateThroughProperties(string userState)
+ {
+ var settings = new TestSettings();
+
+ var properties = new AuthenticationProperties();
+ properties.Items.Add(OpenIdConnectDefaults.UserstatePropertiesKey, userState);
+
+ var server = TestServerBuilder.CreateServer(settings.Options, handler: null, properties: properties);
+ var transaction = await TestTransaction.SendAsync(server, TestDefaultValues.TestHost + TestServerBuilder.ChallengeWithProperties);
+
+ var res = transaction.Response;
+ Assert.Equal(HttpStatusCode.Redirect, res.StatusCode);
+ Assert.NotNull(res.Headers.Location);
+
+ var values = settings.ValidateChallengeRedirect(res.Headers.Location);
+ var actualState = values[OpenIdConnectParameterNames.State];
+ var actualProperties = settings.Options.StateDataFormat.Unprotect(actualState);
+
+ Assert.Equal(userState ?? string.Empty, actualProperties.Items[OpenIdConnectDefaults.UserstatePropertiesKey]);
+ }
+
+ [Theory]
+ [InlineData("sample_user_state")]
+ [InlineData(null)]
+ public async Task OnRedirectToIdentityProviderEventCanSetState(string userState)
+ {
+ var settings = new TestSettings(opt =>
+ {
+ opt.Events = new OpenIdConnectEvents()
+ {
+ OnRedirectToIdentityProvider = context =>
+ {
+ context.ProtocolMessage.State = userState;
+ return Task.FromResult(0);
+ }
+ };
+ });
+
+ var server = settings.CreateTestServer();
+ var transaction = await TestTransaction.SendAsync(server, ChallengeEndpoint);
+
+ var res = transaction.Response;
+ Assert.Equal(HttpStatusCode.Redirect, res.StatusCode);
+ Assert.NotNull(res.Headers.Location);
+
+ var values = settings.ValidateChallengeRedirect(res.Headers.Location);
+ var actualState = values[OpenIdConnectParameterNames.State];
+ var actualProperties = settings.Options.StateDataFormat.Unprotect(actualState);
+
+ if (userState != null)
+ {
+ Assert.Equal(userState, actualProperties.Items[OpenIdConnectDefaults.UserstatePropertiesKey]);
+ }
+ else
+ {
+ Assert.False(actualProperties.Items.ContainsKey(OpenIdConnectDefaults.UserstatePropertiesKey));
+ }
+ }
+
+ [Fact]
+ public async Task OnRedirectToIdentityProviderEventIsHit()
+ {
+ var eventIsHit = false;
+ var settings = new TestSettings(
+ opts =>
+ {
+ opts.Events = new OpenIdConnectEvents()
+ {
+ OnRedirectToIdentityProvider = context =>
+ {
+ eventIsHit = true;
+ return Task.FromResult(0);
+ }
+ };
+ }
+ );
+
+ var server = settings.CreateTestServer();
+ var transaction = await TestTransaction.SendAsync(server, ChallengeEndpoint);
+
+ Assert.True(eventIsHit);
+
+ var res = transaction.Response;
+ Assert.Equal(HttpStatusCode.Redirect, res.StatusCode);
+ Assert.NotNull(res.Headers.Location);
+
+ settings.ValidateChallengeRedirect(
+ res.Headers.Location,
+ OpenIdConnectParameterNames.ClientId,
+ OpenIdConnectParameterNames.ResponseType,
+ OpenIdConnectParameterNames.ResponseMode,
+ OpenIdConnectParameterNames.Scope,
+ OpenIdConnectParameterNames.RedirectUri);
+ }
+
+
+ [Fact]
+ public async Task OnRedirectToIdentityProviderEventCanReplaceValues()
+ {
+ var newClientId = Guid.NewGuid().ToString();
+
+ var settings = new TestSettings(
+ opts =>
+ {
+ opts.Events = new OpenIdConnectEvents()
+ {
+ OnRedirectToIdentityProvider = context =>
+ {
+ context.ProtocolMessage.ClientId = newClientId;
+
+ return Task.FromResult(0);
+ }
+ };
+ }
+ );
+
+ var server = settings.CreateTestServer();
+ var transaction = await TestTransaction.SendAsync(server, ChallengeEndpoint);
+
+ var res = transaction.Response;
+ Assert.Equal(HttpStatusCode.Redirect, res.StatusCode);
+ Assert.NotNull(res.Headers.Location);
+
+ settings.ValidateChallengeRedirect(
+ res.Headers.Location,
+ OpenIdConnectParameterNames.ResponseType,
+ OpenIdConnectParameterNames.ResponseMode,
+ OpenIdConnectParameterNames.Scope,
+ OpenIdConnectParameterNames.RedirectUri);
+
+ var actual = res.Headers.Location.Query.Trim('?').Split('&').Single(seg => seg.StartsWith($"{OpenIdConnectParameterNames.ClientId}="));
+ Assert.Equal($"{OpenIdConnectParameterNames.ClientId}={newClientId}", actual);
+ }
+
+ [Fact]
+ public async Task OnRedirectToIdentityProviderEventCanReplaceMessage()
+ {
+ var newMessage = new MockOpenIdConnectMessage
+ {
+ TestAuthorizeEndpoint = $"http://example.com/{Guid.NewGuid()}/oauth2/signin"
+ };
+
+ var settings = new TestSettings(
+ opts =>
+ {
+ opts.Events = new OpenIdConnectEvents()
+ {
+ OnRedirectToIdentityProvider = context =>
+ {
+ context.ProtocolMessage = newMessage;
+
+ return Task.FromResult(0);
+ }
+ };
+ }
+ );
+
+ var server = settings.CreateTestServer();
+ var transaction = await TestTransaction.SendAsync(server, ChallengeEndpoint);
+
+ var res = transaction.Response;
+ Assert.Equal(HttpStatusCode.Redirect, res.StatusCode);
+ Assert.NotNull(res.Headers.Location);
+
+ // The CreateAuthenticationRequestUrl method is overridden MockOpenIdConnectMessage where
+ // query string is not generated and the authorization endpoint is replaced.
+ Assert.Equal(newMessage.TestAuthorizeEndpoint, res.Headers.Location.AbsoluteUri);
+ }
+ [Fact]
+ public async Task OnRedirectToIdentityProviderEventHandlesResponse()
+ {
+ var settings = new TestSettings(
+ opts =>
+ {
+ opts.Events = new OpenIdConnectEvents()
+ {
+ OnRedirectToIdentityProvider = context =>
+ {
+ context.HandleResponse();
+ return Task.FromResult(0);
+ }
+ };
+ }
+ );
+
+ var server = settings.CreateTestServer();
+ var transaction = await TestTransaction.SendAsync(server, ChallengeEndpoint);
+
+ var res = transaction.Response;
+ Assert.Equal(HttpStatusCode.OK, res.StatusCode);
+ Assert.Null(res.Headers.Location);
+ }
+
+ // This test can be further refined. When one auth middleware skips, the authentication responsibility
+ // will be flowed to the next one. A dummy auth middleware can be added to ensure the correct logic.
+ [Fact]
+ public async Task OnRedirectToIdentityProviderEventSkipResponse()
+ {
+ var settings = new TestSettings(
+ opts =>
+ {
+ opts.Events = new OpenIdConnectEvents()
+ {
+ OnRedirectToIdentityProvider = context =>
+ {
+ context.SkipToNextMiddleware();
+ return Task.FromResult(0);
+ }
+ };
+ }
+ );
+
+ var server = settings.CreateTestServer();
+ var transaction = await TestTransaction.SendAsync(server, ChallengeEndpoint);
+
+ var res = transaction.Response;
+ Assert.Equal(HttpStatusCode.OK, res.StatusCode);
+ Assert.Null(res.Headers.Location);
+ }
+
+ [Fact]
+ public async Task ChallengeSetsNonceAndStateCookies()
+ {
+ var settings = new TestSettings();
+ var server = settings.CreateTestServer();
+ var transaction = await TestTransaction.SendAsync(server, ChallengeEndpoint);
+
+ var firstCookie = transaction.SetCookie.First();
+ Assert.Contains(OpenIdConnectDefaults.CookieNoncePrefix, firstCookie);
+ Assert.Contains("expires", firstCookie);
+
+ var secondCookie = transaction.SetCookie.Skip(1).First();
+ Assert.StartsWith(".AspNetCore.Correlation.OpenIdConnect.", secondCookie);
+ Assert.Contains("expires", secondCookie);
+ }
+
+ private static string ChallengeEndpoint => TestDefaultValues.TestHost + TestServerBuilder.Challenge;
+ }
+}
\ No newline at end of file
diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectConfigurationTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectConfigurationTests.cs
index 4ada6ff859..3603c2bf40 100644
--- a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectConfigurationTests.cs
+++ b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectConfigurationTests.cs
@@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
-using Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect.Infrastructre;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs
index 5853e38967..4a750ea41d 100644
--- a/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs
+++ b/test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs
@@ -2,24 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
-using System.Collections.Generic;
using System.Globalization;
-using System.Linq;
using System.Net;
-using System.Net.Http;
-using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
-using System.Xml.Linq;
-using Microsoft.AspNetCore.Authentication.Cookies;
-using Microsoft.AspNetCore.Authentication.OpenIdConnect;
-using Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect.Infrastructre;
using Microsoft.AspNetCore.Builder;
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Http.Authentication;
-using Microsoft.AspNetCore.TestHost;
-using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Xunit;
@@ -29,318 +16,47 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
{
static string noncePrefix = "OpenIdConnect." + "Nonce.";
static string nonceDelimiter = ".";
- const string Challenge = "/challenge";
- const string ChallengeWithOutContext = "/challengeWithOutContext";
- const string ChallengeWithProperties = "/challengeWithProperties";
const string DefaultHost = @"https://example.com";
- const string ExpectedAuthorizeRequest = @"https://example.com/common/oauth2/signin";
- const string ExpectedLogoutRequest = @"https://example.com/common/oauth2/logout";
const string Logout = "/logout";
- const string Signin = "/signin";
- const string Signout = "/signout";
-
- [Fact]
- public async Task ChallengeWillIssueHtmlFormWhenEnabled()
- {
- var server = CreateServer(new OpenIdConnectOptions
- {
- Authority = TestDefaultValues.DefaultAuthority,
- ClientId = "Test Id",
- Configuration = TestUtilities.DefaultOpenIdConnectConfiguration,
- AuthenticationMethod = OpenIdConnectRedirectBehavior.FormPost
- });
- var transaction = await SendAsync(server, DefaultHost + Challenge);
- Assert.Equal(HttpStatusCode.OK, transaction.Response.StatusCode);
- Assert.Equal("text/html", transaction.Response.Content.Headers.ContentType.MediaType);
- Assert.Contains("form", transaction.ResponseText);
- }
-
- [Fact]
- public async Task ChallengeWillSetDefaults()
- {
- var stateDataFormat = new AuthenticationPropertiesFormaterKeyValue();
- var queryValues = ExpectedQueryValues.Defaults(TestDefaultValues.DefaultAuthority);
- queryValues.State = OpenIdConnectDefaults.AuthenticationPropertiesKey + "=" + stateDataFormat.Protect(new AuthenticationProperties());
- var server = CreateServer(GetOptions(DefaultParameters(), queryValues));
-
- var transaction = await SendAsync(server, DefaultHost + Challenge);
- Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
- queryValues.CheckValues(transaction.Response.Headers.Location.AbsoluteUri, DefaultParameters());
- }
-
- [Fact]
- public async Task ChallengeWillSetNonceAndStateCookies()
- {
- var server = CreateServer(new OpenIdConnectOptions
- {
- Authority = TestDefaultValues.DefaultAuthority,
- ClientId = "Test Id",
- Configuration = TestUtilities.DefaultOpenIdConnectConfiguration
- });
- var transaction = await SendAsync(server, DefaultHost + Challenge);
-
- var firstCookie = transaction.SetCookie.First();
- Assert.Contains(OpenIdConnectDefaults.CookieNoncePrefix, firstCookie);
- Assert.Contains("expires", firstCookie);
-
- var secondCookie = transaction.SetCookie.Skip(1).First();
- Assert.StartsWith(".AspNetCore.Correlation.OpenIdConnect.", secondCookie);
- Assert.Contains("expires", secondCookie);
- }
-
- [Fact]
- public async Task ChallengeWillUseOptionsProperties()
- {
- var queryValues = new ExpectedQueryValues(TestDefaultValues.DefaultAuthority);
- var server = CreateServer(GetOptions(DefaultParameters(), queryValues));
-
- var transaction = await SendAsync(server, DefaultHost + Challenge);
- Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
- queryValues.CheckValues(transaction.Response.Headers.Location.AbsoluteUri, DefaultParameters());
- }
-
- ///
- /// Tests RedirectForAuthenticationContext replaces the OpenIdConnectMesssage correctly.
- ///
- /// Task
- [Fact]
- public async Task ChallengeSettingMessage()
- {
- var configuration = new OpenIdConnectConfiguration
- {
- AuthorizationEndpoint = ExpectedAuthorizeRequest,
- };
-
- var queryValues = new ExpectedQueryValues(TestDefaultValues.DefaultAuthority, configuration)
- {
- RequestType = OpenIdConnectRequestType.Authentication
- };
- var server = CreateServer(GetProtocolMessageOptions());
- var transaction = await SendAsync(server, DefaultHost + Challenge);
- Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
- queryValues.CheckValues(transaction.Response.Headers.Location.AbsoluteUri, new string[] { });
- }
///
/// Tests RedirectForSignOutContext replaces the OpenIdConnectMesssage correctly.
- ///
+ /// summary>
/// Task
[Fact]
public async Task SignOutSettingMessage()
{
- var configuration = new OpenIdConnectConfiguration
+ var setting = new TestSettings(opt =>
{
- EndSessionEndpoint = ExpectedLogoutRequest
- };
-
- var queryValues = new ExpectedQueryValues(TestDefaultValues.DefaultAuthority, configuration)
- {
- RequestType = OpenIdConnectRequestType.Logout
- };
- var server = CreateServer(GetProtocolMessageOptions());
- var transaction = await SendAsync(server, DefaultHost + Signout);
- Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode);
- queryValues.CheckValues(transaction.Response.Headers.Location.AbsoluteUri, new string[] { });
- }
-
- private static OpenIdConnectOptions GetProtocolMessageOptions()
- {
- var options = new OpenIdConnectOptions();
- var fakeOpenIdRequestMessage = new FakeOpenIdConnectMessage(ExpectedAuthorizeRequest, ExpectedLogoutRequest);
- options.AutomaticChallenge = true;
- options.Events = new OpenIdConnectEvents()
- {
- OnRedirectToIdentityProvider = (context) =>
+ opt.Configuration = new OpenIdConnectConfiguration
{
- context.ProtocolMessage = fakeOpenIdRequestMessage;
- return Task.FromResult(0);
- },
- OnRedirectToIdentityProviderForSignOut = (context) =>
- {
- context.ProtocolMessage = fakeOpenIdRequestMessage;
- return Task.FromResult(0);
- }
- };
- options.ClientId = "Test Id";
- options.Configuration = TestUtilities.DefaultOpenIdConnectConfiguration;
- return options;
- }
-
- private class FakeOpenIdConnectMessage : OpenIdConnectMessage
- {
- private readonly string _authorizeRequest;
- private readonly string _logoutRequest;
-
- public FakeOpenIdConnectMessage(string authorizeRequest, string logoutRequest)
- {
- _authorizeRequest = authorizeRequest;
- _logoutRequest = logoutRequest;
- }
-
- public override string CreateAuthenticationRequestUrl()
- {
- return _authorizeRequest;
- }
-
- public override string CreateLogoutRequestUrl()
- {
- return _logoutRequest;
- }
- }
-
- ///
- /// Tests for users who want to add 'state'. There are two ways to do it.
- /// 1. Users set 'state' (OpenIdConnectMessage.State) in the event. The runtime appends to that state.
- /// 2. Users add to the AuthenticationProperties (context.AuthenticationProperties), values will be serialized.
- ///
- ///
- ///
- [Theory, MemberData("StateDataSet")]
- public async Task ChallengeSettingState(string userState, string challenge)
- {
- var queryValues = new ExpectedQueryValues(TestDefaultValues.DefaultAuthority);
- var stateDataFormat = new AuthenticationPropertiesFormaterKeyValue();
- var properties = new AuthenticationProperties();
- if (challenge == ChallengeWithProperties)
- {
- properties.Items.Add("item1", Guid.NewGuid().ToString());
- }
-
- var options = GetOptions(DefaultParameters(new string[] { OpenIdConnectParameterNames.State }), queryValues, stateDataFormat);
- options.AutomaticChallenge = challenge.Equals(ChallengeWithOutContext);
- options.Events = new OpenIdConnectEvents()
- {
- OnRedirectToIdentityProvider = context =>
- {
- context.ProtocolMessage.State = userState;
- context.ProtocolMessage.RedirectUri = queryValues.RedirectUri;
- return Task.FromResult