-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request RC-4.0.0 merging to the main branch approval (#192)
* Expose OIDC and JWT events (#187) * Expose OIDC events for .NET 4.x and Core MVC projects. * Expose JWT events for .NET 4.x and Core WebApi projects. * Expose OIDC events for .NET Core MVC projects. * Expose JWT events for .NET Core WebApi projects. * Add tests. * Update readme.
- Loading branch information
1 parent
77b3847
commit 0435a6d
Showing
44 changed files
with
1,082 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Okta.AspNet SDK migration guide | ||
|
||
This library uses semantic versioning and follows Okta's [library version policy](https://developer.okta.com/code/library-versions/). In short, we don't make breaking changes unless the major version changes! | ||
|
||
## Migrating from Okta.AspNet 1.x to 2.x | ||
|
||
In previous versions, the `OktaMvcOptions` exposed the `SecurityTokenValidated` and `AuthenticationFailed` events you could hook into. Starting in 2.x series, the `OktaMvcOptions` exposes the `OpenIdConnectEvents` property which allows you to hook into all the events provided by the uderlying OIDC middleware. | ||
|
||
_Before:_ | ||
|
||
```csharp | ||
public class Startup | ||
{ | ||
public void Configuration(IAppBuilder app) | ||
{ | ||
app.UseOktaMvc(new OktaMvcOptions() | ||
{ | ||
// ... other configuration options removed for brevity ... | ||
AuthenticationFailed = OnAuthenticationFailed, | ||
}); | ||
} | ||
|
||
public async Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification) | ||
{ | ||
await Task.Run(() => | ||
{ | ||
notification.Response.Redirect("{YOUR-EXCEPTION-HANDLING-ENDPOINT}?message=" + notification.Exception.Message); | ||
notification.HandleResponse(); | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
_Now:_ | ||
|
||
```csharp | ||
app.UseOktaMvc(new OktaMvcOptions() | ||
{ | ||
// ... other configuration options removed for brevity ... | ||
OpenIdConnectEvents = new OpenIdConnectAuthenticationNotifications | ||
{ | ||
AuthenticationFailed = OnAuthenticationFailed, | ||
}, | ||
}); | ||
``` | ||
## Migrating from Okta.AspNetCore 3.x to 4.x | ||
|
||
In previous versions, the `OktaMvcOptions` exposed the `OnTokenValidated`, `OnOktaApiFailure`, `OnUserInformationReceived` and `OnAuthenticationFailed` events you could hook into. Starting in 4.x series, the `OktaMvcOptions` exposes the `OpenIdConnectEvents` property which allows you to hook into all the events provided by the uderlying OIDC middleware. | ||
|
||
_Before:_ | ||
|
||
```csharp | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddOktaMvc(new OktaMvcOptions | ||
{ | ||
// ... other configuration options removed for brevity ... | ||
OnOktaApiFailure = OnOktaApiFailure, | ||
OnAuthenticationFailed = OnAuthenticationFailed, | ||
}); | ||
} | ||
|
||
public async Task OnOktaApiFailure(RemoteFailureContext context) | ||
{ | ||
await Task.Run(() => | ||
{ | ||
context.Response.Redirect("{YOUR-EXCEPTION-HANDLING-ENDPOINT}?message=" + context.Failure.Message); | ||
context.HandleResponse(); | ||
}); | ||
} | ||
|
||
public async Task OnAuthenticationFailed(AuthenticationFailedContext context) | ||
{ | ||
await Task.Run(() => | ||
{ | ||
context.Response.Redirect("{YOUR-EXCEPTION-HANDLING-ENDPOINT}?message=" + context.Exception.Message); | ||
context.HandleResponse(); | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
_Now:_ | ||
|
||
```csharp | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddOktaMvc(new OktaMvcOptions | ||
{ | ||
// ... other configuration options removed for brevity ... | ||
OpenIdConnectEvents = new OpenIdConnectEvents | ||
{ | ||
OnAuthenticationFailed = OnAuthenticationFailed, | ||
OnRemoteFailure = OnOktaApiFailure, | ||
}, | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
## Getting help | ||
|
||
If you have questions about this library or about the Okta APIs, post a question on our [Developer Forum](https://devforum.okta.com). | ||
|
||
If you find a bug or have a feature request for this library specifically, [post an issue](https://github.com/okta/okta-aspnet/issues) here on GitHub. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// <copyright file="HttpClientBuilderShould.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.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Okta.AspNet.Test | ||
{ | ||
public class HttpClientBuilderShould | ||
{ | ||
[Fact] | ||
public async Task InvokeCustomHandler() | ||
{ | ||
var handler = new MockHttpClientHandler(); | ||
|
||
var options = new OktaWebApiOptions(); | ||
options.OktaDomain = "https://test.okta.com"; | ||
options.BackchannelHttpClientHandler = handler; | ||
options.BackchannelTimeout = TimeSpan.FromMinutes(5); | ||
|
||
options.BackchannelHttpClientHandler.Should().NotBeNull(); | ||
|
||
var client = HttpClientBuilder.CreateClient(options); | ||
|
||
var response = await client.GetAsync("http://www.okta.com"); | ||
|
||
handler.NumberOfCalls.Should().BeGreaterThan(0); | ||
client.Timeout.Should().Be(TimeSpan.FromMinutes(5)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// <copyright file="JwtOptionsBuilderShould.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 FluentAssertions; | ||
using Microsoft.Owin.Security.OAuth; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Okta.AspNet.Test | ||
{ | ||
public class JwtOptionsBuilderShould | ||
{ | ||
[Fact] | ||
public void BuildJwtBearerOptions() | ||
{ | ||
var mockAuthnProvider = Substitute.For<OAuthBearerAuthenticationProvider>(); | ||
|
||
var oktaWebApiOptions = new OktaWebApiOptions | ||
{ | ||
OktaDomain = "http://myoktadomain.com", | ||
BackchannelTimeout = TimeSpan.FromMinutes(5), | ||
BackchannelHttpClientHandler = new MockHttpClientHandler(), | ||
OAuthBearerAuthenticationProvider = mockAuthnProvider, | ||
}; | ||
|
||
var jwtOptions = JwtOptionsBuilder.BuildJwtBearerAuthenticationOptions(oktaWebApiOptions); | ||
jwtOptions.Should().NotBeNull(); | ||
jwtOptions.Provider.Should().Be(mockAuthnProvider); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// <copyright file="MockHttpClientHandler.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.Net; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Okta.AspNet.Test | ||
{ | ||
public class MockHttpClientHandler : DelegatingHandler | ||
{ | ||
private readonly string _response; | ||
private readonly HttpStatusCode _statusCode; | ||
|
||
public string Body { get; private set; } | ||
|
||
public int NumberOfCalls { get; private set; } | ||
|
||
public MockHttpClientHandler(string response = "{}", HttpStatusCode statusCode = HttpStatusCode.OK) | ||
{ | ||
_response = response; | ||
_statusCode = statusCode; | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken = default) | ||
{ | ||
NumberOfCalls++; | ||
|
||
if (request.Content != null) | ||
{ | ||
Body = await request.Content.ReadAsStringAsync(); | ||
} | ||
|
||
return new HttpResponseMessage | ||
{ | ||
StatusCode = _statusCode, | ||
Content = new StringContent(_response), | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.