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;
|
2024-05-02 04:16:01 +00:00
|
|
|
import 'package:solian/utils/http.dart';
|
2024-04-13 16:03:50 +00:00
|
|
|
import 'package:solian/utils/service_url.dart';
|
|
|
|
|
2024-04-30 12:31:54 +00:00
|
|
|
class AuthProvider extends ChangeNotifier {
|
2024-05-02 04:51:16 +00:00
|
|
|
AuthProvider() {
|
|
|
|
loadClient();
|
|
|
|
}
|
2024-04-13 16:03:50 +00:00
|
|
|
|
2024-05-02 04:16:01 +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-05-02 04:16:01 +00:00
|
|
|
HttpClient? client;
|
2024-04-13 16:03:50 +00:00
|
|
|
|
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-02 04:16:01 +00:00
|
|
|
final credentials = oauth2.Credentials.fromJson((await storage.read(key: storageKey))!);
|
|
|
|
client = HttpClient(
|
|
|
|
defaultToken: credentials.accessToken,
|
|
|
|
defaultRefreshToken: credentials.refreshToken,
|
|
|
|
onTokenRefreshed: setToken,
|
|
|
|
);
|
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 {
|
2024-05-02 04:51:16 +00:00
|
|
|
client = HttpClient(onTokenRefreshed: setToken);
|
2024-04-13 16:03:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 04:16:01 +00:00
|
|
|
Future<HttpClient> 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-05-02 04:16:01 +00:00
|
|
|
final credentials = (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,
|
2024-05-02 04:16:01 +00:00
|
|
|
))
|
|
|
|
.credentials;
|
|
|
|
|
|
|
|
setToken(credentials.accessToken, credentials.refreshToken!);
|
|
|
|
|
|
|
|
return HttpClient(
|
|
|
|
defaultToken: credentials.accessToken,
|
|
|
|
defaultRefreshToken: credentials.refreshToken,
|
|
|
|
onTokenRefreshed: setToken,
|
2024-04-13 16:03:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-05-02 04:16:01 +00:00
|
|
|
Future<void> setToken(String atk, String rtk) async {
|
2024-04-13 16:03:50 +00:00
|
|
|
if (client != null) {
|
2024-05-02 04:16:01 +00:00
|
|
|
final credentials = oauth2.Credentials(atk, refreshToken: rtk, idToken: atk, scopes: ['openid']);
|
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-02 04:16:01 +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
|
|
|
|
|
|
|
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();
|
2024-05-02 04:16:01 +00:00
|
|
|
return await storage.containsKey(key: storageKey);
|
2024-04-13 16:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|