Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

New Plugin: Provide HTTP_AUTH Authentication for Piwik - Release in Marketplace #514

Closed
robocoder opened this Issue · 40 comments

3 participants

Anthon Pang Anonymous Piwik user Matthieu Aubry
Anthon Pang
Collaborator

Plugin that forwards the current HTTP_AUTH’ed user to Piwik

  • What is HttpAuthLogin plugin ?

It’s a plug-in that authentifies users based on HTTP_AUTH. HTTP_AUTH is a mechanism provided by the webserver itself (I tested this with Apache). The webserver prompts the user for a login and password, matches it to a database (or a file, you decide), and if the authentication is successful, it lets you access the current logged-in user from PHP.

  • What use cases is HttpAuthLogin useful ?

If you have several web services running on the same machine, you can achieve Single Sign-On. For instance, if you run Piwik, a bug tracker, and a WebDAV disk space on the same web server, you could log-in once on the bug tracker, and be automagically logged-in on Piwik.

HTTP_AUTH is extremely widely supported. Even command-line tools like cURL or wget support it. If for some reason you’d like to write a script that fetches stuff out of Piwik, but you’d like it to be authentified, it’d be a lot easier to do with HttpAuthPlugIn.

Contributed by Romain Goyet.

Requires review per [wiki:CodingStandard].

Keywords: third-party-plugin

Anthon Pang
Collaborator

New in version 0.2:

  • Should conform to coding standard
  • Compatible with 0.2.37
  • Logout link

Extract into plugins folder and activate via Settings | Plugins menu.

Anthon Pang
Collaborator

Note: The AuthName from your .htaccess file is not passed to PHP. If you need to change the realm name, "Piwik" is hard-coded in plugins/HttpAuthLogin/Controller.php.

Anthon Pang
Collaborator

Note: token_auth authentication is not available with this plugin

Anthon Pang
Collaborator

A sample .htaccess file can be found in: http://forum.piwik.org/index.php?showtopic=4431

Anthon Pang
Collaborator

But I tested the updated plugin on the 1.0 branch, and it worked fine there.

Anonymous Piwik user

I've just had the time to try the HttpAuthLogin plugin with version 1.0. I logged in as administrator, enabled the plugin and when I reload the page it throws me back to the Piwik login screen. I previously authenticated with the webserver using a different username then administrator, but this username doesn't exist in Piwik. I tried clearing my HTTP auth information and then loading piwik again. I logged in to Apache again but this time Piwik falls into an infinite redirect loop.

Can you give a brief description on how exactly to use this plugin? I use LDAP for authentication in Apache and usernames are user@domain.com. I tried manually creating a user account in Piwik to match my email address, but it wouldn't allow me to use the @ character in the username. Thanks, -Tim

Anthon Pang
Collaborator

Tim: this plugin requires that you set up an .htaccess file and password file. In your case, shouldn't you be using #734?

Anonymous Piwik user

Replying to vipsoft:

Tim: this plugin requires that you set up an .htaccess file and password file. In your case, shouldn't you be using #734?

For one, I'd prefer to stick with multi-layers of security: Password protect all of Piwik with Apache HTTP authentication (over SSL of course) while still having Piwik user accounts handle access control to the analytics data for each site. But I was hoping this plugin would pass along the credentials from HTTP authentication to Piwik. Isn't this what was intended?

As for using the plugin for LDAP authentication, it seemed more complicated to setup and hasn't been updated in 13 months (maybe that just means it's working as expected). Since I already had Apache HTTP authentication setup using LDAP, I thought it would be easier to use the HTTP_AUTH plugin.

-Tim

Anonymous Piwik user

That plugins works fairly well except one big things : the .htaccess breaks the API access with token_auth.

How could it be made so it doesn't break the token_auth ?

Anonymous Piwik user

mmm, it seems that, as soon as you enable this plugin (even without any .htaccess file), the token_auth access is broken. (SitesManager.getSitesWithAdminAccess always returns an empty value)

Matthieu Aubry
Owner

See #1766 for a patch to a possible bug in this plugin.

Anthon Pang
Collaborator

Changelog:

  • 0.3.2 - Piwik_HttpAuthLogin_Auth now extends Piwik_Login_Auth, and calls parent::authenticate() as a fallback for token_auth access; this is a bit cleaner than the fix in #1766
Anonymous Piwik user

Like Tim I also had the redirect loop problem.
I looked into the sources and just added another if else block to make it work on my apache server with mod_sspi authentication.

Auth.php:
...

if(isset($_SERVER['PHP_AUTH_USER']))
{
    $httpLogin = $_SERVER['PHP_AUTH_USER'];
}
else if(isset($_SERVER['HTTP_AUTH_USER']))
{
    $httpLogin = $_SERVER['HTTP_AUTH_USER'];
}
else if(isset($_SERVER['REMOTE_USER']))
{
    $httpLogin = $_SERVER['REMOTE_USER'];
}

...

Anthon Pang
Collaborator

Changelog:

  • 0.3.3 - added domruf's patch for mod_sspi
Anthon Pang
Collaborator

Attachment: v0.3.4
HttpAuthLogin.zip

Anthon Pang
Collaborator

Changelog for 0.3.4

  • use $_ENV instead of $_SERVER in case variables_order excludes 'E'
  • added AUTH_USER and REDIRECT_REMOVE_USER
  • removed HTTP_AUTH_USER
Anonymous Piwik user

This should get listed in the list of plugins b/c this is very valuable for any enterprise organization.

Anonymous Piwik user

Maybe someone is running into similar problems: At first I couldn't get it to work with a clean PIWIK 1.7.1 install, because I always ran into a redirect loop problem, when I activated the plugin.

