1: <?php
2: 3: 4: 5: 6: 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: 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: 35: 36:
37: public static function Key()
38: {
39: $auth = new Auth("KEY");
40: return $auth;
41: }
42:
43: 44: 45: 46:
47: public static function Checksum()
48: {
49: $auth = new Auth("CHECKSUM");
50: return $auth;
51: }
52:
53: 54: 55: 56: 57: 58:
59: public static function Credentials($userName, $password)
60: {
61: $auth = new Auth("CREDENTIALS",$userName,$password);
62: return $auth;
63: }
64:
65: 66: 67: 68: 69: 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: 105: 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: }