From 7b74dbfdc1be020ff197c3527873e700fd09ca09 Mon Sep 17 00:00:00 2001 From: Brian Demers Date: Tue, 1 Feb 2022 11:06:39 -0500 Subject: [PATCH] Idea: Allow Configuration to be parsed automatically Related to: #197 --- .../OktaAuthenticationOptionsExtensions.cs | 18 ++++++++ Okta.AspNetCore/OktaWebApiOptions.cs | 45 +++++++++++++++++++ 2 files changed, 63 insertions(+) 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); } + /// + /// Configures Okta for Web API apps, from global configuration. + /// + /// The application builder. + /// The configuration to load the Okta properties from. + /// The authentication builder. + 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); + } + /// /// Configures Okta for Web API apps. /// 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 /// public sealed class OktaWebApiOptions : AspNet.Abstractions.OktaWebApiOptions { + /// + /// Initializes a new instance of the class. + /// + public OktaWebApiOptions() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The configuration object. + 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); + } + } + /// /// Gets or sets the JwtBearerEvents. ///