|
2 | 2 | using JwtBearerSample.Authentication; |
3 | 3 | using Microsoft.AspNetCore.Authentication; |
4 | 4 | using Microsoft.AspNetCore.Http.HttpResults; |
| 5 | +using Microsoft.OpenApi; |
5 | 6 | using SimpleAuthentication; |
6 | 7 | using SimpleAuthentication.JwtBearer; |
7 | 8 | using SimpleAuthentication.Permissions; |
|
57 | 58 | app.UseExceptionHandler(); |
58 | 59 | app.UseStatusCodePages(); |
59 | 60 |
|
60 | | -app.UseSwagger(); |
| 61 | +app.UseSwagger(options => |
| 62 | +{ |
| 63 | + options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_1; |
| 64 | +}); |
| 65 | + |
61 | 66 | app.UseSwaggerUI(); |
62 | 67 |
|
63 | 68 | app.UseAuthentication(); |
|
79 | 84 | var token = await jwtBearerService.CreateTokenAsync(loginRequest.UserName, claims, absoluteExpiration: expiration); |
80 | 85 | return TypedResults.Ok(new LoginResponse(token)); |
81 | 86 | }) |
82 | | -.WithOpenApi(operation => |
83 | | -{ |
84 | | - operation.Description = "Insert permissions in the scope property (for example: 'profile people:admin')"; |
85 | | - return operation; |
86 | | -}); |
| 87 | +.WithDescription("Insert permissions in the scope property (for example: 'profile people:admin')"); |
87 | 88 |
|
88 | 89 | authApiGroup.MapPost("validate", async Task<Results<Ok<User>, BadRequest>> (string token, bool validateLifetime, IJwtBearerService jwtBearerService) => |
89 | 90 | { |
|
95 | 96 | } |
96 | 97 |
|
97 | 98 | return TypedResults.Ok(new User(result.Principal.Identity!.Name)); |
98 | | -}) |
99 | | -.WithOpenApi(); |
| 99 | +}); |
100 | 100 |
|
101 | 101 | authApiGroup.MapPost("refresh", async (string token, bool validateLifetime, DateTime? expiration, IJwtBearerService jwtBearerService) => |
102 | 102 | { |
103 | 103 | var newToken = await jwtBearerService.RefreshTokenAsync(token, validateLifetime, expiration); |
104 | 104 | return TypedResults.Ok(new LoginResponse(newToken)); |
105 | | -}) |
106 | | -.WithOpenApi(); |
| 105 | +}); |
107 | 106 |
|
108 | 107 | app.MapGet("api/me", (ClaimsPrincipal user) => |
109 | 108 | { |
110 | 109 | return TypedResults.Ok(new User(user.Identity!.Name)); |
111 | 110 | }) |
112 | 111 | .RequireAuthorization() |
113 | 112 | .RequirePermission("profile") |
114 | | -.WithOpenApi(operation => |
115 | | -{ |
116 | | - operation.Description = "This endpoint requires the 'profile' permission"; |
117 | | - return operation; |
118 | | -}); |
| 113 | +.WithDescription("This endpoint requires the 'profile' permission"); |
119 | 114 |
|
120 | 115 | app.MapGet("api/people", () => |
121 | 116 | { |
122 | 117 | return TypedResults.NoContent(); |
123 | 118 | }) |
124 | 119 | .RequireAuthorization(policyNames: "PeopleRead") |
125 | | -.WithOpenApi(operation => |
126 | | -{ |
127 | | - operation.Description = $"This endpoint requires the '{Permissions.PeopleRead}' or '{Permissions.PeopleAdmin}' permissions"; |
128 | | - return operation; |
129 | | -}); |
| 120 | +.WithDescription($"This endpoint requires the '{Permissions.PeopleRead}' or '{Permissions.PeopleAdmin}' permissions"); |
130 | 121 |
|
131 | 122 | app.Run(); |
132 | 123 |
|
|
0 commit comments