2017-08-10 01:56:27 +00:00
|
|
|
import React, {PureComponent} from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-03-23 22:10:42 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2017-03-28 22:45:23 +00:00
|
|
|
import {Dropdown, DropdownButton, DropdownDivider, DropdownItem} from '../base/dropdown';
|
2017-03-23 22:10:42 +00:00
|
|
|
import {trackEvent} from '../../../analytics';
|
|
|
|
import {showModal} from '../modals';
|
|
|
|
import AlertModal from '../modals/alert-modal';
|
|
|
|
import * as models from '../../../models';
|
2017-07-31 17:29:36 +00:00
|
|
|
import {AUTH_BASIC, AUTH_DIGEST, AUTH_BEARER, AUTH_NONE, AUTH_NTLM, AUTH_OAUTH_1, AUTH_OAUTH_2, AUTH_AWS_IAM, AUTH_NETRC, getAuthTypeName} from '../../../common/constants';
|
2017-03-23 22:10:42 +00:00
|
|
|
|
|
|
|
@autobind
|
|
|
|
class AuthDropdown extends PureComponent {
|
|
|
|
async _handleTypeChange (type) {
|
|
|
|
if (type === this.props.authentication.type) {
|
|
|
|
// Type didn't change
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
const newAuthentication = models.request.newAuth(type, this.props.authentication);
|
2017-03-23 22:10:42 +00:00
|
|
|
const defaultAuthentication = models.request.newAuth(this.props.authentication.type);
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
// Prompt the user if fields will change between new and old
|
2017-03-23 22:10:42 +00:00
|
|
|
for (const key of Object.keys(this.props.authentication)) {
|
2017-03-28 22:45:23 +00:00
|
|
|
if (key === 'type') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
const value = this.props.authentication[key];
|
2017-03-28 22:45:23 +00:00
|
|
|
const changedSinceDefault = defaultAuthentication[key] !== value;
|
|
|
|
const willChange = newAuthentication[key] !== value;
|
|
|
|
|
|
|
|
if (changedSinceDefault && willChange) {
|
2017-03-23 22:10:42 +00:00
|
|
|
await showModal(AlertModal, {
|
2017-03-28 22:45:23 +00:00
|
|
|
title: 'Switch Authentication?',
|
|
|
|
message: 'Current authentication settings will be lost',
|
2017-03-23 22:10:42 +00:00
|
|
|
addCancel: true
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trackEvent('Request', 'Auth Type Change', newAuthentication.type);
|
|
|
|
this.props.onChange(newAuthentication);
|
|
|
|
}
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
renderAuthType (type, nameOverride = null) {
|
|
|
|
const currentType = this.props.authentication.type || AUTH_NONE;
|
|
|
|
return (
|
|
|
|
<DropdownItem onClick={this._handleTypeChange} value={type}>
|
|
|
|
{currentType === type ? <i className="fa fa-check"/> : <i className="fa fa-empty"/>}
|
|
|
|
{' '}
|
|
|
|
{nameOverride || getAuthTypeName(type, true)}
|
|
|
|
</DropdownItem>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
render () {
|
2017-03-28 22:45:23 +00:00
|
|
|
const {children, className} = this.props;
|
2017-03-23 22:10:42 +00:00
|
|
|
return (
|
2017-06-16 22:21:41 +00:00
|
|
|
<Dropdown beside debug="true">
|
2017-03-28 22:45:23 +00:00
|
|
|
<DropdownDivider>Auth Types</DropdownDivider>
|
2017-03-23 22:10:42 +00:00
|
|
|
<DropdownButton className={className}>
|
|
|
|
{children}
|
|
|
|
</DropdownButton>
|
2017-03-28 22:45:23 +00:00
|
|
|
{this.renderAuthType(AUTH_BASIC)}
|
|
|
|
{this.renderAuthType(AUTH_OAUTH_1)}
|
|
|
|
{this.renderAuthType(AUTH_OAUTH_2)}
|
|
|
|
{this.renderAuthType(AUTH_DIGEST)}
|
2017-06-01 13:18:42 +00:00
|
|
|
{this.renderAuthType(AUTH_BEARER)}
|
2017-03-28 22:45:23 +00:00
|
|
|
{this.renderAuthType(AUTH_NTLM)}
|
2017-07-12 21:01:14 +00:00
|
|
|
{this.renderAuthType(AUTH_AWS_IAM)}
|
2017-07-31 17:29:36 +00:00
|
|
|
{this.renderAuthType(AUTH_NETRC)}
|
2017-03-28 22:45:23 +00:00
|
|
|
<DropdownDivider>Other</DropdownDivider>
|
|
|
|
{this.renderAuthType(AUTH_NONE, 'No Authentication')}
|
2017-03-23 22:10:42 +00:00
|
|
|
</Dropdown>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AuthDropdown.propTypes = {
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
authentication: PropTypes.object.isRequired,
|
|
|
|
|
|
|
|
// Optional
|
|
|
|
className: PropTypes.string,
|
|
|
|
children: PropTypes.node
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AuthDropdown;
|