forked from ToolJet/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenapiAuth.jsx
More file actions
164 lines (159 loc) · 4.12 KB
/
OpenapiAuth.jsx
File metadata and controls
164 lines (159 loc) · 4.12 KB
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
import React from 'react';
import Input from '@/_ui/Input';
import Oauth from './Oauth';
const OpenapiAuth = ({
username,
password,
bearer_token,
optionchanged,
authObject,
api_keys,
access_token_url,
client_id,
client_secret,
client_auth,
custom_auth_params,
custom_query_params,
add_token_to,
header_prefix,
grant_type,
scopes,
auth_url,
auth_type,
access_token_custom_headers,
}) => {
const apiKeyChanges = (key, value) => {
const apiKeys = api_keys ?? [];
const updatedKeys = apiKeys.map((item) => {
if (Array.isArray(authObject)) {
if (item.parentKey === authObject[0].key) {
item.fields.map((field) => {
if (field.key === key) {
field.value = value;
}
return field;
});
}
} else {
if (item.key === key) {
item.value = value;
}
}
return item;
});
optionchanged('api_keys', updatedKeys);
};
const getCurrentKey = (key) => {
let currentValue;
if (!api_keys) return '';
api_keys.map((item) => {
if (Array.isArray(authObject) && item.parentKey === authObject[0].key) {
item.fields.map((field) => {
if (field.key === key) {
currentValue = field.value;
return;
}
});
if (currentValue) return;
}
if (item.key === key) {
currentValue = item.value;
return;
}
});
return currentValue;
};
const renderApiKeyField = (auth, index) => {
if (auth) {
const value = getCurrentKey(auth.key);
return (
<div className="col-md-12" key={index ?? auth.key}>
<label className="form-label text-muted mt-3">{auth.key}</label>
<Input
type="text"
className="form-control"
onChange={(e) => apiKeyChanges(auth.key, e.target.value)}
value={value}
/>
</div>
);
} else return null;
};
switch (auth_type) {
case 'basic': {
return (
<div>
<div className="col-md-12">
<label className="form-label text-muted mt-3">Username</label>
<Input
type="text"
className="form-control"
onChange={(e) => optionchanged('username', e.target.value)}
value={username}
/>
</div>
<div className="col-md-12">
<label className="form-label text-muted mt-3">Password</label>
<Input
type="text"
className="form-control"
onChange={(e) => optionchanged('password', e.target.value)}
value={password}
/>
</div>
</div>
);
}
case 'bearer': {
return (
<div>
<div className="col-md-12">
<label className="form-label text-muted mt-3">Token</label>
<Input
type="text"
className="form-control"
onChange={(e) => optionchanged('bearer_token', e.target.value)}
value={bearer_token}
/>
</div>
</div>
);
}
case 'apiKey': {
if (Array.isArray(authObject)) {
return (
<div>
{authObject.map((auth, index) => {
return renderApiKeyField(auth, index);
})}
</div>
);
} else {
return renderApiKeyField(authObject);
}
}
case 'oauth2': {
return (
<Oauth
add_token_to={add_token_to}
header_prefix={header_prefix}
access_token_url={access_token_url}
grant_type={grant_type}
optionchanged={optionchanged}
custom_auth_params={custom_auth_params}
custom_query_params={custom_query_params}
client_id={client_id}
client_secret={client_secret}
client_auth={client_auth}
scopes={scopes}
auth_url={auth_url}
access_token_custom_headers={access_token_custom_headers}
authObject={authObject}
/>
);
}
default:
return null;
}
};
export default OpenapiAuth;