title: How to perform JDBC authentication with Scala's Slick library?
body: <h1>Intro</h1>
  <ul>
  <li>I have a microservice written in Akka HTTP which communicates with the frontend via an REST API.</li>
  <li>I use Scala's Slick library to communicate with the PostgreSQL DBMS.</li>
  <li>I want secure the API with HTTP's basic authentication mechanism (the frontend sends an username and password to the microservice in each request).</li>
  <li>The extract credentials from each HTTP API call I want to add into Slick's actions in order to ensure via &quot;JDBC Authentication&quot; that the user is authorized to manipulate a given set of data.</li>
  </ul>
  <h1>Problem</h1>
  <p>While its obvious for me how to accomplish this with plain Java's DriverManager by creating each new connection with a set of credentials and using this connection in order to perform future operations, the Slick library offers something like this:</p>
  <pre class="lang-scala prettyprint-override"><code>val db = Database.forURL(jdbcUrl, superuser, password)
  </code></pre>
  <p>but the documentation states that only one such instance should be created for the whole life of an application:</p>
  <blockquote>
  <p>Note: A Database object usually manages a thread pool and a connection pool. You should always shut it down properly when it is no longer needed (unless the JVM process terminates anyway). Do not create a new Database for every database operation. A single instance is meant to be kept alive for the entire lifetime your your application.</p>
  </blockquote>
  <p>So I am stick to the one <code>db</code> singleton which always runs all actions with the superuser credentials.</p>
  <p>I am new to Slick and I don't know how to add an additional username and password in order to send it together with an action (SQL, DDL statement) to the DBMS that can verify if the user is permit to perform this action. Here is an example of what I want to achieve:</p>
  <pre class="lang-scala prettyprint-override"><code>  override def removeDatabase(name: String): Future[Unit] =
      db.run(sqlu&quot;&quot;&quot;DROP DATABASE IF EXISTS #${name.toLowerCase};&quot;&quot;&quot;).map(_ =&gt; ())
  </code></pre>
  <p>To this drop action I would like add the credentials extract from an HTTP request in order to get an JDBC authentication to ensure that the current user performing this action is authorized to delete this database.</p>
  <h1>What I have did so far?</h1>
  <p>According to the Slick documentation:</p>
  <blockquote>
  <p>In order to drop down to the JDBC level for functionality that is not available in Slick, you can use a SimpleDBIO action which is run on a database thread and gets access to the JDBC Connection:
  <code>val getAutoCommit = SimpleDBIO[Boolean](_.connection.getAutoCommit)</code></p>
  </blockquote>
  <p>I tried this:</p>
  <pre class="lang-scala prettyprint-override"><code>override def removeDatabase(name: String): Future[Unit] = {
    val dropStatement = SimpleDBIO[Unit] { session =&gt; {
      val currentConnection = session.connection
      currentConnection.setClientInfo(&quot;username&quot;, &quot;testUsername&quot;)
      currentConnection.setClientInfo(&quot;password&quot;, &quot;invalidPwd&quot;)
      println(&quot;&gt;&gt;&gt; username: &quot; + currentConnection.getClientInfo(&quot;username&quot;))
      println(&quot;&gt;&gt;&gt; password: &quot; + currentConnection.getClientInfo(&quot;password&quot;))
      sqlu&quot;&quot;&quot;DROP DATABASE IF EXISTS #${name.toLowerCase};&quot;&quot;&quot;
    }
    }
    db.run(dropStatement).map(_ =&gt; ())
  }
  </code></pre>
  <p>But unfortunately it doesn't have any impact, the passed credentials to the client info are ignored:</p>
  <pre class="lang-sh prettyprint-override"><code>&gt;&gt;&gt; username: null 
  &gt;&gt;&gt; password: null
  </code></pre>