2024-05-18 14:23:36 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
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-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';
|
|
|
|
import 'package:oauth2/oauth2.dart' as oauth2;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
|
|
|
|
oauth2.Credentials? credentials;
|
|
|
|
|
2024-05-25 05:00:40 +00:00
|
|
|
Future<void> refreshCredentials() async {
|
|
|
|
final resp = await post('/api/auth/token', {
|
|
|
|
'refresh_token': credentials!.refreshToken,
|
|
|
|
'grant_type': 'refresh_token',
|
|
|
|
});
|
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
throw Exception(resp.bodyString);
|
|
|
|
}
|
|
|
|
credentials = oauth2.Credentials(
|
|
|
|
resp.body['access_token'],
|
|
|
|
refreshToken: resp.body['refresh_token'],
|
|
|
|
idToken: resp.body['access_token'],
|
|
|
|
tokenEndpoint: tokenEndpoint,
|
|
|
|
expiration: DateTime.now().add(const Duration(minutes: 3)),
|
|
|
|
);
|
|
|
|
storage.write(
|
|
|
|
key: 'auth_credentials',
|
|
|
|
value: jsonEncode(credentials!.toJson()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-23 15:54:05 +00:00
|
|
|
Future<Request<T?>> requestAuthenticator<T>(Request<T?> request) async {
|
2024-05-18 14:23:36 +00:00
|
|
|
if (credentials != null && credentials!.isExpired) {
|
2024-05-25 05:00:40 +00:00
|
|
|
refreshCredentials();
|
2024-05-18 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (credentials != null) {
|
|
|
|
request.headers['Authorization'] = 'Bearer ${credentials!.accessToken}';
|
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2024-05-23 15:54:05 +00:00
|
|
|
Future<void> loadCredentials() async {
|
|
|
|
if (await isAuthorized) {
|
|
|
|
final content = await storage.read(key: 'auth_credentials');
|
|
|
|
credentials = oauth2.Credentials.fromJson(jsonDecode(content!));
|
|
|
|
}
|
2024-05-18 14:23:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
Future<oauth2.Credentials> 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-05-18 14:23:36 +00:00
|
|
|
final resp = await oauth2.resourceOwnerPasswordGrant(
|
|
|
|
tokenEndpoint,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
identifier: clientId,
|
|
|
|
secret: clientSecret,
|
|
|
|
scopes: ['*'],
|
|
|
|
basicAuth: false,
|
|
|
|
);
|
|
|
|
|
|
|
|
credentials = oauth2.Credentials(
|
|
|
|
resp.credentials.accessToken,
|
|
|
|
refreshToken: resp.credentials.refreshToken!,
|
|
|
|
idToken: resp.credentials.accessToken,
|
|
|
|
tokenEndpoint: tokenEndpoint,
|
|
|
|
expiration: DateTime.now().add(const Duration(minutes: 3)),
|
|
|
|
);
|
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
storage.write(
|
2024-05-23 15:54:05 +00:00
|
|
|
key: 'auth_credentials',
|
|
|
|
value: jsonEncode(credentials!.toJson()),
|
|
|
|
);
|
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-05-18 14:23:36 +00:00
|
|
|
return credentials!;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-18 14:23:36 +00:00
|
|
|
storage.deleteAll();
|
|
|
|
}
|
|
|
|
|
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-05-26 05:39:21 +00:00
|
|
|
final client = GetConnect(maxAuthRetries: 3);
|
2024-05-23 15:54:05 +00:00
|
|
|
client.httpClient.baseUrl = ServiceFinder.services['passport'];
|
|
|
|
client.httpClient.addAuthenticator(requestAuthenticator);
|
|
|
|
|
|
|
|
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-05-18 14:23:36 +00:00
|
|
|
}
|