motzi

http auth suggestion

Oct 16th, 2011
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.55 KB | None | 0 0
  1. use CGI;
  2. my $cgi = new CGI;
  3.  
  4. authorize();
  5. {
  6.  # веб приложение
  7.  # ...
  8. }
  9.  
  10.  
  11.  
  12. sub authorize
  13. {
  14.  # второй шаг: смотрим если браузер отослал хедер 'Authorization' с данными
  15.  # и просим нажать Cancel :/
  16.  #(без проверки на валидность хеша, тут предполагается что данные верны)
  17.  if( defined $ENV{HTTP_AUTHORIZATION}  ) ########### 2 ####################
  18.  {
  19.     print $cgi->header(-type=>'text/html',
  20.                      -status=>'401 Unauthorized',
  21.                      -cookie=>'sessid=TATATATATATAT',
  22.                      'WWW-Authenticate'=>'Basic realm="PRESS Cancel BUTTON!!1"',
  23.              -charset=>'utf-8'
  24.                     ),
  25.       $cgi->start_html(-title=>''),
  26.       $cgi->h1("Refresh it."),
  27.       $cgi->end_html;  
  28.     exit;
  29.  }
  30.  # третий шаг: смотрим на кук и идём дальше...
  31.  elsif( $ENV{HTTP_COOKIE} =~ /sessid=([^;=]+)/) ########### 3 ####################
  32.  {
  33.    
  34.     # itsok
  35.  
  36.  }
  37.  # первый шаг: отсылаем настоящий запрос авторизации
  38.  else ########### 1 ####################
  39.  {
  40.   basic_auth();
  41.  }
  42. }
  43.  
  44. sub basic_auth
  45. {
  46.   print $cgi->header(-type=>'text/html',
  47.                      -status=>'401 Authorization Required',
  48.                      'WWW-Authenticate'=>'Digest realm="[email protected]"',
  49.              -charset=>'utf-8'
  50.                     ),
  51.     $cgi->start_html(-title=>'Err0r'),
  52.     $cgi->h1("Unauthorized"),
  53.     $cgi->end_html;
  54.   exit;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment