1
1
import 'package:ht_api/src/services/auth_token_service.dart' ;
2
2
import 'package:ht_data_repository/ht_data_repository.dart' ;
3
3
import 'package:ht_shared/ht_shared.dart' ;
4
+ import 'package:logging/logging.dart' ;
4
5
5
6
/// {@template simple_auth_token_service}
6
7
/// A minimal, dependency-free implementation of [AuthTokenService] for debugging.
@@ -10,63 +11,61 @@ import 'package:ht_shared/ht_shared.dart';
10
11
/// {@endtemplate}
11
12
class SimpleAuthTokenService implements AuthTokenService {
12
13
/// {@macro simple_auth_token_service}
13
- const SimpleAuthTokenService ({required HtDataRepository <User > userRepository})
14
- : _userRepository = userRepository;
14
+ const SimpleAuthTokenService ({
15
+ required HtDataRepository <User > userRepository,
16
+ required Logger log,
17
+ }) : _userRepository = userRepository,
18
+ _log = log;
15
19
16
20
final HtDataRepository <User > _userRepository;
21
+ final Logger _log;
17
22
static const String _tokenPrefix = 'valid-token-for-user-id:' ;
18
23
19
24
@override
20
25
Future <String > generateToken (User user) async {
21
- print ( '[SimpleAuthTokenService] Generating token for user ${user .id }' );
26
+ _log. info ( ' Generating token for user ${user .id }' );
22
27
final token = '$_tokenPrefix ${user .id }' ;
23
- print ( '[SimpleAuthTokenService] Generated token: $token ' );
28
+ _log. finer ( ' Generated token: $token ' );
24
29
// Simulate async operation if needed, though not strictly necessary here
25
30
await Future <void >.delayed (Duration .zero);
26
31
return token;
27
32
}
28
33
29
34
@override
30
35
Future <User ?> validateToken (String token) async {
31
- print ( '[SimpleAuthTokenService] Attempting to validate token: $token ' );
36
+ _log. finer ( ' Attempting to validate token: $token ' );
32
37
if (! token.startsWith (_tokenPrefix)) {
33
- print ( '[SimpleAuthTokenService] Validation failed: Invalid prefix.' );
38
+ _log. warning ( ' Validation failed: Invalid prefix.' );
34
39
// Mimic JWT behavior by throwing Unauthorized for invalid format
35
40
throw const UnauthorizedException ('Invalid token format.' );
36
41
}
37
42
38
43
final userId = token.substring (_tokenPrefix.length);
39
- print ( '[SimpleAuthTokenService] Extracted user ID: $userId ' );
44
+ _log. finer ( ' Extracted user ID: $userId ' );
40
45
41
46
if (userId.isEmpty) {
42
- print ( '[SimpleAuthTokenService] Validation failed: Empty user ID.' );
47
+ _log. warning ( ' Validation failed: Empty user ID.' );
43
48
throw const UnauthorizedException ('Invalid token: Empty user ID.' );
44
49
}
45
50
46
51
try {
47
- print (
48
- '[SimpleAuthTokenService] Attempting to read user from repository...' ,
49
- );
52
+ _log.finer ('Attempting to read user from repository...' );
50
53
final user = await _userRepository.read (id: userId);
51
- print ( '[SimpleAuthTokenService] User read successful: ${user .id }' );
54
+ _log. info ( ' User read successful: ${user .id }' );
52
55
return user;
53
56
} on NotFoundException {
54
- print (
55
- '[SimpleAuthTokenService] Validation failed: User ID $userId not found.' ,
56
- );
57
+ _log.warning ('Validation failed: User ID $userId not found.' );
57
58
// Return null if user not found, mimicking successful validation
58
59
// of a token for a non-existent user. The middleware handles this.
59
60
return null ;
60
61
} on HtHttpException catch (e, s) {
61
62
// Handle other potential repository errors
62
- print (
63
- '[SimpleAuthTokenService] Validation failed: Repository error $e \n $s ' ,
64
- );
63
+ _log.warning ('Validation failed: Repository error' , e, s);
65
64
// Re-throw other client/repo exceptions
66
65
rethrow ;
67
66
} catch (e, s) {
68
67
// Catch unexpected errors during validation
69
- print ( '[SimpleAuthTokenService] Unexpected validation error: $ e \n $ s ' );
68
+ _log. severe ( ' Unexpected validation error' , e, s );
70
69
throw OperationFailedException (
71
70
'Simple token validation failed unexpectedly: $e ' ,
72
71
);
@@ -78,8 +77,8 @@ class SimpleAuthTokenService implements AuthTokenService {
78
77
// This service uses simple prefixed tokens, not JWTs with JTI.
79
78
// True invalidation/blacklisting isn't applicable here.
80
79
// This method is implemented to satisfy the AuthTokenService interface.
81
- print (
82
- '[SimpleAuthTokenService] Received request to invalidate token: $token . '
80
+ _log. info (
81
+ 'Received request to invalidate token: $token . '
83
82
'No server-side invalidation is performed for simple tokens.' ,
84
83
);
85
84
// Simulate async operation
0 commit comments