-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
106 lines (94 loc) · 5.18 KB
/
Copy pathServiceCollectionExtensions.cs
File metadata and controls
106 lines (94 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using DfE.CoreLibs.Security.Authorization;
using DfE.CoreLibs.Security.Configurations;
using DfE.CoreLibs.Security.Interfaces;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using Microsoft.AspNetCore.Authentication;
namespace DfE.CoreLibs.Security
{
public static class ServiceCollectionExtensions
{
/// <summary>
/// Registers the Token Service and its dependencies in the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="configuration">The configuration object containing token settings.</param>
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddUserTokenService(this IServiceCollection services, IConfiguration configuration)
{
services.AddOptions<TokenSettings>()
.Bind(configuration.GetSection("Authorization:TokenSettings"))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddScoped<IUserTokenService, UserTokenService>();
services.AddHttpContextAccessor();
return services;
}
/// <summary>
/// Registers the Api Obo Token Service and its dependencies in the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="configuration">The configuration object containing token settings.</param>
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddApiOboTokenService(this IServiceCollection services, IConfiguration configuration)
{
services.AddOptions<TokenSettings>()
.Bind(configuration.GetSection("Authorization:TokenSettings"))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddScoped<IApiOboTokenService, ApiOboTokenService>();
services.AddHttpContextAccessor();
return services;
}
/// <summary>
/// ----<br/>
/// DO NOT USE THIS METHOD IF YOU ARE USING AZURE OR ANY OTHER THIRD PARTY IDENTITY SERVICE PROVIDER TO GENERATE AN ACCESS TOKEN.
/// ONLY USE THIS METHOD IF YOU ARE USING <see cref="IUserTokenService.GetUserTokenAsync"/> TO GENERATE A CUSTOM TOKEN.
/// ----<br/>
/// Adds and configures Custom JWT Bearer authentication which uses Symmetric Security Key to validate a custom token.
/// </summary>
/// <param name="services">The service collection to which authentication services are added.</param>
/// <param name="configuration">The application configuration containing token settings.</param>
/// <param name="authenticationScheme">The authentication scheme.</param>
/// <param name="authenticationBuilder">The authentication builder.</param>
/// <param name="jwtBearerEvents">The JwtBearerEvents.</param>
/// <returns>The updated service collection.</returns>
/// <exception cref="ArgumentNullException">Thrown when the TokenSettings section is missing in configuration.</exception>
public static IServiceCollection AddCustomJwtAuthentication(this IServiceCollection services, IConfiguration configuration, string authenticationScheme, AuthenticationBuilder authenticationBuilder, JwtBearerEvents? jwtBearerEvents = null)
{
var tokenSettingsSection = configuration.GetSection("Authorization:TokenSettings");
services.AddOptions<TokenSettings>()
.Bind(tokenSettingsSection)
.ValidateDataAnnotations()
.ValidateOnStart();
var tokenSettings = tokenSettingsSection.Get<TokenSettings>();
if (tokenSettings == null)
{
#pragma warning disable S3928
throw new ArgumentNullException(nameof(tokenSettings), "TokenSettings section is missing in configuration.");
#pragma warning restore S3928
}
authenticationBuilder.AddJwtBearer(authenticationScheme: authenticationScheme, options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = tokenSettings.Issuer,
ValidateAudience = true,
ValidAudience = tokenSettings.Audience,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenSettings.SecretKey)),
NameClaimType = System.Security.Claims.ClaimTypes.Name,
RoleClaimType = System.Security.Claims.ClaimTypes.Role
};
if (jwtBearerEvents != null)
options.Events = jwtBearerEvents;
});
return services;
}
}
}