OXp1845
OXp1845

Reputation: 493

Mediawiki authentication by checking API with Ruby or Javascript

I want to see if a user is logged in to my mediawiki-application. I think a good way to solve it is to do an API-request to the mediawiki-application and check if the user is logged in. The only problem is that I have no experience at all with JSON or API:s :D.

I've looked around a little bit and found this good looking gem: https://github.com/jnunemaker/httparty

With this gem I can check my api. An example is:

response = HTTParty.get('https://en.wikipedia.org/w/api.php')

With this I get a bunch of useful data. The problem however is that this request is done on behalf of the server and not the user. I won't be able to see if the user is logged in but I can see if my server is logged in. Which I don't care for...

Any solution on how to do this api-request on behalf of the user? Perhaps in one of the views?

If I could do it like mediawiki does it would be fine: https://www.mediawiki.org/wiki/Special:ApiSandbox#action=query&meta=userinfo&format=json

Maybe you could do some javascript that would solve this problem...

Upvotes: 1

Views: 207

Answers (2)

Salix alba
Salix alba

Reputation: 7824

You can use cookies. If you look at the cookies set you have wikiNameUserID, wikiNameUserName and wikiName_session. If they have logged out then wikiNameLoggedOut will be set and wikiNameUserID deleted. This will work as long as you app is on the same domain.

See https://www.mediawiki.org/wiki/Cookie_tracking for details.

Upvotes: 0

leo
leo

Reputation: 8520

You can write a very simple API extension to you MediaWiki installation, that lets you query the API to check if a certain user is logged in. Use User::isLoggedin to do the actual check.

You can use the skeleton at https://www.mediawiki.org/wiki/API:Extensions , and then replace function execute() with something like this:

public function execute() {

    $userName = $this->getMain()->getVal('user');
    $user = User::newFromName($userName);

    $isLoggedIn = $user->isLoggedIn ();

    if ( $isLoggedIn ) {

        $returnValue = 'yes';

    } else {

        $returnValue = 'no';

    }

    $this->getResult()->addValue( null, $this->getModuleName(),
                                  array ( 'loggedin' => $returnValue ) );

    return true;

}

Now you can query the API using Ruby or Javascript (or whatever):

api.php?action=checkIfUserIsLoggedIn&user=AdaLovelace&format=json

Upvotes: 1

Related Questions