Solian/lib/providers/auth.dart

111 lines
3.1 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;
2024-05-02 04:16:01 +00:00
import 'package:solian/utils/http.dart';
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-05-02 04:16:01 +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-05-02 04:16:01 +00:00
HttpClient? client;
Future<bool> loadClient() async {
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,
);
await fetchProfiles();
return true;
} catch (e) {
2024-04-21 12:55:17 +00:00
signoff();
return false;
}
} else {
2024-05-02 04:51:16 +00:00
client = HttpClient(onTokenRefreshed: setToken);
return false;
}
}
2024-05-02 04:16:01 +00:00
Future<HttpClient> createClient(BuildContext context, String username, String password) async {
if (await loadClient()) {
return client!;
}
2024-05-02 04:16:01 +00:00
final credentials = (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,
2024-05-02 04:16:01 +00:00
))
.credentials;
setToken(credentials.accessToken, credentials.refreshToken!);
return HttpClient(
defaultToken: credentials.accessToken,
defaultRefreshToken: credentials.refreshToken,
onTokenRefreshed: setToken,
);
}
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-05-02 04:16:01 +00:00
Future<void> setToken(String atk, String rtk) async {
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-30 12:31:54 +00:00
notifyListeners();
}
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);
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();
2024-05-02 04:16:01 +00:00
return await storage.containsKey(key: storageKey);
}
Future<dynamic> getProfiles() async {
const storage = FlutterSecureStorage();
2024-05-01 16:49:38 +00:00
return jsonDecode(await storage.read(key: profileKey) ?? '{}');
}
}