| | 1 | | using LGDXRobotCloud.API.Authorisation; |
| | 2 | | using LGDXRobotCloud.API.Configurations; |
| | 3 | | using LGDXRobotCloud.API.Middleware; |
| | 4 | | using LGDXRobotCloud.API.Services; |
| | 5 | | using LGDXRobotCloud.API.Services.Administration; |
| | 6 | | using LGDXRobotCloud.API.Services.Automation; |
| | 7 | | using LGDXRobotCloud.API.Services.Common; |
| | 8 | | using LGDXRobotCloud.API.Services.Identity; |
| | 9 | | using LGDXRobotCloud.API.Services.Navigation; |
| | 10 | | using LGDXRobotCloud.Data.DbContexts; |
| | 11 | | using LGDXRobotCloud.Data.Entities; |
| | 12 | | using LGDXRobotCloud.Utilities.Constants; |
| | 13 | | using MassTransit; |
| | 14 | | using Microsoft.AspNetCore.Authentication; |
| | 15 | | using Microsoft.AspNetCore.Authentication.Certificate; |
| | 16 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | 17 | | using Microsoft.AspNetCore.Authorization; |
| | 18 | | using Microsoft.AspNetCore.Identity; |
| | 19 | | using Microsoft.AspNetCore.Server.Kestrel.Https; |
| | 20 | | using Microsoft.EntityFrameworkCore; |
| | 21 | | using Microsoft.IdentityModel.Tokens; |
| | 22 | | using Microsoft.OpenApi.Models; |
| | 23 | | using System.Security.Claims; |
| | 24 | | using System.Security.Cryptography.X509Certificates; |
| | 25 | | using System.Text; |
| | 26 | |
|
| 0 | 27 | | var builder = WebApplication.CreateBuilder(args); |
| 0 | 28 | | builder.WebHost.ConfigureKestrel(cfg => |
| 0 | 29 | | { |
| 0 | 30 | | cfg.ConfigureHttpsDefaults(ctx => ctx.ClientCertificateMode = ClientCertificateMode.AllowCertificate); |
| 0 | 31 | | cfg.AddServerHeader = false; |
| 0 | 32 | | }); |
| | 33 | |
|
| | 34 | | /* |
| | 35 | | * Configuration |
| | 36 | | */ |
| 0 | 37 | | builder.Services.Configure<LgdxRobotCloudConfiguration>( |
| 0 | 38 | | builder.Configuration.GetSection("LGDXRobotCloud") |
| 0 | 39 | | ); |
| 0 | 40 | | builder.Services.Configure<LgdxRobotCloudSecretConfiguration>( |
| 0 | 41 | | builder.Configuration.GetSection("LGDXRobotCloudSecret") |
| 0 | 42 | | ); |
| | 43 | |
|
| | 44 | | /* |
| | 45 | | * Infrastructure |
| | 46 | | */ |
| 0 | 47 | | builder.Services.AddMassTransit(cfg => |
| 0 | 48 | | { |
| 0 | 49 | | cfg.UsingRabbitMq((context, cfg) => |
| 0 | 50 | | { |
| 0 | 51 | | cfg.Host(builder.Configuration["RabbitMq:Host"], builder.Configuration["RabbitMq:VirtualHost"], h => |
| 0 | 52 | | { |
| 0 | 53 | | h.Username(builder.Configuration["RabbitMq:Username"] ?? string.Empty); |
| 0 | 54 | | h.Password(builder.Configuration["RabbitMq:Password"] ?? string.Empty); |
| 0 | 55 | | }); |
| 0 | 56 | | }); |
| 0 | 57 | | }); |
| 0 | 58 | | builder.Services.AddMemoryCache(); |
| 0 | 59 | | builder.Services.AddControllers(); |
| 0 | 60 | | builder.Services.AddEndpointsApiExplorer(); |
| 0 | 61 | | builder.Services.AddSwaggerGen(cfg =>{ |
| 0 | 62 | | cfg.SwaggerDoc("v1", new OpenApiInfo { Title = "LGDXRobot Cloud", Version = "v1" }); |
| 0 | 63 | | cfg.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { |
| 0 | 64 | | Description = "JWT Authorization header using the Bearer scheme.", |
| 0 | 65 | | Name = "Authorization", |
| 0 | 66 | | In = ParameterLocation.Header, |
| 0 | 67 | | Type = SecuritySchemeType.ApiKey, |
| 0 | 68 | | BearerFormat = "JWT", |
| 0 | 69 | | Scheme = "Bearer" |
| 0 | 70 | | }); |
| 0 | 71 | | cfg.AddSecurityRequirement(new OpenApiSecurityRequirement |
| 0 | 72 | | { |
| 0 | 73 | | { |
| 0 | 74 | | new OpenApiSecurityScheme |
| 0 | 75 | | { |
| 0 | 76 | | Reference = new OpenApiReference |
| 0 | 77 | | { |
| 0 | 78 | | Type = ReferenceType.SecurityScheme, |
| 0 | 79 | | Id = "Bearer" |
| 0 | 80 | | } |
| 0 | 81 | | }, |
| 0 | 82 | | [] |
| 0 | 83 | | } |
| 0 | 84 | | }); |
| 0 | 85 | | }); |
| 0 | 86 | | builder.Services.AddGrpc(cfg => cfg.EnableDetailedErrors = true); |
| 0 | 87 | | builder.Services.AddDbContextPool<LgdxContext>(cfg => |
| 0 | 88 | | cfg.UseNpgsql(builder.Configuration["PGSQLConnectionString"]) |
| 0 | 89 | | .LogTo(Console.WriteLine, LogLevel.Information) |
| 0 | 90 | | .EnableSensitiveDataLogging() |
| 0 | 91 | | .EnableDetailedErrors() |
| 0 | 92 | | ); |
| 0 | 93 | | builder.Services.AddHttpContextAccessor(); |
| | 94 | |
|
| | 95 | | /* |
| | 96 | | * Authentication |
| | 97 | | */ |
| 0 | 98 | | builder.Services.AddIdentity<LgdxUser, LgdxRole>() |
| 0 | 99 | | .AddEntityFrameworkStores<LgdxContext>() |
| 0 | 100 | | .AddTokenProvider<DataProtectorTokenProvider<LgdxUser>>(TokenOptions.DefaultProvider); |
| 0 | 101 | | builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) |
| 0 | 102 | | .AddJwtBearer(cfg => |
| 0 | 103 | | { |
| 0 | 104 | | cfg.TokenValidationParameters = new TokenValidationParameters |
| 0 | 105 | | { |
| 0 | 106 | | ValidateIssuer = true, |
| 0 | 107 | | ValidateAudience = true, |
| 0 | 108 | | ValidateLifetime = true, |
| 0 | 109 | | ValidateIssuerSigningKey = true, |
| 0 | 110 | | ValidIssuer = builder.Configuration["LGDXRobotCloudSecret:LgdxUserJwtIssuer"], |
| 0 | 111 | | ValidAudience = builder.Configuration["LGDXRobotCloudSecret:LgdxUserJwtIssuer"], |
| 0 | 112 | | IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["LGDXRobotCloudSecret:Lgd |
| 0 | 113 | | ClockSkew = TimeSpan.Zero |
| 0 | 114 | | }; |
| 0 | 115 | | }); |
| 0 | 116 | | builder.Services.AddTransient<ValidateRobotClientsCertificate>(); |
| 0 | 117 | | builder.Services.AddAuthentication(LgdxRobotCloudAuthenticationSchemes.RobotClientsCertificateScheme) |
| 0 | 118 | | .AddCertificate(LgdxRobotCloudAuthenticationSchemes.RobotClientsCertificateScheme, cfg => |
| 0 | 119 | | { |
| 0 | 120 | | cfg.AllowedCertificateTypes = CertificateTypes.All; |
| 0 | 121 | | cfg.RevocationMode = X509RevocationMode.NoCheck; |
| 0 | 122 | | cfg.Events = new CertificateAuthenticationEvents() |
| 0 | 123 | | { |
| 0 | 124 | | OnCertificateValidated = async ctx => |
| 0 | 125 | | { |
| 0 | 126 | | string subject = ctx.ClientCertificate.Subject; |
| 0 | 127 | | string guid = subject.Substring(subject.IndexOf("OID.0.9.2342.19200300.100.1.1=") + 30, 36); |
| 0 | 128 | | if (guid == string.Empty) |
| 0 | 129 | | { |
| 0 | 130 | | ctx.Fail("Robot ID not found."); |
| 0 | 131 | | return; |
| 0 | 132 | | } |
| 0 | 133 | | var validatior = ctx.HttpContext.RequestServices.GetService<ValidateRobotClientsCertificate>(); |
| 0 | 134 | | if (!await validatior!.Validate(ctx.ClientCertificate, Guid.Parse(guid))) |
| 0 | 135 | | { |
| 0 | 136 | | ctx.Fail("Invalid certificate / Robot not found."); |
| 0 | 137 | | return; |
| 0 | 138 | | } |
| 0 | 139 | | var claims = new [] { |
| 0 | 140 | | new Claim(ClaimTypes.NameIdentifier, guid) |
| 0 | 141 | | }; |
| 0 | 142 | | ctx.Principal = new ClaimsPrincipal(new ClaimsIdentity(claims, ctx.Scheme.Name)); |
| 0 | 143 | | ctx.Success(); |
| 0 | 144 | | } |
| 0 | 145 | | }; |
| 0 | 146 | | }); |
| 0 | 147 | | builder.Services.AddAuthentication(LgdxRobotCloudAuthenticationSchemes.RobotClientsJwtScheme) |
| 0 | 148 | | .AddJwtBearer(LgdxRobotCloudAuthenticationSchemes.RobotClientsJwtScheme, cfg => |
| 0 | 149 | | { |
| 0 | 150 | | cfg.TokenValidationParameters = new TokenValidationParameters |
| 0 | 151 | | { |
| 0 | 152 | | ValidateIssuer = true, |
| 0 | 153 | | ValidateAudience = true, |
| 0 | 154 | | ValidateLifetime = true, |
| 0 | 155 | | ValidateIssuerSigningKey = true, |
| 0 | 156 | | ValidIssuer = builder.Configuration["LGDXRobotCloudSecret:RobotClientsJwtIssuer"], |
| 0 | 157 | | ValidAudience = builder.Configuration["LGDXRobotCloudSecret:RobotClientsJwtIssuer"], |
| 0 | 158 | | IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["LGDXRobotCloudSecret:Rob |
| 0 | 159 | | ClockSkew = TimeSpan.Zero |
| 0 | 160 | | }; |
| 0 | 161 | | }); |
| 0 | 162 | | builder.Services.AddScoped<IAuthorizationHandler, ValidateLgdxUserAccessHandler>(); |
| 0 | 163 | | builder.Services.AddAuthorizationBuilder() |
| 0 | 164 | | .AddPolicy("ValidateLgdxUserAccess", policyBuilder => |
| 0 | 165 | | { |
| 0 | 166 | | policyBuilder.RequireAuthenticatedUser(); |
| 0 | 167 | | policyBuilder.AddRequirements(new ValidateLgdxUserAccessRequirement()); |
| 0 | 168 | | }); |
| | 169 | |
|
| | 170 | | /* |
| | 171 | | * LGDX Depency Injection |
| | 172 | | */ |
| | 173 | | // Administrator |
| 0 | 174 | | builder.Services.AddScoped<IApiKeyService, ApiKeyService>(); |
| 0 | 175 | | builder.Services.AddScoped<IRobotCertificateService, RobotCertificateService>(); |
| 0 | 176 | | builder.Services.AddScoped<IRoleService, RoleService>(); |
| 0 | 177 | | builder.Services.AddScoped<IUserService, UserService>(); |
| | 178 | |
|
| | 179 | | // Automation |
| 0 | 180 | | builder.Services.AddScoped<IAutoTaskService, AutoTaskService>(); |
| 0 | 181 | | builder.Services.AddScoped<IFlowService, FlowService>(); |
| 0 | 182 | | builder.Services.AddScoped<IProgressService, ProgressService>(); |
| | 183 | |
|
| | 184 | | // Identity |
| 0 | 185 | | builder.Services.AddScoped<IAuthService, AuthService>(); |
| 0 | 186 | | builder.Services.AddScoped<ICurrentUserService, CurrentUserService>(); |
| | 187 | |
|
| | 188 | | // Automation |
| 0 | 189 | | builder.Services.AddScoped<IRealmService, RealmService>(); |
| 0 | 190 | | builder.Services.AddScoped<IWaypointService, WaypointService>(); |
| 0 | 191 | | builder.Services.AddScoped<IRobotService, RobotService>(); |
| | 192 | |
|
| | 193 | | // Custom Services |
| 0 | 194 | | builder.Services.AddScoped<ITriggerRetryService, TriggerRetryService>(); |
| 0 | 195 | | builder.Services.AddScoped<ITriggerService, TriggerService>(); |
| 0 | 196 | | builder.Services.AddScoped<IEmailService, EmailService>(); |
| 0 | 197 | | builder.Services.AddSingleton<IEventService, EventService>(); |
| 0 | 198 | | builder.Services.AddScoped<IAutoTaskSchedulerService, AutoTaskSchedulerService>(); |
| 0 | 199 | | builder.Services.AddScoped<IOnlineRobotsService, OnlineRobotsService>(); |
| 0 | 200 | | builder.Services.AddScoped<IAuthenticationService, AuthenticationService>(); |
| | 201 | |
|
| 0 | 202 | | var app = builder.Build(); |
| | 203 | |
|
| | 204 | | // Configure the HTTP request pipeline. |
| 0 | 205 | | if (app.Environment.IsDevelopment()) |
| 0 | 206 | | { |
| 0 | 207 | | app.UseSwagger(); |
| 0 | 208 | | app.UseSwaggerUI(); |
| 0 | 209 | | } |
| | 210 | |
|
| 0 | 211 | | app.UseHttpsRedirection(); |
| | 212 | |
|
| 0 | 213 | | app.UseAuthentication(); |
| 0 | 214 | | app.UseAuthorization(); |
| | 215 | |
|
| 0 | 216 | | app.MapControllerRoute( |
| 0 | 217 | | name: "Area", |
| 0 | 218 | | pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); |
| 0 | 219 | | app.MapGrpcService<RobotClientsService>(); |
| 0 | 220 | | app.UseLgdxExpectionHandling(); |
| | 221 | |
|
| 0 | 222 | | app.Run(); |