2024-02-07 20:40:56 +00:00
|
|
|
import 'dart:convert';
|
2024-02-07 17:25:58 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
2024-03-17 12:22:46 +00:00
|
|
|
import 'package:solaragent/screens/auth.dart';
|
2024-02-07 17:25:58 +00:00
|
|
|
import 'package:oauth2/oauth2.dart' as oauth2;
|
|
|
|
|
2024-03-17 12:22:46 +00:00
|
|
|
final authClient = AuthGuard();
|
|
|
|
|
2024-02-07 20:40:56 +00:00
|
|
|
class AuthGuard {
|
2024-03-17 12:22:46 +00:00
|
|
|
AuthGuard();
|
2024-02-07 20:40:56 +00:00
|
|
|
|
2024-02-08 07:19:37 +00:00
|
|
|
final deviceEndpoint =
|
2024-03-17 12:22:46 +00:00
|
|
|
Uri.parse('https://id.solsynth.dev/api/notifications/subscribe');
|
2024-02-07 20:40:56 +00:00
|
|
|
final authorizationEndpoint =
|
2024-03-17 12:22:46 +00:00
|
|
|
Uri.parse('https://id.solsynth.dev/auth/o/connect');
|
2024-03-23 12:56:32 +00:00
|
|
|
final tokenEndpoint = Uri.parse('https://id.solsynth.dev/api/auth/token');
|
|
|
|
final userinfoEndpoint = Uri.parse('https://id.solsynth.dev/api/users/me');
|
2024-03-17 12:22:46 +00:00
|
|
|
final redirectUrl = Uri.parse('solaragent://auth');
|
2024-02-07 20:40:56 +00:00
|
|
|
|
2024-03-17 12:22:46 +00:00
|
|
|
static const clientId = "solaragent";
|
2024-02-07 20:40:56 +00:00
|
|
|
static const clientSecret = "_F4%q2Eea3";
|
|
|
|
|
|
|
|
static const storage = FlutterSecureStorage();
|
|
|
|
static const storageKey = "identity";
|
|
|
|
static const profileKey = "profiles";
|
|
|
|
|
|
|
|
oauth2.Client? client;
|
|
|
|
|
|
|
|
Future<bool> pickClient() async {
|
|
|
|
if (await storage.containsKey(key: storageKey)) {
|
2024-02-08 07:19:37 +00:00
|
|
|
try {
|
|
|
|
var credentials =
|
|
|
|
oauth2.Credentials.fromJson((await storage.read(key: storageKey))!);
|
|
|
|
client = oauth2.Client(credentials,
|
|
|
|
identifier: clientId, secret: clientSecret);
|
|
|
|
await pullProfiles();
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
2024-03-23 12:56:32 +00:00
|
|
|
signoff();
|
2024-02-08 07:19:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-07 20:40:56 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-07 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 20:40:56 +00:00
|
|
|
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-02-07 17:25:58 +00:00
|
|
|
|
2024-02-07 20:40:56 +00:00
|
|
|
var authorizationUrl = grant.getAuthorizationUrl(redirectUrl);
|
2024-02-07 17:25:58 +00:00
|
|
|
|
2024-02-07 20:40:56 +00:00
|
|
|
if (Platform.isAndroid || Platform.isIOS) {
|
|
|
|
// Use WebView to get authorization url
|
2024-02-10 12:08:25 +00:00
|
|
|
var responseUrl = await Navigator.of(context, rootNavigator: true).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => AuthorizationScreen(authorizationUrl),
|
|
|
|
),
|
|
|
|
);
|
2024-02-07 20:40:56 +00:00
|
|
|
|
|
|
|
var responseUri = Uri.parse(responseUrl);
|
|
|
|
return await grant
|
|
|
|
.handleAuthorizationResponse(responseUri.queryParameters);
|
|
|
|
} else {
|
|
|
|
throw UnimplementedError("unsupported platform");
|
2024-02-07 17:25:58 +00:00
|
|
|
}
|
2024-02-07 20:40:56 +00:00
|
|
|
}
|
2024-02-07 17:25:58 +00:00
|
|
|
|
2024-02-08 07:19:37 +00:00
|
|
|
Future<void> pullProfiles() async {
|
|
|
|
if (client != null) {
|
|
|
|
var userinfo = await client!.get(userinfoEndpoint);
|
|
|
|
storage.write(key: profileKey, value: utf8.decode(userinfo.bodyBytes));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-23 12:56:32 +00:00
|
|
|
Future<void> refreshToken() async {
|
|
|
|
if (client != null) {
|
|
|
|
var credentials = await client?.credentials.refresh(
|
|
|
|
identifier: clientId, secret: clientSecret, basicAuth: false);
|
2024-02-08 07:19:37 +00:00
|
|
|
|
2024-03-23 12:56:32 +00:00
|
|
|
storage.write(key: storageKey, value: credentials!.toJson());
|
2024-02-07 21:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-07 20:40:56 +00:00
|
|
|
|
2024-03-23 12:56:32 +00:00
|
|
|
Future<void> signin(BuildContext context) async {
|
|
|
|
client = await createClient(context);
|
|
|
|
storage.write(key: storageKey, value: client!.credentials.toJson());
|
2024-02-08 07:19:37 +00:00
|
|
|
|
2024-03-23 12:56:32 +00:00
|
|
|
await pullProfiles();
|
2024-02-08 07:19:37 +00:00
|
|
|
}
|
|
|
|
|
2024-03-23 12:56:32 +00:00
|
|
|
void signoff() {
|
|
|
|
storage.delete(key: profileKey);
|
|
|
|
storage.delete(key: storageKey);
|
2024-02-07 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 20:40:56 +00:00
|
|
|
Future<bool> isAuthorized() async {
|
|
|
|
const storage = FlutterSecureStorage();
|
2024-03-23 12:56:32 +00:00
|
|
|
if (await storage.containsKey(key: storageKey)) {
|
|
|
|
if (client != null && client!.credentials.isExpired) {
|
|
|
|
await refreshToken();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-07 17:25:58 +00:00
|
|
|
}
|
2024-02-07 20:40:56 +00:00
|
|
|
|
2024-03-23 12:56:32 +00:00
|
|
|
Future<dynamic> getProfiles() async {
|
2024-02-07 20:40:56 +00:00
|
|
|
const storage = FlutterSecureStorage();
|
|
|
|
return jsonDecode(await storage.read(key: profileKey) ?? "{}");
|
|
|
|
}
|
2024-02-07 17:25:58 +00:00
|
|
|
}
|