@@ -2,6 +2,7 @@ import 'dart:async';
2
2
3
3
import 'package:ht_shared/ht_shared.dart' ;
4
4
import 'package:meta/meta.dart' ;
5
+ import 'package:logging/logging.dart' ;
5
6
6
7
/// {@template token_blacklist_service}
7
8
/// Defines the interface for a service that manages a blacklist of
@@ -51,19 +52,20 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
51
52
/// expired token IDs. Defaults to 1 hour.
52
53
InMemoryTokenBlacklistService ({
53
54
Duration cleanupInterval = const Duration (hours: 1 ),
54
- }) {
55
+ required Logger log,
56
+ }) : _log = log {
55
57
_cleanupTimer = Timer .periodic (cleanupInterval, (_) async {
56
58
try {
57
59
await cleanupExpired ();
58
60
} catch (e) {
59
61
// Log error during cleanup, but don't let it crash the timer
60
- print (
61
- '[InMemoryTokenBlacklistService] Error during scheduled cleanup: $e ' ,
62
+ _log. severe (
63
+ 'Error during scheduled cleanup: $e ' ,
62
64
);
63
65
}
64
66
});
65
- print (
66
- '[InMemoryTokenBlacklistService] Initialized with cleanup interval: '
67
+ _log. info (
68
+ 'Initialized with cleanup interval: '
67
69
'$cleanupInterval ' ,
68
70
);
69
71
}
@@ -73,26 +75,27 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
73
75
final Map <String , DateTime > blacklistStore = {};
74
76
Timer ? _cleanupTimer;
75
77
bool _isDisposed = false ;
78
+ final Logger _log;
76
79
77
80
@override
78
81
Future <void > blacklist (String jti, DateTime expiry) async {
79
82
if (_isDisposed) {
80
- print (
81
- '[InMemoryTokenBlacklistService] Attempted to blacklist on disposed service.' ,
83
+ _log. warning (
84
+ 'Attempted to blacklist on disposed service.' ,
82
85
);
83
86
return ;
84
87
}
85
88
// Simulate async operation
86
89
await Future <void >.delayed (Duration .zero);
87
90
try {
88
91
blacklistStore[jti] = expiry;
89
- print (
90
- '[InMemoryTokenBlacklistService] Blacklisted jti: $jti '
92
+ _log. info (
93
+ 'Blacklisted jti: $jti '
91
94
'(expires: $expiry )' ,
92
95
);
93
96
} catch (e) {
94
- print (
95
- '[InMemoryTokenBlacklistService] Error adding jti $jti to store: $e ' ,
97
+ _log. severe (
98
+ 'Error adding jti $jti to store: $e ' ,
96
99
);
97
100
throw OperationFailedException ('Failed to add token to blacklist: $e ' );
98
101
}
@@ -101,8 +104,8 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
101
104
@override
102
105
Future <bool > isBlacklisted (String jti) async {
103
106
if (_isDisposed) {
104
- print (
105
- '[InMemoryTokenBlacklistService] Attempted to check blacklist on disposed service.' ,
107
+ _log. warning (
108
+ 'Attempted to check blacklist on disposed service.' ,
106
109
);
107
110
return false ;
108
111
}
@@ -122,8 +125,8 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
122
125
}
123
126
return true ; // It's in the blacklist and not expired
124
127
} catch (e) {
125
- print (
126
- '[InMemoryTokenBlacklistService] Error checking blacklist for jti $jti : $e ' ,
128
+ _log. severe (
129
+ 'Error checking blacklist for jti $jti : $e ' ,
127
130
);
128
131
throw OperationFailedException ('Failed to check token blacklist: $e ' );
129
132
}
@@ -132,8 +135,8 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
132
135
@override
133
136
Future <void > cleanupExpired () async {
134
137
if (_isDisposed) {
135
- print (
136
- '[InMemoryTokenBlacklistService] Attempted cleanup on disposed service.' ,
138
+ _log. warning (
139
+ 'Attempted cleanup on disposed service.' ,
137
140
);
138
141
return ;
139
142
}
@@ -150,17 +153,17 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
150
153
151
154
if (expiredKeys.isNotEmpty) {
152
155
expiredKeys.forEach (blacklistStore.remove);
153
- print (
154
- '[InMemoryTokenBlacklistService] Cleaned up ${expiredKeys .length } '
156
+ _log. info (
157
+ 'Cleaned up ${expiredKeys .length } '
155
158
'expired jti entries.' ,
156
159
);
157
160
} else {
158
- print (
159
- '[InMemoryTokenBlacklistService] Cleanup ran, no expired entries found.' ,
161
+ _log. finer (
162
+ 'Cleanup ran, no expired entries found.' ,
160
163
);
161
164
}
162
165
} catch (e) {
163
- print ( '[InMemoryTokenBlacklistService] Error during cleanup process: $e ' );
166
+ _log. severe ( ' Error during cleanup process: $e ' );
164
167
// Optionally rethrow or handle as an internal error
165
168
// For now, just log it to prevent crashing the cleanup timer.
166
169
}
@@ -172,7 +175,7 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
172
175
_isDisposed = true ;
173
176
_cleanupTimer? .cancel ();
174
177
blacklistStore.clear ();
175
- print ( '[InMemoryTokenBlacklistService] Disposed.' );
178
+ _log. info ( ' Disposed.' );
176
179
}
177
180
}
178
181
}
0 commit comments