Browse Source

use react native keystore instead of expo secure store

Arnaud Vergnet 1 year ago
parent
commit
9980e78918

+ 7
- 0
__mocks__/react-native-keychain/index.js View File

@@ -0,0 +1,7 @@
1
+const keychainMock = {
2
+    SECURITY_LEVEL_ANY: "MOCK_SECURITY_LEVEL_ANY",
3
+    SECURITY_LEVEL_SECURE_SOFTWARE: "MOCK_SECURITY_LEVEL_SECURE_SOFTWARE",
4
+    SECURITY_LEVEL_SECURE_HARDWARE: "MOCK_SECURITY_LEVEL_SECURE_HARDWARE",
5
+}
6
+
7
+export default keychainMock;

+ 2
- 40
__tests__/managers/ConnectionManager.test.js View File

@@ -1,7 +1,8 @@
1
+jest.mock('react-native-keychain');
2
+
1 3
 import React from 'react';
2 4
 import ConnectionManager from "../../src/managers/ConnectionManager";
3 5
 import {ERROR_TYPE} from "../../src/utils/WebData";
4
-import * as SecureStore from 'expo-secure-store';
5 6
 
6 7
 let fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native
7 8
 
@@ -25,45 +26,6 @@ test('isLoggedIn no', () => {
25 26
     return expect(c.isLoggedIn()).toBe(false);
26 27
 });
27 28
 
28
-test('recoverLogin error crypto', () => {
29
-    jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
30
-        return Promise.reject();
31
-    });
32
-    return expect(c.recoverLogin()).rejects.toBe(false);
33
-});
34
-
35
-test('recoverLogin success crypto', () => {
36
-    jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
37
-        return Promise.resolve('token1');
38
-    });
39
-    return expect(c.recoverLogin()).resolves.toBe('token1');
40
-});
41
-
42
-test('saveLogin success', () => {
43
-    jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => {
44
-        return Promise.resolve();
45
-    });
46
-    return expect(c.saveLogin('email', 'token2')).resolves.toBeTruthy();
47
-});
48
-
49
-test('saveLogin error', () => {
50
-    jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => {
51
-        return Promise.reject();
52
-    });
53
-    return expect(c.saveLogin('email', 'token3')).rejects.toBeFalsy();
54
-});
55
-
56
-test('recoverLogin error crypto with saved token', () => {
57
-    jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
58
-        return Promise.reject();
59
-    });
60
-    return expect(c.recoverLogin()).resolves.toBe('token2');
61
-});
62
-
63
-test('recoverLogin success saved', () => {
64
-    return expect(c.recoverLogin()).resolves.toBe('token2');
65
-});
66
-
67 29
 test("isConnectionResponseValid", () => {
68 30
     let json = {
69 31
         error: 0,

+ 1
- 1
package.json View File

@@ -27,7 +27,6 @@
27 27
     "expo": "^37.0.0",
28 28
     "expo-barcode-scanner": "~8.1.0",
29 29
     "expo-camera": "latest",
30
-    "expo-secure-store": "~8.1.0",
31 30
     "i18n-js": "^3.3.0",
32 31
     "react": "~16.9.0",
33 32
     "react-dom": "16.9.0",
@@ -40,6 +39,7 @@
40 39
     "react-native-collapsible": "^1.5.2",
41 40
     "react-native-gesture-handler": "~1.6.0",
42 41
     "react-native-image-modal": "^1.0.6",
42
+    "react-native-keychain": "^6.0.0",
43 43
     "react-native-linear-gradient": "^2.5.6",
44 44
     "react-native-localize": "^1.4.0",
45 45
     "react-native-modalize": "^1.3.6",

+ 12
- 12
src/managers/ConnectionManager.js View File

@@ -1,6 +1,6 @@
1 1
 // @flow
2 2
 
3
-import * as SecureStore from 'expo-secure-store';
3
+import * as Keychain from 'react-native-keychain';
4 4
 import {apiRequest, ERROR_TYPE, isResponseValid} from "../utils/WebData";
5 5
 
6 6
 /**
@@ -14,7 +14,7 @@ import {apiRequest, ERROR_TYPE, isResponseValid} from "../utils/WebData";
14 14
  * 500 : SERVER_ERROR -> pb coté serveur
15 15
  */
16 16
 
17
-
17
+const SERVER_NAME = "amicale-insat.fr";
18 18
 const AUTH_PATH = "password";
19 19
 
20 20
 export default class ConnectionManager {
@@ -60,16 +60,16 @@ export default class ConnectionManager {
60 60
             if (this.getToken() !== null)
61 61
                 resolve(this.getToken());
62 62
             else {
63
-                SecureStore.getItemAsync('token')
64
-                    .then((token) => {
65
-                        this.#token = token;
66
-                        if (token !== null) {
63
+                Keychain.getInternetCredentials(SERVER_NAME)
64
+                    .then((data) => {
65
+                        if (data) {
66
+                            this.#token = data.password;
67 67
                             this.onLoginStateChange(true);
68
-                            resolve(token);
68
+                            resolve(this.#token);
69 69
                         } else
70 70
                             reject(false);
71 71
                     })
72
-                    .catch(error => {
72
+                    .catch(() => {
73 73
                         reject(false);
74 74
                     });
75 75
             }
@@ -82,14 +82,14 @@ export default class ConnectionManager {
82 82
 
83 83
     async saveLogin(email: string, token: string) {
84 84
         return new Promise((resolve, reject) => {
85
-            SecureStore.setItemAsync('token', token)
85
+            Keychain.setInternetCredentials(SERVER_NAME, 'token', token)
86 86
                 .then(() => {
87 87
                     this.#token = token;
88 88
                     this.#email = email;
89 89
                     this.onLoginStateChange(true);
90 90
                     resolve(true);
91 91
                 })
92
-                .catch(error => {
92
+                .catch(() => {
93 93
                     reject(false);
94 94
                 });
95 95
         });
@@ -97,13 +97,13 @@ export default class ConnectionManager {
97 97
 
98 98
     async disconnect() {
99 99
         return new Promise((resolve, reject) => {
100
-            SecureStore.deleteItemAsync('token')
100
+            Keychain.resetInternetCredentials(SERVER_NAME)
101 101
                 .then(() => {
102 102
                     this.#token = null;
103 103
                     this.onLoginStateChange(false);
104 104
                     resolve(true);
105 105
                 })
106
-                .catch((error) => {
106
+                .catch(() => {
107 107
                     reject(false);
108 108
                 });
109 109
         });

Loading…
Cancel
Save