2024-04-13 16:03:50 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
import 'package:oauth2/oauth2.dart' as oauth2;
|
|
|
|
import 'package:solian/utils/service_url.dart';
|
|
|
|
|
2024-04-30 12:31:54 +00:00
|
|
|
class AuthProvider extends ChangeNotifier {
|
2024-04-15 15:08:32 +00:00
|
|
|
AuthProvider();
|
2024-04-13 16:03:50 +00:00
|
|
|
|
2024-05-01 09:37:34 +00:00
|
|
|
final deviceEndpoint =
|
|
|
|
getRequestUri('passport', '/api/notifications/subscribe');
|
2024-04-13 16:03:50 +00:00
|
|
|
final tokenEndpoint = getRequestUri('passport', '/api/auth/token');
|
|
|
|
final userinfoEndpoint = getRequestUri('passport', '/api/users/me');
|
|
|
|
final redirectUrl = Uri.parse('solian://auth');
|
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
static const clientId = 'solian';
|
|
|
|
static const clientSecret = '_F4%q2Eea3';
|
2024-04-13 16:03:50 +00:00
|
|
|
|
|
|
|
static const storage = FlutterSecureStorage();
|
2024-05-01 16:49:38 +00:00
|
|
|
static const storageKey = 'identity';
|
|
|
|
static const profileKey = 'profiles';
|
2024-04-13 16:03:50 +00:00
|
|
|
|
2024-04-15 15:08:32 +00:00
|
|
|
/// Before use this variable to make request
|
|
|
|
/// **MAKE SURE YOU HAVE CALL THE isAuthorized() METHOD**
|
2024-04-13 16:03:50 +00:00
|
|
|
oauth2.Client? client;
|
2024-04-15 15:08:32 +00:00
|
|
|
|
2024-04-13 16:03:50 +00:00
|
|
|
DateTime? lastRefreshedAt;
|
|
|
|
|
2024-05-01 03:27:48 +00:00
|
|
|
Future<bool> loadClient() async {
|
2024-04-13 16:03:50 +00:00
|
|
|
if (await storage.containsKey(key: storageKey)) {
|
|
|
|
try {
|
2024-05-01 09:37:34 +00:00
|
|
|
final credentials =
|
|
|
|
oauth2.Credentials.fromJson((await storage.read(key: storageKey))!);
|
|
|
|
client = oauth2.Client(credentials,
|
|
|
|
identifier: clientId, secret: clientSecret);
|
2024-04-13 16:03:50 +00:00
|
|
|
await fetchProfiles();
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
2024-04-21 12:55:17 +00:00
|
|
|
signoff();
|
2024-04-13 16:03:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-01 09:37:34 +00:00
|
|
|
Future<oauth2.Client> createClient(
|
|
|
|
BuildContext context, String username, String password) async {
|
2024-05-01 03:27:48 +00:00
|
|
|
if (await loadClient()) {
|
2024-04-13 16:03:50 +00:00
|
|
|
return client!;
|
|
|
|
}
|
|
|
|
|
2024-04-21 12:55:17 +00:00
|
|
|
return await oauth2.resourceOwnerPasswordGrant(
|
2024-04-13 16:03:50 +00:00
|
|
|
tokenEndpoint,
|
2024-04-21 12:55:17 +00:00
|
|
|
username,
|
|
|
|
password,
|
|
|
|
identifier: clientId,
|
2024-04-13 16:03:50 +00:00
|
|
|
secret: clientSecret,
|
2024-05-01 16:49:38 +00:00
|
|
|
scopes: ['openid'],
|
2024-04-13 16:03:50 +00:00
|
|
|
basicAuth: false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> fetchProfiles() async {
|
|
|
|
if (client != null) {
|
|
|
|
var userinfo = await client!.get(userinfoEndpoint);
|
|
|
|
storage.write(key: profileKey, value: utf8.decode(userinfo.bodyBytes));
|
|
|
|
}
|
2024-04-30 12:31:54 +00:00
|
|
|
notifyListeners();
|
2024-04-13 16:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> refreshToken() async {
|
|
|
|
if (client != null) {
|
2024-05-01 09:37:34 +00:00
|
|
|
final credentials = await client!.credentials.refresh(
|
|
|
|
identifier: clientId, secret: clientSecret, basicAuth: false);
|
|
|
|
client = oauth2.Client(credentials,
|
|
|
|
identifier: clientId, secret: clientSecret);
|
2024-04-14 15:00:04 +00:00
|
|
|
storage.write(key: storageKey, value: credentials.toJson());
|
2024-04-13 16:03:50 +00:00
|
|
|
}
|
2024-04-30 12:31:54 +00:00
|
|
|
notifyListeners();
|
2024-04-13 16:03:50 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 09:37:34 +00:00
|
|
|
Future<void> signin(
|
|
|
|
BuildContext context, String username, String password) async {
|
2024-04-21 12:55:17 +00:00
|
|
|
client = await createClient(context, username, password);
|
2024-04-13 16:03:50 +00:00
|
|
|
storage.write(key: storageKey, value: client!.credentials.toJson());
|
|
|
|
|
|
|
|
await fetchProfiles();
|
|
|
|
}
|
|
|
|
|
2024-04-21 12:55:17 +00:00
|
|
|
void signoff() {
|
2024-04-13 16:03:50 +00:00
|
|
|
storage.delete(key: profileKey);
|
|
|
|
storage.delete(key: storageKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> isAuthorized() async {
|
|
|
|
const storage = FlutterSecureStorage();
|
|
|
|
if (await storage.containsKey(key: storageKey)) {
|
2024-04-15 15:08:32 +00:00
|
|
|
if (client == null) {
|
2024-05-01 03:27:48 +00:00
|
|
|
await loadClient();
|
2024-04-15 15:08:32 +00:00
|
|
|
}
|
2024-05-01 09:37:34 +00:00
|
|
|
if (lastRefreshedAt == null ||
|
|
|
|
DateTime.now()
|
|
|
|
.subtract(const Duration(minutes: 3))
|
|
|
|
.isAfter(lastRefreshedAt!)) {
|
2024-04-15 15:08:32 +00:00
|
|
|
await refreshToken();
|
|
|
|
lastRefreshedAt = DateTime.now();
|
2024-04-13 16:03:50 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<dynamic> getProfiles() async {
|
|
|
|
const storage = FlutterSecureStorage();
|
2024-05-01 16:49:38 +00:00
|
|
|
return jsonDecode(await storage.read(key: profileKey) ?? '{}');
|
2024-04-13 16:03:50 +00:00
|
|
|
}
|
|
|
|
}
|