-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
cookie-modify-modal.js
296 lines (251 loc) · 8.29 KB
/
cookie-modify-modal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// @flow
import * as React from 'react';
import { Tab, TabList, TabPanel, Tabs } from 'react-tabs';
import autobind from 'autobind-decorator';
import * as toughCookie from 'tough-cookie';
import * as models from '../../../models';
import clone from 'clone';
import { DEBOUNCE_MILLIS } from '../../../common/constants';
import Modal from '../base/modal';
import ModalBody from '../base/modal-body';
import ModalHeader from '../base/modal-header';
import ModalFooter from '../base/modal-footer';
import OneLineEditor from '../codemirror/one-line-editor';
import { cookieToString } from 'insomnia-cookies';
import type { Cookie, CookieJar } from '../../../models/cookie-jar';
import type { Workspace } from '../../../models/workspace';
type Props = {
handleRender: Function,
handleGetRenderContext: Function,
nunjucksPowerUserMode: boolean,
isVariableUncovered: boolean,
workspace: Workspace,
cookieJar: CookieJar,
};
type State = {
cookie: Cookie | null,
rawValue: string,
};
@autobind
class CookieModifyModal extends React.PureComponent<Props, State> {
modal: Modal | null;
_rawTimeout: TimeoutID;
_cookieUpdateTimeout: TimeoutID;
constructor(props: any) {
super(props);
this.state = {
cookie: null,
rawValue: '',
};
}
_setModalRef(n: Modal | null) {
this.modal = n;
}
async show(cookie: Cookie) {
// Dunno why this is sent as an array
cookie = cookie[0] || cookie;
const { cookieJar } = this.props;
const oldCookie = cookieJar.cookies.find(c => c.id === cookie.id);
if (!oldCookie) {
// Cookie not found in jar
return;
}
this.setState({ cookie });
this.modal && this.modal.show();
}
hide() {
this.modal && this.modal.hide();
}
static async _saveChanges(cookieJar: CookieJar) {
await models.cookieJar.update(cookieJar);
}
_handleChangeRawValue(e: SyntheticEvent<HTMLInputElement>) {
const value = e.currentTarget.value;
clearTimeout(this._rawTimeout);
this._rawTimeout = setTimeout(async () => {
const oldCookie = this.state.cookie;
let cookie;
try {
// NOTE: Perform toJSON so we have a plain JS object instead of Cookie instance
cookie = toughCookie.Cookie.parse(value).toJSON();
} catch (err) {
console.warn(`Failed to parse cookie string "${value}"`, err);
return;
}
if (!this.state.cookie || !oldCookie) {
return;
}
// Make sure cookie has an id
cookie.id = oldCookie.id;
await this._handleCookieUpdate(cookie);
}, DEBOUNCE_MILLIS * 2);
}
async _handleCookieUpdate(newCookie: Cookie) {
const oldCookie = this.state.cookie;
if (!oldCookie) {
// We don't have a cookie to edit
return;
}
const cookie = clone(newCookie);
// Sanitize expires field
const expires = new Date(cookie.expires || '').getTime();
if (isNaN(expires)) {
delete cookie.expires;
} else {
cookie.expires = expires;
}
// Clone so we don't modify the original
const cookieJar = clone(this.props.cookieJar);
const { cookies } = cookieJar;
const index = cookies.findIndex(c => c.id === cookie.id);
if (index < 0) {
console.warn(`Could not find cookie with id=${cookie.id} to edit`);
return;
}
cookieJar.cookies = [...cookies.slice(0, index), cookie, ...cookies.slice(index + 1)];
this.setState({ cookie });
await CookieModifyModal._saveChanges(cookieJar);
return cookie;
}
_handleChange(field: string, eventOrValue: string | Event) {
const { cookie } = this.state;
let value;
if (typeof eventOrValue === 'string') {
value = eventOrValue.trim();
} else if (eventOrValue.target instanceof HTMLInputElement) {
if (eventOrValue.target.type === 'checkbox') {
value = eventOrValue.target.checked;
} else {
value = eventOrValue.target.value.trim();
}
} else {
// Should never happen
}
const newCookie = Object.assign({}, cookie, { [field]: value });
clearTimeout(this._cookieUpdateTimeout);
this._cookieUpdateTimeout = setTimeout(async () => {
await this._handleCookieUpdate(newCookie);
this.setState({ cookie: newCookie });
}, DEBOUNCE_MILLIS * 2);
}
static _capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
_getRawCookieString() {
const { cookie } = this.state;
if (!cookie) {
return '';
}
try {
return cookieToString(toughCookie.Cookie.fromJSON(JSON.stringify(cookie)));
} catch (err) {
console.warn('Failed to parse cookie string', err);
return '';
}
}
_renderInputField(field: string, error: string | null = null) {
const { cookie } = this.state;
const {
handleRender,
handleGetRenderContext,
nunjucksPowerUserMode,
isVariableUncovered,
} = this.props;
if (!cookie) {
return null;
}
const val = (cookie[field] || '').toString();
return (
<div className="form-control form-control--outlined">
<label>
{CookieModifyModal._capitalize(field)} <span className="danger">{error}</span>
<OneLineEditor
render={handleRender}
getRenderContext={handleGetRenderContext}
nunjucksPowerUserMode={nunjucksPowerUserMode}
isVariableUncovered={isVariableUncovered}
defaultValue={val || ''}
onChange={value => this._handleChange(field, value)}
/>
</label>
</div>
);
}
render() {
const { cookieJar } = this.props;
const { cookie } = this.state;
const checkFields = ['secure', 'httpOnly'];
return (
<Modal ref={this._setModalRef} {...this.props}>
<ModalHeader>Edit Cookie</ModalHeader>
<ModalBody className="cookie-modify">
{cookieJar &&
cookie && (
<Tabs>
<TabList>
<Tab tabIndex="-1">
<button>Friendly</button>
</Tab>
<Tab tabIndex="-1">
<button>Raw</button>
</Tab>
</TabList>
<TabPanel>
<div className="pad">
<div className="form-row">
{this._renderInputField('key')}
{this._renderInputField('value')}
</div>
<div className="form-row">
{this._renderInputField('domain')}
{this._renderInputField('path')}
</div>
{this._renderInputField(
'expires',
isNaN(new Date(cookie.expires || 0).getTime()) ? 'Invalid Date' : null,
)}
</div>
<div className="pad no-pad-top cookie-modify__checkboxes row-around txt-lg">
{checkFields.map((field, i) => {
const checked = !!cookie[field];
return (
<label key={i}>
{CookieModifyModal._capitalize(field)}
<input
className="space-left"
type="checkbox"
name={field}
defaultChecked={checked || false}
onChange={e => this._handleChange(field, e)}
/>
</label>
);
})}
</div>
</TabPanel>
<TabPanel className="react-tabs__tab-panel pad">
<div className="form-control form-control--outlined">
<label>
Raw Cookie String
<input
type="text"
onChange={this._handleChangeRawValue}
defaultValue={this._getRawCookieString()}
/>
</label>
</div>
</TabPanel>
</Tabs>
)}
</ModalBody>
<ModalFooter>
<button className="btn" onClick={this.hide}>
Done
</button>
</ModalFooter>
</Modal>
);
}
}
// export CookieModifyModal;
export default CookieModifyModal;