@reeder

Как правильно реализовать логику авторизации пользователей?

Всем привет!
Столкнулся с проблемой, нужна помошь.
Задача такая:
  • https://site.com -> Агент GoogleImageProxy не требовать авторизацию
  • https://site.com -> Требовать авторизацию у всех кроме администратора и локалхост, файл с пользователями htAdminPasswds и сообшения "Site in developing"
  • https://site.com/mail/ -> Требовать авторизацию eMail ползователей, файл с пользователями htUserPasswds и сообшения "Corporate eMail"
  • https://site.com/share/ -> Не требовать авторизацию
  • https://site.com/Microsoft-Server-ActiveSync -> Не требовать авторизацию

Частично проблему решил но как всегда есть "НО!"
В некоторых запросах (примеры ниже) nginx выдает две строки "WWW-Authenticate: Basic realm" в заголовке
В принципе реализация работает даже с двумя строками но все же хочется исправить.
curl -v https://site.com/
*   Trying 1.1.1.1...
* Connected to site.com (1.1.1.1) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
....
> GET / HTTP/1.1
> Host: site.com
> User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
> Accept: */*
>
< HTTP/1.1 401 Unauthorized
< Server: nginx
< Date: Mon, 23 Nov 2015 19:56:35 GMT
< Content-Type: text/html
< Content-Length: 590
< Connection: keep-alive
< WWW-Authenticate: Basic realm="Site in developing"
< WWW-Authenticate: Basic realm="Site in developing" <----- проблема
<
<html>
....
############################################################################################################################################

$ curl -v https://site.com/mail/
*   Trying 1.1.1.1...
* Connected to site.com (1.1.1.1) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
....
> GET /mail/ HTTP/1.1
> Host: site.com
> User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
> Accept: */*
>
< HTTP/1.1 401 Unauthorized
< Server: nginx
< Date: Mon, 23 Nov 2015 19:58:38 GMT
< Content-Type: text/html
< Content-Length: 590
< Connection: keep-alive
< WWW-Authenticate: Basic realm="Corporate eMail"
< WWW-Authenticate: Basic realm="Site in developing"<----- проблема
<
<html>
....
############################################################################################################################################

$ curl -v https://site.com/nonexistentpage
*   Trying 1.1.1.1...
* Connected to site.com (1.1.1.1) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
....
> GET /nonexistentpage HTTP/1.1
> Host: site.com
> User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
> Accept: */*
>
< HTTP/1.1 401 Unauthorized
< Server: nginx
< Date: Mon, 23 Nov 2015 20:08:10 GMT
< Content-Type: text/html
< Content-Length: 590
< Connection: keep-alive
< WWW-Authenticate: Basic realm="Site in developing"
< WWW-Authenticate: Basic realm="Site in developing" <----- проблема
<
<html>
....

######################################################################
# * * * * * * * * * * * * * Логика авторизации * * * * * * * *  * * *
#-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=--=
# * Переменные
set $realm "off";
set $user_file "";
set $switcher "";
# * Требуем пароль но пропускаем администратора и локалхост
if ($remote_addr !~ "^(127.0.0.1|х.х.х.х)$"){
	set $switcher "A";
}
# * Требуем пароль eMail ползователей
if ($uri ~ "/mail") {
	set $switcher "${switcher}B";
}
# * Объявляем Админ ползователей
if ($switcher = A) {
	set $realm "Site in developing";
	set $user_file "htAdminPasswds";
}
# * Объявляем eMail ползователей
if ($switcher = AB) {
	set $realm "Corporate eMail";
	set $user_file "htUserPasswds";
}
# * Пропускаем без запроса пароля
location /share/ {
	set $realm "off";
}
if ( $http_user_agent ~ GoogleImageProxy ){
	set $realm "off";
}
if ($uri ~ "/Microsoft-Server-ActiveSync"){
	set $realm "off";
}
# * Запрос пароля
auth_basic $realm;
auth_basic_user_file $user_file;
######################################################################


$ nginx -V
nginx version: nginx/1.4.6 (Ubuntu)
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04)
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module
  • Вопрос задан
  • 544 просмотра
Пригласить эксперта
Ответы на вопрос 2
@inkvizitor68sl
Linux-сисадмин с 8 летним стажем.
П*ц, простите.

location / {
set $root_access "Site in developing"; 
if ( $http_user_agent ~ GoogleImageProxy ){ set $root_access off; }
if ($remote_addr ~ "^(127.0.0.1|х.х.х.х)$"){ set $root_access off; }
auth_basic $root_access;
auth_basic_user_file htUserPasswds;
}
location /mail/ {
auth_basic "Corporate eMail";
auth_basic_user_file htUserPasswds;
}
location /share/ {}
location /Microsoft-Server-ActiveSync {}


Всю логику авторизации для каждого из location - внутри location. Все переменные - там же.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы