/
SamlForm.js
195 lines (182 loc) · 5.7 KB
/
SamlForm.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
import React from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import {
Button,
Col,
Pane,
Row,
Select,
TextField,
PaneFooter,
} from '@folio/stripes/components';
import stripesFinalForm from '@folio/stripes/final-form';
import { Field } from 'react-final-form';
import styles from './SSOSettings.css';
const validate = (values) => {
const errors = {};
if (!values.samlBinding) {
errors.samlBinding = <FormattedMessage id="ui-tenant-settings.settings.saml.validate.binding" />;
}
if (!values.samlAttribute) {
errors.samlAttribute = <FormattedMessage id="ui-tenant-settings.settings.saml.validate.fillIn" />;
}
if (!values.userProperty) {
errors.userProperty = <FormattedMessage id="ui-tenant-settings.settings.saml.validate.userProperty" />;
}
return errors;
};
class SamlForm extends React.Component {
static propTypes = {
validateIdpUrl: PropTypes.func.isRequired,
handleSubmit: PropTypes.func.isRequired,
reset: PropTypes.func,
pristine: PropTypes.bool,
submitting: PropTypes.bool,
initialValues: PropTypes.object.isRequired, // eslint-disable-line react/no-unused-prop-types
optionLists: PropTypes.shape({
identifierOptions: PropTypes.arrayOf(PropTypes.object),
samlBindingOptions: PropTypes.arrayOf(PropTypes.object),
}),
parentMutator: PropTypes.shape({ // eslint-disable-line react/no-unused-prop-types
urlValidator: PropTypes.shape({
reset: PropTypes.func.isRequired,
GET: PropTypes.func.isRequired,
}).isRequired,
downloadFile: PropTypes.shape({
GET: PropTypes.func.isRequired,
reset: PropTypes.func.isRequired,
}),
}),
label: PropTypes.node,
};
constructor(props) {
super(props);
this.downloadMetadata = this.downloadMetadata.bind(this);
this.updateMetadataInvalidated = this.updateMetadataInvalidated.bind(this);
}
updateMetadataInvalidated() {
this.props.initialValues.metadataInvalidated = false;
this.forceUpdate();
}
downloadMetadata() {
this.props.parentMutator.downloadFile.reset();
this.props.parentMutator.downloadFile.GET().then((result) => {
const anchor = document.createElement('a');
anchor.href = `data:text/plain;base64,${result.fileContent}`;
anchor.download = 'sp-metadata.xml';
anchor.click();
this.updateMetadataInvalidated();
});
}
render() {
const {
handleSubmit,
pristine,
submitting,
initialValues,
optionLists,
label,
validateIdpUrl,
} = this.props;
const identifierOptions = (optionLists.identifierOptions || []).map(i => (
{ id: i.key, label: i.label, value: i.key, selected: initialValues.userProperty === i.key }
));
const samlBindingOptions = optionLists.samlBindingOptions.map(i => (
{ id: i.key, label: i.label, value: i.key, selected: initialValues.samlBinding === i.key }
));
const footer = (
<PaneFooter
renderEnd={(
<Button
type="submit"
buttonStyle="primary"
disabled={(pristine || submitting)}
>
<FormattedMessage id="stripes-core.button.save" />
</Button>
)}
/>
);
return (
<form
id="form-saml"
onSubmit={handleSubmit}
className={styles.samlForm}
>
<Pane
defaultWidth="fill"
fluidContentWidth
paneTitle={label}
footer={footer}
>
<Row>
<Col xs={12} id="fill_idpUrl">
<Field
label={<FormattedMessage id="ui-tenant-settings.settings.saml.idpUrl" />}
name="idpUrl"
id="samlconfig_idpUrl"
component={TextField}
required
fullWidth
validate={validateIdpUrl}
/>
<div hidden={!this.props.initialValues.metadataInvalidated}>
<FormattedMessage id="ui-tenant-settings.settings.saml.idpUrlChanged" />
</div>
<Button
onClick={this.downloadMetadata}
>
<FormattedMessage id="ui-tenant-settings.settings.saml.downloadMetadata" />
</Button>
</Col>
</Row>
<Row>
<Col id="select_samlBinding">
<Field
label={<FormattedMessage id="ui-tenant-settings.settings.saml.binding" />}
name="samlBinding"
id="samlconfig_samlBinding"
placeholder="---"
component={Select}
dataOptions={samlBindingOptions}
fullWidth
required
/>
</Col>
</Row>
<Row>
<Col id="fill_attribute">
<Field
label={<FormattedMessage id="ui-tenant-settings.settings.saml.attribute" />}
name="samlAttribute"
id="samlconfig_samlAttribute"
component={TextField}
required
fullWidth
/>
</Col>
</Row>
<Row>
<Col id="select_userProperty">
<Field
label={<FormattedMessage id="ui-tenant-settings.settings.saml.userProperty" />}
name="userProperty"
id="samlconfig_userProperty"
placeholder="---"
component={Select}
dataOptions={identifierOptions}
fullWidth
required
/>
</Col>
</Row>
</Pane>
</form>
);
}
}
export default stripesFinalForm({
validate,
navigationCheck: true,
})(SamlForm);