ddev / ddev-adminer

Adminer service for DDEV
Apache License 2.0
8 stars 7 forks source link

Auto-login to database #1

Closed rfay closed 2 years ago

rfay commented 2 years ago

Right now it's a pain to log into the database, even though the credentials are easy, but there ought to be a way.

I was just studying https://hub.docker.com/_/adminer and ... not a word!

We can certainly add a build: to the docker-compose.adminer.yaml, but weirdly it doesn't claim anything that you can do. Maybe we have to look a little more at adminer itself.

rfay commented 2 years ago

Looks like https://github.com/TimWolla/docker-adminer/issues/13 has a path forward on this...

bserem commented 2 years ago

It has a very old code snippet, doesn't seem to work with most recent adminer versions. Unless adminer re-thinks its policy we seem to be out of luck.

Related: https://stackoverflow.com/questions/70291849/adminer-autologin

wotnak commented 2 years ago

The only option I'm aware that doesn't require modifying adminer source code is to create a plugin that will be injecting credentials and submitting the login form using javascript. E.g. something like this: .ddev/adminer/ddev-login.php

<?php
class DDEVLogin {
    function loginForm() {
    ?>
        <script type="text/javascript" <?php echo nonce(); ?>>
        addEventListener('load', function () {
            // Prevent infinite reload loop when auto login failed.
            if (document.querySelector('.error')) {
                return
            }
            document.querySelector('[name="auth[driver]"]').value = '<?php echo $_ENV['DDEV_DB_DRIVER']; ?>'
            document.querySelector('[name="auth[db]"]').value = '<?php echo $_ENV['DDEV_DB_NAME']; ?>'
            document.querySelector('[name="auth[username]"]').value = '<?php echo $_ENV['DDEV_DB_USER']; ?>'
            document.querySelector('[name="auth[password]"]').value = '<?php echo $_ENV['DDEV_DB_PASS']; ?>'
            document.querySelector('[name="auth[permanent]"]:not(:checked)')?.click()
            document.querySelector('[type=submit][value=Login]').click()
        });
        </script>
    <?php
    }
}
return new DDEVLogin();

.ddev/docker-compose.adminer.yaml

version: '3.6'
services:
  adminer:
    [...]
    environment:
      - ADMINER_DEFAULT_SERVER=ddev-${DDEV_SITENAME}-db
      - DDEV_DB_DRIVER=pgsql
      - DDEV_DB_NAME=db
      - DDEV_DB_USER=db
      - DDEV_DB_PASS=db
    [...]
    volumes:
      - "./adminer:/var/www/html/plugins-enabled"
[...]
wotnak commented 2 years ago

Done a bit more research and found a way to auto login without submitting login form using js: .ddev/adminer/ddev-login.php

<?php
class DDEVLogin {
    function __construct() {
        if (
            $_SESSION["pwds"][$_ENV['DDEV_DB_DRIVER']][$_ENV['ADMINER_DEFAULT_SERVER']][$_ENV['DDEV_DB_USER']] !== $_ENV['DDEV_DB_PASS']
            && $_SESSION["db"][$_ENV['DDEV_DB_DRIVER']][$_ENV['ADMINER_DEFAULT_SERVER']][$_ENV['DDEV_DB_USER']][$_ENV['DDEV_DB_PASS']] !== true
            || isset($_POST["auth"])
        ) {
            // If the current session doesn't have a valid connection details
            // (so you're not logged in) or the login form was submitted,
            // make Adminer think that you just submitted the login form
            // with the correct connection details.
            $_POST["auth"]["server"] = $_ENV['ADMINER_DEFAULT_SERVER'];
            $_POST["auth"]["driver"] = $_ENV['DDEV_DB_DRIVER'];
            $_POST["auth"]["db"] = $_ENV['DDEV_DB_NAME'];
            $_POST["auth"]["username"] = $_ENV['DDEV_DB_USER'];
            $_POST["auth"]["password"] = $_ENV['DDEV_DB_PASS'];
            $_POST["auth"]["permanent"] = 1;
        } else {
            // If logged in and on the home page redirect to the currently
            // logged in database server page. 
            if (
                $_GET[$_ENV['DDEV_DB_DRIVER']] !== $_ENV['ADMINER_DEFAULT_SERVER']
                || $_GET['username'] !== $_ENV['DDEV_DB_USER']
            ) {
                $query = $_ENV['DDEV_DB_DRIVER'] . '=' . $_ENV['ADMINER_DEFAULT_SERVER'] . '&username=' . $_ENV['DDEV_DB_USER'];
                header('Location: /?'. $query);
            }
        }
    }
}

return new DDEVLogin();