use react native keystore instead of expo secure store

This commit is contained in:
Arnaud Vergnet 2020-04-27 22:55:24 +02:00
parent 2b620fdc2d
commit 9980e78918
4 changed files with 22 additions and 53 deletions

View file

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

View file

@ -1,7 +1,8 @@
jest.mock('react-native-keychain');
import React from 'react'; import React from 'react';
import ConnectionManager from "../../src/managers/ConnectionManager"; import ConnectionManager from "../../src/managers/ConnectionManager";
import {ERROR_TYPE} from "../../src/utils/WebData"; import {ERROR_TYPE} from "../../src/utils/WebData";
import * as SecureStore from 'expo-secure-store';
let fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native let fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native
@ -25,45 +26,6 @@ test('isLoggedIn no', () => {
return expect(c.isLoggedIn()).toBe(false); return expect(c.isLoggedIn()).toBe(false);
}); });
test('recoverLogin error crypto', () => {
jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
return Promise.reject();
});
return expect(c.recoverLogin()).rejects.toBe(false);
});
test('recoverLogin success crypto', () => {
jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
return Promise.resolve('token1');
});
return expect(c.recoverLogin()).resolves.toBe('token1');
});
test('saveLogin success', () => {
jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => {
return Promise.resolve();
});
return expect(c.saveLogin('email', 'token2')).resolves.toBeTruthy();
});
test('saveLogin error', () => {
jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => {
return Promise.reject();
});
return expect(c.saveLogin('email', 'token3')).rejects.toBeFalsy();
});
test('recoverLogin error crypto with saved token', () => {
jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
return Promise.reject();
});
return expect(c.recoverLogin()).resolves.toBe('token2');
});
test('recoverLogin success saved', () => {
return expect(c.recoverLogin()).resolves.toBe('token2');
});
test("isConnectionResponseValid", () => { test("isConnectionResponseValid", () => {
let json = { let json = {
error: 0, error: 0,

View file

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

View file

@ -1,6 +1,6 @@
// @flow // @flow
import * as SecureStore from 'expo-secure-store'; import * as Keychain from 'react-native-keychain';
import {apiRequest, ERROR_TYPE, isResponseValid} from "../utils/WebData"; import {apiRequest, ERROR_TYPE, isResponseValid} from "../utils/WebData";
/** /**
@ -14,7 +14,7 @@ import {apiRequest, ERROR_TYPE, isResponseValid} from "../utils/WebData";
* 500 : SERVER_ERROR -> pb coté serveur * 500 : SERVER_ERROR -> pb coté serveur
*/ */
const SERVER_NAME = "amicale-insat.fr";
const AUTH_PATH = "password"; const AUTH_PATH = "password";
export default class ConnectionManager { export default class ConnectionManager {
@ -60,16 +60,16 @@ export default class ConnectionManager {
if (this.getToken() !== null) if (this.getToken() !== null)
resolve(this.getToken()); resolve(this.getToken());
else { else {
SecureStore.getItemAsync('token') Keychain.getInternetCredentials(SERVER_NAME)
.then((token) => { .then((data) => {
this.#token = token; if (data) {
if (token !== null) { this.#token = data.password;
this.onLoginStateChange(true); this.onLoginStateChange(true);
resolve(token); resolve(this.#token);
} else } else
reject(false); reject(false);
}) })
.catch(error => { .catch(() => {
reject(false); reject(false);
}); });
} }
@ -82,14 +82,14 @@ export default class ConnectionManager {
async saveLogin(email: string, token: string) { async saveLogin(email: string, token: string) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
SecureStore.setItemAsync('token', token) Keychain.setInternetCredentials(SERVER_NAME, 'token', token)
.then(() => { .then(() => {
this.#token = token; this.#token = token;
this.#email = email; this.#email = email;
this.onLoginStateChange(true); this.onLoginStateChange(true);
resolve(true); resolve(true);
}) })
.catch(error => { .catch(() => {
reject(false); reject(false);
}); });
}); });
@ -97,13 +97,13 @@ export default class ConnectionManager {
async disconnect() { async disconnect() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
SecureStore.deleteItemAsync('token') Keychain.resetInternetCredentials(SERVER_NAME)
.then(() => { .then(() => {
this.#token = null; this.#token = null;
this.onLoginStateChange(false); this.onLoginStateChange(false);
resolve(true); resolve(true);
}) })
.catch((error) => { .catch(() => {
reject(false); reject(false);
}); });
}); });