77using CodeBeam . UltimateAuth . Core . MultiTenancy ;
88using CodeBeam . UltimateAuth . Core . Options ;
99using CodeBeam . UltimateAuth . Credentials ;
10+ using CodeBeam . UltimateAuth . Policies . Abstractions ;
11+ using CodeBeam . UltimateAuth . Policies . Defaults ;
12+ using CodeBeam . UltimateAuth . Policies . Registry ;
1013using CodeBeam . UltimateAuth . Server . Abstactions ;
1114using CodeBeam . UltimateAuth . Server . Abstractions ;
1215using CodeBeam . UltimateAuth . Server . Auth ;
13- using CodeBeam . UltimateAuth . Server . Auth . Context ;
1416using CodeBeam . UltimateAuth . Server . Cookies ;
1517using CodeBeam . UltimateAuth . Server . Endpoints ;
1618using CodeBeam . UltimateAuth . Server . Infrastructure ;
2325using CodeBeam . UltimateAuth . Server . Options ;
2426using CodeBeam . UltimateAuth . Server . Services ;
2527using CodeBeam . UltimateAuth . Server . Stores ;
26- using CodeBeam . UltimateAuth . Users ;
2728using Microsoft . Extensions . Configuration ;
2829using Microsoft . Extensions . DependencyInjection ;
2930using Microsoft . Extensions . DependencyInjection . Extensions ;
@@ -39,6 +40,7 @@ public static IServiceCollection AddUltimateAuthServer(this IServiceCollection s
3940 services . AddUltimateAuth ( ) ;
4041 AddUsersInternal ( services ) ;
4142 AddCredentialsInternal ( services ) ;
43+ AddUltimateAuthPolicies ( services ) ;
4244 return services . AddUltimateAuthServerInternal ( ) ;
4345 }
4446
@@ -47,6 +49,7 @@ public static IServiceCollection AddUltimateAuthServer(this IServiceCollection s
4749 services . AddUltimateAuth ( configuration ) ;
4850 AddUsersInternal ( services ) ;
4951 AddCredentialsInternal ( services ) ;
52+ AddUltimateAuthPolicies ( services ) ;
5053 services . Configure < UAuthServerOptions > ( configuration . GetSection ( "UltimateAuth:Server" ) ) ;
5154
5255 return services . AddUltimateAuthServerInternal ( ) ;
@@ -57,6 +60,7 @@ public static IServiceCollection AddUltimateAuthServer(this IServiceCollection s
5760 services . AddUltimateAuth ( ) ;
5861 AddUsersInternal ( services ) ;
5962 AddCredentialsInternal ( services ) ;
63+ AddUltimateAuthPolicies ( services ) ;
6064 services . Configure ( configure ) ;
6165
6266 return services . AddUltimateAuthServerInternal ( ) ;
@@ -248,14 +252,40 @@ private static IServiceCollection AddUltimateAuthServerInternal(this IServiceCol
248252 //services.TryAddScoped<ITokenEndpointHandler, TokenEndpointHandler>();
249253 //services.TryAddScoped<IUserInfoEndpointHandler, UserInfoEndpointHandler>();
250254
251- services . ConfigureHttpJsonOptions ( o =>
255+ return services ;
256+ }
257+
258+ internal static IServiceCollection AddUltimateAuthPolicies ( this IServiceCollection services , Action < AccessPolicyRegistry > ? configure = null )
259+ {
260+ if ( services . Any ( d => d . ServiceType == typeof ( AccessPolicyRegistry ) ) )
261+ throw new InvalidOperationException ( "UltimateAuth policies already registered." ) ;
262+
263+ var registry = new AccessPolicyRegistry ( ) ;
264+
265+ DefaultPolicySet . Register ( registry ) ;
266+ configure ? . Invoke ( registry ) ;
267+ services . AddSingleton ( registry ) ;
268+ services . AddSingleton < IAccessPolicyProvider > ( sp =>
269+ {
270+ var compiled = registry . Build ( ) ;
271+ return new DefaultAccessPolicyProvider ( compiled , sp ) ;
272+ } ) ;
273+
274+ services . TryAddScoped < IAccessAuthority > ( sp =>
252275 {
253- o . SerializerOptions . Converters . Add ( new UserKeyJsonConverter ( ) ) ;
276+ var invariants = sp . GetServices < IAccessInvariant > ( ) ;
277+ var globalPolicies = sp . GetServices < IAccessPolicy > ( ) ;
278+
279+ return new DefaultAccessAuthority ( invariants , globalPolicies ) ;
254280 } ) ;
255281
282+ services . TryAddScoped < IAccessOrchestrator , UAuthAccessOrchestrator > ( ) ;
283+
284+
256285 return services ;
257286 }
258287
288+
259289 // =========================
260290 // USERS (FRAMEWORK-REQUIRED)
261291 // =========================
0 commit comments