Skip to content

Commit

Permalink
Port 3rd party contribution to ASP.NET (#191) (#207)
Browse files Browse the repository at this point in the history
* Port 3rd party contribution to ASP.NET (#205).
* Update dependencies.
  • Loading branch information
laura-rodriguez authored May 3, 2022
1 parent dbcc01d commit 1c1e81e
Show file tree
Hide file tree
Showing 21 changed files with 321 additions and 112 deletions.
18 changes: 12 additions & 6 deletions Okta.AspNet.Abstractions.Test/Okta.AspNet.Abstractions.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;net5.0</TargetFrameworks>
Expand All @@ -7,11 +7,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Okta.AspNet.Abstractions/Okta.AspNet.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

<ItemGroup>
<PackageReference Include="AsyncUsageAnalyzers" Version="1.0.0-alpha003" PrivateAssets="All" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.7.1" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.17.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.7.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.17.0" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
<AdditionalFiles Include="..\stylecop.json" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Okta.AspNet.Test/JwtOptionsBuilderShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public void BuildJwtBearerOptions()
OAuthBearerAuthenticationProvider = mockAuthnProvider,
};

var jwtOptions = JwtOptionsBuilder.BuildJwtBearerAuthenticationOptions(oktaWebApiOptions);
var jwtOptions = JwtOptionsBuilder.BuildJwtBearerAuthenticationOptions("customAuthType", oktaWebApiOptions);
jwtOptions.Should().NotBeNull();
jwtOptions.Provider.Should().Be(mockAuthnProvider);
jwtOptions.AuthenticationType.Should().Be("customAuthType");
}
}
}
11 changes: 7 additions & 4 deletions Okta.AspNet.Test/Okta.AspNet.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@

<ItemGroup>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="FluentAssertions" Version="5.3.0" />
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NSubstitute" Version="4.2.0" />
<PackageReference Include="NSubstitute" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<ProjectReference Include="..\Okta.AspNet\Okta.AspNet.csproj" />
<PackageReference Include="AsyncUsageAnalyzers" Version="1.0.0-alpha003" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta007" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<AdditionalFiles Include="..\stylecop.json" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ public void BuildOpenIdConnectAuthenticationOptionsCorrectly()
BackchannelHttpClientHandler = new MockHttpClientHandler(),
};

var oidcOptions = new OpenIdConnectAuthenticationOptionsBuilder(oktaMvcOptions).BuildOpenIdConnectAuthenticationOptions();
var oidcOptions = new OpenIdConnectAuthenticationOptionsBuilder("customAuthType", oktaMvcOptions).BuildOpenIdConnectAuthenticationOptions();

oidcOptions.ClientId.Should().Be(oktaMvcOptions.ClientId);
oidcOptions.ClientSecret.Should().Be(oktaMvcOptions.ClientSecret);
oidcOptions.PostLogoutRedirectUri.Should().Be(oktaMvcOptions.PostLogoutRedirectUri);
oidcOptions.AuthenticationMode.Should().Be(AuthenticationMode.Active);
oidcOptions.BackchannelTimeout.Should().Be(TimeSpan.MaxValue);
oidcOptions.BackchannelHttpHandler.GetType().Should().Be(typeof(MockHttpClientHandler));
oidcOptions.AuthenticationType.Should().Be("customAuthType");

var issuer = UrlHelper.CreateIssuerUrl(oktaMvcOptions.OktaDomain, oktaMvcOptions.AuthorizationServerId);
oidcOptions.Authority.Should().Be(issuer);
Expand Down Expand Up @@ -82,12 +83,13 @@ public void CallUserInformationProviderWhenGetClaimsFromUserInfoEndpointIsTrue()
List<Claim> claims = new List<Claim>();
claims.Add(new Claim("testClaimType", "testClaimValue"));

var oidcOptions = new OpenIdConnectAuthenticationOptionsBuilder(oktaMvcOptions, new MockUserInformationProvider(claims)).BuildOpenIdConnectAuthenticationOptions();
var oidcOptions = new OpenIdConnectAuthenticationOptionsBuilder(OktaDefaults.MvcAuthenticationType, oktaMvcOptions, new MockUserInformationProvider(claims)).BuildOpenIdConnectAuthenticationOptions();

