Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Allow Configuration to be parsed automatically #198

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Okta.AspNetCore/OktaAuthenticationOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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>
Expand Down
45 changes: 45 additions & 0 deletions Okta.AspNetCore/OktaWebApiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Net.Http;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Configuration;

namespace Okta.AspNetCore
{
Expand All @@ -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>
Expand Down