// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import * as Utils from 'utils/utils.jsx';
import Constants from 'utils/constants.jsx';
import AdminSettings from './admin_settings.jsx';
import DropdownSetting from './dropdown_setting.jsx';
import SettingsGroup from './settings_group.jsx';
import TextSetting from './text_setting.jsx';
import React from 'react';
import {FormattedHTMLMessage, FormattedMessage} from 'react-intl';
export default class OAuthSettings extends AdminSettings {
constructor(props) {
super(props);
this.getConfigFromState = this.getConfigFromState.bind(this);
this.getStateFromConfig = this.getStateFromConfig.bind(this);
this.renderSettings = this.renderSettings.bind(this);
this.renderOffice365 = this.renderOffice365.bind(this);
this.renderGoogle = this.renderGoogle.bind(this);
this.renderGitLab = this.renderGitLab.bind(this);
this.changeType = this.changeType.bind(this);
}
getConfigFromState(config) {
config.GitLabSettings.Enable = false;
config.GoogleSettings.Enable = false;
config.Office365Settings.Enable = false;
if (this.state.oauthType === Constants.GITLAB_SERVICE) {
config.GitLabSettings.Enable = true;
config.GitLabSettings.Id = this.state.id;
config.GitLabSettings.Secret = this.state.secret;
config.GitLabSettings.UserApiEndpoint = this.state.userApiEndpoint;
config.GitLabSettings.AuthEndpoint = this.state.authEndpoint;
config.GitLabSettings.TokenEndpoint = this.state.tokenEndpoint;
}
if (this.state.oauthType === Constants.GOOGLE_SERVICE) {
config.GoogleSettings.Enable = true;
config.GoogleSettings.Id = this.state.id;
config.GoogleSettings.Secret = this.state.secret;
config.GoogleSettings.UserApiEndpoint = 'https://www.googleapis.com/plus/v1/people/me';
config.GoogleSettings.AuthEndpoint = 'https://accounts.google.com/o/oauth2/v2/auth';
config.GoogleSettings.TokenEndpoint = 'https://www.googleapis.com/oauth2/v4/token';
config.GoogleSettings.Scope = 'profile email';
}
if (this.state.oauthType === Constants.OFFICE365_SERVICE) {
config.Office365Settings.Enable = true;
config.Office365Settings.Id = this.state.id;
config.Office365Settings.Secret = this.state.secret;
config.Office365Settings.UserApiEndpoint = 'https://graph.microsoft.com/v1.0/me';
config.Office365Settings.AuthEndpoint = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
config.Office365Settings.TokenEndpoint = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
config.Office365Settings.Scope = 'User.Read';
}
return config;
}
getStateFromConfig(config) {
this.config = config;
let oauthType = 'off';
let settings = {};
if (config.GitLabSettings.Enable) {
oauthType = Constants.GITLAB_SERVICE;
settings = config.GitLabSettings;
} else if (config.GoogleSettings.Enable) {
oauthType = Constants.GOOGLE_SERVICE;
settings = config.GoogleSettings;
} else if (config.Office365Settings.Enable) {
oauthType = Constants.OFFICE365_SERVICE;
settings = config.Office365Settings;
}
return {
oauthType,
id: settings.Id,
secret: settings.Secret,
userApiEndpoint: settings.UserApiEndpoint,
authEndpoint: settings.AuthEndpoint,
tokenEndpoint: settings.TokenEndpoint
};
}
changeType(id, value) {
let settings = {};
if (value === Constants.GITLAB_SERVICE) {
settings = this.config.GitLabSettings;
} else if (value === Constants.GOOGLE_SERVICE) {
settings = this.config.GoogleSettings;
} else if (value === Constants.OFFICE365_SERVICE) {
settings = this.config.Office365Settings;
}
this.setState({
id: settings.Id,
secret: settings.Secret,
userApiEndpoint: settings.UserApiEndpoint,
authEndpoint: settings.AuthEndpoint,
tokenEndpoint: settings.TokenEndpoint
});
this.handleChange(id, value);
}
renderTitle() {
return (