Skip to content

Commit

Permalink
Merge pull request #214 from fluentcms/213-move-addauthentication-cod…
Browse files Browse the repository at this point in the history
…e-from-programcs-to-api-project

#213 AddJwtAuthentication
  • Loading branch information
pournasserian authored Nov 22, 2023
2 parents 8cd3ca7 + 3fbc3a3 commit a796c88
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
48 changes: 48 additions & 0 deletions src/FluentCMS.Api/JwtServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using FluentCMS.Providers.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FluentCMS.Api;

public static class JwtServiceExtensions
{
public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, IConfigurationManager configurationManager)
{
var options = configurationManager.GetInstance<JwtOptions>("Jwt") ??
throw new ArgumentNullException("Jwt options in config file is not defined!");

services.AddAuthentication(configureOptions =>
{
var defaultScheme = "Bearer";
configureOptions.DefaultAuthenticateScheme = defaultScheme;
configureOptions.DefaultScheme = defaultScheme;
configureOptions.DefaultChallengeScheme = defaultScheme;
})
.AddJwtBearer(jwt =>
{
var key = Encoding.UTF8.GetBytes(options.Secret);
jwt.SaveToken = true;
jwt.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = true,
ValidateAudience = true,
RequireExpirationTime = true,
ValidateLifetime = true,
SaveSigninToken = true,
ValidAudience = options.Audience,
ValidIssuer = options.Issuer
};
});

return services;
}

}
1 change: 0 additions & 1 deletion src/FluentCMS.Api/SwaggerServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerUI;

Expand Down
32 changes: 1 addition & 31 deletions src/FluentCMS.Web.UI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,7 @@
};
});

// TODO: Move to somewhere else
#region Identity

var options = builder.Configuration.GetInstance<JwtOptions>("Jwt");

services.AddAuthentication(configureOptions =>
{
var defaultScheme = "Bearer";
configureOptions.DefaultAuthenticateScheme = defaultScheme;
configureOptions.DefaultScheme = defaultScheme;
configureOptions.DefaultChallengeScheme = defaultScheme;
})
.AddJwtBearer(jwt =>
{
var key = Encoding.UTF8.GetBytes(options.Secret);
jwt.SaveToken = true;
jwt.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = true,
ValidateAudience = true,
RequireExpirationTime = true,
ValidateLifetime = true,
SaveSigninToken = true,
ValidAudience = options.Audience,
ValidIssuer = options.Issuer
};
});

#endregion
services.AddJwtAuthentication(builder.Configuration);

var app = builder.Build();

Expand Down

0 comments on commit a796c88

Please sign in to comment.