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:solian/screens/auth.dart';
|
|
|
|
import 'package:oauth2/oauth2.dart' as oauth2;
|
|
|
|
import 'package:solian/utils/service_url.dart';
|
|
|
|
|
|
|
|
class AuthProvider {
|
2024-04-15 15:08:32 +00:00
|
|
|
AuthProvider();
|
2024-04-13 16:03:50 +00:00
|
|
|
|
|
|
|
final deviceEndpoint =
|
|
|
|
getRequestUri('passport', '/api/notifications/subscribe');
|
|
|
|
final authorizationEndpoint = getRequestUri('passport', '/auth/o/connect');
|
|
|
|
final tokenEndpoint = getRequestUri('passport', '/api/auth/token');
|
|
|
|
final userinfoEndpoint = getRequestUri('passport', '/api/users/me');
|
|
|
|
final redirectUrl = Uri.parse('solian://auth');
|
|
|
|
|
|
|
|
static const clientId = "solian";
|
|
|
|
static const clientSecret = "_F4%q2Eea3";
|
|
|
|
|
|
|
|
static const storage = FlutterSecureStorage();
|
|
|
|
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**
|
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;
|
|
|
|
|
|
|
|
Future<bool> pickClient() async {
|
|
|
|
if (await storage.containsKey(key: storageKey)) {
|
|
|
|
try {
|
2024-04-14 15:00:04 +00:00
|
|
|
final credentials =
|
2024-04-13 16:03:50 +00:00
|
|
|
oauth2.Credentials.fromJson((await storage.read(key: storageKey))!);
|
|
|
|
client = oauth2.Client(credentials,
|
|
|
|
identifier: clientId, secret: clientSecret);
|
|
|
|
await fetchProfiles();
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
signOff();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<oauth2.Client> createClient(BuildContext context) async {
|
|
|
|
// If logged in
|
|
|
|
if (await pickClient()) {
|
|
|
|
return client!;
|
|
|
|
}
|
|
|
|
|
|
|
|
var grant = oauth2.AuthorizationCodeGrant(
|
|
|
|
clientId,
|
|
|
|
authorizationEndpoint,
|
|
|
|
tokenEndpoint,
|
|
|
|
secret: clientSecret,
|
|
|
|
basicAuth: false,
|
|
|
|
);
|
|
|
|
|
2024-04-14 07:58:27 +00:00
|
|
|
var authorizationUrl =
|
|
|
|
grant.getAuthorizationUrl(redirectUrl, scopes: ["openid"]);
|
2024-04-13 16:03:50 +00:00
|
|
|
|
2024-04-14 15:00:04 +00:00
|
|
|
var responseUrl = await Navigator.of(context, rootNavigator: true).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => AuthorizationScreen(authorizationUrl),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
var responseUri = Uri.parse(responseUrl);
|
|
|
|
return await grant.handleAuthorizationResponse(responseUri.queryParameters);
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> refreshToken() async {
|
|
|
|
if (client != null) {
|
2024-04-15 15:08:32 +00:00
|
|
|
final credentials = await client!.credentials.refresh(
|
2024-04-13 16:03:50 +00:00
|
|
|
identifier: clientId, secret: clientSecret, basicAuth: false);
|
2024-04-15 15:08:32 +00:00
|
|
|
client = oauth2.Client(credentials,
|
2024-04-14 15:00:04 +00:00
|
|
|
identifier: clientId, secret: clientSecret);
|
|
|
|
storage.write(key: storageKey, value: credentials.toJson());
|
2024-04-13 16:03:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> signIn(BuildContext context) async {
|
|
|
|
client = await createClient(context);
|
|
|
|
storage.write(key: storageKey, value: client!.credentials.toJson());
|
|
|
|
|
|
|
|
await fetchProfiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 pickClient();
|
|
|
|
}
|
|
|
|
if (lastRefreshedAt == null ||
|
|
|
|
DateTime.now()
|
|
|
|
.subtract(const Duration(minutes: 3))
|
|
|
|
.isAfter(lastRefreshedAt!)) {
|
|
|
|
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();
|
|
|
|
return jsonDecode(await storage.read(key: profileKey) ?? "{}");
|
|
|
|
}
|
|
|
|
}
|