95 lines
3.5 KiB
Dart
95 lines
3.5 KiB
Dart
|
|
import 'package:localstorage/localstorage.dart';
|
|
import 'package:oc_front/core/sections/header/header.dart';
|
|
import 'package:oc_front/core/services/api_service.dart';
|
|
import 'package:oc_front/core/services/perms_service.dart';
|
|
import 'package:oc_front/models/response.dart';
|
|
class AuthService {
|
|
static var isAuth = const bool.fromEnvironment('AUTH_MODE', defaultValue: false);
|
|
static APIService<SimpleData> service = APIService(
|
|
baseURL: const String.fromEnvironment('AUTH_HOST', defaultValue: 'http://localhost:8080/auth'),
|
|
);
|
|
static Future<void> init() async {
|
|
if (!isAuth) {
|
|
return;
|
|
}
|
|
PermsService.init(localStorage.getItem('accessToken') ?? "");
|
|
bool ok = await introspect().catchError( (e) => false );
|
|
if (ok) {
|
|
var now = DateTime.now();
|
|
var expires = DateTime.parse(localStorage.getItem('expiresIn') ?? DateTime.now().toIso8601String());
|
|
var duration = expires.difference(now);
|
|
refresh(localStorage.getItem('accessToken') ?? "", localStorage.getItem('username') ?? "", duration);
|
|
} else {
|
|
localStorage.setItem('accessToken', '');
|
|
localStorage.setItem('username', '');
|
|
localStorage.setItem('expiresIn', '');
|
|
}
|
|
}
|
|
|
|
static bool isConnected() {
|
|
if (!isAuth) {
|
|
return true;
|
|
}
|
|
return (localStorage.getItem('accessToken') ?? "") != "";
|
|
}
|
|
|
|
static String? getUsername() {
|
|
if (!isAuth) {
|
|
return "no auth user";
|
|
}
|
|
return localStorage.getItem('username') ?? "unknown";
|
|
}
|
|
|
|
static Future<void> login(String username, String password) async {
|
|
var token = await service.post("/ldap/login", <String, dynamic> {
|
|
"username": username,
|
|
"password": password
|
|
}, null);
|
|
if (token.code == 200 && token.data != null) {
|
|
localStorage.setItem('accessToken', token.data?.value['access_token']);
|
|
localStorage.setItem('tokenType', token.data?.value['token_type']);
|
|
localStorage.setItem('username', username);
|
|
localStorage.setItem('expiresIn',
|
|
DateTime.now().add(Duration(seconds: token.data?.value['expires_in'])).toIso8601String());
|
|
HeaderConstants.headerKey.currentState?.setState(() {});
|
|
PermsService.init(token.data?.value['access_token']);
|
|
refresh(token.data?.value['access_token'] ?? "", username, Duration(seconds: token.data?.value['expires_in']));
|
|
}
|
|
}
|
|
|
|
static Future<void> logout() async {
|
|
var token = await service.delete("/ldap/logout", null);
|
|
if (token.code == 200 && token.data != null) {
|
|
localStorage.setItem('accessToken', '');
|
|
localStorage.setItem('username', '');
|
|
localStorage.setItem('expiresIn', '');
|
|
PermsService.clear();
|
|
}
|
|
}
|
|
|
|
static Future<bool> introspect() async {
|
|
if (!isConnected()) {
|
|
return false;
|
|
}
|
|
var isIntrospected = await service.get("/introspect", true, null);
|
|
return isIntrospected.code == 200;
|
|
}
|
|
|
|
static Future<void> refresh(String accessToken, String username, Duration duration) async {
|
|
Future.delayed(duration, () {
|
|
service.post("/refresh", <String, dynamic> {
|
|
"access_token": accessToken,
|
|
"username": username
|
|
}, null).then((token) {
|
|
if (token.code == 200 && token.data != null) {
|
|
PermsService.init(token.data?.value['access_token']);
|
|
localStorage.setItem('accessToken', token.data?.value['access_token']);
|
|
localStorage.setItem('username', username);
|
|
localStorage.setItem('expiresIn',
|
|
DateTime.now().add(Duration(seconds: token.data?.value['expires_in']) - Duration(seconds: 10)).toIso8601String());
|
|
}
|
|
});
|
|
});
|
|
}
|
|
} |