diff --git a/Okta.AspNetCore/OktaAuthenticationOptionsExtensions.cs b/Okta.AspNetCore/OktaAuthenticationOptionsExtensions.cs index 53b5cf7..273c82f 100644 --- a/Okta.AspNetCore/OktaAuthenticationOptionsExtensions.cs +++ b/Okta.AspNetCore/OktaAuthenticationOptionsExtensions.cs @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.OpenIdConnect; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Okta.AspNet.Abstractions; @@ -38,6 +39,23 @@ public static AuthenticationBuilder AddOktaMvc(this AuthenticationBuilder builde return AddCodeFlow(builder, options); } + /// <summary> + /// Configures Okta for Web API apps, from global configuration. + /// </summary> + /// <param name="builder">The application builder.</param> + /// <param name="configuration">The configuration to load the Okta properties from.</param> + /// <returns>The authentication builder.</returns> + public static AuthenticationBuilder AddOktaWebApi(this AuthenticationBuilder builder, IConfiguration configuration) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + var options = new OktaWebApiOptions(configuration); + return AddOktaWebApi(builder, options); + } + /// <summary> /// Configures Okta for Web API apps. /// </summary> diff --git a/Okta.AspNetCore/OktaWebApiOptions.cs b/Okta.AspNetCore/OktaWebApiOptions.cs index f29ef8a..d39b7bc 100644 --- a/Okta.AspNetCore/OktaWebApiOptions.cs +++ b/Okta.AspNetCore/OktaWebApiOptions.cs @@ -6,6 +6,7 @@ using System; using System.Net.Http; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.Extensions.Configuration; namespace Okta.AspNetCore { @@ -14,6 +15,50 @@ namespace Okta.AspNetCore /// </summary> public sealed class OktaWebApiOptions : AspNet.Abstractions.OktaWebApiOptions { + /// <summary> + /// Initializes a new instance of the <see cref="OktaWebApiOptions"/> class. + /// </summary> + public OktaWebApiOptions() + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="OktaWebApiOptions"/> class. + /// </summary> + /// <param name="configuration">The configuration object.</param> + public OktaWebApiOptions(IConfiguration configuration) + { + var domain = configuration["Okta:OktaDomain"]; + if (!string.IsNullOrWhiteSpace(domain)) + { + this.OktaDomain = domain; + } + + var authServerId = configuration["Okta:AuthorizationServerId"]; + if (!string.IsNullOrWhiteSpace(authServerId)) + { + this.AuthorizationServerId = authServerId; + } + + var audience = configuration["Okta:Audience"]; + if (!string.IsNullOrWhiteSpace(audience)) + { + this.Audience = audience; + } + + var timeout = configuration["Okta:BackchannelTimeout"]; + if (!string.IsNullOrWhiteSpace(timeout)) + { + this.BackchannelTimeout = TimeSpan.Parse(timeout); + } + + var clockSkew = configuration["Okta:ClockSkew"]; + if (!string.IsNullOrWhiteSpace(clockSkew)) + { + this.ClockSkew = TimeSpan.Parse(clockSkew); + } + } + /// <summary> /// Gets or sets the JwtBearerEvents. /// </summary>