54 lines
1.4 KiB
Dart
54 lines
1.4 KiB
Dart
import 'dart:math';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:base32/base32.dart';
|
|
import 'package:libac_dart/nbt/NbtUtils.dart';
|
|
import 'package:libac_dart/nbt/impl/CompoundTag.dart';
|
|
import 'package:libac_dart/utils/uuid/NbtUUID.dart';
|
|
import 'package:libac_dart/utils/uuid/UUID.dart';
|
|
|
|
/// The user class is a user object.
|
|
///
|
|
/// This, most basic object, will only contain data needed for user identification. Issues, groups, etc shall be handled in another area.
|
|
class User {
|
|
String sName;
|
|
UUID ID;
|
|
|
|
User({required this.sName, required this.ID});
|
|
}
|
|
|
|
String generateTOTPSecret({int length = 32}) {
|
|
final random = Random.secure();
|
|
final List<int> bytes = List.generate(length, (_) => random.nextInt(256));
|
|
return base32.encode(Uint8List.fromList(bytes));
|
|
}
|
|
|
|
class DBUser {
|
|
String sName;
|
|
UUID ID;
|
|
late String TOTPSecret;
|
|
|
|
DBUser({required this.sName, required this.ID, String? totp}) {
|
|
if (totp == null)
|
|
TOTPSecret = generateTOTPSecret();
|
|
else
|
|
TOTPSecret = totp!;
|
|
}
|
|
|
|
factory DBUser.load({required CompoundTag serialized}) {
|
|
UUID IDv4 = UUID.generate(4);
|
|
NbtUUID saved = NbtUtils.readUUID(serialized, "id");
|
|
IDv4 = saved.toUUID();
|
|
DBUser user = DBUser(
|
|
sName: serialized.get("name")?.asString() ?? "",
|
|
ID: IDv4,
|
|
totp: serialized.get("mfa_secret")?.asString() ?? "",
|
|
);
|
|
|
|
return user;
|
|
}
|
|
|
|
void regenerateTOTP() {
|
|
TOTPSecret = generateTOTPSecret();
|
|
}
|
|
}
|