-
Notifications
You must be signed in to change notification settings - Fork 36
/
auth.tsx
252 lines (219 loc) · 7.02 KB
/
auth.tsx
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
/******************************************************************************
*
* Copyright (c) 2019, the jupyter-fs authors.
*
* This file is part of the jupyter-fs library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
ExpansionPanel,
ExpansionPanelDetails,
ExpansionPanelSummary,
IconButton,
InputAdornment,
TextField,
Typography,
} from '@material-ui/core';
import * as React from 'react';
import { IFSOptions, IFSResource } from './filesystem';
import { visibilityIcon, visibilityOffIcon } from './icons';
class Template {
public pattern = /(?:)/;
constructor(public template: string) {}
substitute(dict: {[key: string]: string}) {
return this.template.replace(this.pattern, (_, p1) => dict[p1]);
}
tokens() {
return this.template.match(this.pattern) || [];
}
}
export class DoubleBraceTemplate extends Template {
public pattern = /(?<=\{\{)\w*?(?=\}\})/g;
}
function tokensFromUrl(url: string): string[] {
return new DoubleBraceTemplate(url).tokens();
}
function _askRequired(spec: IFSResource) {
return spec.auth === 'ask' && !spec.init;
}
export function askRequired(specs: IFSResource[]) {
for (const spec of specs) {
if (_askRequired(spec)) {
return true;
}
}
return false;
}
export class AskDialog<
P extends AskDialog.IProps = AskDialog.IProps,
S extends AskDialog.IState = AskDialog.IState
> extends React.Component<P, S> {
static displayName = "AskDialog";
constructor(props: P, options: IFSOptions) {
super(props);
this.state = AskDialog.initialState({open: true, options}) as Readonly<S>;
}
render() {
return (
<div>
<Dialog className="jfs-ask-dialog" open={this.state.open} onClose={this._onClose.bind(this)}>
<DialogTitle className="jfs-ask jfs-ask-dialog-title">Please enter token values for filesystem resources</DialogTitle>
<DialogContent className="jfs-ask jfs-ask-dialog-content">
{this._form()}
</DialogContent>
<DialogActions className="jfs-ask jfs-ask-dialog-actions">
<Button className="jfs-ask jfs-ask-dialog-actions-button-cancel" onClick={this._onClose.bind(this)} color="primary">
Cancel
</Button>
<Button className="jfs-ask jfs-ask-dialog-actions-button-submit" onClick={this._onSubmit.bind(this)} color="primary">
Submit
</Button>
</DialogActions>
</Dialog>
</div>
);
}
protected _form() {
return (
<form className="jfs-ask jfs-ask-form" onSubmit={this._onSubmit.bind(this)} noValidate autoComplete="off">
{this._formInner()}
</form>
)
}
protected _formInner() {
return this.props.resources.map((resource) => {
// ask for credentials if needed, or state why not
const askReq = _askRequired(resource)
const inputs = askReq ? this._inputs(resource.url) : [];
const tokens = tokensFromUrl(resource.url);
let reason = '';
if (resource.init && this.props.options.cache) {
reason = 'already initialized';
} else if (!tokens.length) {
reason = 'no template parameters';
}
const summary = `${resource.name}:${reason && ` ${reason}`}`;
return [
<ExpansionPanel
className="jfs-ask jfs-ask-panel"
disabled={!!reason}
expanded={!reason}
key={`${resource.name}_panel`}
>
<ExpansionPanelSummary className="jfs-ask jfs-ask-panel-summary">
<Typography>{summary}</Typography>
{!reason && <Typography>{resource.url}</Typography>}
</ExpansionPanelSummary>
<ExpansionPanelDetails className="jfs-ask jfs-ask-panel-details">
{inputs}
</ExpansionPanelDetails>
</ExpansionPanel>
];
});
}
protected _inputs(url: string): React.ReactNodeArray {
return tokensFromUrl(url).map(token => {
return (
<TextField
className="jfs-ask jfs-ask-input"
autoFocus
fullWidth
key={`${url}_${token}`}
label={token}
margin="dense"
name={token}
onChange={this._onChange(url).bind(this)}
type={this.state.visibility[url]?.[token] ? 'text' : 'password'}
value={this.state.values[url]?.[token] || ''}
InputProps={{
endAdornment: (
<InputAdornment position="end">
<IconButton
onClick={this._onClickVisiblity(url, token).bind(this)}
onMouseDown={this._onMouseDownVisibility.bind(this)}
edge="end"
>
{this.state.visibility[url]?.[token] ? <visibilityIcon.react/> : <visibilityOffIcon.react/>}
</IconButton>
</InputAdornment>
),
}}
/>
);
});
}
protected _onChange(url: string) {
return function(event: React.ChangeEvent<HTMLInputElement>) {
const target = event.target;
this._setValue(url, target.name, target.value);
}
}
protected _onClickVisiblity(url: string, key: string) {
return function() {
this._toggleVisibility(url, key);
}
};
protected _onMouseDownVisibility(event: React.MouseEvent<HTMLButtonElement>) {
event.preventDefault();
};
protected _onOpen() {
this.setState({ open: true });
}
protected _onClose() {
// close the dialog and blank the form
this.setState(AskDialog.initialState({options: this.state.options}));
this.props.handleClose();
};
protected async _onSubmit(
event: React.FormEvent<HTMLFormElement>
): Promise<void> {
event.preventDefault();
this.props.handleSubmit(this.state.values);
this._onClose();
}
protected _setValue(url: string, key: string, value: string) {
const urlValues = {...this.state.values[url], [key]: value};
this.setState({
values: {...this.state.values, [url]: urlValues}
});
}
protected _toggleVisibility(url: string, key: string) {
const urlVis = {...this.state.visibility[url], [key]: !this.state.visibility[url]?.[key]};
this.setState({
visibility: {...this.state.visibility, [url]: urlVis}
});
}
}
/**
* A namespace for AskDialog statics.
*/
export namespace AskDialog {
/**
* The input props for an AskDialog component
*/
export interface IProps {
handleClose: () => void;
handleSubmit: (values: {[url: string]: {[key: string]: string}}) => void;
options: IFSOptions;
resources: IFSResource[];
}
/**
* The initial state for a new AskDialog
*/
export const initialState = ({open = false, options}: {open?: boolean, options: IFSOptions}) => {return {
open,
options: {...options},
values: {} as {[url: string]: {[key: string]: string}},
visibility: {} as {[url: string]: {[key: string]: string}},
}}
/**
* The state for an AskDialog component
*/
export type IState = ReturnType<typeof initialState>;
}