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-02-07 20:40:56 +00:00
|
|
|
import 'package:goatagent/screens/auth.dart';
|
2024-02-07 17:25:58 +00:00
|
|
|
import 'package:oauth2/oauth2.dart' as oauth2;
|
|
|
|
|
2024-02-07 20:40:56 +00:00
|
|
|
class AuthGuard {
|
|
|
|
static final AuthGuard _singleton = AuthGuard._internal();
|
|
|
|
|
|
|
|
final authorizationEndpoint =
|
|
|
|
Uri.parse('https://id.smartsheep.studio/auth/o/connect');
|
|
|
|
final tokenEndpoint =
|
|
|
|
Uri.parse('https://id.smartsheep.studio/api/auth/token');
|
|
|
|
final userinfoEndpoint =
|
|
|
|
Uri.parse('https://id.smartsheep.studio/api/users/me');
|
|
|
|
final redirectUrl = Uri.parse('goatagent://auth');
|
|
|
|
|
|
|
|
static const clientId = "goatagent";
|
|
|
|
static const clientSecret = "_F4%q2Eea3";
|
|
|
|
|
|
|
|
static const storage = FlutterSecureStorage();
|
|
|
|
static const storageKey = "identity";
|
|
|
|
static const profileKey = "profiles";
|
|
|
|
|
|
|
|
factory AuthGuard() {
|
|
|
|
return _singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
oauth2.Client? client;
|
|
|
|
|
|
|
|
Future<bool> pickClient() async {
|
|
|
|
if (await storage.containsKey(key: storageKey)) {
|
|
|
|
var credentials =
|
|
|
|
oauth2.Credentials.fromJson((await storage.read(key: storageKey))!);
|
|
|
|
client = oauth2.Client(credentials,
|
|
|
|
identifier: clientId, secret: clientSecret);
|
|
|
|
print(await storage.readAll());
|
|
|
|
return true;
|
|
|
|
} 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) {
|
|
|
|
// Let Goatpass know it is embed in an app
|
|
|
|
authorizationUrl = authorizationUrl.replace(
|
|
|
|
queryParameters: {"embedded": "yes"}
|
|
|
|
..addAll(authorizationUrl.queryParameters));
|
|
|
|
|
|
|
|
// Use WebView to get authorization url
|
|
|
|
var responseUrl = await Navigator.of(context).push(MaterialPageRoute(
|
|
|
|
builder: (context) => AuthorizationPage(authorizationUrl),
|
|
|
|
));
|
|
|
|
|
|
|
|
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-07 20:40:56 +00:00
|
|
|
Future<void> login(BuildContext context) async {
|
2024-02-07 17:25:58 +00:00
|
|
|
try {
|
2024-02-07 20:40:56 +00:00
|
|
|
client = await createClient(context);
|
|
|
|
var userinfo = await client!.read(userinfoEndpoint);
|
2024-02-07 17:25:58 +00:00
|
|
|
|
2024-02-07 20:40:56 +00:00
|
|
|
storage.write(key: profileKey, value: userinfo);
|
|
|
|
storage.write(key: storageKey, value: client!.credentials.toJson());
|
2024-02-07 21:03:38 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
}
|
2024-02-07 20:40:56 +00:00
|
|
|
|
2024-02-07 21:03:38 +00:00
|
|
|
void logout() {
|
|
|
|
try {
|
|
|
|
storage.delete(key: profileKey);
|
|
|
|
storage.delete(key: storageKey);
|
2024-02-07 17:25:58 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 20:40:56 +00:00
|
|
|
Future<bool> isAuthorized() async {
|
|
|
|
const storage = FlutterSecureStorage();
|
|
|
|
return await storage.containsKey(key: storageKey);
|
2024-02-07 17:25:58 +00:00
|
|
|
}
|
2024-02-07 20:40:56 +00:00
|
|
|
|
|
|
|
Future<dynamic> readProfiles() async {
|
|
|
|
const storage = FlutterSecureStorage();
|
|
|
|
return jsonDecode(await storage.read(key: profileKey) ?? "{}");
|
|
|
|
}
|
|
|
|
|
|
|
|
AuthGuard._internal();
|
2024-02-07 17:25:58 +00:00
|
|
|
}
|