|
5 | 5 |
|
6 | 6 | using Microsoft.AspNetCore.Authorization; |
7 | 7 | using Microsoft.AspNetCore.Components.Web; |
8 | | -using Microsoft.AspNetCore.Components.WebAssembly.Authentication; |
9 | 8 | using Microsoft.AspNetCore.Components.WebAssembly.Hosting; |
10 | 9 |
|
11 | 10 | using MudBlazor.Services; |
12 | 11 |
|
13 | 12 | using MudExtensions.Services; |
14 | 13 |
|
15 | | -using AuthorizationMessageHandler = Ahk.GradeManagement.Client.Network.AuthorizationMessageHandler; |
16 | | - |
17 | 14 | namespace Ahk.GradeManagement.Client; |
18 | 15 |
|
19 | 16 | public class Program |
20 | 17 | { |
21 | 18 | public const string ServerApi = "ServerAPI"; |
22 | | - public const string SubjectApi = "SubjectAPI"; |
23 | 19 |
|
24 | 20 | public static async Task Main(string[] args) |
25 | 21 | { |
26 | 22 | var builder = WebAssemblyHostBuilder.CreateDefault(args); |
27 | 23 | builder.RootComponents.Add<App>("#app"); |
28 | 24 | builder.RootComponents.Add<HeadOutlet>("head::after"); |
29 | 25 |
|
30 | | - builder.Services.AddScoped(sp => new HttpClient |
31 | | - { |
32 | | - BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) |
33 | | - }); |
34 | | - |
35 | 26 | builder.Services.AddMsalAuthentication(options => |
36 | 27 | { |
37 | 28 | builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication); |
38 | 29 | options.ProviderOptions.DefaultAccessTokenScopes.Add("api://0ff49368-7e23-4e6a-9c57-973a6cac8bbd/AHK2.API"); |
39 | 30 | //Add scope for email |
40 | 31 | }); |
| 32 | + builder.Services.AddScoped<IAuthorizationHandler, UserTypeAuthorizationHandler>(); |
| 33 | + builder.Services.AddAuthorizationCore(options => |
| 34 | + { |
| 35 | + options.AddPolicy(Policy.RequireAdmin, policy => policy.Requirements.Add(new UserTypeRequirement([UserType.Admin]))); |
| 36 | + options.AddPolicy(Policy.RequireTeacher, policy => policy.Requirements.Add(new UserTypeRequirement([UserType.Teacher, UserType.Admin]))); |
| 37 | + }); |
41 | 38 |
|
42 | 39 | builder.Services.AddScoped<CrudSnackbarService>(); |
43 | | - |
44 | | - builder.Services.AddTransient(sp => new AuthorizationMessageHandler(sp.GetRequiredService<IAccessTokenProvider>())); |
45 | | - builder.Services.AddTransient(sp => new ExceptionMessageHandler(sp.GetRequiredService<CrudSnackbarService>())); |
46 | | - |
| 40 | + builder.Services.AddScoped<SubjectService>(); |
47 | 41 | builder.Services.AddSingleton<SelectedSubjectService>(); |
48 | 42 | builder.Services.AddSingleton<CurrentUserService>(); |
49 | 43 |
|
50 | | - builder.Services.AddTransient(sp => new SubjectHeaderHandler(sp.GetRequiredService<SelectedSubjectService>())); |
| 44 | + builder.Services.AddTransient<AuthorizationMessageHandler>(); |
| 45 | + builder.Services.AddTransient<ExceptionMessageHandler>(); |
| 46 | + builder.Services.AddTransient<SubjectHeaderHandler>(); |
51 | 47 |
|
52 | | - // Registering HttpClient that uses our custom handler |
53 | | - builder.Services.AddHttpClient(ServerApi, client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)) |
54 | | - .AddHttpMessageHandler<ExceptionMessageHandler>() |
| 48 | + builder.Services.ConfigureHttpClientDefaults(b => b |
55 | 49 | .AddHttpMessageHandler<AuthorizationMessageHandler>() |
56 | | - .AddHttpMessageHandler<SubjectHeaderHandler>(); |
57 | | - |
58 | | - builder.Services.AddScoped(sp => new SubjectClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi))); |
59 | | - builder.Services.AddScoped<SubjectService>(); |
60 | | - builder.Services.AddScoped(sp => new CourseClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi))); |
61 | | - builder.Services.AddScoped(sp => new SemesterClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi))); |
62 | | - builder.Services.AddScoped(sp => new LanguageClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi))); |
63 | | - builder.Services.AddScoped(sp => new UserClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi))); |
64 | | - builder.Services.AddScoped(sp => new GroupClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi))); |
65 | | - builder.Services.AddScoped(sp => new ExerciseClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi))); |
66 | | - builder.Services.AddScoped(sp => new AssignmentClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi))); |
67 | | - builder.Services.AddScoped(sp => new StudentClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi))); |
68 | | - builder.Services.AddScoped(sp => new DashboardClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient(ServerApi))); |
69 | | - |
70 | | - builder.Services.AddAuthorizationCore(options => |
71 | | - { |
72 | | - options.AddPolicy(Policy.RequireAdmin, policy => policy.Requirements.Add(new UserTypeRequirement([UserType.Admin]))); |
73 | | - options.AddPolicy(Policy.RequireTeacher, policy => policy.Requirements.Add(new UserTypeRequirement([UserType.Teacher, UserType.Admin]))); |
74 | | - }); |
75 | | - |
76 | | - builder.Services.AddScoped<IAuthorizationHandler, UserTypeAuthorizationHandler>(); |
| 50 | + .AddHttpMessageHandler<ExceptionMessageHandler>() |
| 51 | + .AddHttpMessageHandler<SubjectHeaderHandler>() |
| 52 | + .ConfigureHttpClient(client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))); |
| 53 | + |
| 54 | + builder.Services.AddHttpClient<SubjectClient>(); |
| 55 | + builder.Services.AddHttpClient<CourseClient>(); |
| 56 | + builder.Services.AddHttpClient<SemesterClient>(); |
| 57 | + builder.Services.AddHttpClient<LanguageClient>(); |
| 58 | + builder.Services.AddHttpClient<UserClient>(); |
| 59 | + builder.Services.AddHttpClient<GroupClient>(); |
| 60 | + builder.Services.AddHttpClient<ExerciseClient>(); |
| 61 | + builder.Services.AddHttpClient<AssignmentClient>(); |
| 62 | + builder.Services.AddHttpClient<StudentClient>(); |
| 63 | + builder.Services.AddHttpClient<DashboardClient>(); |
77 | 64 |
|
78 | 65 | builder.Services.AddMudServices(); |
79 | 66 | builder.Services.AddMudExtensions(); |
|
0 commit comments