oidcOptions.ClientId.Should().Be(oktaMvcOptions.ClientId);
oidcOptions.ClientSecret.Should().Be(oktaMvcOptions.ClientSecret);
oidcOptions.PostLogoutRedirectUri.Should().Be(oktaMvcOptions.PostLogoutRedirectUri);
oidcOptions.AuthenticationMode.Should().Be(AuthenticationMode.Active);
oidcOptions.AuthenticationType.Should().Be(OktaDefaults.MvcAuthenticationType);

var issuer = UrlHelper.CreateIssuerUrl(oktaMvcOptions.OktaDomain, oktaMvcOptions.AuthorizationServerId);
oidcOptions.Authority.Should().Be(issuer);
Expand Down Expand Up @@ -118,7 +120,7 @@ public void SetAuthenticationModeToPassiveWhenLoginModeIsSelfHosted()
LoginMode = LoginMode.SelfHosted,
};

var oidcOptions = new OpenIdConnectAuthenticationOptionsBuilder(oktaMvcOptions).BuildOpenIdConnectAuthenticationOptions();
var oidcOptions = new OpenIdConnectAuthenticationOptionsBuilder(OktaDefaults.MvcAuthenticationType, oktaMvcOptions).BuildOpenIdConnectAuthenticationOptions();

oidcOptions.AuthenticationMode.Should().Be(AuthenticationMode.Passive);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// <copyright file="MiddlewareShould.cs" company="Okta, Inc">
// Copyright (c) 2018-present Okta, Inc. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
// </copyright>

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Dispatcher;
using Microsoft.Owin.Testing;
using Owin;
using Xunit;

namespace Okta.AspNet.WebApi.IntegrationTest
{
public class MiddlewareWithCustomAuthTypeShould : IDisposable
{
private TestServer _server;

private string BaseUrl { get; set; }

private string ProtectedEndpoint { get; set; }

private MockHttpMessageHandler MockHttpHandler { get; set; }

public MiddlewareWithCustomAuthTypeShould()
{
BaseUrl = "http://localhost:8080";
ProtectedEndpoint = string.Format("{0}/api/messages", BaseUrl);
MockHttpHandler = new MockHttpMessageHandler();

_server = TestServer.Create(app =>
{
var startup = new StartupWithCustomAuthType();
startup.HttpMessageHandler = MockHttpHandler;
startup.Configuration(app);

HttpConfiguration config = new HttpConfiguration();
config.Services.Replace(typeof(IAssembliesResolver), new WebApiResolver());
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
});

_server.BaseAddress = new Uri(BaseUrl);
}

[Fact]
public async Task Returns401WhenAccessToProtectedRouteWithoutTokenAsync()
{
using (var client = new HttpClient(_server.Handler))
{
var response = await client.GetAsync(ProtectedEndpoint);
Assert.True(response.StatusCode == System.Net.HttpStatusCode.Unauthorized);
}
}

[Fact]
public async Task Returns401WhenAccessToProtectedRouteWithInvalidTokenAsync()
{
var accessToken = "thisIsAnInvalidToken";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, ProtectedEndpoint);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

using (var client = new HttpClient(_server.Handler))
{
var response = await client.SendAsync(request);
Assert.True(response.StatusCode == System.Net.HttpStatusCode.Unauthorized);
}
}

[Fact]
public async Task InvokeCustomEventsAsync()
{
var accessToken = "thisIsAnInvalidToken";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, ProtectedEndpoint);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

using (var client = new HttpClient(_server.Handler))
{
var response = await client.SendAsync(request);
Assert.True(response.Headers.Contains("myCustomHeader"));
}
}

[Fact]
public async Task InvokeCustomHandlerAsync()
{
var accessToken = "thisIsAnInvalidToken";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, ProtectedEndpoint);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

using (var client = new HttpClient(_server.Handler))
{
var response = await client.SendAsync(request);
Assert.True(MockHttpHandler.NumberOfCalls > 0);
}
}

public void Dispose()
{
_server.Dispose();
}
}
}
Loading

0 comments on commit 1c1e81e

Please sign in to comment.