1010using System . Text ;
1111using Xero . Demo . Api . Domain . Security ;
1212using Xero . Demo . Api . Xero . Demo . Domain . Models ;
13+ using Xero . Demo . Api . Xero . Demo . Domain . Services ;
14+ using static Xero . Demo . Api . Domain . Models . CONSTANTS ;
1315
1416namespace Xero . Demo . Api . Domain . Infrastructure
1517{
1618 public static partial class AddAuthorizationExtension
1719 {
18- public static IServiceCollection AddRolesAndPolicyAuthorization ( this IServiceCollection services )
20+ public static IServiceCollection AddSecurity ( this IServiceCollection services )
1921 {
20- services . AddScoped < IUserService , UserService > ( ) ;
21- services . AddAuthorization (
22- config =>
23- {
24- config . AddPolicy ( "ShouldBeAnAdmin" , options =>
25- {
26- options . RequireAuthenticatedUser ( ) ;
27- options . AuthenticationSchemes . Add ( JwtBearerDefaults . AuthenticationScheme ) ;
28- options . Requirements . Add ( new ShouldBeAnAdminRequirement ( ) ) ;
29- } ) ;
30-
31- config . AddPolicy ( "ShouldBeAnEditor" , options =>
32- {
33- options . RequireClaim ( ClaimTypes . Role ) ;
34- options . RequireRole ( "Reader" ) ;
35- options . RequireAuthenticatedUser ( ) ;
36- options . AuthenticationSchemes . Add ( JwtBearerDefaults . AuthenticationScheme ) ;
37- options . Requirements . Add ( new ShouldBeAReaderRequirement ( ) ) ;
38- } ) ;
39-
40- config . AddPolicy ( "ShouldBeAReader" , options =>
41- {
42- options . RequireClaim ( ClaimTypes . Role ) ;
43- options . RequireRole ( "Reader" ) ;
44- options . RequireAuthenticatedUser ( ) ;
45- options . AuthenticationSchemes . Add ( JwtBearerDefaults . AuthenticationScheme ) ;
46- options . Requirements . Add ( new ShouldBeAReaderRequirement ( ) ) ;
47- } ) ;
48-
49- config . AddPolicy ( "ShouldContainRole" , options =>
50- options . RequireClaim ( ClaimTypes . Role ) ) ;
51- } ) ;
52-
5322 return services ;
5423 }
5524
5625 public static IServiceCollection AddJwtAuthentication ( this IServiceCollection services , IConfiguration configuration )
5726 {
58- services . AddAuthentication ( x =>
59- {
60- x . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
61- x . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
62- } )
63- . AddJwtBearer ( x =>
27+ services . AddScoped < IUserService , UserService > ( ) ;
28+
29+ services . AddAuthentication ( JwtBearerDefaults . AuthenticationScheme )
30+ . AddJwtBearer ( options =>
6431 {
65- x . RequireHttpsMetadata = false ;
66- x . SaveToken = true ;
67- x . TokenValidationParameters = new TokenValidationParameters
32+ options . RequireHttpsMetadata = false ;
33+ options . SaveToken = true ;
34+ options . TokenValidationParameters = new TokenValidationParameters
6835 {
36+ ValidateIssuer = true ,
37+ ValidateAudience = true ,
38+ ValidateLifetime = true ,
6939 ValidateIssuerSigningKey = true ,
70- IssuerSigningKey = new SymmetricSecurityKey ( Encoding . ASCII . GetBytes ( "hdftasdvjrlvfvfjyvwevfcmdfkjsdnhvzfmbnsdfvm" ) ) ,
71- ValidateIssuer = false ,
72- ValidateAudience = false
40+ ValidIssuer = configuration [ "Jwt:Issuer" ] ,
41+ ValidAudience = configuration [ "Jwt:Audience" ] ,
42+ IssuerSigningKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( configuration [ "Jwt:SecretKey" ] ) ) ,
43+ ClockSkew = TimeSpan . Zero
7344 } ;
7445 } ) ;
75- return services ;
76- }
77- }
7846
79- [ AttributeUsage ( AttributeTargets . Class | AttributeTargets . Method ) ]
80- public class AuthorizeAttribute : Attribute , IAuthorizationFilter
81- {
82- public void OnAuthorization ( AuthorizationFilterContext context )
83- {
84- var user = ( User ) context . HttpContext . Items [ "User" ] ;
85- if ( user == null )
86- {
87- // not logged in
88- context . Result = new JsonResult ( new { message = "Unauthorized" } ) { StatusCode = StatusCodes . Status401Unauthorized } ;
89- }
47+ services . AddAuthorization ( ) ;
48+ return services ;
9049 }
9150 }
9251}
0 commit comments