Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fanningert committed Jul 15, 2021
1 parent 977a1eb commit 09a424f
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 1 deletion.
Empty file added .gitignore
Empty file.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
# webtrees_simpleautologin
[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0)

# Simple Auto Login for Webtrees
This module provides a simple way for a SSO auto login in combination with a authentication proxy (like oauth2-proxy).

## Installation
Requires webtrees 2.0.

### Using Git
If you are using ``git``, you could also clone the current main branch directly into your ``modules_v4`` directory
by calling:

```
git clone https://github.com/fanningert/webtrees_simpleautologin.git modules_v4/webtrees_simpleautologin
```

### Manual installation
To manually install the module, perform the following steps:

1. Download the [latest release](https://github.com/fanningert/webtrees_simpleautologin/releases/latest).
2. Upload the downloaded file to your web server.
3. Unzip the package into your ``modules_v4`` directory.
4. Rename the folder to ``webtrees_simpleautologin``

## Enable module
After installation, the module is allways on.
7 changes: 7 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use Composer\Autoload\ClassLoader;

$loader = new ClassLoader();
$loader->addPsr4('at\\fanninger\\WebtreesModules\\SimpleAutoLogin\\', __DIR__ . '/src');
$loader->register();
12 changes: 12 additions & 0 deletions module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace at\fanninger\WebtreesModules\SimpleAutoLogin;

if (defined('WT_MODULES_DIR')) {
//this is a webtrees 2.x module. it cannot be used with webtrees 1.x. See README.md.
return;
}

require_once __DIR__ . '/autoload.php';

return new Modules\SimpleAutoLoginModule();
102 changes: 102 additions & 0 deletions src/Modules/SimpleAutoLoginModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

// https://github.com/fisharebest/webtrees/issues/806
// https://github.com/socialconnect/auth

declare(strict_types=1);

namespace at\fanninger\WebtreesModules\SimpleAutoLogin\Modules;

use Fisharebest\Webtrees\Module\AbstractModule;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
use Fisharebest\Webtrees\Http\RequestHandlers\LoginPage;

class SimpleAutoLoginModule extends AbstractModule implements ModuleCustomInterface {
use ModuleCustomTrait;

/**
* Constructor. The constructor is called on *all* modules, even ones that are disabled.
* This is a good place to load business logic ("services"). Type-hint the parameters and
* they will be injected automatically.
*/
public function __construct()
{
// NOTE: If your module is dependent on any of the business logic ("services"),
// then you would type-hint them in the constructor and let webtrees inject them
// for you. However, we can't use dependency injection on anonymous classes like
// this one. For an example of this, see the example-server-configuration module.
}

/**
* Bootstrap. This function is called on *enabled* modules.
* It is a good place to register routes and views.
*
* @return void
*/
public function boot(): void
{
app()->bind(LoginPage::class, SimpleAutoLoginPage::class);
}

/**
* How should this module be identified in the control panel, etc.?
*
* @return string
*/
public function title(): string
{
return 'Simple Auto Login';
}

/**
* A sentence describing what this module does.
*
* @return string
*/
public function description(): string
{
return 'Add authentication proxy support to webtrees';
}

/**
* The person or organisation who created this module.
*
* @return string
*/
public function customModuleAuthorName(): string
{
return 'Thomas Fanniger';
}

/**
* The version of this module.
*
* @return string
*/
public function customModuleVersion(): string
{
return '0.0.1';
}

/**
* A URL that will provide the latest version of this module.
*
* @return string
*/
public function customModuleLatestVersionUrl(): string
{
return 'https://github.com/fanningert/webtrees_simpleautologin/releases/latest';
}

/**
* Where to get support for this module. Perhaps a github repository?
*
* @return string
*/
public function customModuleSupportUrl(): string
{
return 'https://github.com/fanningert/webtrees_simpleautologin';
}
};

94 changes: 94 additions & 0 deletions src/Modules/SimpleAutoLoginPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace at\fanninger\WebtreesModules\SimpleAutoLogin\Modules;

use Exception;
use Fisharebest\Webtrees\Http\RequestHandlers\LoginPage;
use Fisharebest\Webtrees\Http\RequestHandlers\HomePage;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Carbon;
use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Log;
use Fisharebest\Webtrees\Services\UserService;
use Fisharebest\Webtrees\Services\TreeService;
use Fisharebest\Webtrees\Session;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

use function route;

class SimpleAutoLoginPage extends LoginPage
{

/** @var UserService */
private $user_service;

/**
* LoginController constructor.
*
* @param UserService $user_service
*/
public function __construct(TreeService $tree_service, UserService $user_service)
{
parent::__construct($tree_service);
$this->user_service = $user_service;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
$tree = $request->getAttribute('tree');

$params = (array) $request->getParsedBody();
$url = array_key_exists('url', $params) ? $params['url'] : route(HomePage::class);

$server_params = (array) $request->getServerParams();

if (array_key_exists('REMOTE_USER', $server_params) && $server_params['REMOTE_USER'] !== '') {
$username = $server_params['REMOTE_USER'];
}elseif (array_key_exists('HTTP_X_FORWARDED_PREFERRED_USERNAME', $server_params) && $server_params['HTTP_X_FORWARDED_PREFERRED_USERNAME'] !== '') {
$username = $server_params['HTTP_X_FORWARDED_PREFERRED_USERNAME'];
}

if ($username !== '') {
$user = $this->user_service->findByIdentifier($username);
}

if ($user !== null) {
try {
if ($user->getPreference(UserInterface::PREF_IS_EMAIL_VERIFIED) !== '1') {
Log::addAuthenticationLog('Login failed (not verified by user): ' . $username);
throw new Exception(I18N::translate('This account has not been verified. Please check your email for a verification message.'));
}

if ($user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) !== '1') {
Log::addAuthenticationLog('Login failed (not approved by admin): ' . $username);
throw new Exception(I18N::translate('This account has not been approved. Please wait for an administrator to approve it.'));
}

Auth::login($user);
Log::addAuthenticationLog('Login: ' . Auth::user()->userName() . '/' . Auth::user()->realName());
Auth::user()->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, (string) Carbon::now()->unix());

Session::put('language', Auth::user()->getPreference(UserInterface::PREF_LANGUAGE));
Session::put('theme', Auth::user()->getPreference(UserInterface::PREF_THEME));
I18N::init(Auth::user()->getPreference(UserInterface::PREF_LANGUAGE));

return redirect($url);
} catch (Exception $ex) {
// Failed to log in.
FlashMessages::addMessage($ex->getMessage(), 'danger');

return redirect(route(LoginPage::class, [
'tree' => $tree instanceof Tree ? $tree->name() : null,
'username' => $username,
'url' => $url,
]));
}
}else{
return parent::handle($request);
}
}
}

0 comments on commit 09a424f

Please sign in to comment.