Skip to content

Commit 81e6b49

Browse files
authored
Merge pull request #1048 from lowcoder-org/datasources_oauth_cookie_fields
Added oauth and cookie fields for data sources
2 parents 81665b4 + d2e42f8 commit 81e6b49

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

client/packages/lowcoder-sdk/dataSource.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,18 @@ export interface DataSourcePluginMeta extends DataSourcePluginBasicInfo {
319319
DataSourceConfig,
320320
{
321321
extra?: DynamicConfigObject;
322+
authConfig?: {
323+
type: AuthType;
324+
authId?: string;
325+
} & (
326+
| {
327+
username: string; // basic auth
328+
password: string;
329+
}
330+
| OAuthConfig
331+
);
332+
333+
sslConfig?: SSLConfig;
322334
}
323335
>;
324336
queryConfig: DynamicConfigObject | QueryConfig;

client/packages/lowcoder/src/pages/datasource/form/httpDatasourceForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { useHostCheck } from "./useHostCheck";
2424
import { useSelector } from "react-redux";
2525
import { getUser } from "redux/selectors/usersSelectors";
2626

27-
const AuthTypeOptions = [
27+
export const AuthTypeOptions = [
2828
{ label: "None", value: "NO_AUTH" },
2929
{ label: "Basic", value: "BASIC_AUTH" },
3030
{ label: "Digest", value: "DIGEST_AUTH" },

client/packages/lowcoder/src/pages/datasource/form/pluginDataSourceForm.tsx

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
TacoMarkDown,
2323
} from "lowcoder-design";
2424
import { DatasourceFormProps } from "./datasourceFormRegistry";
25-
import { DatasourceNameFormInputItem, GeneralSettingFormSectionLabel } from "../form";
25+
import { AdvancedSettingFormSectionLabel, CertValidationFormItem, DatasourceNameFormInputItem, ForwardCookiesFormItem, GeneralSettingFormSectionLabel, encryptedPlaceholder } from "../form";
2626
import {
2727
DataSourceParamConfig,
2828
ParamOption,
@@ -38,6 +38,9 @@ import { FieldData } from "rc-field-form/es/interface";
3838
import { default as Alert } from "antd/es/alert";
3939
import { default as Form } from "antd/es/form";
4040
import { default as Input } from "antd/es/input";
41+
import { AuthType, AuthTypeOptions } from "./httpDatasourceForm";
42+
import { useSelector } from "react-redux";
43+
import { getUser } from "@lowcoder-ee/redux/selectors/usersSelectors";
4144

4245
const TooltipWrapper = styled.div`
4346
.markdown-body {
@@ -130,14 +133,24 @@ export const PluginDataSourceForm = (props: DatasourceFormProps) => {
130133
const [extraParamConfigs, setExtraParamConfigs] = useState<DataSourceParamConfig[]>([]);
131134
const [isExtraParamsRefreshing, setExtraParamRefreshing] = useState(false);
132135
const [isExtraParamsRefreshError, setExtraParamRefreshError] = useState(false);
136+
133137
const pluginDef = dataSourceTypeInfo?.definition || datasource.pluginDefinition;
134138
const pluginName = dataSourceTypeInfo?.id || datasource.pluginDefinition?.id;
135139
const isEditing = !!datasource;
136140
const hasDynamicConfig = !!pluginDef?.dataSourceConfig?.extra;
141+
142+
const [authType, setAuthType] = useState(pluginDef?.dataSourceConfig?.authConfig?.type);
143+
const [authId, setAuthId] = useState(pluginDef?.dataSourceConfig?.authConfig?.authId);
137144

138145
const readyStatusCallbackRef = useRef(onFormReadyStatusChange);
139146
readyStatusCallbackRef.current = onFormReadyStatusChange;
140147

148+
const userAuthSources = useSelector(getUser).connections?.filter(authSource => authSource.source !== "EMAIL");;
149+
const userAuthSourcesOptions = userAuthSources?.map(item => ({
150+
label: item.source,
151+
value: item.authId
152+
})) || [];
153+
141154
const handleRefreshExtraParams = useCallback(() => {
142155
if (!pluginName) {
143156
return;
@@ -205,6 +218,65 @@ export const PluginDataSourceForm = (props: DatasourceFormProps) => {
205218
const initialValues = getDefaultValues(pluginDef, datasource);
206219
const hasGeneralSettings = dataSourceConfig.params?.[0]?.type !== "groupTitle";
207220

221+
const UsernameFormItem = (
222+
<FormInputItem
223+
name={"username"}
224+
label="Username"
225+
initialValue={(dataSourceConfig?.authConfig as any)?.username}
226+
required={true}
227+
rules={[{ required: true, message: trans("query.userNameRequiredMessage") }]}
228+
labelWidth={142}
229+
/>
230+
);
231+
232+
const PasswordFormItem = (
233+
<FormInputItem
234+
name={"password"}
235+
label="Password"
236+
initialValue={(dataSourceConfig?.authConfig as any)?.password}
237+
required={true}
238+
rules={[{ required: !dataSourceConfig, message: trans("query.passwordRequiredMessage") }]}
239+
labelWidth={142}
240+
// eslint-disable-next-line only-ascii/only-ascii
241+
placeholder={props.datasource ? encryptedPlaceholder : "••••••••••••"}
242+
/>
243+
);
244+
245+
const showAuthItem = (type: AuthType) => {
246+
switch (type) {
247+
case "BASIC_AUTH":
248+
return (
249+
<>
250+
{UsernameFormItem}
251+
{PasswordFormItem}
252+
</>
253+
);
254+
case "DIGEST_AUTH":
255+
return (
256+
<>
257+
{UsernameFormItem}
258+
{PasswordFormItem}
259+
</>
260+
);
261+
}
262+
};
263+
264+
const showUserAuthSourceSelector = () => {
265+
if (authType === "OAUTH2_INHERIT_FROM_LOGIN") {
266+
return (
267+
<FormSelectItem
268+
name={"authId"}
269+
label="User Authentication Source"
270+
options={userAuthSourcesOptions}
271+
initialValue={dataSourceConfig?.authConfig ? authId : null}
272+
afterChange={(value) => setAuthId(value) }
273+
labelWidth={142}
274+
/>
275+
);
276+
}
277+
return null;
278+
};
279+
208280
return (
209281
<DatasourceForm form={form} initialValues={initialValues} onFieldsChange={handleFieldsChange}>
210282
<Form.Item noStyle name="extra">
@@ -237,6 +309,25 @@ export const PluginDataSourceForm = (props: DatasourceFormProps) => {
237309
);
238310
})}
239311
</FormSection>
312+
<FormSection $size={props.size}>
313+
<FormSectionLabel>{trans("query.authentication")}</FormSectionLabel>
314+
<FormSelectItem
315+
name={"authConfigType"}
316+
label={trans("query.authenticationType")}
317+
options={AuthTypeOptions}
318+
initialValue={dataSourceConfig?.authConfig?.type ?? "NO_AUTH"}
319+
afterChange={(value) => setAuthType(value)}
320+
labelWidth={142}
321+
/>
322+
{showUserAuthSourceSelector()}
323+
{showAuthItem(authType)}
324+
</FormSection>
325+
326+
<FormSection $size={props.size}>
327+
<AdvancedSettingFormSectionLabel />
328+
<CertValidationFormItem datasource={props.datasource} />
329+
<ForwardCookiesFormItem datasource={props.datasource} />
330+
</FormSection>
240331
{isExtraParamsRefreshing && (
241332
<Alert showIcon type="info" message={trans("query.dynamicDataSourceConfigLoadingText")} />
242333
)}

0 commit comments

Comments
 (0)