Browse Source

Use updated verify-email endpoint to enable 2FA

Vishnu Mohandas 4 years ago
parent
commit
04879370d1

+ 15 - 1
lib/core/configuration.dart

@@ -43,6 +43,7 @@ class Configuration {
   static const nameKey = "name";
   static const nameKey = "name";
   static const secretKeyKey = "secret_key";
   static const secretKeyKey = "secret_key";
   static const tokenKey = "token";
   static const tokenKey = "token";
+  static const encryptedTokenKey = "encrypted_token";
   static const userIDKey = "user_id";
   static const userIDKey = "user_id";
 
 
   final kTempFolderDeletionTimeBuffer = Duration(days: 1).inMicroseconds;
   final kTempFolderDeletionTimeBuffer = Duration(days: 1).inMicroseconds;
@@ -187,7 +188,7 @@ class Configuration {
     );
     );
   }
   }
 
 
-  Future<void> decryptAndSaveKey(
+  Future<void> decryptAndSaveSecrets(
       String password, KeyAttributes attributes) async {
       String password, KeyAttributes attributes) async {
     final kek = await CryptoUtil.deriveKey(
     final kek = await CryptoUtil.deriveKey(
       utf8.encode(password),
       utf8.encode(password),
@@ -208,6 +209,11 @@ class Configuration {
         Sodium.base642bin(attributes.secretKeyDecryptionNonce));
         Sodium.base642bin(attributes.secretKeyDecryptionNonce));
     await setKey(Sodium.bin2base64(key));
     await setKey(Sodium.bin2base64(key));
     await setSecretKey(Sodium.bin2base64(secretKey));
     await setSecretKey(Sodium.bin2base64(secretKey));
+    final token = CryptoUtil.openSealSync(
+        Sodium.base642bin(getEncryptedToken()),
+        Sodium.base642bin(attributes.publicKey),
+        secretKey);
+    await setToken(Sodium.bin2base64(token));
   }
   }
 
 
   Future<KeyAttributes> createNewRecoveryKey() async {
   Future<KeyAttributes> createNewRecoveryKey() async {
@@ -265,6 +271,14 @@ class Configuration {
     await _preferences.setString(tokenKey, token);
     await _preferences.setString(tokenKey, token);
   }
   }
 
 
+  Future<void> setEncryptedToken(String encryptedToken) async {
+    await _preferences.setString(encryptedTokenKey, encryptedToken);
+  }
+
+  String getEncryptedToken() {
+    return _preferences.getString(encryptedTokenKey);
+  }
+
   String getEmail() {
   String getEmail() {
     return _preferences.getString(emailKey);
     return _preferences.getString(emailKey);
   }
   }

+ 11 - 9
lib/services/user_service.dart

@@ -79,13 +79,13 @@ class UserService {
     }
     }
   }
   }
 
 
-  Future<void> getCredentials(BuildContext context, String ott) async {
+  Future<void> verifyEmail(BuildContext context, String ott) async {
     final dialog = createProgressDialog(context, "please wait...");
     final dialog = createProgressDialog(context, "please wait...");
     await dialog.show();
     await dialog.show();
     try {
     try {
-      final response = await _dio.get(
-        _config.getHttpEndpoint() + "/users/credentials",
-        queryParameters: {
+      final response = await _dio.post(
+        _config.getHttpEndpoint() + "/users/verify-email",
+        data: {
           "email": _config.getEmail(),
           "email": _config.getEmail(),
           "ott": ott,
           "ott": ott,
         },
         },
@@ -95,7 +95,7 @@ class UserService {
         await _saveConfiguration(response);
         await _saveConfiguration(response);
         showToast("email verification successful!");
         showToast("email verification successful!");
         var page;
         var page;
-        if (Configuration.instance.getKeyAttributes() != null) {
+        if (Configuration.instance.getEncryptedToken() != null) {
           page = PasswordReentryPage();
           page = PasswordReentryPage();
         } else {
         } else {
           page = PasswordEntryPage();
           page = PasswordEntryPage();
@@ -194,11 +194,13 @@ class UserService {
 
 
   Future<void> _saveConfiguration(Response response) async {
   Future<void> _saveConfiguration(Response response) async {
     await Configuration.instance.setUserID(response.data["id"]);
     await Configuration.instance.setUserID(response.data["id"]);
-    await Configuration.instance.setToken(response.data["token"]);
-    final keyAttributes = response.data["keyAttributes"];
-    if (keyAttributes != null) {
+    if (response.data["encryptedToken"] != null) {
       await Configuration.instance
       await Configuration.instance
-          .setKeyAttributes(KeyAttributes.fromMap(keyAttributes));
+          .setEncryptedToken(response.data["encryptedToken"]);
+      await Configuration.instance.setKeyAttributes(
+          KeyAttributes.fromMap(response.data["keyAttributes"]));
+    } else {
+      await Configuration.instance.setToken(response.data["token"]);
     }
     }
   }
   }
 }
 }

+ 1 - 1
lib/ui/home_widget.dart

@@ -270,7 +270,7 @@ class _HomeWidgetState extends State<HomeWidget> {
       return;
       return;
     }
     }
     final ott = Uri.parse(link).queryParameters["ott"];
     final ott = Uri.parse(link).queryParameters["ott"];
-    UserService.instance.getCredentials(context, ott);
+    UserService.instance.verifyEmail(context, ott);
   }
   }
 
 
   Widget _getMainGalleryWidget() {
   Widget _getMainGalleryWidget() {

+ 2 - 2
lib/ui/landing_page_widget.dart

@@ -195,7 +195,7 @@ class _LandingPageWidgetState extends State<LandingPageWidget> {
 
 
   void _navigateToSignUpPage() {
   void _navigateToSignUpPage() {
     var page;
     var page;
-    if (Configuration.instance.getToken() == null) {
+    if (Configuration.instance.getEncryptedToken() == null) {
       page = EmailEntryPage();
       page = EmailEntryPage();
     } else {
     } else {
       // No key
       // No key
@@ -221,7 +221,7 @@ class _LandingPageWidgetState extends State<LandingPageWidget> {
 
 
   void _navigateToSignInPage() {
   void _navigateToSignInPage() {
     var page;
     var page;
-    if (Configuration.instance.getToken() == null) {
+    if (Configuration.instance.getEncryptedToken() == null) {
       page = LoginPage();
       page = LoginPage();
     } else {
     } else {
       // No key
       // No key

+ 1 - 1
lib/ui/ott_verification_page.dart

@@ -86,7 +86,7 @@ class _OTTVerificationPageState extends State<OTTVerificationPage> {
                           _verificationCodeController.text.isEmpty
                           _verificationCodeController.text.isEmpty
                       ? null
                       ? null
                       : () {
                       : () {
-                          UserService.instance.getCredentials(
+                          UserService.instance.verifyEmail(
                               context, _verificationCodeController.text);
                               context, _verificationCodeController.text);
                         },
                         },
                   fontSize: 18,
                   fontSize: 18,

+ 1 - 1
lib/ui/password_reentry_page.dart

@@ -100,7 +100,7 @@ class _PasswordReentryPageState extends State<PasswordReentryPage> {
                         createProgressDialog(context, "please wait...");
                         createProgressDialog(context, "please wait...");
                     await dialog.show();
                     await dialog.show();
                     try {
                     try {
-                      await Configuration.instance.decryptAndSaveKey(
+                      await Configuration.instance.decryptAndSaveSecrets(
                           _passwordController.text,
                           _passwordController.text,
                           Configuration.instance.getKeyAttributes());
                           Configuration.instance.getKeyAttributes());
                     } catch (e) {
                     } catch (e) {