|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:convert'; |
| 3 | + |
| 4 | +import 'package:chopper/chopper.dart'; |
| 5 | +import 'package:convert/convert.dart'; |
| 6 | +import 'package:cryptography/cryptography.dart'; |
| 7 | +import 'package:meta/meta.dart'; |
| 8 | + |
| 9 | +import 'authentication_exception.dart'; |
| 10 | +import 'login_service.dart'; |
| 11 | +import 'models/right.dart'; |
| 12 | +import 'models/session_info.dart'; |
| 13 | +import 'models/user.dart'; |
| 14 | +import 'user_credentials.dart'; |
| 15 | + |
| 16 | +// ignore: must_be_immutable |
| 17 | +abstract class LoginManager implements RequestInterceptor, Authenticator { |
| 18 | + static const _pbkd2Bits = 256; |
| 19 | + static final _challangeRegexp = RegExp( |
| 20 | + r'^2\$(\d+)\$([0-9a-f]+)\$(\d+)\$([0-9a-f]+)$', |
| 21 | + caseSensitive: false, |
| 22 | + ); |
| 23 | + |
| 24 | + var _sid = SessionInfo.invalidSid; |
| 25 | + var _rights = const <Right>[]; |
| 26 | + |
| 27 | + late final ChopperClient _client; |
| 28 | + |
| 29 | + List<Right> get rights => _rights; |
| 30 | + |
| 31 | + // ignore: use_setters_to_change_properties |
| 32 | + @internal |
| 33 | + void linkToClient(ChopperClient client) { |
| 34 | + _client = client; |
| 35 | + } |
| 36 | + |
| 37 | + @override |
| 38 | + FutureOr<Request> onRequest(Request request) async { |
| 39 | + if (request.isLogin) { |
| 40 | + return request; |
| 41 | + } |
| 42 | + |
| 43 | + if (_sid == SessionInfo.invalidSid) { |
| 44 | + await _performLogin(); |
| 45 | + } |
| 46 | + |
| 47 | + return _copyRequestWithSid(request); |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + FutureOr<Request?> authenticate(Request request, Response response) async { |
| 52 | + if (response.statusCode != 403 || request.isLogin) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + await _performLogin(); |
| 57 | + return _copyRequestWithSid(request); |
| 58 | + } |
| 59 | + |
| 60 | + @protected |
| 61 | + FutureOr<UserCredentials> obtainCredentials(List<User> knownUsers); |
| 62 | + |
| 63 | + Request _copyRequestWithSid(Request request) => request.copyWith(parameters: { |
| 64 | + ...request.parameters, |
| 65 | + 'sid': _sid, |
| 66 | + }); |
| 67 | + |
| 68 | + Future<void> _performLogin() async { |
| 69 | + final loginService = _client.getService<LoginService>(); |
| 70 | + |
| 71 | + final sessionInfo = _extractSessionInfo( |
| 72 | + _sid == SessionInfo.invalidSid |
| 73 | + ? await loginService.getLoginStatus() |
| 74 | + : await loginService.checkSessionValid(sid: _sid), |
| 75 | + ); |
| 76 | + |
| 77 | + final blockTimeout = Future.delayed( |
| 78 | + Duration(seconds: sessionInfo.blockTime), |
| 79 | + ); |
| 80 | + |
| 81 | + final credentials = await obtainCredentials(sessionInfo.users); |
| 82 | + |
| 83 | + final response = await _solveVersion2Challange(sessionInfo, credentials); |
| 84 | + |
| 85 | + await blockTimeout; |
| 86 | + final loginResult = _extractSessionInfo( |
| 87 | + await loginService.login( |
| 88 | + username: credentials.username, |
| 89 | + response: response, |
| 90 | + ), |
| 91 | + ); |
| 92 | + |
| 93 | + if (loginResult.sid == SessionInfo.invalidSid) { |
| 94 | + throw AuthenticationException.invalidCredentials(); |
| 95 | + } |
| 96 | + |
| 97 | + _sid = loginResult.sid; |
| 98 | + _rights = loginResult.rights; |
| 99 | + } |
| 100 | + |
| 101 | + Future<String> _solveVersion2Challange( |
| 102 | + SessionInfo sessionInfo, |
| 103 | + UserCredentials credentials, |
| 104 | + ) async { |
| 105 | + final challangeMatch = _challangeRegexp.firstMatch(sessionInfo.challange); |
| 106 | + if (challangeMatch == null) { |
| 107 | + throw AuthenticationException.invalidChallangeFormat(); |
| 108 | + } |
| 109 | + |
| 110 | + final iter1 = int.parse(challangeMatch[1]!); |
| 111 | + final salt1 = hex.decode(challangeMatch[2]!); |
| 112 | + final iter2 = int.parse(challangeMatch[3]!); |
| 113 | + final salt2 = hex.decode(challangeMatch[4]!); |
| 114 | + |
| 115 | + final pbkdfRound1 = Pbkdf2( |
| 116 | + macAlgorithm: Hmac.sha256(), |
| 117 | + iterations: iter1, |
| 118 | + bits: _pbkd2Bits, |
| 119 | + ); |
| 120 | + final pbkdfRound2 = Pbkdf2( |
| 121 | + macAlgorithm: Hmac.sha256(), |
| 122 | + iterations: iter2, |
| 123 | + bits: _pbkd2Bits, |
| 124 | + ); |
| 125 | + |
| 126 | + final hash1 = await pbkdfRound1.deriveKey( |
| 127 | + secretKey: SecretKey(utf8.encode(credentials.password)), |
| 128 | + nonce: salt1, |
| 129 | + ); |
| 130 | + final hash2 = await pbkdfRound2.deriveKey( |
| 131 | + secretKey: hash1, |
| 132 | + nonce: salt2, |
| 133 | + ); |
| 134 | + |
| 135 | + return '${hex.encode(salt2)}\$${hex.encode(await hash2.extractBytes())}'; |
| 136 | + } |
| 137 | + |
| 138 | + SessionInfo _extractSessionInfo(Response<SessionInfo> response) { |
| 139 | + if (!response.isSuccessful || response.body == null) { |
| 140 | + throw AuthenticationException.invalidStatus( |
| 141 | + response.statusCode, |
| 142 | + response.error ?? response.bodyString, |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + return response.body!; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +extension _RequestX on Request { |
| 151 | + bool get isLogin => url.contains('/login_sid.lua'); |
| 152 | +} |
0 commit comments