title: RESTful service - using Phil Sturgeon Rest Server : Logic / function for validating the users with their username and password?
body: <p>I'm writing a simple RESTful service, using Phil Sturgeon Rest Server. Can anyone provide me a solution for login using username and password. I am able to get all the json reponse without login. </p>
  
  <p><strong>Porblem 1</strong> : <code>$config['rest_auth'] = 'basic';</code> </p>
  
  <blockquote>
    <p>An Error Was Encountered</p>
    
    <p>The configuration file ldap.php does not exist.</p>
    
    <p>The same happens with <code>$config['rest_auth'] = 'digest';</code> </p>
  </blockquote>
  
  <p>I haven't used "ldap" earlier and don't know how it works apart from a few basic information. So could you please tell me what could be the reason for this error ? </p>
  
  <blockquote>
    <p><strong>Tried out Solutions</strong> </p>
    
    <p>I changed the value of <code>$config['auth_source'] = 'ldap';</code> to <code>$config['auth_source'] = '';</code> , Now REST Login Usernames are working for both basic and digest , ie;</p>
  </blockquote>
  
  <p><code>$config['rest_auth'] = 'basic';</code> or <code>$config['rest_auth'] = 'digest';</code>. </p>
  
  <pre><code>$config['rest_valid_logins'] = ['admin' =&gt; '1234','sudheesh'=&gt;'test'];
  </code></pre>
  
  <blockquote>
    <p><strong>Prevailing issue :</strong> unable to use <strong>session</strong> for authentication</p>
  </blockquote>
  
  <p>Tried the commented notes from Phil Sturgeon ie; </p>
  
  <blockquote>
    <p>Note: If 'rest_auth' is set to 'session' then change 'auth_source' to
    the name of the session variable</p>
  </blockquote>
  
  <p><strong>How the session is created in MODEL, it is here  :</strong> </p>
  
  <pre><code>if ($query-&gt;num_rows() == 1) {
              // If there is a user, then create session data
              $row = $query-&gt;row();
              $data = array(
                  'id' =&gt; $row-&gt;id,
                  'name' =&gt; $row-&gt;full_name,
                  'email' =&gt; $row-&gt;email,
                  'phone' =&gt; $row-&gt;phone,
                  'acc_status' =&gt; $row-&gt;rec_status,
                  'validated' =&gt; true
  
              );
              $this-&gt;session-&gt;set_userdata($data);
              //$this-&gt;session-&gt;set_authkey('1e957ebc35631ab22d5bd6526bd14ea2');
              //print_r($data);
              return $data;
  </code></pre>
  
  <p><strong>Question is :</strong> How can I change the 'auth_source' to
   the name of the session variable , </p>
  
  <blockquote>
    <p>Right now it is <code>$config['auth_source'] = ''</code></p>
    
    <p>Do i have to change it to : <code>$config['validated']</code> , if I do this am not getting the access , I have read <a href="http://pythonhackers.com/p/chriskacerguis/codeigniter-restserver" rel="nofollow">here</a> that:</p>
    
    <p>If you're tying this library into an AJAX endpoint where clients
    authenticate using PHP sessions then you may not like either of the
    digest nor basic authentication methods. In that case, you can tell
    the REST Library what PHP session variable to check for. If the
    variable exists, then the user is authorized. It will be up to your
    application to set that variable. You can define the variable in
    <code>$config['auth_source']</code>. Then tell the library to use a php session
    variable by setting <code>$config['rest_auth']</code> to <code>session</code>.</p>
  </blockquote>
  
  <p>Is there any suggestions ? </p>
  
  <p><strong>Problem 2</strong> : How can I grant API access to users with a valid username and password ? </p>
  
  <blockquote>
    <p>Can anyone provide me with a function or detailed information on how to implement this ? </p>
  </blockquote>
  
  <p><strong>Other Doubts</strong> : </p>
  
  <blockquote>
    <p><code>$config['rest_valid_logins'] = ['admin' =&gt; '1234'];</code></p>
  </blockquote>
  
  <p>The description for this 'REST Login Usernames' says if ldap is configured this is ignored. </p>
  
  <p><strong><em>Question</em></strong> : How can I use this Array of usernames and passwords for login, without configuring LDAP. </p>
  
  <blockquote>
    <p>REST Login Class and Function </p>
  </blockquote>
  
  <p>This says,  If library authentication is used define the class and function name. 
  The function should accept two parameters: <code>class-&gt;function($username, $password)</code>.
  In other cases override the function _perform_library_auth in your controller.</p>
  
  <p>For digest authentication the library function should return already a stored md5(username:restrealm:password) for that username.</p>
  
  <p>e.g: md5('admin:REST API:1234') = '1e957ebc35631ab22d5bd6526bd14ea2'</p>
  
  <pre><code>$config['auth_library_class'] = '';
  $config['auth_library_function'] = '';
  </code></pre>
  
  <p><strong>Question:</strong> Can I use this to allow users with a valid username and password to access the API ? If Yes , Do you have any functions already written to help in this scenario , any help would be highly appreciated. Thank you very much . 
  If you know answers for any of my issues, please help. Thanks again.       </p>