Overview

Namespaces

  • Core
    • BaseClasses
    • Cookie
    • Crontab
    • DB
    • Input
    • Mailer
    • Middleware
      • Auth
      • CSRF
    • Route
    • Session
    • System
  • None

Classes

  • Bridge
  • Core\BaseClasses\BaseAuth
  • Core\BaseClasses\BaseController
  • Core\BaseClasses\BaseCSRF
  • Core\Cookie\Cookie
  • Core\Crontab\Crontab
  • Core\DB\DB
  • Core\Input\Input
  • Core\Mailer\Mailer
  • Core\Middleware\Auth\Auth
  • Core\Middleware\CSRF\CSRF
  • Core\Route\Route
  • Core\Session\Session
  • Core\System\System

Functions

  • back
  • ControllerAutoload
  • dbd
  • ErrorHandler
  • ExceptionHandler
  • json
  • loadConfig
  • MiddlewareAutoload
  • redirect
  • SPAutoload
  • view
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Created by PhpStorm.
  4:  * @Author: Shakti Phartiyal
  5:  * Date: 11/24/16
  6:  * Time: 4:43 PM
  7:  */
  8: namespace Core\Middleware\Auth;
  9: 
 10: use Core\BaseClasses\BaseController;
 11: use Core\Session\Session;
 12: use Core\System\System;
 13: use Core\BaseClasses\BaseAuth;
 14: 
 15: class Auth extends BaseAuth
 16: {
 17:     private $type = null;
 18:     private $userName = null;
 19:     private $password = null;
 20:     /**
 21:      * Auth constructor.
 22:      */
 23:     private function __construct($type,$userName=null,$password=null)
 24:     {
 25:         $this->type = $type;
 26:         if($userName !=null && $password!=null)
 27:         {
 28:             $this->userName = $userName;
 29:             $this->password = $password;
 30:         }
 31:     }
 32: 
 33:     /**
 34:      * Used to define that a key based authentication is being used.
 35:      * @return Auth
 36:      */
 37:     public static function Key()
 38:     {
 39:         $auth = new Auth("KEY");
 40:         return $auth;
 41:     }
 42: 
 43:     /**
 44:      * * Used to define that a checksum based authentication is being used.
 45:      * @return Auth
 46:      */
 47:     public static function Checksum()
 48:     {
 49:         $auth = new Auth("CHECKSUM");
 50:         return $auth;
 51:     }
 52: 
 53:     /**
 54:      * * Used to define that a username/password based authentication is being used.
 55:      * @param $userName
 56:      * @param $password
 57:      * @return Auth
 58:      */
 59:     public static function Credentials($userName, $password)
 60:     {
 61:         $auth = new Auth("CREDENTIALS",$userName,$password);
 62:         return $auth;
 63:     }
 64: 
 65:     /**
 66:      * Authorize a user to access a particular resource
 67:      * @param $authType
 68:      * @param null $redirectTo
 69:      * @return bool
 70:      */
 71:     public function Authorize($authType, $redirectTo=null)
 72:     {
 73:         $auth = new BaseAuth();
 74:         if($authType == "session")
 75:         {
 76:             if(Session::has('user_id') && Session::has('user_name') && Session::has('login_time'))
 77:             {
 78:                 $match = $auth->matchSessionData(Session::get('user_id'),Session::get('user_name'));
 79:                 if($match)
 80:                 {
 81:                     return true;
 82:                 }
 83:                 else
 84:                 {
 85:                     if($redirectTo!=null)
 86:                     {
 87:                         BaseController::redirect($redirectTo);
 88:                     }
 89:                     return false;
 90:                 }
 91:             }
 92:             else
 93:             {
 94:                 if($redirectTo!=null)
 95:                 {
 96:                     BaseController::redirect($redirectTo);
 97:                 }
 98:                 return false;
 99:             }
100:         }
101:     }
102: 
103:     /**
104:      * Authenticate a user and return its details
105:      * @return bool|mixed
106:      */
107:     public function Authenticate()
108:     {
109:         $auth = new BaseAuth();
110:         if($this->type == "KEY")
111:         {
112:             if(!$auth->headers()->hasAuthKey())
113:             {
114:                 System::GiveError(401, "Missing Auth Key");
115:             }
116:             if(!$auth->keyHasAssociatedID($auth->getAuthKey()))
117:             {
118:                 System::GiveError(401, "Invalid Auth Key");
119:             }
120:         }
121:         else if($this->type == "CHECKSUM")
122:         {
123: 
124:         }
125:         else if($this->type == "CREDENTIALS")
126:         {
127:             $authDetails = $auth->matchCredentials($this->userName,$this->password);
128:             if($authDetails)
129:             {
130:                 Session::set('user_id',$authDetails['id']);
131:                 Session::set('user_name',$authDetails['user_name']);
132:                 Session::set('login_time',time());
133:             }
134:             return $authDetails;
135:         }
136:     }
137: }
API documentation generated by ApiGen