Skip to content

Instantly share code, notes, and snippets.

@dploeger
Last active July 18, 2022 07:30
Show Gist options
  • Save dploeger/ba87d8ba2f51a527d298629197d7be29 to your computer and use it in GitHub Desktop.
Save dploeger/ba87d8ba2f51a527d298629197d7be29 to your computer and use it in GitHub Desktop.
FileGator Elgg Auth Adapter

Elgg Auth Adapter for Filegator

This auth adapter allows to login into Filegator using Elgg as an auth backend. The adapter simply uses Elggs Webservices method auth.gettoken to check if the given credentials are valid.

There is no other feature than authenticating.

Installation

Simply put the above file into backend/Services/Auth/Adapters.

Configuration

To use the adapter, configure it in configuration.php like this:

        'Filegator\Services\Auth\AuthInterface' => [
            'handler' => '\Filegator\Services\Auth\Adapters\Elgg',
            'config' => [
		    'elggApiUrl' => '<your elgg url>/services/api/rest/json/?method=auth.gettoken',
		    'elggAdmins' => [<a list of usernames which should be filegator admins>]

            ],
        ],

Warning

All authenticated users will get all rights in Filegator.

<?php
/*
* Elgg Auth Adaptor for FileGator (https://filegator.io)
* (c) 2022 Waldbühne Heessen
*/
namespace Filegator\Services\Auth\Adapters;
use Filegator\Services\Auth\AuthInterface;
use Filegator\Services\Auth\User;
use Filegator\Services\Auth\UsersCollection;
use Filegator\Services\Service;
use Filegator\Services\Session\SessionStorageInterface as Session;
use Filegator\Utils\PasswordHash;
class Elgg implements Service, AuthInterface
{
const SESSION_KEY = 'elgg_auth';
const SESSION_HASH = 'elgg_auth_hash';
const GUEST_USERNAME = 'guest';
protected $session;
protected $file;
protected $elggApiUrl;
protected $elggAdmins;
public function __construct(Session $session)
{
$this->session = $session;
}
public function init(array $config = [])
{
$this->elggApiUrl = $config['elggApiUrl'];
$this->elggAdmins = $config['elggAdmins'];
}
public function user(): ?User
{
return $this->session ? $this->session->get(self::SESSION_KEY, null) : null;
}
public function forget()
{
return $this->session->invalidate();
}
public function authenticate($username, $password): bool
{
// prevent anonymous auth
if (!isset($password) || empty($password)) return false;
if (!isset($username) || empty($username)) return false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->elggApiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$username&password=$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$output = curl_exec($ch);
$result = json_decode($output);
if (is_null($result)) {
return false;
}
if ($result->status == -1) {
return false;
}
$user = new User();
$user->setUsername($username);
$isAdmin = false;
foreach ($this->elggAdmins as $admin) {
if ($username == $admin) {
$isAdmin = true;
}
}
$user->setRole($isAdmin ? 'admin' : 'user');
$user->setHomeDir('/');
$user->setPermissions('read|write|upload|download|batchdownload|zip', true);
$this->store($user);
return true;
}
public function find($username): ?User
{
return null;
}
public function store(User $user)
{
return $this->session->set(self::SESSION_KEY, $user);
}
public function update($username, User $user, $password = ''): User
{
return $user;
}
public function add(User $user, $password): User {
return $user;
}
public function delete(User $user) {
return;
}
public function getGuest(): User {
return new User();
}
public function allUsers(): UsersCollection {
return new UsersCollection();
}
}
Copyright 2022 Westfälische Freilichtspiele Waldbühne Heessen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment