TinyAuth is a small authentication container designed to overlay a login page on top of other containerised services. Sometimes called "Forwarded Authentication".
It's by far the simplest of the few authentication container options out there, but it does have a slightly undesirable default configuration setup IMO. Docker Labels.
TinyAuth's documentation assumes you're using a Docker label based container stack - where everything from TinyAuth to the containerised app you're protecting, to the reverse proxy - are all configured by applying Docker labels to the containers.
This is not how I like to do things. It often requires many containers to have access to the Docker Socket (which, unknown to most, cannot be mounted read-only by nature of being a socket) and so exposes us to extra risk. I also prefer that each service has their own configuration files which helps to keep the stacks portable.
There's limited TinyAuth documentation for this at the moment (maybe more coming in v5) but it is possible to have a basic setup with the simpler or more traditional configuration.
The info was spread across a few tickets, discussions and PRs on GitHub, so I'll summarise here.
TinyAuth Middleware Architecture
Authentication middleware works with reverse proxies (e.g. Caddy) to force a connection through the authentication middleware before the connection is allowed into the containerised app you're protecting.
The middleware authenticator then provides extra headers to the app to indicate which user has successfully authenticated. These can then be used for authorization.
Note: Authenticate is who you are. Authorise is what you're allowed to do. Unfortunately the old and standard HTTP header specification get this definition wrong. Also, keep en eye out for the American vs British spellings in header i.e. "z" vs "s".
The authentication flow looks a little like this:
Browser --> Reverse Proxy --> Authentication Middleware (Login)
|
^ [+Authentication Headers]
| v
| Reverse Proxy
|
Reverse Proxy <-- App <-- [+copied Authentication Headers]
Due to the way TinyAuth sets cookies, it must be on a subdomain of a parent domain, and so must your app.
e.g. "myapp.example.org" and "auth.example.org"
TinyAuth does indeed set a cookie on the parent domain, but your app does not interact with, or need to read this cookie for authentication or authorisation. It just helps TinyAuth to remember that you're already logged in and to let you through to the app on subsequent requests.
The app itself can check that you're logged in, or whom you are, by reading the clear-text "Remote-User", "Remote-Email", "Remote-Name" headers that are sent back by TinyAuth by default.
But how is that secure? Well, it's only secure under certain circumstances.
The "Remote-..." headers are only send back to the Reverse Proxy (like Caddy) from TinyAuth on the internal network, they never get to the user's browser.
Then the Reverse Proxy then needs to copy these headers into the internal request to the app, which can then read them.
But none of these steps send those headers back to the user.
However, if your app blindly accepts "Remote-..." headers on any connection to it. It's trivial for someone to spoof them, bypassing your authentication (if there's any access to the app outside of the reverse proxy).
The trick here is to make sure the Reverse Proxy removes any "Remote-..." headers from browser client connections, and also tell your app to whitelist the Reverse Proxy as a "trusted proxy" and only read the "Remote-..." headers from Trusted Proxies.
Trusting the proxy, and thus it's headers, will be different for each app. If you're writing your own, this is often trusted by an internal IP range - however I prefer to use a non-standard API key on a "Via-Key" header. Only your specific proxy knows the keys and only sends the key header internally to your app which also knows the keys. No need to worry about IPs or Dockers sometimes sketchy IP routing.
TinyAuth and Caddy without Labels
At the moment, in TinyAuth v4, it's possible to have a simple setup without labels, but for anything complicated like ACL's or anything listed on the labels config page (link only applicable under latest v4, the docs URLs are unfortunately not version tagged), you'll need to wait for v5.
To get this working well in a local development environment you'll also what yo setup a domain name that you can add subdomains too. i.e. not localhost
The easiest way is to edit your systems host file (/etc/hosts on Linux/MacOS) to include:
test.internal 127.0.0.1
app.test.internal 127.0.0.1
auth.test.internal 127.0.0.1
127.0.0.1 just means "localhost", and ".internal" is reserved for internal network usage.services:
caddy:
container_name: test1-caddy
image: docker.io/caddy:latest
volumes:
- ./caddy:/etc/caddy
- ./dist:/srv/dist
ports:
- 80:80
- 443:443
tinyauth:
image: ghcr.io/steveiliop56/tinyauth:v4
environment:
- FORGOT_PASSWORD_MESSAGE=Contact the admin.
- APP_URL=https://auth.test.internal
- USERS=user:$$2a$$10$$UdLYoJ5lgPsC0RKqYH/jMua7zIn0g9kPqWmhYayJYLaZQ/FTmH2/u
volumes:
- "./tinyauth:/data"
php:
container_name: test-php
image: docker.io/php:8.5-fpm
volumes:
- ./dist:/srv/dist
Contribute to this snippet on Codeberg: tinyauth-caddy-without-labels/compose.yml.
In this example, we'll be protecting a PHP app (maybe just a test file in "./dist/") at "app.test.internal" with TinyAuth running on "auth.test.internal".
I've also added a default username of "user" and password of "password". Do not use this credential hash one in the real world! You can generate your own with OTP/2FA using this command inside the TinyAuth container, then update the compose.yml:
./tinyauth user create --interactive && ./tinyauth totp generate --interactive
Here's an example PHP test file stored at "./dist/public/index.php" (note that both Caddy and the PHP FPM container should mount this at the same path inside each container - this makes PHP work with Caddy):<?php
var_dump($_SERVER);
Contribute to this snippet on Codeberg: tinyauth-caddy-without-labels/dist/public/index.php.
This proof-of-concept exmaple "app" will just print out all connection info, including the "secret" internal headers from TinyAuth and Caddy.
In a real application, if you want to know who logged in, you'll "trust" the "Remote-..." headers. You'd need to confirm you trust the proxy and then match the username to some internal user in your app.
But, if you don't need to know who is logged in... only that someone did, then you don't need to worry about trusting the proxy or reading the headers.
TinyAuth & PHP Caddy File
Here's an example of how to setup TinyAuth on Caddy without using a special/custom Caddy container image that uses labels for config.(auth_forwarder) {
# Ideally we'd prevent spoofing of auth during a bypass of forward_auth (i.e. path match, app controller issue) - but caddy will defer deletions until request, which will remove our legit headers also.
#request_header -Remote-*
forward_auth tinyauth:3000 {
uri /api/auth/caddy
# copy authentication response headers to app for authorisation.
copy_headers Remote-User Remote-Groups Remote-Name Remote-Email
}
# App should check this key before accepting `Remote-*` headers. A way of trusting this proxy when IP's can be sketchy due to Docker userland proxy. This can only be obtained by the app on the internal proxy connection. Setting here rather than `header_up` as we want it across all auth imports.
request_header Via-Key "iHhyJokmS6claDuR"
}
test.internal {
tls internal {
on_demand
}
root * /srv/dist/public
import caddy-sensible-defaults
@auth_paths path /admin /admin/*
handle @auth_paths {
import auth_forwarder
}
# Enable the static file server.
file_server
# Serve PHP files via php-fpm.
# https://caddyserver.com/docs/caddyfile/directives/php_fastcgi#examples
php_fastcgi lumber-php:9000
}
auth.test.internal {
tls internal {
on_demand
}
import caddy-sensible-defaults
reverse_proxy tinyauth:3000
}
Contribute to this snippet on Codeberg: tinyauth-caddy-without-labels/caddy/example.Caddyfile.
For local development I usually set the TLS (HTTP certificates that Caddy produces) to use an internal certificate and to generate this on-demand. This means that the first time you successfully connect, it may take a moment to generate a certificate, and you'll get a security warning in your browser that it's trying to a self-signed (snake oil) certificate rather than a real one.
I'm also importing some common Caddy default configurations with the import caddy-sensible-defaults you can read more about these here - however this is not required for TinyAuth to work.
Just for an example I've applied TinyAuth to only the path "/admin" so it'll only try to protect that path.
Notice that I've specified two admin paths e.g. @auth_paths path /admin /admin/* this is due to the way Caddy does path matching, just /admin* would match "/admin2" which is maybe undesired.
Note that the Caddy PHP shorthand command for a PHP reverse proxy php_fastcgi set's up your projects "index.php" to be the router, rather than the file structure as default in Apache2 - this can be tweaked by passing some other parameters. You also need to have file_server to ensure assets like images, CSS, JS etc are delivered by Caddy, as by default only PHP files are routed to the PHP container.
root * /srv/dist/public is simply a path to the PHP site that's mounted in the Caddy container.
You can change or remove "Via-Key" as needed.
Make sure to uncomment DISABLE_ANALYTICS is you don't want to send usage metrics to TinyAuth developers.
Comments & Questions
to send in your thoughts.
Comments may be featured here unless you say otherwise. You can encrypt emails with PGP too, learn more about my email replies here.
PGP: 9ba2c5570aec2933970053e7967775cb1020ef23