Basic Auth जोड़ना
In this page:
Basic Auth कैसे काम करता है
जब एक browser किसी protected resource की request करता है, Apache एक 401 Unauthorized status और एक WWW-Authenticate header से reply करता है। browser अपना built-in login prompt दिखाता है, फिर credentials के साथ request दोबारा भेजता है, एक Authorization header में base64-encoded (encrypted नहीं -- Basic Auth को network पर safe रहने के लिए हमेशा HTTPS के साथ pair होना चाहिए)।
एक Password File बनाना
Apache credentials को htpasswd से बनाई एक password file के against check करता है, जो DocumentRoot के बाहर रखी जाती है ताकि इसे कभी सीधे download न किया जा सके। sudo htpasswd -c /etc/apache2/.htpasswd alice पहले user के साथ एक नई file बनाता है (password के लिए prompt करते हुए); बाकी users के लिए -c drop करें ताकि file overwrite न हो।
Directives
एक <Directory> block (या .htaccess, अगर allowed हो) के अंदर: AuthType Basic Basic Auth select करता है, AuthName "Restricted Area" वह text set करता है जो browser के login prompt में दिखता है, AuthUserFile /etc/apache2/.htpasswd password file की ओर point करता है, और Require valid-user कहता है कि उस file में कोई भी username/password pair accepted है।
Specific Users तक Restrict करना
Require valid-user की जगह, Require user alice bob password file में सिर्फ उन दो specific usernames को accept करता है, भले ही इसमें दूसरे users exist करते हों -- useful जब एक shared .htpasswd file कई protected areas को different allowed users के साथ cover करती है।
उदाहरण: Password-protecting a directory
# Create the password file (first user)
# sudo htpasswd -c /etc/apache2/.htpasswd alice
<Directory "/var/www/example.com/admin">
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
{# Flagged by hand after confirming a runner can't handle this example
(a shell command / go.mod file stored as a TopicExample, a language
feature the configured runner version doesn't support, or output
that blows a runner's sandbox limit) -- see TopicExample.norun.
Never render the run button for these, regardless of language,
since it would just fail at execute_code (or worse, hang the
Judge0 queue on a submission that can never finish cleanly). #}
- .htpasswd file को public DocumentRoot के अंदर store करना, जहाँ एक misconfiguration किसी को इसे download करके offline crack करने दे सकती है।
- production में plain HTTP पर HTTP Basic Auth इस्तेमाल करना, network traffic observe कर पाने वाले किसी को भी credentials expose करते हुए।
- दूसरे user के लिए
htpasswdको-cके साथ दोबारा चलाना, जो file overwrite कर देता है और पहला user जोड़ने की बजाय delete कर देता है।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: