View source
<?php
module_load_include('inc', 'deploy', 'includes/DeployAuthenticator');
class DeployRemoteCcAuthenticatorSession implements \DeployAuthenticator {
public $config = array();
public function __construct(\DeployService $service, array $config = array()) {
$this->service = $service;
$this->config += array(
'username' => '',
'password' => '',
);
$this->config = array_merge($this->config, $config);
$this->context = $this->service
->getContext();
}
public function deploy(\Traversable $iterator) {
list($response, $token) = $this
->logIn();
$this->service
->deploy($iterator);
$this
->logOut($response, $token);
}
public function configForm(&$form_state) {
return array(
'username' => array(
'#type' => 'textfield',
'#title' => t('Username'),
'#description' => t('Enter the username that you want to authenticate with on this endpoint.'),
'#default_value' => $this->config['username'],
),
'password' => array(
'#type' => 'password',
'#title' => t('Password'),
'#description' => t('Enter the password that you want to authenticate with on this endpoint.'),
'#default_value' => $this->config['password'],
),
);
}
public function logIn() {
$response = new \stdClass();
$token = array();
if (!empty($this->config['username'])) {
$login_url = $this->service->config['url'] . '/user/login';
$options = array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json',
),
'data' => drupal_json_encode(array(
'username' => $this->config['username'],
'password' => $this->config['password'],
)),
'context' => $this->context,
);
if ($this->config['debug']) {
watchdog('deploy', 'Authentication request: %url <pre>@options</pre>', array(
'%url' => $login_url,
'@options' => print_r($options, TRUE),
), WATCHDOG_DEBUG);
}
$response = drupal_http_request($login_url, $options);
if ($this->config['debug']) {
watchdog('deploy', 'Authentication response: <pre>@response</pre>', array(
'@response' => print_r($response, TRUE),
), WATCHDOG_DEBUG);
}
if (isset($response->error) || !in_array($response->code, array(
200,
304,
))) {
throw new \DeployAuthenticationException(t('Authentication error: @code @error', array(
'@code' => $response->code,
'@error' => $response->error,
)));
}
$response_data = drupal_json_decode($response->data);
if ($response_data['session_name'] && $response_data['sessid']) {
$this->service->config['headers']['Cookie'] = $response_data['session_name'] . "=" . $response_data['sessid'] . ";";
$token = isset($response_data['token']) ? $response_data['token'] : NULL;
if (!$token) {
$user = NULL;
$parts = parse_url($this->service->config['url']);
$port = '';
if (isset($parts['port'])) {
$port = ':' . $parts['port'];
}
if (isset($parts['user'])) {
$user = $parts['user'];
}
if (isset($parts['pass'])) {
$pass = $parts['pass'];
}
$token_url = $parts['scheme'] . '://' . ($user ? $user . ':' . $pass . '@' : '') . $parts['host'] . $port . '/services/session/token';
$options = array(
'method' => 'GET',
'headers' => $this->service->config['headers'],
'context' => $this->context,
);
$token_response = drupal_http_request($token_url, $options);
if (isset($token_response->error) || !in_array($token_response->code, array(
200,
304,
))) {
throw new \DeployAuthenticationException(t('Authentication error: @code @error', array(
'@code' => $token_response->code,
'@error' => t('Failed to retrieve CSRF token.'),
)));
}
if ($this->config['debug']) {
watchdog('deploy', 'Session CSRF Token response: @response', array(
'@response' => print_r($token_response, TRUE),
), WATCHDOG_DEBUG);
}
$token = trim($token_response->data);
}
$this->service->config['headers']['X-CSRF-Token'] = $token;
}
else {
throw new \DeployAuthenticationException(t("No session was returned from the authentication request."));
}
}
else {
watchdog('deploy', 'Deployment authentication was done without user credentials.', array(), WATCHDOG_WARNING);
}
return array(
$response,
$token,
);
}
public function logOut($response, $token) {
$logout_url = $this->service->config['url'] . '/user/logout';
if (!empty($this->service->config['headers']['Cookie'])) {
$options = array(
'method' => 'POST',
'headers' => array(
'Cookie' => $response->headers['set-cookie'],
'X-CSRF-Token' => $token,
),
'context' => $this->context,
);
$response = drupal_http_request($logout_url, $options);
if ($this->config['debug']) {
watchdog('deploy', 'Logout response: <pre>@response</pre>', array(
'@response' => print_r($response, TRUE),
), WATCHDOG_DEBUG);
}
}
}
}