Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-login to database #1

Closed
rfay opened this issue Mar 21, 2022 · 4 comments
Closed

Auto-login to database #1

rfay opened this issue Mar 21, 2022 · 4 comments

Comments

@rfay
Copy link
Member

rfay commented Mar 21, 2022

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
Copy link
Member Author

rfay commented Mar 22, 2022

Looks like TimWolla/docker-adminer#13 has a path forward on this...

@bserem
Copy link
Collaborator

bserem commented Mar 22, 2022

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
Copy link

wotnak commented Mar 22, 2022

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
Copy link

wotnak commented Mar 25, 2022

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants