cozy-keys-lib 3.9.1 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/WebVaultClient.js +75 -10
- package/src/WebVaultClient.spec.js +23 -0
- package/src/components/VaultContext.jsx +48 -29
- package/src/components/VaultContext.spec.jsx +14 -8
- package/src/components/VaultUnlocker.jsx +9 -8
- package/src/components/defaults.js +12 -6
- package/src/components/defaults.spec.js +34 -0
- package/src/components/vaultUnlockContext.jsx +6 -16
- package/src/components/vaultUnlockContext.spec.jsx +0 -17
- package/src/index.js +1 -0
- package/transpiled/WebVaultClient.js +480 -283
- package/transpiled/WebVaultClient.spec.js +36 -0
- package/transpiled/components/VaultContext.js +55 -28
- package/transpiled/components/VaultContext.spec.js +9 -9
- package/transpiled/components/VaultUnlocker.js +6 -8
- package/transpiled/components/defaults.js +23 -11
- package/transpiled/components/defaults.spec.js +94 -0
- package/transpiled/components/vaultUnlockContext.js +3 -8
- package/transpiled/components/vaultUnlockContext.spec.js +8 -48
- package/transpiled/index.js +2 -1
package/package.json
CHANGED
package/src/WebVaultClient.js
CHANGED
|
@@ -11,6 +11,7 @@ import { ApiService } from './@bitwarden/jslib/services/api.service'
|
|
|
11
11
|
import { AppIdService } from './@bitwarden/jslib/services/appId.service'
|
|
12
12
|
import { AuthService } from './@bitwarden/jslib/services/auth.service'
|
|
13
13
|
import { CipherService } from './@bitwarden/jslib/services/cipher.service'
|
|
14
|
+
|
|
14
15
|
import { CollectionService } from './@bitwarden/jslib/services/collection.service'
|
|
15
16
|
import { ContainerService } from './@bitwarden/jslib/services/container.service'
|
|
16
17
|
import { CryptoService } from './@bitwarden/jslib/services/crypto.service'
|
|
@@ -36,6 +37,9 @@ import { KdfType } from './@bitwarden/jslib/enums/kdfType'
|
|
|
36
37
|
|
|
37
38
|
import { ImportCiphersRequest } from './@bitwarden/jslib/models/request/importCiphersRequest'
|
|
38
39
|
import { CipherRequest } from './@bitwarden/jslib/models/request/cipherRequest'
|
|
40
|
+
import { CipherString } from './@bitwarden/jslib/models/domain/cipherString'
|
|
41
|
+
import { SymmetricCryptoKey } from './@bitwarden/jslib/models/domain/symmetricCryptoKey'
|
|
42
|
+
|
|
39
43
|
import { KdfRequest } from './@bitwarden/jslib/models/request/kdfRequest'
|
|
40
44
|
|
|
41
45
|
import WebPlatformUtilsService from './WebPlatformUtilsService'
|
|
@@ -501,6 +505,23 @@ class WebVaultClient {
|
|
|
501
505
|
return await this.cryptoService.getKey()
|
|
502
506
|
}
|
|
503
507
|
|
|
508
|
+
/**
|
|
509
|
+
* @typedef EncryptionKey
|
|
510
|
+
* @property {SymmetricCryptoKey} key - The encryption key
|
|
511
|
+
* @property {CipherString} encryptedKey - The encrypted key
|
|
512
|
+
*
|
|
513
|
+
* Generate a new encryption key
|
|
514
|
+
* @return {EncryptionKey} the generated key and its encrypted version
|
|
515
|
+
*/
|
|
516
|
+
async generateEncryptionKey() {
|
|
517
|
+
const encKey = await this.getEncryptionKey()
|
|
518
|
+
if (!encKey) {
|
|
519
|
+
throw new Error('NO_ENCRYPTION_KEY')
|
|
520
|
+
}
|
|
521
|
+
const [key, encryptedKey] = await this.cryptoService.makeEncKey(encKey)
|
|
522
|
+
return { key, encryptedKey }
|
|
523
|
+
}
|
|
524
|
+
|
|
504
525
|
/**
|
|
505
526
|
* Manually encrypt the encryption key
|
|
506
527
|
* Usefull for a password change, with a new master key
|
|
@@ -582,16 +603,6 @@ class WebVaultClient {
|
|
|
582
603
|
return this.getAll({ type: CipherType.Login })
|
|
583
604
|
}
|
|
584
605
|
|
|
585
|
-
/**
|
|
586
|
-
* Decrypt a cipher
|
|
587
|
-
* @param {Cipher} cipher
|
|
588
|
-
* @return {CipherView} decrypted cipher
|
|
589
|
-
*/
|
|
590
|
-
async decrypt(cipher) {
|
|
591
|
-
this.attachToGlobal()
|
|
592
|
-
return cipher.decrypt()
|
|
593
|
-
}
|
|
594
|
-
|
|
595
606
|
/**
|
|
596
607
|
* Get all data from the local vault and decrypt them
|
|
597
608
|
*
|
|
@@ -734,6 +745,60 @@ class WebVaultClient {
|
|
|
734
745
|
return this.cipherService.encrypt(cipherView)
|
|
735
746
|
}
|
|
736
747
|
|
|
748
|
+
/**
|
|
749
|
+
* Decrypt a cipher
|
|
750
|
+
* @param {Cipher} cipher
|
|
751
|
+
* @return {CipherView} decrypted cipher
|
|
752
|
+
*/
|
|
753
|
+
async decrypt(cipher) {
|
|
754
|
+
this.attachToGlobal()
|
|
755
|
+
return cipher.decrypt()
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* Decrypt an encryption key, encrypted with the vault encryption key
|
|
760
|
+
* @param {string} encryptedKey - The string-encoded encrypted key
|
|
761
|
+
* @return {SymmetricCryptoKey} The encryption key
|
|
762
|
+
*/
|
|
763
|
+
async decryptEncryptionKey(encryptedKey) {
|
|
764
|
+
const encKey = await this.getEncryptionKey()
|
|
765
|
+
if (!encKey) {
|
|
766
|
+
throw new Error('NO_ENCRYPTION_KEY')
|
|
767
|
+
}
|
|
768
|
+
const cipher = new CipherString(encryptedKey)
|
|
769
|
+
const decryptedBuffer = await this.cryptoService.decryptToBytes(
|
|
770
|
+
cipher,
|
|
771
|
+
encKey
|
|
772
|
+
)
|
|
773
|
+
return new SymmetricCryptoKey(decryptedBuffer)
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* Encrypt a file
|
|
778
|
+
* @param {ArrayBuffer} file - file to encrypt
|
|
779
|
+
* @param {string} encryptedKey - the encrypted encryption key
|
|
780
|
+
* @return {ArrayBuffer} encrypted file
|
|
781
|
+
*/
|
|
782
|
+
async encryptFile(file, encryptedKey) {
|
|
783
|
+
this.attachToGlobal()
|
|
784
|
+
|
|
785
|
+
const encryptionKey = await this.decryptEncryptionKey(encryptedKey)
|
|
786
|
+
return this.cryptoService.encryptToBytes(file, encryptionKey)
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* Decrypt a file
|
|
791
|
+
* @param {ArrayBuffer} encryptedFile - encrypted file
|
|
792
|
+
* @param {string} encryptedKey - the encrypted decryption key
|
|
793
|
+
* @return {ArrayBuffer} decrypted file
|
|
794
|
+
*/
|
|
795
|
+
async decryptFile(encryptedFile, encryptedKey) {
|
|
796
|
+
this.attachToGlobal()
|
|
797
|
+
|
|
798
|
+
const decryptionKey = await this.decryptEncryptionKey(encryptedKey)
|
|
799
|
+
return this.cryptoService.decryptFromBytes(encryptedFile, decryptionKey)
|
|
800
|
+
}
|
|
801
|
+
|
|
737
802
|
/**
|
|
738
803
|
* Save a new or modified (encrypted) cipher to the server
|
|
739
804
|
* @param {Cipher} - cipher to save
|
|
@@ -3,6 +3,7 @@ import { Utils } from './@bitwarden/jslib/misc/utils'
|
|
|
3
3
|
import fs from 'fs'
|
|
4
4
|
import path from 'path'
|
|
5
5
|
import range from 'lodash/range'
|
|
6
|
+
import { SymmetricCryptoKey } from './@bitwarden/jslib/models/domain/symmetricCryptoKey'
|
|
6
7
|
|
|
7
8
|
jest.spyOn(Utils, 'init').mockImplementation(() => {})
|
|
8
9
|
|
|
@@ -348,4 +349,26 @@ describe('WebVaultClient', () => {
|
|
|
348
349
|
)
|
|
349
350
|
})
|
|
350
351
|
})
|
|
352
|
+
|
|
353
|
+
describe('decryptEncryptionKey', () => {
|
|
354
|
+
const client = new WebVaultClient('https://me.cozy.wtf')
|
|
355
|
+
jest
|
|
356
|
+
.spyOn(client.cryptoService, 'decryptToBytes')
|
|
357
|
+
.mockResolvedValue(new ArrayBuffer(64))
|
|
358
|
+
jest
|
|
359
|
+
.spyOn(client, 'getEncryptionKey')
|
|
360
|
+
.mockResolvedValue(new SymmetricCryptoKey(new ArrayBuffer(64)))
|
|
361
|
+
|
|
362
|
+
afterEach(() => {
|
|
363
|
+
jest.clearAllMocks()
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
it('should decrypt the given key with correct length', async () => {
|
|
367
|
+
const encryptedKey = 'xyz'
|
|
368
|
+
const key = await client.decryptEncryptionKey(encryptedKey)
|
|
369
|
+
expect(key.key.byteLength).toBe(64)
|
|
370
|
+
expect(key.encType).toBe(2)
|
|
371
|
+
expect(key.encKey.byteLength).toBe(32)
|
|
372
|
+
})
|
|
373
|
+
})
|
|
351
374
|
})
|
|
@@ -14,8 +14,16 @@ class VaultProvider extends React.Component {
|
|
|
14
14
|
constructor(props) {
|
|
15
15
|
super(props)
|
|
16
16
|
|
|
17
|
+
let vaultClient = this.props.vaultClient
|
|
18
|
+
if (this.props.client) {
|
|
19
|
+
vaultClient = this.props.client
|
|
20
|
+
// eslint-disable-next-line no-console
|
|
21
|
+
console.warn(
|
|
22
|
+
'Passing client in props is deprecated, please use vaultClient instead.'
|
|
23
|
+
)
|
|
24
|
+
}
|
|
17
25
|
this.state = {
|
|
18
|
-
|
|
26
|
+
vaultClient: vaultClient,
|
|
19
27
|
locked: true
|
|
20
28
|
}
|
|
21
29
|
|
|
@@ -24,77 +32,88 @@ class VaultProvider extends React.Component {
|
|
|
24
32
|
}
|
|
25
33
|
|
|
26
34
|
componentDidMount() {
|
|
27
|
-
|
|
28
|
-
|
|
35
|
+
const { cozyClient, instance } = this.props
|
|
36
|
+
if (cozyClient) {
|
|
37
|
+
const uri = cozyClient.getStackClient().uri
|
|
38
|
+
if (!uri) {
|
|
39
|
+
cozyClient.on('login', () => {
|
|
40
|
+
this.setupClient(cozyClient.getStackClient().uri)
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (instance) {
|
|
45
|
+
this.setupClient(this.props.instance)
|
|
29
46
|
}
|
|
30
47
|
}
|
|
31
48
|
|
|
32
49
|
componentWillUnmount() {
|
|
33
|
-
const {
|
|
34
|
-
if (!
|
|
50
|
+
const { vaultClient } = this.state
|
|
51
|
+
if (!vaultClient) {
|
|
35
52
|
return
|
|
36
53
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
54
|
+
vaultClient.removeListener('unlock', this.updateLockedState)
|
|
55
|
+
vaultClient.removeListener('lock', this.updateLockedState)
|
|
56
|
+
vaultClient.removeListener('login', this.updateLockedState)
|
|
40
57
|
}
|
|
41
58
|
|
|
42
59
|
async updateLockedState() {
|
|
43
|
-
const {
|
|
44
|
-
const locked = await
|
|
60
|
+
const { vaultClient } = this.state
|
|
61
|
+
const locked = await vaultClient.isLocked()
|
|
45
62
|
if (locked != this.state.locked) {
|
|
46
63
|
this.setState({ locked })
|
|
47
64
|
}
|
|
48
65
|
}
|
|
49
66
|
|
|
50
|
-
setupClient() {
|
|
67
|
+
setupClient(instance) {
|
|
51
68
|
const unsafeStorage = this.props.unsafeStorage
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
this.props.
|
|
55
|
-
unsafeStorage,
|
|
56
|
-
this.props.vaultData
|
|
57
|
-
)
|
|
69
|
+
const vaultClient =
|
|
70
|
+
this.props.vaultClient ||
|
|
71
|
+
this.props.client ||
|
|
72
|
+
getVaultClient(instance, unsafeStorage, this.props.vaultData)
|
|
58
73
|
|
|
59
74
|
this.setState(
|
|
60
75
|
{
|
|
61
|
-
|
|
76
|
+
vaultClient,
|
|
62
77
|
locked: true
|
|
63
78
|
},
|
|
64
79
|
() => {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
80
|
+
vaultClient.on('unlock', this.updateLockedState)
|
|
81
|
+
vaultClient.on('lock', this.updateLockedState)
|
|
82
|
+
vaultClient.on('login', this.updateLockedState)
|
|
68
83
|
|
|
69
84
|
this.updateLockedState()
|
|
70
85
|
}
|
|
71
86
|
)
|
|
72
87
|
|
|
73
88
|
if (this.props.setClient) {
|
|
74
|
-
this.props.setClient(
|
|
89
|
+
this.props.setClient(vaultClient)
|
|
75
90
|
}
|
|
76
91
|
}
|
|
77
92
|
|
|
78
93
|
render() {
|
|
79
|
-
|
|
80
|
-
return client ? (
|
|
94
|
+
return (
|
|
81
95
|
<VaultContext.Provider value={this.state}>
|
|
82
96
|
{this.props.children}
|
|
83
97
|
</VaultContext.Provider>
|
|
84
|
-
)
|
|
98
|
+
)
|
|
85
99
|
}
|
|
86
100
|
}
|
|
87
101
|
|
|
88
102
|
VaultProvider.propTypes = {
|
|
89
|
-
instance: PropTypes.string
|
|
103
|
+
instance: PropTypes.string,
|
|
104
|
+
vaultClient: PropTypes.object,
|
|
105
|
+
client: PropTypes.object, // deprecated
|
|
90
106
|
unsafeStorage: PropTypes.bool,
|
|
91
|
-
setClient: PropTypes.func
|
|
107
|
+
setClient: PropTypes.func,
|
|
108
|
+
cozyClient: PropTypes.object
|
|
92
109
|
}
|
|
93
110
|
|
|
94
111
|
const withVaultClient = BaseComponent => {
|
|
95
112
|
const Component = props => (
|
|
96
113
|
<VaultContext.Consumer>
|
|
97
|
-
{({
|
|
114
|
+
{({ vaultClient }) => (
|
|
115
|
+
<BaseComponent vaultClient={vaultClient} {...props} />
|
|
116
|
+
)}
|
|
98
117
|
</VaultContext.Consumer>
|
|
99
118
|
)
|
|
100
119
|
|
|
@@ -106,7 +125,7 @@ const withVaultClient = BaseComponent => {
|
|
|
106
125
|
|
|
107
126
|
const useVaultClient = () => {
|
|
108
127
|
const ctx = useContext(VaultContext)
|
|
109
|
-
return ctx.
|
|
128
|
+
return ctx.vaultClient
|
|
110
129
|
}
|
|
111
130
|
|
|
112
131
|
export { VaultContext, VaultProvider, withVaultClient, useVaultClient }
|
|
@@ -21,26 +21,32 @@ describe('VaultProvider', () => {
|
|
|
21
21
|
</VaultProvider>
|
|
22
22
|
)
|
|
23
23
|
|
|
24
|
-
const
|
|
24
|
+
const vaultClient = component.state('vaultClient')
|
|
25
25
|
|
|
26
|
-
expect(ChildComponent).toHaveBeenCalledWith({
|
|
26
|
+
expect(ChildComponent).toHaveBeenCalledWith({ vaultClient, locked: true })
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
vaultClient.unlock()
|
|
29
29
|
|
|
30
30
|
await new Promise(resolve =>
|
|
31
31
|
setTimeout(() => {
|
|
32
|
-
expect(ChildComponent).toHaveBeenCalledTimes(
|
|
33
|
-
expect(ChildComponent).toHaveBeenCalledWith({
|
|
32
|
+
expect(ChildComponent).toHaveBeenCalledTimes(3)
|
|
33
|
+
expect(ChildComponent).toHaveBeenCalledWith({
|
|
34
|
+
vaultClient,
|
|
35
|
+
locked: false
|
|
36
|
+
})
|
|
34
37
|
resolve()
|
|
35
38
|
})
|
|
36
39
|
)
|
|
37
40
|
|
|
38
|
-
|
|
41
|
+
vaultClient.lock()
|
|
39
42
|
|
|
40
43
|
await new Promise(resolve =>
|
|
41
44
|
setTimeout(() => {
|
|
42
|
-
expect(ChildComponent).toHaveBeenCalledTimes(
|
|
43
|
-
expect(ChildComponent).toHaveBeenCalledWith({
|
|
45
|
+
expect(ChildComponent).toHaveBeenCalledTimes(4)
|
|
46
|
+
expect(ChildComponent).toHaveBeenCalledWith({
|
|
47
|
+
vaultClient,
|
|
48
|
+
locked: true
|
|
49
|
+
})
|
|
44
50
|
resolve()
|
|
45
51
|
})
|
|
46
52
|
)
|
|
@@ -4,7 +4,6 @@ import PropTypes from 'prop-types'
|
|
|
4
4
|
import Spinner from 'cozy-ui/transpiled/react/Spinner'
|
|
5
5
|
import Overlay from 'cozy-ui/transpiled/react/Overlay'
|
|
6
6
|
import withLocales from 'cozy-ui/transpiled/react/I18n/withLocales'
|
|
7
|
-
import { useClient } from 'cozy-client'
|
|
8
7
|
|
|
9
8
|
import localesEn from '../locales/en.json'
|
|
10
9
|
import localesFr from '../locales/fr.json'
|
|
@@ -24,10 +23,9 @@ const VaultUnlocker = ({
|
|
|
24
23
|
closable,
|
|
25
24
|
onUnlock,
|
|
26
25
|
UnlockForm,
|
|
27
|
-
|
|
26
|
+
addCheckShouldUnlock
|
|
28
27
|
}) => {
|
|
29
|
-
const
|
|
30
|
-
const { locked, client: vaultClient } = useContext(VaultContext)
|
|
28
|
+
const { locked, vaultClient } = useContext(VaultContext)
|
|
31
29
|
|
|
32
30
|
const [showSpinner, setShowSpinner] = useState(false)
|
|
33
31
|
const [isChecking, setIsChecking] = useState(true)
|
|
@@ -42,7 +40,10 @@ const VaultUnlocker = ({
|
|
|
42
40
|
|
|
43
41
|
useEffect(() => {
|
|
44
42
|
const doCheckShouldUnlock = async () => {
|
|
45
|
-
const shouldUnlock = await checkShouldUnlock(
|
|
43
|
+
const shouldUnlock = await checkShouldUnlock(
|
|
44
|
+
vaultClient,
|
|
45
|
+
addCheckShouldUnlock
|
|
46
|
+
)
|
|
46
47
|
|
|
47
48
|
setShouldUnlock(shouldUnlock)
|
|
48
49
|
setIsChecking(false)
|
|
@@ -75,12 +76,12 @@ const VaultUnlocker = ({
|
|
|
75
76
|
VaultUnlocker.propTypes = {
|
|
76
77
|
onDismiss: PropTypes.func.isRequired,
|
|
77
78
|
closable: PropTypes.bool,
|
|
78
|
-
onUnlock: PropTypes.func.isRequired
|
|
79
|
+
onUnlock: PropTypes.func.isRequired,
|
|
80
|
+
addCheckShouldUnlock: PropTypes.func
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
VaultUnlocker.defaultProps = {
|
|
82
|
-
UnlockForm
|
|
83
|
-
checkShouldUnlock: checkShouldUnlock
|
|
84
|
+
UnlockForm
|
|
84
85
|
}
|
|
85
86
|
|
|
86
87
|
export default withLocales(locales)(VaultUnlocker)
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Check whether or not the vault should be unlocked
|
|
3
|
+
*
|
|
4
|
+
* @param {object} vaultClient - The vault client
|
|
5
|
+
* @param {function} addCheckShouldUnlock - An additional check method to unlock
|
|
6
|
+
* @returns {boolean} True if the vault should be unlocked
|
|
7
|
+
*/
|
|
8
|
+
export const checkShouldUnlock = async (
|
|
9
|
+
vaultClient,
|
|
10
|
+
addCheckShouldUnlock = () => true
|
|
11
|
+
) => {
|
|
12
|
+
return (await addCheckShouldUnlock()) && (await vaultClient.isLocked())
|
|
7
13
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { checkShouldUnlock } from './defaults'
|
|
2
|
+
import WebVaultClient from '../WebVaultClient'
|
|
3
|
+
|
|
4
|
+
jest.mock('../CozyUtils', () => ({
|
|
5
|
+
checkHasInstalledExtension: jest.fn(),
|
|
6
|
+
getEmail: jest.fn().mockReturnValue('me@test.fr'),
|
|
7
|
+
isInstance: jest.fn().mockReturnValue(true)
|
|
8
|
+
}))
|
|
9
|
+
|
|
10
|
+
describe('checkShouldUnlock', () => {
|
|
11
|
+
const vaultClient = new WebVaultClient('https://me.cozy.wtf')
|
|
12
|
+
|
|
13
|
+
it('should return true when the vault is locked', async () => {
|
|
14
|
+
jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(true)
|
|
15
|
+
const shouldUnlock = await checkShouldUnlock(vaultClient)
|
|
16
|
+
expect(shouldUnlock).toBe(true)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should return false when the vault is already unlocked', async () => {
|
|
20
|
+
jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(false)
|
|
21
|
+
const shouldUnlock = await checkShouldUnlock(vaultClient)
|
|
22
|
+
expect(shouldUnlock).toBe(false)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('should return false when additional check function return false', async () => {
|
|
26
|
+
jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(true)
|
|
27
|
+
const additionalUnlockCheck = () => false
|
|
28
|
+
const shouldUnlock = await checkShouldUnlock(
|
|
29
|
+
vaultClient,
|
|
30
|
+
additionalUnlockCheck
|
|
31
|
+
)
|
|
32
|
+
expect(shouldUnlock).toBe(false)
|
|
33
|
+
})
|
|
34
|
+
})
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import React, { useMemo, useState, useContext } from 'react'
|
|
2
|
-
import { useClient } from 'cozy-client'
|
|
3
|
-
|
|
4
2
|
import { useVaultClient } from './VaultContext'
|
|
5
3
|
import { checkShouldUnlock } from './defaults'
|
|
6
4
|
|
|
@@ -42,8 +40,7 @@ export const useVaultUnlockContext = () => {
|
|
|
42
40
|
* </VaultProvider>
|
|
43
41
|
* ```
|
|
44
42
|
*/
|
|
45
|
-
export const VaultUnlockProvider = ({ children,
|
|
46
|
-
const client = useClient()
|
|
43
|
+
export const VaultUnlockProvider = ({ children, addCheckShouldUnlock }) => {
|
|
47
44
|
const vaultClient = useVaultClient()
|
|
48
45
|
const [showingUnlockForm, setShowingUnlockForm] = useState(false)
|
|
49
46
|
const [unlockFormProps, setUnlockFormProps] = useState(null)
|
|
@@ -64,7 +61,10 @@ export const VaultUnlockProvider = ({ children, checkShouldUnlock }) => {
|
|
|
64
61
|
unlockFormProps.onDismiss && unlockFormProps.onDismiss()
|
|
65
62
|
}
|
|
66
63
|
|
|
67
|
-
const shouldUnlock = await checkShouldUnlock(
|
|
64
|
+
const shouldUnlock = await checkShouldUnlock(
|
|
65
|
+
vaultClient,
|
|
66
|
+
addCheckShouldUnlock
|
|
67
|
+
)
|
|
68
68
|
|
|
69
69
|
if (shouldUnlock) {
|
|
70
70
|
setUnlockFormProps({
|
|
@@ -84,13 +84,7 @@ export const VaultUnlockProvider = ({ children, checkShouldUnlock }) => {
|
|
|
84
84
|
unlockFormProps,
|
|
85
85
|
vaultClient
|
|
86
86
|
}
|
|
87
|
-
}, [
|
|
88
|
-
showingUnlockForm,
|
|
89
|
-
unlockFormProps,
|
|
90
|
-
vaultClient,
|
|
91
|
-
checkShouldUnlock,
|
|
92
|
-
client
|
|
93
|
-
])
|
|
87
|
+
}, [showingUnlockForm, unlockFormProps, vaultClient, addCheckShouldUnlock])
|
|
94
88
|
|
|
95
89
|
return (
|
|
96
90
|
<vaultUnlockContext.Provider value={value}>
|
|
@@ -99,10 +93,6 @@ export const VaultUnlockProvider = ({ children, checkShouldUnlock }) => {
|
|
|
99
93
|
)
|
|
100
94
|
}
|
|
101
95
|
|
|
102
|
-
VaultUnlockProvider.defaultProps = {
|
|
103
|
-
checkShouldUnlock: checkShouldUnlock
|
|
104
|
-
}
|
|
105
|
-
|
|
106
96
|
export const withVaultUnlockContext = Component => {
|
|
107
97
|
const Wrapper = props => {
|
|
108
98
|
const vaultUnlockContextValue = useVaultUnlockContext()
|
|
@@ -4,17 +4,12 @@ import {
|
|
|
4
4
|
VaultUnlockProvider,
|
|
5
5
|
useVaultUnlockContext
|
|
6
6
|
} from './vaultUnlockContext'
|
|
7
|
-
import { checkHasInstalledExtension } from '../CozyUtils'
|
|
8
7
|
import { useVaultClient } from './VaultContext'
|
|
9
8
|
|
|
10
9
|
jest.mock('./VaultContext', () => ({
|
|
11
10
|
useVaultClient: jest.fn()
|
|
12
11
|
}))
|
|
13
12
|
|
|
14
|
-
jest.mock('../CozyUtils', () => ({
|
|
15
|
-
checkHasInstalledExtension: jest.fn()
|
|
16
|
-
}))
|
|
17
|
-
|
|
18
13
|
describe('vault unlock context', () => {
|
|
19
14
|
const Component = ({ onUnlock }) => {
|
|
20
15
|
const { showingUnlockForm, showUnlockForm } = useVaultUnlockContext()
|
|
@@ -51,27 +46,15 @@ describe('vault unlock context', () => {
|
|
|
51
46
|
useVaultClient.mockReturnValue({
|
|
52
47
|
isLocked: jest.fn().mockResolvedValue(true)
|
|
53
48
|
})
|
|
54
|
-
checkHasInstalledExtension.mockResolvedValue(true)
|
|
55
49
|
const { root, onUnlock } = await setup()
|
|
56
50
|
root.getByText('showingUnlockForm: true')
|
|
57
51
|
expect(onUnlock).not.toHaveBeenCalled()
|
|
58
52
|
})
|
|
59
53
|
|
|
60
|
-
it('should not show unlock form and call onUnlock if extension is not installed', async () => {
|
|
61
|
-
useVaultClient.mockReturnValue({
|
|
62
|
-
isLocked: jest.fn().mockResolvedValue(true)
|
|
63
|
-
})
|
|
64
|
-
checkHasInstalledExtension.mockResolvedValue(false)
|
|
65
|
-
const { root, onUnlock } = await setup()
|
|
66
|
-
expect(root.getByText('showingUnlockForm: false')).toBeTruthy()
|
|
67
|
-
expect(onUnlock).toHaveBeenCalled()
|
|
68
|
-
})
|
|
69
|
-
|
|
70
54
|
it('should not show unlock form and call onUnlock if vault is unlocked', async () => {
|
|
71
55
|
useVaultClient.mockReturnValue({
|
|
72
56
|
isLocked: jest.fn().mockResolvedValue(false)
|
|
73
57
|
})
|
|
74
|
-
checkHasInstalledExtension.mockResolvedValue(true)
|
|
75
58
|
const { root, onUnlock } = await setup()
|
|
76
59
|
expect(root.getByText('showingUnlockForm: false')).toBeTruthy()
|
|
77
60
|
expect(onUnlock).toHaveBeenCalled()
|
package/src/index.js
CHANGED