cozy-keys-lib 3.10.0 → 3.11.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-keys-lib",
3
- "version": "3.10.0",
3
+ "version": "3.11.2",
4
4
  "description": "Interactions between cozy-keys and a bitwarden server",
5
5
  "browser": "transpiled/index.js",
6
6
  "main": "transpiled/index.node.js",
@@ -72,6 +72,7 @@
72
72
  "big-integer": "^1.6.44",
73
73
  "classnames": "^2.2.6",
74
74
  "cozy-device-helper": "^1.7.5",
75
+ "cozy-intent": "^2.0.2",
75
76
  "lodash": "^4.17.15",
76
77
  "lunr": "^2.3.6",
77
78
  "microee": "^0.0.6",
@@ -85,8 +86,9 @@
85
86
  },
86
87
  "peerDependencies": {
87
88
  "cozy-client": "^16.10.2",
88
- "cozy-ui": "^42.3.0",
89
89
  "cozy-flags": "^2.6.0",
90
+ "cozy-intent": "^2.0.2",
91
+ "cozy-ui": "^42.3.0",
90
92
  "react": "^16.8.3",
91
93
  "react-dom": "^16.9.0"
92
94
  }
@@ -1,4 +1,4 @@
1
- import React, { useState, useCallback, useMemo } from 'react'
1
+ import React, { useState, useCallback, useMemo, useEffect } from 'react'
2
2
  import cx from 'classnames'
3
3
  import get from 'lodash/get'
4
4
  import PropTypes from 'prop-types'
@@ -13,6 +13,7 @@ import Icon from 'cozy-ui/transpiled/react/Icon'
13
13
  import CozyTheme from 'cozy-ui/transpiled/react/CozyTheme'
14
14
  import { IllustrationDialog } from 'cozy-ui/transpiled/react/CozyDialogs'
15
15
  import KeychainIcon from 'cozy-ui/transpiled/react/Icons/Keychain'
16
+ import { useWebviewIntent } from 'cozy-intent'
16
17
 
17
18
  import CloudIcon from './IconCozySecurity'
18
19
  import PasswordField from './PasswordField'
