// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import React from 'react';
import AdminSettings from './admin_settings.jsx';
import BooleanSetting from './boolean_setting.jsx';
import TextSetting from './text_setting.jsx';
import FileUploadSetting from './file_upload_setting.jsx';
import RemoveFileSetting from './remove_file_setting.jsx';
import {FormattedMessage, FormattedHTMLMessage} from 'react-intl';
import SettingsGroup from './settings_group.jsx';
import * as Utils from 'utils/utils.jsx';
import * as AdminActions from 'actions/admin_actions.jsx';
export default class SamlSettings extends AdminSettings {
constructor(props) {
super(props);
this.getConfigFromState = this.getConfigFromState.bind(this);
this.renderSettings = this.renderSettings.bind(this);
this.uploadCertificate = this.uploadCertificate.bind(this);
this.removeCertificate = this.removeCertificate.bind(this);
}
getConfigFromState(config) {
config.SamlSettings.Enable = this.state.enable;
config.SamlSettings.Verify = this.state.verify;
config.SamlSettings.Encrypt = this.state.encrypt;
config.SamlSettings.IdpUrl = this.state.idpUrl;
config.SamlSettings.IdpDescriptorUrl = this.state.idpDescriptorUrl;
config.SamlSettings.AssertionConsumerServiceURL = this.state.assertionConsumerServiceURL;
config.SamlSettings.IdpCertificateFile = this.state.idpCertificateFile;
config.SamlSettings.PublicCertificateFile = this.state.publicCertificateFile;
config.SamlSettings.PrivateKeyFile = this.state.privateKeyFile;
config.SamlSettings.FirstNameAttribute = this.state.firstNameAttribute;
config.SamlSettings.LastNameAttribute = this.state.lastNameAttribute;
config.SamlSettings.EmailAttribute = this.state.emailAttribute;
config.SamlSettings.UsernameAttribute = this.state.usernameAttribute;
config.SamlSettings.NicknameAttribute = this.state.nicknameAttribute;
config.SamlSettings.PositionAttribute = this.state.positionAttribute;
config.SamlSettings.LocaleAttribute = this.state.localeAttribute;
config.SamlSettings.LoginButtonText = this.state.loginButtonText;
return config;
}
getStateFromConfig(config) {
const settings = config.SamlSettings;
return {
enable: settings.Enable,
verify: settings.Verify,
encrypt: settings.Encrypt,
idpUrl: settings.IdpUrl,
idpDescriptorUrl: settings.IdpDescriptorUrl,
assertionConsumerServiceURL: settings.AssertionConsumerServiceURL,
idpCertificateFile: settings.IdpCertificateFile,
publicCertificateFile: settings.PublicCertificateFile,
privateKeyFile: settings.PrivateKeyFile,
firstNameAttribute: settings.FirstNameAttribute,
lastNameAttribute: settings.LastNameAttribute,
emailAttribute: settings.EmailAttribute,
usernameAttribute: settings.UsernameAttribute,
nicknameAttribute: settings.NicknameAttribute,
positionAttribute: settings.PositionAttribute,
localeAttribute: settings.LocaleAttribute,
loginButtonText: settings.LoginButtonText
};
}
componentWillMount() {
AdminActions.samlCertificateStatus(
(data) => {
const files = {};
if (!data.idp_certificate_file) {
files.idpCertificateFile = '';
}
if (!data.public_certificate_file) {
files.publicCertificateFile = '';
}
if (!data.private_key_file) {
files.privateKeyFile = '';
}
this.setState(files);
}
);
}
uploadCertificate(id, file, callback) {
const complete = () => {
const fileName = file.name;
this.handleChange(id, fileName);
this.setState({[id]: fileName, [`${id}Error`]: null});
if (callback && typeof callback === 'function') {
callback();
}
};
function fail(error) {
if (callback && typeof callback === 'function') {
callback(error.message);
}
}
if (id === 'idpCertificateFile') {
AdminActions.uploadIdpSamlCertificate(file, complete, fail);
} else if (id === 'publicCertificateFile') {
AdminActions.uploadPublicSamlCertificate(file, complete, fail);
} else if (id === 'privateKeyFile') {
AdminActions.uploadPrivateSamlCertificate(file, complete, fail);
}
}
removeCertificate(id, callback) {
const complete = () => {
this.handleChange(id, '');
this.setState({[id]: null, [`${id}Error`]: null});
};
const fail = (error) => {
if (callback && typeof callback === 'function') {
callback();
}
this.setState({[id]: null, [`${id}Error`]: error.message});
};
if (id === 'idpCertificateFile') {
AdminActions.removeIdpSamlCertificate(complete, fail);
} else if (id === 'publicCertificateFile') {
AdminActions.removePublicSamlCertificate(complete, fail);
} else if (id === 'privateKeyFile') {
AdminActions.removePrivateSamlCertificate(complete, fail);
}
}
renderTitle() {
return (
);
}
renderSettings() {
const licenseEnabled = global.window.mm_license.IsLicensed === 'true' && global.window.mm_license.SAML === 'true';
if (!licenseEnabled) {
return null;
}
let idpCert;
let privKey;
let pubCert;
if (this.state.idpCertificateFile) {
idpCert = (
}
helpText={
}
removeButtonText={Utils.localizeMessage('admin.saml.remove.idp_certificate', 'Remove Identity Provider Certificate')}
removingText={Utils.localizeMessage('admin.saml.removing.certificate', 'Removing Certificate...')}
fileName={this.state.idpCertificateFile}
onSubmit={this.removeCertificate}
disabled={!this.state.enable}
/>
);
} else {
idpCert = (
}
helpText={
}
uploadingText={Utils.localizeMessage('admin.saml.uploading.certificate', 'Uploading Certificate...')}
disabled={!this.state.enable}
fileType='.crt,.cer'
onSubmit={this.uploadCertificate}
error={this.state.idpCertificateFileError}
/>
);
}
if (this.state.privateKeyFile) {
privKey = (
}
helpText={
}
removeButtonText={Utils.localizeMessage('admin.saml.remove.privKey', 'Remove Service Provider Private Key')}
removingText={Utils.localizeMessage('admin.saml.removing.privKey', 'Removing Private Key...')}
fileName={this.state.privateKeyFile}
onSubmit={this.removeCertificate}
disabled={!this.state.enable || !this.state.encrypt}
/>
);
} else {
privKey = (
}
helpText={
}
uploadingText={Utils.localizeMessage('admin.saml.uploading.privateKey', 'Uploading Private Key...')}
disabled={!this.state.enable || !this.state.encrypt}
fileType='.key'
onSubmit={this.uploadCertificate}
error={this.state.privateKeyFileError}
/>
);
}
if (this.state.publicCertificateFile) {
pubCert = (
}
helpText={
}
removeButtonText={Utils.localizeMessage('admin.saml.remove.sp_certificate', 'Remove Service Provider Certificate')}
removingText={Utils.localizeMessage('admin.saml.removing.certificate', 'Removing Certificate...')}
fileName={this.state.publicCertificateFile}
onSubmit={this.removeCertificate}
disabled={!this.state.enable || !this.state.encrypt}
/>
);
} else {
pubCert = (
}
helpText={
}
uploadingText={Utils.localizeMessage('admin.saml.uploading.certificate', 'Uploading Certificate...')}
disabled={!this.state.enable || !this.state.encrypt}
fileType='.crt,.cer'
onSubmit={this.uploadCertificate}
error={this.state.publicCertificateFileError}
/>
);
}
return (
}
helpText={
}
value={this.state.enable}
onChange={this.handleChange}
/>
}
placeholder={Utils.localizeMessage('admin.saml.idpUrlEx', 'Ex "https://idp.example.org/SAML2/SSO/Login"')}
helpText={
}
value={this.state.idpUrl}
onChange={this.handleChange}
disabled={!this.state.enable}
/>
}
placeholder={Utils.localizeMessage('admin.saml.idpDescriptorUrlEx', 'Ex "https://idp.example.org/SAML2/issuer"')}
helpText={
}
value={this.state.idpDescriptorUrl}
onChange={this.handleChange}
disabled={!this.state.enable}
/>
{idpCert}
}
helpText={
}
value={this.state.verify}
disabled={!this.state.enable}
onChange={this.handleChange}
/>
}
placeholder={Utils.localizeMessage('admin.saml.assertionConsumerServiceURLEx', 'Ex "https:///login/sso/saml"')}
helpText={
}
value={this.state.assertionConsumerServiceURL}
onChange={this.handleChange}
disabled={!this.state.enable || !this.state.verify}
/>
}
helpText={
}
value={this.state.encrypt}
disabled={!this.state.enable}
onChange={this.handleChange}
/>
{privKey}
{pubCert}
}
placeholder={Utils.localizeMessage('admin.saml.emailAttrEx', 'Ex "Email" or "PrimaryEmail"')}
helpText={
}
value={this.state.emailAttribute}
onChange={this.handleChange}
disabled={!this.state.enable}
/>
}
placeholder={Utils.localizeMessage('admin.saml.usernameAttrEx', 'Ex "Username"')}
helpText={
}
value={this.state.usernameAttribute}
onChange={this.handleChange}
disabled={!this.state.enable}
/>
}
placeholder={Utils.localizeMessage('admin.saml.firstnameAttrEx', 'Ex "FirstName"')}
helpText={
}
value={this.state.firstNameAttribute}
onChange={this.handleChange}
disabled={!this.state.enable}
/>
}
placeholder={Utils.localizeMessage('admin.saml.lastnameAttrEx', 'Ex "LastName"')}
helpText={
}
value={this.state.lastNameAttribute}
onChange={this.handleChange}
disabled={!this.state.enable}
/>
}
placeholder={Utils.localizeMessage('admin.saml.nicknameAttrEx', 'Ex "Nickname"')}
helpText={
}
value={this.state.nicknameAttribute}
onChange={this.handleChange}
disabled={!this.state.enable}
/>
}
placeholder={Utils.localizeMessage('admin.saml.positionAttrEx', 'E.g.: "Role"')}
helpText={
}
value={this.state.positionAttribute}
onChange={this.handleChange}
disabled={!this.state.enable}
/>
}
placeholder={Utils.localizeMessage('admin.saml.localeAttrEx', 'Ex "Locale" or "PrimaryLanguage"')}
helpText={
}
value={this.state.localeAttribute}
onChange={this.handleChange}
disabled={!this.state.enable}
/>
}
placeholder={Utils.localizeMessage('admin.saml.loginButtonTextEx', 'Ex "With OKTA"')}
helpText={
}
value={this.state.loginButtonText}
onChange={this.handleChange}
disabled={!this.state.enable}
/>
);
}
}