2024-05-18 14:23:36 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
2024-06-06 12:49:18 +00:00
|
|
|
import 'dart:developer';
|
2024-05-18 14:23:36 +00:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:get/get_connect/http/src/request/request.dart';
|
2024-07-07 03:56:25 +00:00
|
|
|
import 'package:mutex/mutex.dart';
|
2024-06-27 16:05:43 +00:00
|
|
|
import 'package:solian/controllers/chat_events_controller.dart';
|
2024-05-25 05:00:40 +00:00
|
|
|
import 'package:solian/providers/account.dart';
|
2024-05-26 05:39:21 +00:00
|
|
|
import 'package:solian/providers/chat.dart';
|
2024-05-18 14:23:36 +00:00
|
|
|
import 'package:solian/services.dart';
|
2024-06-29 09:35:18 +00:00
|
|
|
|
|
|
|
class TokenSet {
|
|
|
|
final String accessToken;
|
|
|
|
final String refreshToken;
|
|
|
|
final DateTime? expiredAt;
|
|
|
|
|
|
|
|
TokenSet({
|
|
|
|
required this.accessToken,
|
|
|
|
required this.refreshToken,
|
|
|
|
this.expiredAt,
|
|
|
|
});
|
|
|
|
|
|
|
|
factory TokenSet.fromJson(Map<String, dynamic> json) => TokenSet(
|
|
|
|
accessToken: json['access_token'],
|
|
|
|
refreshToken: json['refresh_token'],
|
|
|
|
expiredAt: json['expired_at'] != null
|
|
|
|
? DateTime.parse(json['expired_at'])
|
|
|
|
: null,
|
|
|
|
);
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
'access_token': accessToken,
|
|
|
|
'refresh_token': refreshToken,
|
|
|
|
'expired_at': expiredAt?.toIso8601String(),
|
|
|
|
};
|
|
|
|
|
|
|
|
bool get isExpired => expiredAt?.isBefore(DateTime.now()) ?? true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class RiskyAuthenticateException implements Exception {
|
|
|
|
final int ticketId;
|
|
|
|
|
|
|
|
RiskyAuthenticateException(this.ticketId);
|
|
|
|
}
|
2024-05-18 14:23:36 +00:00
|
|
|
|
|
|
|
class AuthProvider extends GetConnect {
|
2024-05-21 16:05:03 +00:00
|
|
|
final tokenEndpoint =
|
|
|
|
Uri.parse('${ServiceFinder.services['passport']}/api/auth/token');
|
2024-05-18 14:23:36 +00:00
|
|
|
|
|
|
|
static const clientId = 'solian';
|
|
|
|
static const clientSecret = '_F4%q2Eea3';
|
|
|
|
|
|
|
|
static const storage = FlutterSecureStorage();
|
|
|
|
|
2024-07-07 03:56:25 +00:00
|
|
|
TokenSet? credentials;
|
|
|
|
Mutex credentialsRefreshMutex = Mutex();
|
|
|
|
|
2024-05-18 14:23:36 +00:00
|
|
|
@override
|
|
|
|
void onInit() {
|
|
|
|
httpClient.baseUrl = ServiceFinder.services['passport'];
|
2024-05-23 15:54:05 +00:00
|
|
|
loadCredentials();
|
2024-05-18 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-25 05:00:40 +00:00
|
|
|
Future<void> refreshCredentials() async {
|
2024-07-07 03:56:25 +00:00
|
|
|
try {
|
|
|
|
credentialsRefreshMutex.acquire();
|
|
|
|
if (!credentials!.isExpired) return;
|
|
|
|
final resp = await post('/api/auth/token', {
|
|
|
|
'refresh_token': credentials!.refreshToken,
|
|
|
|
'grant_type': 'refresh_token',
|
|
|
|
});
|
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
throw Exception(resp.bodyString);
|
|
|
|
}
|
|
|
|
credentials = TokenSet(
|
|
|
|
accessToken: resp.body['access_token'],
|
|
|
|
refreshToken: resp.body['refresh_token'],
|
|
|
|
expiredAt: DateTime.now().add(const Duration(minutes: 3)),
|
|
|
|
);
|
|
|
|
storage.write(
|
|
|
|
key: 'auth_credentials',
|
|
|
|
value: jsonEncode(credentials!.toJson()),
|
|
|
|
);
|
|
|
|
} catch (_) {
|
|
|
|
rethrow;
|
|
|
|
} finally {
|
|
|
|
credentialsRefreshMutex.release();
|
2024-05-25 05:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 15:54:05 +00:00
|
|
|
Future<Request<T?>> requestAuthenticator<T>(Request<T?> request) async {
|
2024-06-06 12:49:18 +00:00
|
|
|
try {
|
|
|
|
await ensureCredentials();
|
2024-07-07 03:56:25 +00:00
|
|
|
request.headers['Authorization'] = 'Bearer ${credentials!.accessToken}';
|
2024-06-06 12:49:18 +00:00
|
|
|
} catch (_) {}
|
2024-05-18 14:23:36 +00:00
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2024-06-22 14:39:32 +00:00
|
|
|
GetConnect configureClient(
|
|
|
|
String service, {
|
2024-06-06 12:49:18 +00:00
|
|
|
timeout = const Duration(seconds: 5),
|
|
|
|
}) {
|
|
|
|
final client = GetConnect(
|
|
|
|
maxAuthRetries: 3,
|
|
|
|
timeout: timeout,
|
2024-06-29 09:35:18 +00:00
|
|
|
userAgent: 'Solian/1.1',
|
|
|
|
sendUserAgent: true,
|
2024-06-06 12:49:18 +00:00
|
|
|
);
|
|
|
|
client.httpClient.addAuthenticator(requestAuthenticator);
|
2024-06-22 14:39:32 +00:00
|
|
|
client.httpClient.baseUrl = ServiceFinder.services[service];
|
2024-06-06 12:49:18 +00:00
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> ensureCredentials() async {
|
|
|
|
if (!await isAuthorized) throw Exception('unauthorized');
|
2024-07-07 03:56:25 +00:00
|
|
|
if (credentials == null) await loadCredentials();
|
2024-06-06 12:49:18 +00:00
|
|
|
|
2024-07-07 03:56:25 +00:00
|
|
|
if (credentials!.isExpired) {
|
2024-06-06 12:49:18 +00:00
|
|
|
await refreshCredentials();
|
2024-06-23 10:51:49 +00:00
|
|
|
log('Refreshed credentials at ${DateTime.now()}');
|
2024-06-06 12:49:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 15:54:05 +00:00
|
|
|
Future<void> loadCredentials() async {
|
|
|
|
if (await isAuthorized) {
|
|
|
|
final content = await storage.read(key: 'auth_credentials');
|
2024-07-07 03:56:25 +00:00
|
|
|
credentials = TokenSet.fromJson(jsonDecode(content!));
|
2024-05-23 15:54:05 +00:00
|
|
|
}
|
2024-05-18 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
2024-06-29 09:35:18 +00:00
|
|
|
Future<TokenSet> signin(
|
2024-05-23 15:54:05 +00:00
|
|
|
BuildContext context,
|
|
|
|
String username,
|
|
|
|
String password,
|
|
|
|
) async {
|
2024-06-01 13:39:28 +00:00
|
|
|
_cachedUserProfileResponse = null;
|
2024-05-23 15:54:05 +00:00
|
|
|
|
2024-06-29 09:35:18 +00:00
|
|
|
final client = ServiceFinder.configureClient('passport');
|
|
|
|
|
|
|
|
// Create ticket
|
|
|
|
final resp = await client.post('/api/auth', {
|
|
|
|
'username': username,
|
|
|
|
'password': password,
|
|
|
|
});
|
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
throw Exception(resp.body);
|
|
|
|
} else if (resp.body['is_finished'] == false) {
|
|
|
|
throw RiskyAuthenticateException(resp.body['ticket']['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign token
|
|
|
|
final tokenResp = await post('/api/auth/token', {
|
|
|
|
'code': resp.body['ticket']['grant_token'],
|
|
|
|
'grant_type': 'grant_token',
|
|
|
|
});
|
|
|
|
if (tokenResp.statusCode != 200) {
|
|
|
|
throw Exception(tokenResp.bodyString);
|
|
|
|
}
|
2024-05-18 14:23:36 +00:00
|
|
|
|
2024-07-07 03:56:25 +00:00
|
|
|
credentials = TokenSet(
|
2024-06-29 09:35:18 +00:00
|
|
|
accessToken: tokenResp.body['access_token'],
|
|
|
|
refreshToken: tokenResp.body['refresh_token'],
|
|
|
|
expiredAt: DateTime.now().add(const Duration(minutes: 3)),
|
2024-05-18 14:23:36 +00:00
|
|
|
);
|
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
storage.write(
|
2024-05-23 15:54:05 +00:00
|
|
|
key: 'auth_credentials',
|
2024-07-07 03:56:25 +00:00
|
|
|
value: jsonEncode(credentials!.toJson()),
|
2024-05-23 15:54:05 +00:00
|
|
|
);
|
2024-05-18 14:23:36 +00:00
|
|
|
|
2024-05-25 05:00:40 +00:00
|
|
|
Get.find<AccountProvider>().connect();
|
2024-05-25 05:19:16 +00:00
|
|
|
Get.find<AccountProvider>().notifyPrefetch();
|
2024-05-26 05:39:21 +00:00
|
|
|
Get.find<ChatProvider>().connect();
|
2024-05-25 05:00:40 +00:00
|
|
|
|
2024-07-07 03:56:25 +00:00
|
|
|
return credentials!;
|
2024-05-18 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void signout() {
|
2024-06-01 13:39:28 +00:00
|
|
|
_cachedUserProfileResponse = null;
|
2024-05-23 15:54:05 +00:00
|
|
|
|
2024-05-26 05:39:21 +00:00
|
|
|
Get.find<ChatProvider>().disconnect();
|
2024-05-25 05:00:40 +00:00
|
|
|
Get.find<AccountProvider>().disconnect();
|
2024-05-25 05:19:16 +00:00
|
|
|
Get.find<AccountProvider>().notifications.clear();
|
|
|
|
Get.find<AccountProvider>().notificationUnread.value = 0;
|
2024-05-25 05:00:40 +00:00
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
final chatHistory = ChatEventController();
|
2024-06-23 11:13:07 +00:00
|
|
|
chatHistory.initialize().then((_) async {
|
2024-06-27 16:05:43 +00:00
|
|
|
await chatHistory.database.localEvents.wipeLocalEvents();
|
2024-06-23 11:13:07 +00:00
|
|
|
});
|
|
|
|
|
2024-05-18 14:23:36 +00:00
|
|
|
storage.deleteAll();
|
|
|
|
}
|
|
|
|
|
2024-06-06 15:28:19 +00:00
|
|
|
// Data Layer
|
|
|
|
|
2024-06-01 13:39:28 +00:00
|
|
|
Response? _cachedUserProfileResponse;
|
2024-05-21 16:05:03 +00:00
|
|
|
|
2024-05-18 14:23:36 +00:00
|
|
|
Future<bool> get isAuthorized => storage.containsKey(key: 'auth_credentials');
|
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
Future<Response> getProfile({noCache = false}) async {
|
2024-06-01 13:39:28 +00:00
|
|
|
if (!noCache && _cachedUserProfileResponse != null) {
|
|
|
|
return _cachedUserProfileResponse!;
|
2024-05-21 16:05:03 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 14:39:32 +00:00
|
|
|
final client = configureClient('passport');
|
2024-05-23 15:54:05 +00:00
|
|
|
|
|
|
|
final resp = await client.get('/api/users/me');
|
2024-05-31 17:25:45 +00:00
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
throw Exception(resp.bodyString);
|
|
|
|
} else {
|
2024-06-01 13:39:28 +00:00
|
|
|
_cachedUserProfileResponse = resp;
|
2024-05-31 17:25:45 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
return resp;
|
|
|
|
}
|
2024-07-12 05:15:46 +00:00
|
|
|
|
|
|
|
Future<Response?> getProfileWithCheck({noCache = false}) async {
|
|
|
|
if (!await isAuthorized) return null;
|
|
|
|
|
|
|
|
return await getProfile(noCache: noCache);
|
|
|
|
}
|
2024-05-18 14:23:36 +00:00
|
|
|
}
|