Search code examples
phprestauthenticationbasic-authenticationslim

creating Basic authentication in Slim framework


I had seen 2 questions on SO and several topics on google but that didn't helped me out

  1. Authentication Based REST API with Slim
  2. Securing a REST API and Slim Framework

Slim provides you different methods like PUT,GET,POST etc. I want to implement basic authorization like being implemented by many API's. First question: Is SSL necessay? (I dont have currently) Second question: How to implement it? as in i have to send username and password in headers in encrypted form and then after this I have to use this authentication in each API call Any help?


Solution

  • You can use Slim Authentication and XSS Middlewares, for example HttpBasic. So added the Extras to your Slim Framework:

    https://github.com/codeguy/Slim-Extras/tree/master/Middleware

    How to use it? As as this:

    use \Slim\Slim;
    use \Slim\Extras\Middleware\HttpBasicAuth;
    
    $app = new Slim();
    $app->add(new HttpBasicAuth('theUsername', 'thePassword'));