A Protected Static Website with Caddy and Pocket ID
Intro
In the Intranet post we talked about using just a static website, and there's a stigma here. If you ask any IT specialist "How do I get a protected static website?", they'll tell you to get a VPN. Which is at least overcomplicated.
The second stigma is that you need different password to access every resource, although auth services, one of which is Pocket ID, are available and are not that complicated. Yes, you definitely can and should have a single authentication system for all your services, because it's convenient and safer. Every time a user types in a password, it's actually bad from a security viewpoint.
And there's a third common misconception, which actually made me write this post: you need more than a web server and an auth service to protect a static website. The reason is that an auth service usually expects an application to handle the authentication flow and communicate with it. A static website doesn't do that: it simply serves files and has no backend or authentication endpoints. This is where a reverse proxy comes in: it handles the authentication flow with the auth service and only serves the website to authenticated users. But with Caddy (a webserver) you can get a reverse proxy as well.
I'd briefly describe how I set up a web server (Caddy) and an auth service (Pocket ID) for a static website, as it required some debugging. Both products I used are written in Go, which gives you a few advantages right away:
- Small resource footprint
- Fast and scalable, especially with more requests
- It's a single binary, so you can use Docker, but you can install it on bare metal as well
Config
The whole process is as follows:
- Add records to hosts.conf to point to app.local.lan (the website) and auth.local.lan (the auth service)
- Install Caddy (probably bare metal) and Pocket ID, probably with Docker
- Install a security module for Caddy, which can integrate a static website with Pocket ID
- Configure Pocket ID: two lines in the config file and a few steps in a web interface
- Add lines to Caddy's config
I found Pocket ID configuration easy, but here are the steps for reference:
- You add a client
- You add a user group
- You give this user group access to the client
Any chatbot or documentation can guide you, but every chatbot I tested starts going in circles with Caddy's config, as the security module config is not so obvious. So here it is:
{
# debug
security {
oauth identity provider generic {
delay_start 3
realm generic
driver generic
client_id <your client id created in Pocket ID>
client_secret <your client secret created in Pocket ID>
scopes openid email profile
base_auth_url https://auth.local.lan
metadata_url http://127.0.0.1:1411/.well-known/openid-configuration
}
authentication portal my_portal {
enable identity provider generic
cookie insecure off
trust login redirect uri domain exact app.local.lan path prefix /
transform user {
match realm generic
action add role authp/user
}
}
authorization policy my_policy {
set auth url /caddy-security/oauth2/generic
allow roles authp/user
}
}
}
app.local.lan {
tls internal
@portal path /caddy-security/*
route @portal {
authenticate with my_portal
}
route {
authorize with my_policy
root * /var/www/html
file_server
}
}
auth.local.lan {
tls internal
reverse_proxy 127.0.0.1:1411
}
Beware, this is for localhost only, so it doesn't use real SSL certificates, which Caddy can easily install for you.
Caddy is really nice:
- It has a validation tool to tell you about the errors in your config.
- It has a formatting tool to fix the issues.
- It has a debug mode (uncomment in the beginning) and you can check the logs to troubleshoot any issues.
Conclusion
I guess the main outcome is that everyone needs to be challenged, even your friendly IT colleagues, and pushed to use the modern tech stack, especially if it cuts costs dramatically.