From b1cff8658201c039c58cd2d31ed1bf38ce114c3f Mon Sep 17 00:00:00 2001 From: hz Date: Fri, 22 Aug 2025 23:47:14 -0500 Subject: [PATCH] Making swagger functional --- Auth/Auth.csproj | 1 + Auth/Program.cs | 33 +++++++++++++++++++++++++++++++ Auth/Services/BlacklistService.cs | 2 +- Auth/Services/UserService.cs | 2 +- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Auth/Auth.csproj b/Auth/Auth.csproj index d3ec58d..9d1ed49 100644 --- a/Auth/Auth.csproj +++ b/Auth/Auth.csproj @@ -11,6 +11,7 @@ + diff --git a/Auth/Program.cs b/Auth/Program.cs index ffad145..9de164e 100644 --- a/Auth/Program.cs +++ b/Auth/Program.cs @@ -1,6 +1,7 @@ using Auth.Services; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; +using Microsoft.OpenApi.Models; using System.Security.Claims; using System.Text; @@ -11,6 +12,31 @@ builder.Services.AddControllers(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +// Swagger + JWT auth in Swagger +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(c => +{ + c.SwaggerDoc("v1", new OpenApiInfo { Title = "Auth API", Version = "v1" }); + c.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme + { + Type = SecuritySchemeType.Http, + Scheme = "bearer", + BearerFormat = "JWT", + Description = "Paste your access token here (no 'Bearer ' prefix needed)." + }); + c.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { Type = ReferenceType.SecurityScheme, Id = "bearerAuth" } + }, + Array.Empty() + } + }); +}); + // AuthN/JWT var cfg = builder.Configuration; var jwtKey = cfg["Jwt:Key"] ?? throw new Exception("Jwt:Key missing"); @@ -46,7 +72,14 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) builder.Services.AddAuthorization(); var app = builder.Build(); + app.MapGet("/healthz", () => Results.Ok("ok")); +app.UseSwagger(); +app.UseSwaggerUI(o => +{ + o.SwaggerEndpoint("/swagger/v1/swagger.json", "Auth API v1"); + o.RoutePrefix = "swagger"; +}); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); diff --git a/Auth/Services/BlacklistService.cs b/Auth/Services/BlacklistService.cs index 42aaddf..27707f4 100644 --- a/Auth/Services/BlacklistService.cs +++ b/Auth/Services/BlacklistService.cs @@ -19,7 +19,7 @@ public class BlacklistService var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb"; var client = new MongoClient(cs); var db = client.GetDatabase(dbName); - _col = db.GetCollection("token_blacklist"); + _col = db.GetCollection("BlacklistedTokens"); // TTL index so revocations expire automatically var keys = Builders.IndexKeys.Ascending(x => x.ExpiresAt); diff --git a/Auth/Services/UserService.cs b/Auth/Services/UserService.cs index 535bcf8..8ecc2c2 100644 --- a/Auth/Services/UserService.cs +++ b/Auth/Services/UserService.cs @@ -13,7 +13,7 @@ public class UserService var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb"; var client = new MongoClient(cs); var db = client.GetDatabase(dbName); - _col = db.GetCollection("users"); + _col = db.GetCollection("Users"); var keys = Builders.IndexKeys.Ascending(u => u.Username); _col.Indexes.CreateOne(new CreateIndexModel(keys, new CreateIndexOptions { Unique = true }));