|
1 | | -using OsmoDoc.API.Models; |
2 | 1 | using Microsoft.AspNetCore.Mvc; |
3 | 2 | using Serilog.Events; |
4 | 3 | using Serilog; |
|
9 | 8 | using Microsoft.IdentityModel.Tokens; |
10 | 9 | using System.Text; |
11 | 10 | using Swashbuckle.AspNetCore.Filters; |
| 11 | +using StackExchange.Redis; |
| 12 | +using OsmoDoc.API.Models; |
| 13 | +using OsmoDoc.Services; |
| 14 | +using System.IdentityModel.Tokens.Jwt; |
12 | 15 |
|
13 | 16 | WebApplicationBuilder builder = WebApplication.CreateBuilder(args); |
14 | 17 |
|
|
25 | 28 | string dotenv = Path.GetFullPath(Path.Combine(root, "..", ".env")); |
26 | 29 | OsmoDoc.API.DotEnv.Load(dotenv); |
27 | 30 |
|
| 31 | +// Register REDIS service |
| 32 | +builder.Services.AddSingleton<IConnectionMultiplexer>( |
| 33 | + ConnectionMultiplexer.Connect(Environment.GetEnvironmentVariable("REDIS_URL") ?? throw new Exception("No REDIS URL specified")) |
| 34 | +); |
| 35 | +builder.Services.AddScoped<IRedisTokenStoreService, RedisTokenStoreService>(); |
| 36 | + |
28 | 37 | // Configure request size limit |
29 | 38 | long requestBodySizeLimitBytes = Convert.ToInt64(builder.Configuration.GetSection("CONFIG:REQUEST_BODY_SIZE_LIMIT_BYTES").Value); |
30 | 39 |
|
|
94 | 103 | return true; |
95 | 104 | } |
96 | 105 | }; |
| 106 | + |
| 107 | + options.Events = new JwtBearerEvents |
| 108 | + { |
| 109 | + OnTokenValidated = async context => |
| 110 | + { |
| 111 | + IRedisTokenStoreService tokenStore = context.HttpContext.RequestServices.GetRequiredService<IRedisTokenStoreService>(); |
| 112 | + JwtSecurityToken? token = context.SecurityToken as JwtSecurityToken; |
| 113 | + string tokenString = context.Request.Headers["Authorization"].ToString().Replace("bearer ", ""); |
| 114 | + |
| 115 | + if (!await tokenStore.IsTokenValidAsync(tokenString)) |
| 116 | + { |
| 117 | + context.Fail("Token has been revoked."); |
| 118 | + } |
| 119 | + } |
| 120 | + }; |
97 | 121 | }); |
98 | 122 |
|
99 | 123 | // Configure Error Response from Model Validations |
|
0 commit comments