Solian/lib/providers/auth.dart

121 lines
3.4 KiB
Dart
Raw Normal View History

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-05-01 09:37:34 +00:00
final deviceEndpoint =
getRequestUri('passport', '/api/notifications/subscribe');
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';
static const storage = FlutterSecureStorage();
2024-05-01 16:49:38 +00:00
static const storageKey = 'identity';
static const profileKey = 'profiles';
2024-04-15 15:08:32 +00:00
/// Before use this variable to make request
/// **MAKE SURE YOU HAVE CALL THE isAuthorized() METHOD**
oauth2.Client? client;
2024-04-15 15:08:32 +00:00
DateTime? lastRefreshedAt;
Future<bool> loadClient() async {
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);
await fetchProfiles();
return true;
} catch (e) {
2024-04-21 12:55:17 +00:00
signoff();
return false;
}
} else {
return false;
}
}
2024-05-01 09:37:34 +00:00
Future<oauth2.Client> createClient(
BuildContext context, String username, String password) async {
if (await loadClient()) {
return client!;
}
2024-04-21 12:55:17 +00:00
return await oauth2.resourceOwnerPasswordGrant(
tokenEndpoint,
2024-04-21 12:55:17 +00:00
username,
password,
identifier: clientId,
secret: clientSecret,
2024-05-01 16:49:38 +00:00
scopes: ['openid'],
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();
}
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-30 12:31:54 +00:00
notifyListeners();
}
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);
storage.write(key: storageKey, value: client!.credentials.toJson());
await fetchProfiles();
}
2024-04-21 12:55:17 +00:00
void signoff() {
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) {
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();
}
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) ?? '{}');
}
}