|
1 | | -using System.Diagnostics; |
| 1 | +using exercise.wwwapi; |
2 | 2 | using exercise.wwwapi.Configuration; |
3 | 3 | using exercise.wwwapi.Data; |
| 4 | +using exercise.wwwapi.DTOs.Comments; |
| 5 | +using exercise.wwwapi.DTOs.Comments.UpdateComment; |
| 6 | +using exercise.wwwapi.DTOs.Notes; |
| 7 | +using exercise.wwwapi.DTOs.Posts; |
| 8 | +using exercise.wwwapi.DTOs.Posts.UpdatePost; |
4 | 9 | using exercise.wwwapi.DTOs.Register; |
5 | 10 | using exercise.wwwapi.DTOs.UpdateUser; |
| 11 | +using exercise.wwwapi.DTOs.Users; |
6 | 12 | using exercise.wwwapi.Endpoints; |
7 | 13 | using exercise.wwwapi.EndPoints; |
| 14 | +using exercise.wwwapi.Models; |
8 | 15 | using exercise.wwwapi.Repository; |
| 16 | +using exercise.wwwapi.Validators.NoteValidators; |
| 17 | +using exercise.wwwapi.Validators.PostValidators; |
9 | 18 | using exercise.wwwapi.Validators.UserValidators; |
10 | 19 | using FluentValidation; |
11 | 20 | using Microsoft.AspNetCore.Authentication.JwtBearer; |
12 | 21 | using Microsoft.EntityFrameworkCore; |
13 | 22 | using Microsoft.IdentityModel.Tokens; |
14 | 23 | using Microsoft.OpenApi.Models; |
15 | 24 | using Scalar.AspNetCore; |
| 25 | +using System.Diagnostics; |
| 26 | +using System.IdentityModel.Tokens.Jwt; |
| 27 | +using System.Security.Claims; |
16 | 28 | using System.Text; |
17 | | -using exercise.wwwapi; |
18 | | -using exercise.wwwapi.Models; |
19 | | -using exercise.wwwapi.DTOs.Notes; |
20 | | -using exercise.wwwapi.Validators.NoteValidators; |
21 | | -using exercise.wwwapi.DTOs.Posts; |
22 | | -using exercise.wwwapi.Validators.PostValidators; |
23 | | -using exercise.wwwapi.DTOs.Posts.UpdatePost; |
24 | | -using exercise.wwwapi.DTOs.Comments; |
25 | | -using exercise.wwwapi.DTOs.Comments.UpdateComment; |
26 | | -using exercise.wwwapi.DTOs.Users; |
27 | 29 |
|
28 | 30 |
|
29 | 31 | var builder = WebApplication.CreateBuilder(args); |
|
169 | 171 | if (app.Environment.IsDevelopment()) |
170 | 172 | { |
171 | 173 | app.UseSwagger(c => c.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi2_0); |
172 | | - app.UseSwaggerUI(); |
173 | | - app.UseSwaggerUI(options => options.SwaggerEndpoint("/openapi/v3.json", "Demo API")); |
| 174 | + |
| 175 | + // Generate a JWT token using your existing signing key |
| 176 | + var devJwtToken = GenerateDevJwtToken(token); |
| 177 | + |
| 178 | + app.UseSwaggerUI(c => |
| 179 | + { |
| 180 | + c.SwaggerEndpoint("/swagger/v1/swagger.json", "Demo API"); |
| 181 | + c.SwaggerEndpoint("/openapi/v3.json", "Demo API"); |
| 182 | + |
| 183 | + c.HeadContent = $@" |
| 184 | + <script> |
| 185 | + window.addEventListener('load', function() {{ |
| 186 | + setTimeout(function() {{ |
| 187 | + if (window.ui && window.ui.preauthorizeApiKey) {{ |
| 188 | + window.ui.preauthorizeApiKey('Bearer', 'Bearer {devJwtToken}'); |
| 189 | + console.log('Swagger UI auto-authenticated with dev token'); |
| 190 | + }} else {{ |
| 191 | + console.log('Swagger UI not ready for auto-authentication'); |
| 192 | + }} |
| 193 | + }}, 2000); |
| 194 | + }}); |
| 195 | + </script>"; |
| 196 | + }); |
174 | 197 | app.MapScalarApiReference(); |
175 | 198 | } |
176 | 199 |
|
|
195 | 218 | app.ConfigureCourseEndpoints(); |
196 | 219 | app.Run(); |
197 | 220 |
|
| 221 | +static string GenerateDevJwtToken(string signingKey) |
| 222 | +{ |
| 223 | + var tokenHandler = new JwtSecurityTokenHandler(); |
| 224 | + var key = Encoding.UTF8.GetBytes(signingKey); |
| 225 | + |
| 226 | + var claims = new List<Claim> |
| 227 | + { |
| 228 | + new Claim(ClaimTypes.Name, "Development User"), |
| 229 | + new Claim(ClaimTypes.Email, "[email protected]"), |
| 230 | + new Claim(ClaimTypes.Role, "Teacher") |
| 231 | + }; |
| 232 | + |
| 233 | + var tokenDescriptor = new SecurityTokenDescriptor |
| 234 | + { |
| 235 | + Subject = new ClaimsIdentity(claims), |
| 236 | + Expires = DateTime.UtcNow.AddDays(30), |
| 237 | + SigningCredentials = new SigningCredentials( |
| 238 | + new SymmetricSecurityKey(key), |
| 239 | + SecurityAlgorithms.HmacSha256Signature) |
| 240 | + }; |
| 241 | + |
| 242 | + var jwtToken = tokenHandler.CreateToken(tokenDescriptor); |
| 243 | + return tokenHandler.WriteToken(jwtToken); |
| 244 | +} |
| 245 | + |
198 | 246 | public partial class Program |
199 | 247 | { |
200 | 248 | } // needed for testing - please ignore |
201 | 249 |
|
| 250 | + |
| 251 | + |
0 commit comments