Last active
September 1, 2021 00:56
-
-
Save Roy-Orbison/0978408fa60afd6b18a30719271f0f8d to your computer and use it in GitHub Desktop.
Adminer wrapper template for externally authenticated installations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function adminer_object() { | |
class YourClass extends Adminer { | |
protected $externals; | |
function __construct($externals) { | |
$this->externals = $externals; | |
} | |
function name() { | |
return 'YourApp Adminer'; | |
} | |
function credentials() { | |
if (!$this->externals->authenticated) { | |
auth_error('External authentication expired.'); | |
} | |
return [ | |
$this->externals->host, | |
$this->externals->user, | |
$this->externals->pass, | |
]; | |
} | |
function database() { | |
return $this->externals->database; | |
} | |
function loginForm() { | |
if ($this->externals->authenticated) { | |
$nonce = null; | |
$response_headers = headers_list(); | |
while ($response_headers) { | |
$response_header = array_pop($response_headers); | |
if (preg_match('/^Content-Security-Policy:.*?\'nonce-([^\']+)/', $response_header, $nonce_matches)) { | |
$nonce = $nonce_matches[1]; | |
break; | |
} | |
} | |
if ($nonce !== null) { | |
?> | |
<style>form { display: none; }</style> | |
<?php | |
} | |
parent::loginForm();# form fields required for login() to be triggered | |
if ($nonce !== null) { | |
?> | |
<script nonce="<?= $nonce ?>">document.forms[0].submit()</script> | |
<?php | |
} | |
} | |
else { | |
?> | |
<a href="#link-to-your-log-in-page">Log in</a> | |
<?php | |
} | |
} | |
function login($login, $password) { | |
return $this->externals->authenticated; | |
} | |
} | |
# add logic to integrate with your system, here | |
$host = $yourDbConfig->host; | |
$database = $yourDbConfig->database; | |
$user = $yourDbConfig->user; | |
$password = $yourDbConfig->password; | |
$authenticated = $yourAuth->isLoggedIn() && $yourAuth->hasPermissionTo('ruin_your_db'); | |
if (empty($_GET['db']) && $database != '') { | |
$_GET['db'] = $database; | |
} | |
return new YourClass((object) compact('host', 'database', 'user', 'pass', 'authenticated')); | |
} | |
require 'path/to/your/adminer.php'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is now a proper plugin.