Csgs

What Is a Quick & Easy Way to Add HTTP Authentication to an Application with a Security Filter?

Asked by Csgs 2 years ago security http application


Pablo G
0
 
Using httpAuthFilter you can add a single-user application-wide HTTP authentication. Here's how it works:

all:
  auth:
    Realm:      Password required
    username:   MyUsername
    password:   MyPassword

/apps/myApp/config/filters.yml (add after security filter):

httpAuthFilter:

  Class:    HttpAuthFilter
/apps/myApp/lib/filters/httpAuthFilter.class.php:
<?php

class httpAuthFilter extends sfFilter
{

    public function execute($filterChain)
    {
        if($this->isFirstCall()) {
            if(!isset($_SERVER['PHP_AUTH_USER']) or
               ($_SERVER['PHP_AUTH_USER'] == sfConfig::get('app_auth_username') && $_SERVER['PHP_AUTH_PW'] == sfConfig::get('app_auth_password')) == false) {

                $this->sendHttpAuthHeaders();
            }
        }

        $filterChain->execute();
    }

    public function sendHttpAuthHeaders()
    {
        header('WWW-Authenticate: Basic realm="' . SfConfig::get('app_auth_realm') . '"');
        header('HTTP/1.0 401 Unauthorized');
        exit();
    }

}

by Pablo G 2 years ago

Answer this question

What Is a Quick & Easy Way to Add HTTP Authentication to an Application with a Security Filter?

0 errors found:

 
0