@@ -77,6 +78,31 @@ const UnlockForm = props => {
77
78
 
78
79
  const canAuthWithOIDC = canClientAuthWithOIDC(client)
79
80
 
81
+ const webviewIntent = useWebviewIntent()
82
+
83
+ useEffect(() => {
84
+ webviewIntent &&
85
+ webviewIntent.call(
86
+ 'setFlagshipUI',
87
+ {
88
+ bottomTheme: 'light',
89
+ topTheme: 'light'
90
+ },
91
+ 'cozy-keys-lib/UnlockForm'
92
+ )
93
+
94
+ return () =>
95
+ webviewIntent &&
96
+ webviewIntent.call(
97
+ 'setFlagshipUI',
98
+ {
99
+ bottomTheme: 'light',
100
+ topTheme: 'light'
101
+ },
102
+ 'cozy-keys-lib/UnlockForm'
103
+ )
104
+ }, [webviewIntent])
105
+
80
106
  return (
81
107
  <CozyTheme variant="inverted">
82
108
  <IllustrationDialog
@@ -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
- client: null,
26
+ vaultClient: vaultClient,
19
27
  locked: true
20
28
  }
21
29
 
@@ -24,24 +32,22 @@ class VaultProvider extends React.Component {
24
32
  }
25
33
 
26
34
  componentDidMount() {
27
- if (this.props.instance) {
28
- this.setupClient()
29
- }
35
+ this.setupClient()
30
36
  }
31
37
 
32
38
  componentWillUnmount() {
33
- const { client } = this.state
34
- if (!client) {
39
+ const { vaultClient } = this.state
40
+ if (!vaultClient) {
35
41
  return
36
42
  }
37
- client.removeListener('unlock', this.updateLockedState)
38
- client.removeListener('lock', this.updateLockedState)
39
- client.removeListener('login', this.updateLockedState)
43
+ vaultClient.removeListener('unlock', this.updateLockedState)
44
+ vaultClient.removeListener('lock', this.updateLockedState)
45
+ vaultClient.removeListener('login', this.updateLockedState)
40
46
  }
41
47
 
42
48
  async updateLockedState() {
43
- const { client } = this.state
44
- const locked = await client.isLocked()
49
+ const { vaultClient } = this.state
50
+ const locked = await vaultClient.isLocked()
45
51
  if (locked != this.state.locked) {
46
52
  this.setState({ locked })
47
53
  }
@@ -49,35 +55,32 @@ class VaultProvider extends React.Component {
49
55
 
50
56
  setupClient() {
51
57
  const unsafeStorage = this.props.unsafeStorage
52
-
53
- const client = getVaultClient(
54
- this.props.instance,
55
- unsafeStorage,
56
- this.props.vaultData
57
- )
58
+ const vaultClient =
59
+ this.props.vaultClient ||
60
+ getVaultClient(this.props.instance, unsafeStorage, this.props.vaultData)
58
61
 
59
62
  this.setState(
60
63
  {
61
- client,
64
+ vaultClient,
62
65
  locked: true
63
66
  },
64
67
  () => {
65
- client.on('unlock', this.updateLockedState)
66
- client.on('lock', this.updateLockedState)
67
- client.on('login', this.updateLockedState)
68
+ vaultClient.on('unlock', this.updateLockedState)
69
+ vaultClient.on('lock', this.updateLockedState)
70
+ vaultClient.on('login', this.updateLockedState)
68
71
 
69
72
  this.updateLockedState()
70
73
  }
71
74
  )
72
75
 
73
76
  if (this.props.setClient) {
74
- this.props.setClient(client)
77
+ this.props.setClient(vaultClient)
75
78
  }
76
79
  }
77
80
 
78
81
  render() {
79
- const { client } = this.state
80
- return client ? (
82
+ const { vaultClient } = this.state
83
+ return vaultClient ? (
81
84
  <VaultContext.Provider value={this.state}>
82
85
  {this.props.children}
83
86
  </VaultContext.Provider>
@@ -86,7 +89,9 @@ class VaultProvider extends React.Component {
86
89
  }
87
90
 
88
91
  VaultProvider.propTypes = {
89
- instance: PropTypes.string.isRequired,
92
+ instance: PropTypes.string,
93
+ vaultClient: PropTypes.object,
94
+ client: PropTypes.object, // deprecated
90
95
  unsafeStorage: PropTypes.bool,
91
96
  setClient: PropTypes.func
92
97
  }
@@ -94,7 +99,9 @@ VaultProvider.propTypes = {
94
99
  const withVaultClient = BaseComponent => {
95
100
  const Component = props => (
96
101
  <VaultContext.Consumer>
97
- {({ client }) => <BaseComponent vaultClient={client} {...props} />}
102
+ {({ vaultClient }) => (
103
+ <BaseComponent vaultClient={vaultClient} {...props} />
104
+ )}
98
105
  </VaultContext.Consumer>
99
106
  )
100
107
 
@@ -106,7 +113,7 @@ const withVaultClient = BaseComponent => {
106
113
 
107
114
  const useVaultClient = () => {
108
115
  const ctx = useContext(VaultContext)
109
- return ctx.client
116
+ return ctx.vaultClient
110
117
  }
111
118
 
112
119
  export { VaultContext, VaultProvider, withVaultClient, useVaultClient }
@@ -21,26 +21,32 @@ describe('VaultProvider', () => {
21
21
  </VaultProvider>
22
22
  )
23
23
 
24
- const client = component.state('client')
24
+ const vaultClient = component.state('vaultClient')
25
25
 
26
- expect(ChildComponent).toHaveBeenCalledWith({ client, locked: true })
26
+ expect(ChildComponent).toHaveBeenCalledWith({ vaultClient, locked: true })
27
27
 
28
- client.unlock()
28
+ vaultClient.unlock()
29
29
 
30
30
  await new Promise(resolve =>
31
31
  setTimeout(() => {
32
32
  expect(ChildComponent).toHaveBeenCalledTimes(2)
33
- expect(ChildComponent).toHaveBeenCalledWith({ client, locked: false })
33
+ expect(ChildComponent).toHaveBeenCalledWith({
34
+ vaultClient,
35
+ locked: false
36
+ })
34
37
  resolve()
35
38
  })
36
39
  )
37
40
 
38
- client.lock()
41
+ vaultClient.lock()
39
42
 
40
43
  await new Promise(resolve =>
41
44
  setTimeout(() => {
42
45
  expect(ChildComponent).toHaveBeenCalledTimes(3)
43
- expect(ChildComponent).toHaveBeenCalledWith({ client, locked: true })
46
+ expect(ChildComponent).toHaveBeenCalledWith({
47
+ vaultClient,
48
+ locked: true
49
+ })
44
50
  resolve()
45
51
  })
46
52
  )
@@ -27,7 +27,7 @@ const VaultUnlocker = ({
27
27
  checkShouldUnlock
28
28
  }) => {
29
29
  const cozyClient = useClient()
30
- const { locked, client: vaultClient } = useContext(VaultContext)
30
+ const { locked, vaultClient } = useContext(VaultContext)
31
31
 
32
32
  const [showSpinner, setShowSpinner] = useState(false)
33
33
  const [isChecking, setIsChecking] = useState(true)
package/src/index.js CHANGED
@@ -18,3 +18,4 @@ export {
18
18
  VaultUnlockProvider,
19
19
  withVaultUnlockContext
20
20
  } from './components/vaultUnlockContext'
21
+ export { UnlockForm } from './components/UnlockForm'
@@ -1,7 +1,7 @@
1
1
  import _regeneratorRuntime from "@babel/runtime/regenerator";
2
2
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
3
  import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
4
- import React, { useState, useCallback, useMemo } from 'react';
4
+ import React, { useState, useCallback, useMemo, useEffect } from 'react';
5
5
  import cx from 'classnames';
6
6
  import get from 'lodash/get';
7
7
  import PropTypes from 'prop-types';
@@ -15,6 +15,7 @@ import Icon from 'cozy-ui/transpiled/react/Icon';
15
15
  import CozyTheme from 'cozy-ui/transpiled/react/CozyTheme';
16
16
  import { IllustrationDialog } from 'cozy-ui/transpiled/react/CozyDialogs';
17
17
  import KeychainIcon from 'cozy-ui/transpiled/react/Icons/Keychain';
18
+ import { useWebviewIntent } from 'cozy-intent';
18
19
  import CloudIcon from './IconCozySecurity';
19
20
  import PasswordField from './PasswordField';
20
21
  import { useVaultClient } from './VaultContext';
@@ -108,6 +109,19 @@ var UnlockForm = function UnlockForm(props) {
108
109
  return getPassphraseResetUrl(client);
109
110
  }, [client]);
110
111
  var canAuthWithOIDC = canClientAuthWithOIDC(client);
112
+ var webviewIntent = useWebviewIntent();
113
+ useEffect(function () {
114
+ webviewIntent && webviewIntent.call('setFlagshipUI', {
115
+ bottomTheme: 'light',
116
+ topTheme: 'light'
117
+ }, 'cozy-keys-lib/UnlockForm');
118
+ return function () {
119
+ return webviewIntent && webviewIntent.call('setFlagshipUI', {
120
+ bottomTheme: 'light',
121
+ topTheme: 'light'
122
+ }, 'cozy-keys-lib/UnlockForm');
123
+ };
124
+ }, [webviewIntent]);
111
125
  return React.createElement(CozyTheme, {
112
126
  variant: "inverted"
113
127
  }, React.createElement(IllustrationDialog, {
@@ -29,8 +29,16 @@ function (_React$Component) {
29
29
  _classCallCheck(this, VaultProvider);
30
30
 
31
31
  _this = _possibleConstructorReturn(this, _getPrototypeOf(VaultProvider).call(this, props));
32
+ var vaultClient = _this.props.vaultClient;
33
+
34
+ if (_this.props.client) {
35
+ vaultClient = _this.props.client; // eslint-disable-next-line no-console
36
+
37
+ console.warn('Passing client in props is deprecated, please use vaultClient instead.');
38
+ }
39
+
32
40
  _this.state = {
33
- client: null,
41
+ vaultClient: vaultClient,
34
42
  locked: true
35
43
  };
36
44
  _this.updateLockedState = _this.updateLockedState.bind(_assertThisInitialized(_this));
@@ -41,22 +49,20 @@ function (_React$Component) {
41
49
  _createClass(VaultProvider, [{
42
50
  key: "componentDidMount",
43
51
  value: function componentDidMount() {
44
- if (this.props.instance) {
45
- this.setupClient();
46
- }
52
+ this.setupClient();
47
53
  }
48
54
  }, {
49
55
  key: "componentWillUnmount",
50
56
  value: function componentWillUnmount() {
51
- var client = this.state.client;
57
+ var vaultClient = this.state.vaultClient;
52
58
 
53
- if (!client) {
59
+ if (!vaultClient) {
54
60
  return;
55
61
  }
56
62
 
57
- client.removeListener('unlock', this.updateLockedState);
58
- client.removeListener('lock', this.updateLockedState);
59
- client.removeListener('login', this.updateLockedState);
63
+ vaultClient.removeListener('unlock', this.updateLockedState);
64
+ vaultClient.removeListener('lock', this.updateLockedState);
65
+ vaultClient.removeListener('login', this.updateLockedState);
60
66
  }
61
67
  }, {
62
68
  key: "updateLockedState",
@@ -64,14 +70,14 @@ function (_React$Component) {
64
70
  var _updateLockedState = _asyncToGenerator(
65
71
  /*#__PURE__*/
66
72
  _regeneratorRuntime.mark(function _callee() {
67
- var client, locked;
73
+ var vaultClient, locked;
68
74
  return _regeneratorRuntime.wrap(function _callee$(_context) {
69
75
  while (1) {
70
76
  switch (_context.prev = _context.next) {
71
77
  case 0:
72
- client = this.state.client;
78
+ vaultClient = this.state.vaultClient;
73
79
  _context.next = 3;
74
- return client.isLocked();
80
+ return vaultClient.isLocked();
75
81
 
76
82
  case 3:
77
83
  locked = _context.sent;
@@ -102,27 +108,27 @@ function (_React$Component) {
102
108
  var _this2 = this;
103
109
 
104
110
  var unsafeStorage = this.props.unsafeStorage;
105
- var client = getVaultClient(this.props.instance, unsafeStorage, this.props.vaultData);
111
+ var vaultClient = this.props.vaultClient || getVaultClient(this.props.instance, unsafeStorage, this.props.vaultData);
106
112
  this.setState({
107
- client: client,
113
+ vaultClient: vaultClient,
108
114
  locked: true
109
115
  }, function () {
110
- client.on('unlock', _this2.updateLockedState);
111
- client.on('lock', _this2.updateLockedState);
112
- client.on('login', _this2.updateLockedState);
116
+ vaultClient.on('unlock', _this2.updateLockedState);
117
+ vaultClient.on('lock', _this2.updateLockedState);
118
+ vaultClient.on('login', _this2.updateLockedState);
113
119
 
114
120
  _this2.updateLockedState();
115
121
  });
116
122
 
117
123
  if (this.props.setClient) {
118
- this.props.setClient(client);
124
+ this.props.setClient(vaultClient);
119
125
  }
120
126
  }
121
127
  }, {
122
128
  key: "render",
123
129
  value: function render() {
124
- var client = this.state.client;
125
- return client ? React.createElement(VaultContext.Provider, {
130
+ var vaultClient = this.state.vaultClient;
131
+ return vaultClient ? React.createElement(VaultContext.Provider, {
126
132
  value: this.state
127
133
  }, this.props.children) : null;
128
134
  }
@@ -132,7 +138,10 @@ function (_React$Component) {
132
138
  }(React.Component);
133
139
 
134
140
  VaultProvider.propTypes = {
135
- instance: PropTypes.string.isRequired,
141
+ instance: PropTypes.string,
142
+ vaultClient: PropTypes.object,
143
+ client: PropTypes.object,
144
+ // deprecated
136
145
  unsafeStorage: PropTypes.bool,
137
146
  setClient: PropTypes.func
138
147
  };
@@ -140,9 +149,9 @@ VaultProvider.propTypes = {
140
149
  var withVaultClient = function withVaultClient(BaseComponent) {
141
150
  var Component = function Component(props) {
142
151
  return React.createElement(VaultContext.Consumer, null, function (_ref) {
143
- var client = _ref.client;
152
+ var vaultClient = _ref.vaultClient;
144
153
  return React.createElement(BaseComponent, _extends({
145
- vaultClient: client
154
+ vaultClient: vaultClient
146
155
  }, props));
147
156
  });
148
157
  };
@@ -153,7 +162,7 @@ var withVaultClient = function withVaultClient(BaseComponent) {
153
162
 
154
163
  var useVaultClient = function useVaultClient() {
155
164
  var ctx = useContext(VaultContext);
156
- return ctx.client;
165
+ return ctx.vaultClient;
157
166
  };
158
167
 
159
168
  export { VaultContext, VaultProvider, withVaultClient, useVaultClient };
@@ -12,7 +12,7 @@ describe('VaultProvider', function () {
12
12
  _asyncToGenerator(
13
13
  /*#__PURE__*/
14
14
  _regeneratorRuntime.mark(function _callee() {
15
- var ChildComponent, component, client;
15
+ var ChildComponent, component, vaultClient;
16
16
  return _regeneratorRuntime.wrap(function _callee$(_context) {
17
17
  while (1) {
18
18
  switch (_context.prev = _context.next) {
@@ -21,18 +21,18 @@ describe('VaultProvider', function () {
21
21
  component = mount(React.createElement(VaultProvider, {
22
22
  instance: 'test@example.com'
23
23
  }, React.createElement(VaultContext.Consumer, null, ChildComponent)));
24
- client = component.state('client');
24
+ vaultClient = component.state('vaultClient');
25
25
  expect(ChildComponent).toHaveBeenCalledWith({
26
- client: client,
26
+ vaultClient: vaultClient,
27
27
  locked: true
28
28
  });
29
- client.unlock();
29
+ vaultClient.unlock();
30
30
  _context.next = 7;
31
31
  return new Promise(function (resolve) {
32
32
  return setTimeout(function () {
33
33
  expect(ChildComponent).toHaveBeenCalledTimes(2);
34
34
  expect(ChildComponent).toHaveBeenCalledWith({
35
- client: client,
35
+ vaultClient: vaultClient,
36
36
  locked: false
37
37
  });
38
38
  resolve();
@@ -40,13 +40,13 @@ describe('VaultProvider', function () {
40
40
  });
41
41
 
42
42
  case 7:
43
- client.lock();
43
+ vaultClient.lock();
44
44
  _context.next = 10;
45
45
  return new Promise(function (resolve) {
46
46
  return setTimeout(function () {
47
47
  expect(ChildComponent).toHaveBeenCalledTimes(3);
48
48
  expect(ChildComponent).toHaveBeenCalledWith({
49
- client: client,
49
+ vaultClient: vaultClient,
50
50
  locked: true
51
51
  });
52
52
  resolve();
@@ -28,7 +28,7 @@ var VaultUnlocker = function VaultUnlocker(_ref) {
28
28
 
29
29
  var _useContext = useContext(VaultContext),
30
30
  locked = _useContext.locked,
31
- vaultClient = _useContext.client;
31
+ vaultClient = _useContext.vaultClient;
32
32
 
33
33
  var _useState = useState(false),
34
34
  _useState2 = _slicedToArray(_useState, 2),
@@ -0,0 +1,82 @@
1
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
+ import { checkShouldUnlock } from './defaults';
4
+ import WebVaultClient from '../WebVaultClient';
5
+ jest.mock('../CozyUtils', function () {
6
+ return {
7
+ checkHasInstalledExtension: jest.fn(),
8
+ getEmail: jest.fn().mockReturnValue('me@test.fr'),
9
+ isInstance: jest.fn().mockReturnValue(true)
10
+ };
11
+ });
12
+ describe('checkShouldUnlock', function () {
13
+ var vaultClient = new WebVaultClient('https://me.cozy.wtf');
14
+ it('should return true when the vault is locked', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
15
+ var shouldUnlock;
16
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
17
+ while (1) {
18
+ switch (_context.prev = _context.next) {
19
+ case 0:
20
+ jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(true);
21
+ _context.next = 3;
22
+ return checkShouldUnlock(vaultClient);
23
+
24
+ case 3:
25
+ shouldUnlock = _context.sent;
26
+ expect(shouldUnlock).toBe(true);
27
+
28
+ case 5:
29
+ case "end":
30
+ return _context.stop();
31
+ }
32
+ }
33
+ }, _callee);
34
+ })));
35
+ it('should return false when the vault is already unlocked', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
36
+ var shouldUnlock;
37
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
38
+ while (1) {
39
+ switch (_context2.prev = _context2.next) {
40
+ case 0:
41
+ jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(false);
42
+ _context2.next = 3;
43
+ return checkShouldUnlock(vaultClient);
44
+
45
+ case 3:
46
+ shouldUnlock = _context2.sent;
47
+ expect(shouldUnlock).toBe(false);
48
+
49
+ case 5:
50
+ case "end":
51
+ return _context2.stop();
52
+ }
53
+ }
54
+ }, _callee2);
55
+ })));
56
+ it('should return false when additional check function return false', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
57
+ var additionalUnlockCheck, shouldUnlock;
58
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
59
+ while (1) {
60
+ switch (_context3.prev = _context3.next) {
61
+ case 0:
62
+ jest.spyOn(vaultClient, 'isLocked').mockResolvedValue(true);
63
+
64
+ additionalUnlockCheck = function additionalUnlockCheck() {
65
+ return false;
66
+ };
67
+
68
+ _context3.next = 4;
69
+ return checkShouldUnlock(vaultClient, additionalUnlockCheck);
70
+
71
+ case 4:
72
+ shouldUnlock = _context3.sent;
73
+ expect(shouldUnlock).toBe(false);
74
+
75
+ case 6:
76
+ case "end":
77
+ return _context3.stop();
78
+ }
79
+ }
80
+ }, _callee3);
81
+ })));
82
+ });
@@ -9,4 +9,5 @@ import _VaultUnlocker from './components/VaultUnlocker';
9
9
  export { _VaultUnlocker as VaultUnlocker };
10
10
  import _VaultUnlockPlaceholder from './components/VaultUnlockPlaceholder';
11
11
  export { _VaultUnlockPlaceholder as VaultUnlockPlaceholder };
12
- export { useVaultUnlockContext, VaultUnlockProvider, withVaultUnlockContext } from './components/vaultUnlockContext';
12
+ export { useVaultUnlockContext, VaultUnlockProvider, withVaultUnlockContext } from './components/vaultUnlockContext';
13
+ export { UnlockForm } from './components/UnlockForm';