Skip to content

Instantly share code, notes, and snippets.

@Roy-Orbison
Last active September 1, 2021 00:56
Show Gist options
  • Save Roy-Orbison/0978408fa60afd6b18a30719271f0f8d to your computer and use it in GitHub Desktop.
Save Roy-Orbison/0978408fa60afd6b18a30719271f0f8d to your computer and use it in GitHub Desktop.
Adminer wrapper template for externally authenticated installations
<?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';
@Roy-Orbison
Copy link
Author

This is now a proper plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment