0

I want to log in to WordPress dashboard with http X-User header provided by reverse proxy server.

I'm planning to use WordPress as a CMS where our organization members can freely write articles. To maximize user experience, I want to allow users to login to the dashboard with our organization account. Our servers are constructed on Docker containers behind one nginx reverse proxy server container, which checks the request by questioning to the authentication server, and notify the application server by adding X-User header.

Here's my nginx.conf

location /_login {
  internal;
  proxy_pass http://auth-server/path/to/api;
}
location @unauthorized {
  internal;
  return 302 http://auth-client.example.com/path/to/login-form
}
location / {
  auth_request /_login;
  auth_request_set $user $upstream_http_x_user; 
  proxy_set_header X-User $user;
  proxy_pass http://wordpress/;
  error_page 403 @unauthorized;
}

Is there any appropriate plugin to do that? Or I have to create a new plugin?

1 Answer 1

1

Finally I found a solution.

Create wp-content/plugins/some-plugin.php:

<?php
/*
Plugin Name: Some Plugin
*/

add_action('login_init', function() {
    if ($_SERVER['HTTP_X_USER']) {
        $user = get_user_by('login', $_SERVER['HTTP_X_USER']);
        if ($user) {
            wp_clear_auth_cookie();
            wp_set_auth_cookie($user->ID);
            do_action('wp_login', $user->login, $user);
            wp_safe_redirect(isset($_GET['redirect_to']) ? $_GET['redirect_to'] : admin_url());
            exit;
        }
    }
}, 1);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.