Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.7 KiB

  1. import 'dart:convert';
  2. import 'package:Envelope/utils/encryption/aes_helper.dart';
  3. import 'package:Envelope/utils/encryption/crypto_utils.dart';
  4. import 'package:pointycastle/impl.dart';
  5. import 'package:shared_preferences/shared_preferences.dart';
  6. class MyProfile {
  7. String id;
  8. String username;
  9. RSAPrivateKey privateKey;
  10. RSAPublicKey publicKey;
  11. DateTime loggedInAt;
  12. MyProfile({
  13. required this.id,
  14. required this.username,
  15. required this.privateKey,
  16. required this.publicKey,
  17. required this.loggedInAt,
  18. });
  19. factory MyProfile._fromJson(Map<String, dynamic> json) {
  20. DateTime loggedInAt = DateTime.now();
  21. if (json.containsKey('logged_in_at')) {
  22. loggedInAt = DateTime.parse(json['logged_in_at']);
  23. }
  24. return MyProfile(
  25. id: json['user_id'],
  26. username: json['username'],
  27. privateKey: CryptoUtils.rsaPrivateKeyFromPem(json['asymmetric_private_key']),
  28. publicKey: CryptoUtils.rsaPublicKeyFromPem(json['asymmetric_public_key']),
  29. loggedInAt: loggedInAt,
  30. );
  31. }
  32. @override
  33. String toString() {
  34. return '''
  35. user_id: $id
  36. username: $username
  37. logged_in_at: $loggedInAt
  38. public_key: $publicKey
  39. private_key: $privateKey
  40. ''';
  41. }
  42. String toJson() {
  43. return jsonEncode(<String, dynamic>{
  44. 'user_id': id,
  45. 'username': username,
  46. 'asymmetric_private_key': CryptoUtils.encodeRSAPrivateKeyToPem(privateKey),
  47. 'asymmetric_public_key': CryptoUtils.encodeRSAPublicKeyToPem(publicKey),
  48. 'logged_in_at': loggedInAt.toIso8601String(),
  49. });
  50. }
  51. static Future<MyProfile> login(Map<String, dynamic> json, String password) async {
  52. json['asymmetric_private_key'] = AesHelper.aesDecrypt(
  53. password,
  54. base64.decode(json['asymmetric_private_key'])
  55. );
  56. MyProfile profile = MyProfile._fromJson(json);
  57. final preferences = await SharedPreferences.getInstance();
  58. print(profile.toJson());
  59. preferences.setString('profile', profile.toJson());
  60. return profile;
  61. }
  62. static Future<void> logout() async {
  63. final preferences = await SharedPreferences.getInstance();
  64. preferences.remove('profile');
  65. }
  66. static Future<MyProfile> getProfile() async {
  67. final preferences = await SharedPreferences.getInstance();
  68. String? profileJson = preferences.getString('profile');
  69. if (profileJson == null) {
  70. throw Exception('No profile');
  71. }
  72. return MyProfile._fromJson(json.decode(profileJson));
  73. }
  74. static Future<bool> isLoggedIn() async {
  75. MyProfile profile = await MyProfile.getProfile();
  76. return profile.loggedInAt.isBefore((DateTime.now()).add(const Duration(hours: 12)));
  77. }
  78. Future<RSAPrivateKey> getPrivateKey() async {
  79. MyProfile profile = await MyProfile.getProfile();
  80. return profile.privateKey;
  81. }
  82. }