cozy-keys-lib 4.0.0 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-keys-lib",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "Interactions between cozy-keys and a bitwarden server",
5
5
  "browser": "transpiled/index.js",
6
6
  "main": "transpiled/index.node.js",
@@ -32,7 +32,18 @@ class VaultProvider extends React.Component {
32
32
  }
33
33
 
34
34
  componentDidMount() {
35
- this.setupClient()
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)
46
+ }
36
47
  }
37
48
 
38
49
  componentWillUnmount() {
@@ -53,12 +64,12 @@ class VaultProvider extends React.Component {
53
64
  }
54
65
  }
55
66
 
56
- setupClient() {
67
+ setupClient(instance) {
57
68
  const unsafeStorage = this.props.unsafeStorage
58
69
  const vaultClient =
59
70
  this.props.vaultClient ||
60
71
  this.props.client ||
61
- getVaultClient(this.props.instance, unsafeStorage, this.props.vaultData)
72
+ getVaultClient(instance, unsafeStorage, this.props.vaultData)
62
73
 
63
74
  this.setState(
64
75
  {
@@ -80,12 +91,11 @@ class VaultProvider extends React.Component {
80
91
  }
81
92
 
82
93
  render() {
83
- const { vaultClient } = this.state
84
- return vaultClient ? (
94
+ return (
85
95
  <VaultContext.Provider value={this.state}>
86
96
  {this.props.children}
87
97
  </VaultContext.Provider>
88
- ) : null
98
+ )
89
99
  }
90
100
  }
91
101
 
@@ -94,7 +104,8 @@ VaultProvider.propTypes = {
94
104
  vaultClient: PropTypes.object,
95
105
  client: PropTypes.object, // deprecated
96
106
  unsafeStorage: PropTypes.bool,
97
- setClient: PropTypes.func
107
+ setClient: PropTypes.func,
108
+ cozyClient: PropTypes.object
98
109
  }
99
110
 
100
111
  const withVaultClient = BaseComponent => {
@@ -29,7 +29,7 @@ describe('VaultProvider', () => {
29
29
 
30
30
  await new Promise(resolve =>
31
31
  setTimeout(() => {
32
- expect(ChildComponent).toHaveBeenCalledTimes(2)
32
+ expect(ChildComponent).toHaveBeenCalledTimes(3)
33
33
  expect(ChildComponent).toHaveBeenCalledWith({
34
34
  vaultClient,
35
35
  locked: false
@@ -42,7 +42,7 @@ describe('VaultProvider', () => {
42
42
 
43
43
  await new Promise(resolve =>
44
44
  setTimeout(() => {
45
- expect(ChildComponent).toHaveBeenCalledTimes(3)
45
+ expect(ChildComponent).toHaveBeenCalledTimes(4)
46
46
  expect(ChildComponent).toHaveBeenCalledWith({
47
47
  vaultClient,
48
48
  locked: true
@@ -49,7 +49,25 @@ function (_React$Component) {
49
49
  _createClass(VaultProvider, [{
50
50
  key: "componentDidMount",
51
51
  value: function componentDidMount() {
52
- this.setupClient();
52
+ var _this2 = this;
53
+
54
+ var _this$props = this.props,
55
+ cozyClient = _this$props.cozyClient,
56
+ instance = _this$props.instance;
57
+
58
+ if (cozyClient) {
59
+ var uri = cozyClient.getStackClient().uri;
60
+
61
+ if (!uri) {
62
+ cozyClient.on('login', function () {
63
+ _this2.setupClient(cozyClient.getStackClient().uri);
64
+ });
65
+ }
66
+ }
67
+
68
+ if (instance) {
69
+ this.setupClient(this.props.instance);
70
+ }
53
71
  }
54
72
  }, {
55
73
  key: "componentWillUnmount",
@@ -104,20 +122,20 @@ function (_React$Component) {
104
122
  }()
105
123
  }, {
106
124
  key: "setupClient",
107
- value: function setupClient() {
108
- var _this2 = this;
125
+ value: function setupClient(instance) {
126
+ var _this3 = this;
109
127
 
110
128
  var unsafeStorage = this.props.unsafeStorage;
111
- var vaultClient = this.props.vaultClient || this.props.client || getVaultClient(this.props.instance, unsafeStorage, this.props.vaultData);
129
+ var vaultClient = this.props.vaultClient || this.props.client || getVaultClient(instance, unsafeStorage, this.props.vaultData);
112
130
  this.setState({
113
131
  vaultClient: vaultClient,
114
132
  locked: true
115
133
  }, function () {
116
- vaultClient.on('unlock', _this2.updateLockedState);
117
- vaultClient.on('lock', _this2.updateLockedState);
118
- vaultClient.on('login', _this2.updateLockedState);
134
+ vaultClient.on('unlock', _this3.updateLockedState);
135
+ vaultClient.on('lock', _this3.updateLockedState);
136
+ vaultClient.on('login', _this3.updateLockedState);
119
137
 
120
- _this2.updateLockedState();
138
+ _this3.updateLockedState();
121
139
  });
122
140
 
123
141
  if (this.props.setClient) {
@@ -127,10 +145,9 @@ function (_React$Component) {
127
145
  }, {
128
146
  key: "render",
129
147
  value: function render() {
130
- var vaultClient = this.state.vaultClient;
131
- return vaultClient ? React.createElement(VaultContext.Provider, {
148
+ return React.createElement(VaultContext.Provider, {
132
149
  value: this.state
133
- }, this.props.children) : null;
150
+ }, this.props.children);
134
151
  }
135
152
  }]);
136
153
 
@@ -143,7 +160,8 @@ VaultProvider.propTypes = {
143
160
  client: PropTypes.object,
144
161
  // deprecated
145
162
  unsafeStorage: PropTypes.bool,
146
- setClient: PropTypes.func
163
+ setClient: PropTypes.func,
164
+ cozyClient: PropTypes.object
147
165
  };
148
166
 
149
167
  var withVaultClient = function withVaultClient(BaseComponent) {
@@ -30,7 +30,7 @@ describe('VaultProvider', function () {
30
30
  _context.next = 7;
31
31
  return new Promise(function (resolve) {
32
32
  return setTimeout(function () {
33
- expect(ChildComponent).toHaveBeenCalledTimes(2);
33
+ expect(ChildComponent).toHaveBeenCalledTimes(3);
34
34
  expect(ChildComponent).toHaveBeenCalledWith({
35
35
  vaultClient: vaultClient,
36
36
  locked: false
@@ -44,7 +44,7 @@ describe('VaultProvider', function () {
44
44
  _context.next = 10;
45
45
  return new Promise(function (resolve) {
46
46
  return setTimeout(function () {
47
- expect(ChildComponent).toHaveBeenCalledTimes(3);
47
+ expect(ChildComponent).toHaveBeenCalledTimes(4);
48
48
  expect(ChildComponent).toHaveBeenCalledWith({
49
49
  vaultClient: vaultClient,
50
50
  locked: true