You are here

class ServicesClientConnectionSessionAuth in Services Client 7

Same name and namespace in other branches
  1. 7.2 services_client_connection/plugins/ServicesClientConnectionSessionAuth.inc \ServicesClientConnectionSessionAuth

@file Session authentication for 3.x version

Hierarchy

Expanded class hierarchy of ServicesClientConnectionSessionAuth

1 string reference to 'ServicesClientConnectionSessionAuth'
_services_client_connection_auth in services_client_connection/include/plugin_definition.inc
List of auth plugins provided by module

File

services_client_connection/plugins/ServicesClientConnectionSessionAuth.inc, line 8
Session authentication for 3.x version

View source
class ServicesClientConnectionSessionAuth extends ServicesClientConnectionAuth {

  /**
   * Session ID
   */
  protected $sessid = NULL;

  /**
   * Session name
   */
  protected $session_name = NULL;

  /**
   * Logged in remote user
   */
  protected $user = NULL;

  /**
   * CSFR token.
   */
  protected $csrf_token = NULL;

  /**
   * Implements configForm().
   */
  public function configForm(&$form, &$form_state) {
    $form['username'] = array(
      '#type' => 'textfield',
      '#title' => t('Username'),
      '#default_value' => isset($this->config['username']) ? $this->config['username'] : '',
    );
    $form['password'] = array(
      '#type' => 'textfield',
      '#title' => t('Password'),
      '#default_value' => isset($this->config['password']) ? $this->config['password'] : '',
    );
    $form['token'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use tokens'),
      '#description' => t("Services 3.5+ requires CSRF tokens in each request."),
      '#default_value' => isset($this->config['token']) ? $this->config['token'] : '',
    );
  }

  /**
   * Implements configFormSubmit().
   */
  public function configFormSubmit(&$form, &$form_state) {
    parent::configFormSubmit($form, $form_state);
    $form_state['config']['username'] = $form_state['values']['username'];
    $form_state['config']['password'] = $form_state['values']['password'];
    $form_state['config']['token'] = $form_state['values']['token'];
  }

  /**
   * Implements login().
   */
  public function login() {
    $response = $this->client
      ->action('user', 'login', array(
      'username' => $this->config['username'],
      'password' => $this->config['password'],
    ));
    $this->sessid = $response['sessid'];
    $this->session_name = $response['session_name'];
    $this->user = $response['user'];

    // If configured to use CSRF token retrieve token first.
    if (!empty($this->config['token'])) {
      $response = $this->client
        ->action('user', 'token');
      if (!empty($response['token'])) {
        $this->csrf_token = $response['token'];
      }
    }
  }

  /**
   * Implements logout().
   */
  public function logout() {
    if ($this->sessid) {
      $response = $this->client
        ->action('user', 'logout');
      $this->sessid = NULL;
      $this->session_name = NULL;
      $this->user = NULL;
      $this->csrf_token = NULL;
    }
  }

  /**
   * Implements prepareRequest().
   *
   * @param ServicesClientConnectionHttpRequest $request
   */
  public function prepareRequest(ServicesClientConnectionHttpRequest &$request) {
    parent::prepareRequest($request);
    if ($this->sessid) {
      $request->cookie[] = $this->session_name . '=' . $this->sessid;
    }
    if ($this->csrf_token) {
      $request->http_headers['X-CSRF-Token'] = $this->csrf_token;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ServicesClientConnectionAuth::connect public function Make initial conneciton to client
ServicesClientConnectionPlugin::$client protected property Reference to client
ServicesClientConnectionPlugin::$config protected property Plugin specific configuration
ServicesClientConnectionPlugin::$connection protected property Connection definition
ServicesClientConnectionPlugin::configFormValidate public function 1
ServicesClientConnectionPlugin::processResponse public function 2
ServicesClientConnectionPlugin::__construct public function 1
ServicesClientConnectionSessionAuth::$csrf_token protected property CSFR token.
ServicesClientConnectionSessionAuth::$sessid protected property Session ID
ServicesClientConnectionSessionAuth::$session_name protected property Session name
ServicesClientConnectionSessionAuth::$user protected property Logged in remote user
ServicesClientConnectionSessionAuth::configForm public function Implements configForm(). Overrides ServicesClientConnectionPlugin::configForm
ServicesClientConnectionSessionAuth::configFormSubmit public function Implements configFormSubmit(). Overrides ServicesClientConnectionPlugin::configFormSubmit
ServicesClientConnectionSessionAuth::login public function Implements login(). Overrides ServicesClientConnectionAuth::login
ServicesClientConnectionSessionAuth::logout public function Implements logout(). Overrides ServicesClientConnectionAuth::logout
ServicesClientConnectionSessionAuth::prepareRequest public function Implements prepareRequest(). Overrides ServicesClientConnectionPlugin::prepareRequest