Skip to content

Commit 21397a1

Browse files
authored
Merge pull request #9 from lreb/okta
Okta and JWT self generated
2 parents d83d4f8 + 24549f3 commit 21397a1

File tree

19 files changed

+474
-3
lines changed

19 files changed

+474
-3
lines changed

FacwareBase.API/Controllers/AlbumController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using FacwareBase.API.Helpers.Domain.POCO;
44
using FacwareBase.API.Helpers.OData;
55
using Microsoft.AspNet.OData;
6+
using Microsoft.AspNetCore.Authorization;
67
using Microsoft.AspNetCore.Http;
78
using Microsoft.AspNetCore.Mvc;
89
using Microsoft.Extensions.Logging;
@@ -12,6 +13,7 @@ namespace FacwareBase.API.Controllers
1213
/// <summary>
1314
/// Demo Controller with OData feature
1415
/// </summary>
16+
[Authorize]
1517
public class AlbumController : ODataController
1618
{
1719
private readonly ILogger<AlbumController> _logger;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using FacwareBase.API.Helpers.Domain.POCO;
4+
using FacwareBase.API.Helpers.Jwt;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace FacwareBase.API.Controllers.Authentication
8+
{
9+
/// <summary>
10+
/// Provides authentication feature JWT
11+
/// </summary>
12+
[Route("api/[controller]")]
13+
public class AuthenticationController: ControllerBase
14+
{
15+
private readonly IJwtUtility _jwtUtility;
16+
public AuthenticationController(IJwtUtility jwtUtility)
17+
{
18+
_jwtUtility = jwtUtility;
19+
}
20+
21+
/// <summary>
22+
/// Create session
23+
/// </summary>
24+
/// <param name="user">User <see cref="User"/></param>
25+
/// <returns>JWT data</returns>
26+
[HttpPost]
27+
public async Task<IActionResult> Create([FromBody]User user)
28+
{
29+
IList<string> roles = new List<string>(){"Administrator","DemoRole"};
30+
31+
var token = await _jwtUtility.GenerateJwt(user, roles);
32+
return Ok(new
33+
{
34+
User = new {
35+
Token = token,
36+
Name = user.Name,
37+
Email = user.Email
38+
}
39+
});
40+
}
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using FacwareBase.API.Helpers.Jwt;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.AspNetCore.Authentication.JwtBearer;
5+
using Microsoft.IdentityModel.Tokens;
6+
using System.Text;
7+
8+
namespace FacwareBase.API.Extensions.Authentication
9+
{
10+
/// <summary>
11+
/// Utility to JWT
12+
/// </summary>
13+
public static class JwtExtension
14+
{
15+
/// <summary>
16+
/// app setting for okta
17+
/// </summary>
18+
public static readonly string JwtOptionsSection = "Okta";
19+
/// <summary>
20+
/// enables okata service
21+
/// </summary>
22+
/// <param name="services">application service <see cref="IServiceCollection"/></param>
23+
/// <param name="configuration">application configuratio <see cref="IConfiguration"/></param>
24+
public static void ConfigureJwt(this IServiceCollection services, IConfiguration configuration)
25+
{
26+
var jwt = configuration.GetSection(JwtOptions.JwtOptionsSection).Get<JwtOptions>();
27+
28+
services
29+
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
30+
.AddJwtBearer(options =>
31+
{
32+
options.TokenValidationParameters = new TokenValidationParameters
33+
{
34+
ValidateIssuerSigningKey = true,
35+
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwt.Secret)),
36+
ValidateIssuer = false,
37+
ValidateAudience = false
38+
};
39+
});
40+
}
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Okta.AspNetCore;
4+
using FacwareBase.API.Helpers.Okta;
5+
using System;
6+
7+
namespace FacwareBase.Api.Extensions
8+
{
9+
/// <summary>
10+
/// enables okata service
11+
/// </summary>
12+
public static class OktaExtension
13+
{
14+
/// <summary>
15+
/// app setting for okta
16+
/// </summary>
17+
public static readonly string OktaConfiguartion = "Okta";
18+
/// <summary>
19+
/// enables okata service
20+
/// </summary>
21+
/// <param name="services">application service <see cref="IServiceCollection"/></param>
22+
/// <param name="configuration">application configuratio <see cref="IConfiguration"/></param>
23+
public static void ConfigureOkta(this IServiceCollection services, IConfiguration configuration)
24+
{
25+
var okta = configuration.GetSection(OktaConfiguartion).Get<OktaHelper>();
26+
27+
services.AddAuthentication(options =>
28+
{
29+
options.DefaultAuthenticateScheme = OktaDefaults.ApiAuthenticationScheme;
30+
options.DefaultChallengeScheme = OktaDefaults.ApiAuthenticationScheme;
31+
options.DefaultSignInScheme = OktaDefaults.ApiAuthenticationScheme;
32+
})
33+
.AddCookie()
34+
.AddJwtBearer(options =>
35+
{
36+
options.Authority = okta.Authority;
37+
options.Audience = okta.Audience;
38+
options.RequireHttpsMetadata = Convert.ToBoolean(okta.RequireHttpsMetadata);
39+
});
40+
}
41+
}
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using FacwareBase.API.Helpers.Jwt;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace FacwareBase.API.Extensions.DependencyInyection
5+
{
6+
/// <summary>
7+
/// register all dependencies
8+
/// </summary>
9+
public static class DependencyInyectionExtension
10+
{
11+
/// <summary>
12+
/// Configure all dependecy inyection
13+
/// </summary>
14+
/// <param name="services">Application services<see cref="IServiceCollection"/></param>
15+
public static void DependencyInyectionConfiguration(this IServiceCollection services)
16+
{
17+
services.AddScoped<IJwtUtility,JwtUtility>();
18+
}
19+
}
20+
}

FacwareBase.API/Extensions/HealthCheck/CustomHealthCheck.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public Task<HealthCheckResult> CheckHealthAsync(
3434
Dictionary<string, object> data = new Dictionary<string, object>();
3535
data["environment"] = _config["HealthChecks:Environment"];
3636
data["corsAllowedOrigin"] = _config.GetSection("Cors:AllowedOrigin").Get<string[]>();
37+
data["authenticationMode"] = _config["Authentication:AuthenticationMode"];
3738
// data["connection"] = _config["ConnectionStrings:ApplicationConfigurationConnectionString"];
3839
return Task.FromResult(
3940
HealthCheckResult.Healthy("Test health check data", data));

FacwareBase.API/FacwareBase.API.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.1.1" />
1212
<PackageReference Include="AWSSDK.Core" Version="3.5.1.10" />
1313
<PackageReference Include="EntityFramework6.Npgsql" Version="6.4.1" />
14+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.8" />
1415
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.8" />
1516
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.4.1" />
1617
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.8" />
@@ -20,12 +21,14 @@
2021
</PackageReference>
2122
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
2223
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
24+
<PackageReference Include="Okta.AspNetCore" Version="3.3.0" />
2325
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
2426
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
2527
<PackageReference Include="Serilog.Sinks.AwsCloudWatch" Version="4.0.161" />
2628
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
2729
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.1" />
2830
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.5.1" />
31+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.7.1" />
2932
</ItemGroup>
3033

3134
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace FacwareBase.API.Helpers.Authentication
2+
{
3+
/// <summary>
4+
/// Authentication configurations
5+
/// </summary>
6+
public class AuthenticationOptions
7+
{
8+
/// <summary>
9+
/// Section in app settings
10+
/// </summary>
11+
public const string AuthenticationOptionsSection = "Authentication";
12+
/// <summary>
13+
/// Mode value in basis to AuthenticationModes <see cref="AuthenticationModes"/>
14+
/// </summary>
15+
/// <value></value>
16+
public string AuthenticationMode { get; set; }
17+
}
18+
/// <summary>
19+
/// Authentication modes
20+
/// </summary>
21+
public static class AuthenticationModes
22+
{
23+
/// <summary>
24+
/// Okta mode
25+
/// </summary>
26+
public const string Okta = "Okta";
27+
/// <summary>
28+
/// Bearer JWT
29+
/// </summary>
30+
public const string Jwt = "Jwt";
31+
}
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace FacwareBase.API.Helpers.Domain.POCO
2+
{
3+
/// <summary>
4+
/// demo user model
5+
/// </summary>
6+
public class User
7+
{
8+
public int Id { get; set; }
9+
public string Name { get; set; }
10+
public string Email { get; set; }
11+
public string Password { get; set; }
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using FacwareBase.API.Helpers.Domain.POCO;
4+
5+
namespace FacwareBase.API.Helpers.Jwt
6+
{
7+
/// <summary>
8+
/// JWT tools
9+
/// </summary>
10+
public interface IJwtUtility
11+
{
12+
/// <summary>
13+
/// Generates JWT token
14+
/// </summary>
15+
/// <param name="user">System user entity<see cref="User"/></param>
16+
/// <param name="roles">role asseigned to user, string list<see cref="string"/></param>
17+
/// <returns></returns>
18+
Task<string> GenerateJwt(User user, IList<string> roles);
19+
}
20+
}

0 commit comments

Comments
 (0)