Password Protection
Secure your static sites with password authentication
Protect your Forgejo Pages sites with secure password authentication.
Overview
The password protection feature allows you to require authentication before users can access your site. It uses:
- SHA256 password hashing - Passwords never stored in plaintext
- HMAC-signed cookies - Prevents cookie tampering
- Secure cookies - HttpOnly, Secure (HTTPS), SameSite=Strict
- Beautiful login UI - Gradient design with centred form
Quick Setup
Step 1: Generate Password Hash
echo -n "your-password-here" | shasum -a 256
Example output:
5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
Step 2: Add to .pages File
enabled: true
password: 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
Step 3: Configure Traefik (Optional)
http:
middlewares:
pages-server:
plugin:
pages-server:
pagesDomain: pages.example.com
forgejoHost: https://git.example.com
authCookieDuration: 3600
authSecretKey: "randomly-generated-secret-key"
Step 4: Commit and Push
git add .pages
git commit -m "Add password protection"
git push
Password Generation Methods
Using shasum (Mac/Linux)
echo -n "mypassword" | shasum -a 256
Using OpenSSL
openssl rand -base64 16
echo -n "your-password" | openssl dgst -sha256
Using Python
import hashlib
password = "mypassword"
hash_object = hashlib.sha256(password.encode())
print(hash_object.hexdigest())
Using Node.js
const crypto = require('crypto');
const password = 'mypassword';
const hash = crypto.createHash('sha256').update(password).digest('hex');
console.log(hash);
Removing password protection.
Remove the password: line from .pages:
enabled: true
# password: removed
Wait up to 60 seconds for cache to expire, or clear cache manually.
Troubleshooting
Login page doesn’t appear
Solutions:
- Verify
password:field exists in.pagesfile - Check hash is valid SHA256 (64 hex characters)
- Wait 60 seconds for cache to update
- Clear password cache:
redis-cli DEL "password:username:repository"
Wrong password error even with correct password
Solutions:
- Verify using hash in
.pages, not plaintext - Check for extra spaces/newlines in hash
- Re-generate hash:
echo -n "password" | shasum -a 256 - Ensure exact password match (case-sensitive)
Cookie not persisting
Solutions:
- Ensure site served over HTTPS
- Check browser allows cookies
- Verify
authCookieDurationis positive number - Check browser cookie settings
Getting redirected after login
Solutions:
- Clear browser cookies for the site
- Check
authSecretKeyhasn’t changed - Verify system clock is accurate
- Check browser JavaScript is enabled
Best Practices
- Use strong passwords - 16+ characters, mixed case, numbers, symbols
- Configure authSecretKey - Enables HMAC cookie signing
- Set appropriate cookie duration - Balance security vs. convenience
- Use HTTPS - Required for secure cookies
- Rotate passwords - Change periodically for sensitive sites
- Monitor access - Check logs for failed login attempts
Example Configurations
Basic Protection
enabled: true
password: 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
With Custom Domain
enabled: true
custom_domain: private.example.com
password: 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8