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-09-15 08:02:56 +00:00
|
|
|
import 'package:solian/background.dart';
|
2024-08-21 07:39:29 +00:00
|
|
|
import 'package:solian/exceptions/request.dart';
|
2024-08-21 07:25:50 +00:00
|
|
|
import 'package:solian/exceptions/unauthorized.dart';
|
2024-09-15 18:37:20 +00:00
|
|
|
import 'package:solian/models/auth.dart';
|
2024-09-15 04:25:50 +00:00
|
|
|
import 'package:solian/providers/database/database.dart';
|
2024-07-16 11:46:53 +00:00
|
|
|
import 'package:solian/providers/websocket.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 =
|
2024-07-16 11:46:53 +00:00
|
|
|
Uri.parse(ServiceFinder.buildUrl('auth', '/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;
|
|
|
|
|
2024-05-18 14:23:36 +00:00
|
|
|
@override
|
|
|
|
void onInit() {
|
2024-07-16 11:46:53 +00:00
|
|
|
httpClient.baseUrl = ServiceFinder.buildUrl('auth', null);
|
2024-07-24 17:18:47 +00:00
|
|
|
refreshAuthorizeStatus().then((_) {
|
|
|
|
loadCredentials();
|
|
|
|
refreshUserProfile();
|
|
|
|
});
|
2024-05-18 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 16:20:11 +00:00
|
|
|
Completer<void>? _refreshCompleter;
|
|
|
|
|
2024-05-25 05:00:40 +00:00
|
|
|
Future<void> refreshCredentials() async {
|
2024-07-26 16:20:11 +00:00
|
|
|
if (_refreshCompleter != null) {
|
|
|
|
await _refreshCompleter!.future;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
_refreshCompleter = Completer<void>();
|
|
|
|
}
|
|
|
|
|
2024-07-07 03:56:25 +00:00
|
|
|
try {
|
|
|
|
if (!credentials!.isExpired) return;
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await post('/auth/token', {
|
2024-07-07 03:56:25 +00:00
|
|
|
'refresh_token': credentials!.refreshToken,
|
|
|
|
'grant_type': 'refresh_token',
|
|
|
|
});
|
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-07-07 03:56:25 +00:00
|
|
|
}
|
|
|
|
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()),
|
|
|
|
);
|
2024-07-26 16:20:11 +00:00
|
|
|
_refreshCompleter!.complete();
|
|
|
|
log('Refreshed credentials at ${DateTime.now()}');
|
|
|
|
} catch (e) {
|
|
|
|
_refreshCompleter!.completeError(e);
|
2024-07-07 03:56:25 +00:00
|
|
|
rethrow;
|
|
|
|
} finally {
|
2024-07-26 16:20:11 +00:00
|
|
|
_refreshCompleter = null;
|
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-09-16 03:57:16 +00:00
|
|
|
Future<GetConnect> configureClient(
|
2024-06-22 14:39:32 +00:00
|
|
|
String service, {
|
2024-06-06 12:49:18 +00:00
|
|
|
timeout = const Duration(seconds: 5),
|
2024-09-16 03:57:16 +00:00
|
|
|
}) async {
|
2024-06-06 12:49:18 +00:00
|
|
|
final client = GetConnect(
|
|
|
|
maxAuthRetries: 3,
|
|
|
|
timeout: timeout,
|
2024-09-16 03:57:16 +00:00
|
|
|
userAgent: await ServiceFinder.getUserAgent(),
|
2024-06-29 09:35:18 +00:00
|
|
|
sendUserAgent: true,
|
2024-06-06 12:49:18 +00:00
|
|
|
);
|
2024-10-10 16:28:09 +00:00
|
|
|
client.httpClient.addRequestModifier(requestAuthenticator);
|
2024-07-16 11:46:53 +00:00
|
|
|
client.httpClient.baseUrl = ServiceFinder.buildUrl(service, null);
|
2024-06-06 12:49:18 +00:00
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> ensureCredentials() async {
|
2024-08-21 07:25:50 +00:00
|
|
|
if (isAuthorized.isFalse) throw const UnauthorizedException();
|
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-05-23 15:54:05 +00:00
|
|
|
Future<void> loadCredentials() async {
|
2024-07-24 17:18:47 +00:00
|
|
|
if (isAuthorized.isTrue) {
|
2024-05-23 15:54:05 +00:00
|
|
|
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,
|
2024-09-15 18:37:20 +00:00
|
|
|
AuthTicket ticket,
|
2024-05-23 15:54:05 +00:00
|
|
|
) async {
|
2024-07-24 17:18:47 +00:00
|
|
|
userProfile.value = null;
|
2024-05-23 15:54:05 +00:00
|
|
|
|
2024-06-29 09:35:18 +00:00
|
|
|
// Assign token
|
2024-07-16 11:46:53 +00:00
|
|
|
final tokenResp = await post('/auth/token', {
|
2024-09-15 18:37:20 +00:00
|
|
|
'code': ticket.grantToken!,
|
2024-06-29 09:35:18 +00:00
|
|
|
'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-07-16 11:46:53 +00:00
|
|
|
Get.find<WebSocketProvider>().connect();
|
|
|
|
Get.find<WebSocketProvider>().notifyPrefetch();
|
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-07-24 17:18:47 +00:00
|
|
|
isAuthorized.value = false;
|
|
|
|
userProfile.value = null;
|
2024-05-23 15:54:05 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
Get.find<WebSocketProvider>().disconnect();
|
|
|
|
Get.find<WebSocketProvider>().notifications.clear();
|
|
|
|
Get.find<WebSocketProvider>().notificationUnread.value = 0;
|
2024-05-25 05:00:40 +00:00
|
|
|
|
2024-09-15 04:25:50 +00:00
|
|
|
AppDatabase.removeDatabase();
|
2024-09-15 08:02:56 +00:00
|
|
|
autoStopBackgroundNotificationService();
|
2024-09-15 04:25:50 +00:00
|
|
|
|
2024-05-18 14:23:36 +00:00
|
|
|
storage.deleteAll();
|
|
|
|
}
|
|
|
|
|
2024-06-06 15:28:19 +00:00
|
|
|
// Data Layer
|
|
|
|
|
2024-07-24 17:18:47 +00:00
|
|
|
RxBool isAuthorized = false.obs;
|
|
|
|
Rx<Map<String, dynamic>?> userProfile = Rx(null);
|
2024-05-21 16:05:03 +00:00
|
|
|
|
2024-07-24 17:18:47 +00:00
|
|
|
Future<void> refreshAuthorizeStatus() async {
|
|
|
|
isAuthorized.value = await storage.containsKey(key: 'auth_credentials');
|
|
|
|
}
|
2024-05-21 16:05:03 +00:00
|
|
|
|
2024-07-24 17:18:47 +00:00
|
|
|
Future<void> refreshUserProfile() async {
|
2024-09-15 08:54:07 +00:00
|
|
|
if (!isAuthorized.value) return;
|
2024-09-16 03:57:16 +00:00
|
|
|
final client = await configureClient('auth');
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await client.get('/users/me');
|
2024-05-31 17:25:45 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-05-31 17:25:45 +00:00
|
|
|
}
|
|
|
|
|
2024-07-24 17:18:47 +00:00
|
|
|
userProfile.value = resp.body;
|
2024-07-12 05:15:46 +00:00
|
|
|
}
|
2024-05-18 14:23:36 +00:00
|
|
|
}
|