After a lot of trial and error I found out that I have to deactivate the standard authentication and after that insert:

PluginsInstalled[] = "HttpAuthLogin"
Plugins[] = "HttpAuthLogin"

in the config/config.ini.php manually. It works now!

Anonymous Piwik user

I've found that not setting token auth upon authentication breaks some functionality as the superuser. So I wrote an updated version of Auth.php that sets the token_auth (instead of NULL) on successful authentication, and also replaces the Zend_Registry calls with the appropriate calls Piwik_config::getInstance() and Piwik_FetchOne.

Anonymous Piwik user
    public function authenticate()
    {
        $rootLogin = Piwik_config::getInstance()->superuser['login'];
        $rootPassword = Piwik_Config::getInstance()->superuser['password'];

        $httpLogin = null;
        if(isset($_SERVER['PHP_AUTH_USER']))
        {
            $httpLogin = $_SERVER['PHP_AUTH_USER'];
        }
        else if(isset($_ENV['AUTH_USER']))
        {
            $httpLogin = $_ENV['AUTH_USER'];
        }
        else if(isset($_ENV['REMOTE_USER']))
        {
            $httpLogin = $_ENV['REMOTE_USER'];
        }
        else if(isset($_ENV['REDIRECT_REMOTE_USER']))
        {
            $httpLogin = $_ENV['REDIRECT_REMOTE_USER'];
        }

        if(isset($httpLogin))
        {
            if($httpLogin === $rootLogin)
            {
                $rootToken = Piwik_UsersManager_API::getInstance()->getTokenAuth($rootLogin, $rootPassword);
                return new Piwik_Auth_Result(Piwik_Auth_Result::SUCCESS_SUPERUSER_AUTH_CODE, $httpLogin, $rootToken );
            }

            $auth = Piwik_FetchRow(
                'SELECT login, token_auth FROM '.Piwik_Common::prefixTable('user').' WHERE login = ?',
                array($httpLogin)
            );

            if(isset($auth['login'])
                && $auth['login'] === $httpLogin)
            {
                return new Piwik_Auth_Result(Piwik_Auth_Result::SUCCESS, $httpLogin, $auth['token_auth'] );
            }

            return new Piwik_Auth_Result( Piwik_Auth_Result::FAILURE, $httpLogin, NULL );
        }

        return parent::authenticate();
    }
Anonymous Piwik user

+1 following

Anonymous Piwik user

Hey. I added a rewrite for piwik 2.0. It seams to work for me but it may need a review.

Anonymous Piwik user

i keep getting

Cannot access parent:: when current class scope has no parent

in

piwik/plugins/HttpAuthLogin2/Auth.php line 84

Anonymous Piwik user

thx, I fixed this and another critical bug in db query

Anonymous Piwik user

@nougad thanks for your work on this. I was wondering, though, why did you choose to change Auth to implementing Core Auth instead of extending the Login plugin like the previous version? It seems like a lot of additional code for which you could simply fall back on the Login plugin instead.

Anonymous Piwik user

@drammons Do you mean I should extend the exiting Auth class instead of implementing the Auth interface? I had no real reason and your suggestion makes sense. I will change it that way. Thx

Anonymous Piwik user

Yes! That is exactly what I meant. Good work!

Anonymous Piwik user

I had to add:

use Piwik\Common;

...to Auth.php to get this working.

Anonymous Piwik user

Attachment: Rewrite for piwik 2.0
HttpAuthLogin2.tgz

Anonymous Piwik user

Heads up as ticket:4564 rolls out, adding support for multiple super users.

Anonymous Piwik user

Piwik 2.0.3 still tells me this plugin doesn't work Piwik. What am I doing wrong?

Anonymous Piwik user

@digantk - sure you used the HttpAuthLogin2.tgz file? Not the HttpAuthLogin.tgz one

Anonymous Piwik user

Replying to nougad:

@digantk - sure you used the HttpAuthLogin2.tgz file? Not the HttpAuthLogin.tgz one

I'm very sure. The directory is HttpAuthLogin2 and has the HttpAuthLogin2.php file in it and the error message in the plugin manager says HttpAuthLogin2. :-/

Matthieu Aubry
Owner

Along with the Piwik 2.0 release and new design for Piwik, we have also launched the official Plugins Marketplace to let any developer share their work to the thousands of Piwik users worldwide.

Maybe you'd like to publish your plugin there?

In any case, keep up the good work and we hope you enjoy Piwik 2!

--> See also example of the Ldap plugin #734 published on the Marketplace at: http://plugins.piwik.org/LoginLdap

Anonymous Piwik user

Please add it to the marketplace. Without this plugin (and soon), I'll have lose the argument against Google Analytics and I'll have to drop the use of Piwik.

Matthieu Aubry
Owner

Thanks for the suggestion. Indeed I think it makes sense that the Piwik team officially supports this important plugin. We will release it on the Marketplace soon, and will then officially support it going forward.
stay tuned!

Matthieu Aubry
Owner

In 5d49e72: Refs #514 Adding HttpAuth plugin as submodule, now hosted at: https://github.com/piwik/plugin-LoginHttpAuth

Matthieu Aubry
Owner

In 8d156bd: Refactor core Login to allow for clean LoginHttpAuth logic refs #514

Matthieu Aubry
Owner

Thanks for your patience! the HttpAuth login is now published on the Marketplace at: http://plugins.piwik.org/LoginHttpAuth

please test it and report any issue in the Github issue tracker: https://github.com/piwik/plugin-LoginHttpAuth/issues

Cheers!

Anthon Pang robocoder added this to the 2.1 - Piwik 2.1 milestone
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.