Search code examples
.htaccessresthttpauthenticationhttp-authentication

htaccess authentication rest api methods


I have REST API service built using CakePHP and I need to protect some of my methods using http authentication. For example I have methods like:

POST /api/store
{
   name: "John",
   surname: "Johnny",
   ...
}

and I want to protect this particular path (/api/store) to protect with http authentication to call cron job

http://username:password@server.com/api/store

is sth like that possible? If so, then how? Thank you!


Solution

  • We solved this. As long as that url /api/store is not physical path, we need to make different approach thank using basic http auth secured folder.

    SetEnvIf Request_URI ^/api/store protected_method=true
    # we need to match url with regex and set it to "protected_method" variable    
    
    # Commom auth
    AuthUserFile /absolute/path/to/directory/of/api
    AuthName "This method is pwd protected"
    AuthType Basic
    
    Order Deny,Allow
    Deny from all
    Satisfy any
    
    Require valid-user
    #check if requested url is one of protected_method
    Allow from env=!protected_method