File tree Expand file tree Collapse file tree 3 files changed +70
-1
lines changed Expand file tree Collapse file tree 3 files changed +70
-1
lines changed Original file line number Diff line number Diff line change 1
- using OsmoDoc . API . Models ;
2
1
using Microsoft . AspNetCore . Mvc ;
3
2
using Serilog . Events ;
4
3
using Serilog ;
10
9
using System . Text ;
11
10
using Swashbuckle . AspNetCore . Filters ;
12
11
using OsmoDoc . Pdf ;
12
+ using StackExchange . Redis ;
13
+ using OsmoDoc . API . Models ;
14
+ using OsmoDoc . Services ;
15
+ using System . IdentityModel . Tokens . Jwt ;
13
16
14
17
WebApplicationBuilder builder = WebApplication . CreateBuilder ( args ) ;
15
18
32
35
builder . Configuration . GetSection ( "STATIC_FILE_PATHS:HTML_TO_PDF_TOOL" ) . Value !
33
36
) ;
34
37
38
+ // Register REDIS service
39
+ builder . Services . AddSingleton < IConnectionMultiplexer > (
40
+ ConnectionMultiplexer . Connect ( Environment . GetEnvironmentVariable ( "REDIS_URL" ) ?? throw new Exception ( "No REDIS URL specified" ) )
41
+ ) ;
42
+ builder . Services . AddScoped < IRedisTokenStoreService , RedisTokenStoreService > ( ) ;
43
+
35
44
// Configure request size limit
36
45
long requestBodySizeLimitBytes = Convert . ToInt64 ( builder . Configuration . GetSection ( "CONFIG:REQUEST_BODY_SIZE_LIMIT_BYTES" ) . Value ) ;
37
46
101
110
return true ;
102
111
}
103
112
} ;
113
+
114
+ options . Events = new JwtBearerEvents
115
+ {
116
+ OnTokenValidated = async context =>
117
+ {
118
+ IRedisTokenStoreService tokenStore = context . HttpContext . RequestServices . GetRequiredService < IRedisTokenStoreService > ( ) ;
119
+ JwtSecurityToken ? token = context . SecurityToken as JwtSecurityToken ;
120
+ string tokenString = context . Request . Headers [ "Authorization" ] . ToString ( ) . Replace ( "bearer " , "" ) ;
121
+
122
+ if ( ! await tokenStore . IsTokenValidAsync ( tokenString ) )
123
+ {
124
+ context . Fail ( "Token has been revoked." ) ;
125
+ }
126
+ }
127
+ } ;
104
128
} ) ;
105
129
106
130
// Configure Error Response from Model Validations
Original file line number Diff line number Diff line change
1
+ using System . Threading . Tasks ;
2
+
3
+ namespace OsmoDoc . Services ;
4
+
5
+ public interface IRedisTokenStoreService
6
+ {
7
+ Task StoreTokenAsync ( string token , string email ) ;
8
+ Task < bool > IsTokenValidAsync ( string token ) ;
9
+ Task RevokeTokenAsync ( string token ) ;
10
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Threading . Tasks ;
3
+ using Newtonsoft . Json ;
4
+ using StackExchange . Redis ;
5
+
6
+ namespace OsmoDoc . Services ;
7
+
8
+ public class RedisTokenStoreService : IRedisTokenStoreService
9
+ {
10
+ private readonly IDatabase _db ;
11
+ private const string KeyPrefix = "valid_token:" ;
12
+
13
+ public RedisTokenStoreService ( IConnectionMultiplexer redis )
14
+ {
15
+ this . _db = redis . GetDatabase ( ) ;
16
+ }
17
+
18
+ public Task StoreTokenAsync ( string token , string email )
19
+ {
20
+ return this . _db . StringSetAsync ( $ "{ KeyPrefix } { token } ", JsonConvert . SerializeObject ( new {
21
+ issuedTo = email ,
22
+ issuedAt = DateTime . UtcNow
23
+ } ) ) ;
24
+ }
25
+
26
+ public Task < bool > IsTokenValidAsync ( string token )
27
+ {
28
+ return this . _db . KeyExistsAsync ( $ "{ KeyPrefix } { token } ") ;
29
+ }
30
+
31
+ public Task RevokeTokenAsync ( string token )
32
+ {
33
+ return this . _db . KeyDeleteAsync ( $ "{ KeyPrefix } { token } ") ;
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments