<commit_before><?php
/*
  Plugin Name: Portwise Authentication
  Plugin URI: https://github.com/malmostad/intranet-wordpress-themes
  Description: Authentication with Portwise reveresed proxy http headers method
  Version: 0.0.1
  Author: <NAME>
  Author URI: https://github.com/malmostad

  Define the following constants in wp-config.php:
    define('PORTWISE_IP_ADDRESS', '172.16.58.3');
    define('PORTWISE_TOKEN', '<PASSWORD> token');
    define('PORTWISE_REQUIRE_SSL', true);
    define('PORTWISE_SIGNOUT_URL', 'https://sso.example.org/wa/logout');
*/

$portwise = new PortwiseAuthentication();

class PortwiseAuthentication {
  function __construct() {
    add_action('after_setup_theme', array($this, 'signon'));
    add_action('wp_logout', array($this, 'signout'));
    error_log("TRUST: " . $this->trust_request());
  }

  public function signon() {
    error_log("portwise_authentication");
    if (!is_user_logged_in()) {
      error_log("START AUTH");
      $username = 'admin';
      if (get_user_by('login', $username)) {
        $user = get_user_by('login', $username);
        wp_set_current_user($user->ID, $user->user_login);
        wp_set_auth_cookie($user->ID, false, PORTWISE_REQUIRE_SSL);
        do_action('wp_login', $user->user_login);
        error_log("is_user_logged_in: " . is_user_logged_in());
      }
    }
  }

  public function signout() {
    wp_clear_auth_cookie();
    wp_redirect(PORTWISE_SIGNOUT_URL);
    exit;
  }

  private function trust_request() {
    return ($_SERVER['REMOTE_ADDR'] == PORTWISE_IP_ADDRESS && // HTTP_CLIENT_IP HTTP_X_FORWARDED_FOR
        $_SERVER['HTTP_X_TOKEN'] == PORTWISE_TOKEN &&
        !empty($_SERVER['HTTP_X_UID']) &&
        (PORTWISE_REQUIRE_SSL ? $_SERVER['HTTPS'] == "on" : true));
  }

  private function add_user($userdata) {
    wp_insert_user($userdata);
  }

  private function update_user($userdata) {
    wp_update_user($userdata);
  }
}<commit_msg>Create and update authenticated user<commit_after><?php
/*
  Plugin Name: Portwise Authentication
  Plugin URI: https://github.com/malmostad/intranet-wordpress-themes
  Description: Authentication with Portwise reveresed proxy http headers method
               If user is present, it is updated with attributes from Portwise
               If user isn't present, it is created with attributes from Portwise
  Version: 0.0.1
  Author: <NAME>
  Author URI: https://github.com/malmostad

  Define the following constants in wp-config.php:
    define('PORTWISE_IP_ADDRESS', '172.16.58.3');
    define('PORTWISE_TOKEN', '<PASSWORD>');
    define('PORTWISE_REQUIRE_SSL', true);
    define('PORTWISE_SIGNOUT_URL', 'https://sso.example.org/wa/logout');
*/

$portwise = new PortwiseAuthentication();

class PortwiseAuthentication {
  function __construct() {
    add_action('after_setup_theme', array($this, 'signon'));
    add_action('wp_logout', array($this, 'signout'));
    $_SERVER['HTTP_X_TOKEN'] = PORTWISE_TOKEN;
    $_SERVER['HTTP_X_UID'] = "martha2";
  }

  public function signon() {
    error_log("portwise_authentication");
    if ($this->trust_request() && !is_user_logged_in()) {
      error_log("START AUTH");

      if (username_exists($this->username)) {
        $user = get_user_by('login', $this->$username);
        $this->update_user($user);
      } else {
        $user = $this->add_user();
      }

      if ($user) {
        wp_set_current_user($user->ID, $user->user_login);
        wp_set_auth_cookie($user->ID, false, PORTWISE_REQUIRE_SSL);
        do_action('wp_login', $user->user_login);
      }
    }
  }

  public function signout() {
    wp_clear_auth_cookie();
    wp_redirect(PORTWISE_SIGNOUT_URL);
    exit;
  }

  private function trust_request() {
    return ($_SERVER['REMOTE_ADDR'] == PORTWISE_IP_ADDRESS && // HTTP_CLIENT_IP HTTP_X_FORWARDED_FOR
        $_SERVER['HTTP_X_TOKEN'] == PORTWISE_TOKEN &&
        isset($_SERVER['HTTP_X_UID']) &&
        (PORTWISE_REQUIRE_SSL ? $_SERVER['HTTPS'] == "on" : true));
  }

  private function add_user() {
    $id = wp_insert_user(array(
      'user_login'   => $this->username(),
      'user_email'   => $this->email(),
      'nickname'     => $this->nickname(),
      'display_name' => $this->display_name(),
      'user_pass'    => <PASSWORD>(64, true, true) // unusable pw
    ));
    return get_user_by('id', $id);
  }

  private function update_user($user) {
    return wp_update_user(array(
      'id'           => $user->ID,
      'user_email'   => $this->email(),
      'nickname'     => $this->nickname(),
      'display_name' => $this->display_name()
    ));
  }

  private function username() {
    return $_SERVER['HTTP_X_UID'];
  }

  private function display_name() {
    return $_SERVER['HTTP_X_DISPLAYNAME']? $_SERVER['HTTP_X_DISPLAYNAME'] : $_SERVER['HTTP_X_UID'];
  }

  private function nickname() {
    return $this->displayname();
  }

  private function email() {
    return $_SERVER['HTTP_X_EMAIL']? $_SERVER['HTTP_X_EMAIL'] : "{$_SERVER['HTTP_X_UID']}@malmo.se";
  }
}
{'fraction_non_alphanumeric': 0.1519392243102759, 'fraction_numerical': 0.004998000799680128, 'mean_word_length': 3.728733459357278, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 40, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '17802', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1658, 'n_words': 296}