Skip to content
Snippets Groups Projects
Commit de5fe6ea authored by Alex Barth's avatar Alex Barth
Browse files

Roll back HTTP Basic Auth UI support.

Basic auth can be used by supplying URLs like http://user:pw@example.com/feed .
The authentication in URL approach is powerful enough for most use cases. The only advantage
that the UI that is about to be removed here would have had is that it could change
basic auth credentials for a series of feeds in a single spot. Will post this patch
on the issue queue for further consideration.
parent acf53eae
No related branches found
No related tags found
No related merge requests found
......@@ -6,10 +6,6 @@
* Home of the FeedsHTTPFetcher and related classes.
*/
define('FEEDS_HTTP_NO_BASIC_AUTH', 0);
define('FEEDS_HTTP_BASIC_AUTH_PER_SOURCE', 1);
define('FEEDS_HTTP_BASIC_AUTH_PER_IMPORTER', 2);
/**
* Definition of the import batch object created on the fetching stage by
* FeedsHTTPFetcher.
......@@ -63,9 +59,7 @@ class FeedsHTTPFetcher extends FeedsFetcher {
*/
public function fetch(FeedsSource $source) {
$source_config = $source->getConfigFor($this);
$url = $source_config['source'];
$url = $this->basicAuth($url, isset($source_config['basic_auth']) ? $source_config['basic_auth'] : array());
return new FeedsHTTPBatch($url);
return new FeedsHTTPBatch($source_config['source']);
}
/**
......@@ -91,16 +85,6 @@ class FeedsHTTPFetcher extends FeedsFetcher {
'#maxlength' => NULL,
'#required' => TRUE,
);
if (isset($this->config['basic_auth']) && $this->config['basic_auth'] == FEEDS_HTTP_BASIC_AUTH_PER_SOURCE) {
$form['basic_auth'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => empty($source_config['basic_auth']['basic_auth_user']),
'#title' => t('Authentication'),
'#description' => t('Enter user name and password for authentication. Leave empty if no authentication is required.'),
);
$form['basic_auth'] += $this->basicAuthForm(isset($source_config['basic_auth']) ? $source_config['basic_auth'] : array());
}
return $form;
}
......@@ -108,90 +92,7 @@ class FeedsHTTPFetcher extends FeedsFetcher {
* Override parent::configDefaults().
*/
public function configDefaults() {
return array(
'basic_auth' => FEEDS_HTTP_NO_BASIC_AUTH,
'basic_auth_user' => '',
'basic_auth_password' => '',
);
}
/**
* Override parent::configForm();
*/
public function configForm() {
$form = array();
$form['basic_auth'] = array(
'#type' => 'fieldset',
'#title' => t('HTTP Basic Authentication'),
);
$form['basic_auth']['basic_auth'] = array(
'#type' => 'radios',
'#options' => array(
FEEDS_HTTP_NO_BASIC_AUTH => t('No authentication'),
FEEDS_HTTP_BASIC_AUTH_PER_SOURCE => t('Specify credentials when creating a feed.'),
FEEDS_HTTP_BASIC_AUTH_PER_IMPORTER => t('One set of credentials for all feeds.'),
),
'#default_value' => isset($this->config['basic_auth']) ? $this->config['basic_auth'] : FEEDS_HTTP_NO_BASIC_AUTH,
);
$form['basic_auth'] += $this->basicAuthForm($this->config);
return $form;
}
/**
* Validate config form.
*/
public function configFormValidate(&$values) {
// Don't accidentally wipe out password.
if (empty($values['basic_auth_password']) && !empty($this->config['basic_auth_password'])) {
$values['basic_auth_password'] = $this->config['basic_auth_password'];
}
if ($values['basic_auth'] != FEEDS_HTTP_BASIC_AUTH_PER_IMPORTER) {
$values['basic_auth_user'] = '';
$values['basic_auth_password'] = '';
}
}
/**
* Basic auth form.
*/
protected function basicAuthForm($config) {
$form = array();
$form['basic_auth_user'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => empty($config['basic_auth_user']) ? '' : $config['basic_auth_user'],
);
$form['basic_auth_password'] = array(
'#type' => 'password',
'#title' => t('Password'),
);
return $form;
}
/**
* Encode basic authentication credentials in URL depending on configuration.
*/
protected function basicAuth($url, $source_config = array()) {
if (!isset($this->config['basic_auth'])) {
return $url;
}
elseif ($this->config['basic_auth'] == FEEDS_HTTP_BASIC_AUTH_PER_SOURCE) {
if (!empty($source_config['basic_auth_user']) && !empty($source_config['basic_auth_password'])) {
return $this->basicAuthEncodeCredentials($url, $source_config['basic_auth_user'], $source_config['basic_auth_password']);
}
}
elseif ($this->config['basic_auth'] == FEEDS_HTTP_BASIC_AUTH_PER_IMPORTER) {
return $this->basicAuthEncodeCredentials($url, $this->config['basic_auth_user'], $this->config['basic_auth_password']);
}
return $url;
}
/**
* Encode basic authentication credentials into URL.
*/
protected function basicAuthEncodeCredentials($url, $user, $password) {
$parsed = parse_url($url);
return str_replace("{$parsed['scheme']}://", "{$parsed['scheme']}://$user:$password@", $url);
return array('auto_detect_feeds' => FALSE);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment