summaryrefslogtreecommitdiffstats
path: root/src/Psecio/Gatekeeper/Gatekeeper.php
blob: 5741252379021451464f75ec683ae09ab67bd695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<?php

namespace Psecio\Gatekeeper;

use \Psecio\Gatekeeper\Model\User;

class Gatekeeper
{
    /**
     * Database (PDO) instance
     * @var \PDO
     */
    private static $pdo;

    /**
     * Allowed actions
     * @var array
     */
    private static $actions = array(
        'find', 'delete', 'create', 'save'
    );

    /**
     * Current data source
     * @var \Psecio\Gatekeeper\DataSource
     */
    private static $datasource;

    /**
     * Throttling enabled or disabled
     * @var boolean
     */
    private static $throttleStatus = true;

    /**
     * Current set of restrictions
     * @var array
     */
    private static $restrictions = array();

    /**
     * Current configuration options
     * @var array
     */
    private static $config = array();

    /**
     * Initialize the Gatekeeper instance, set up environment file and PDO connection
     *
     * @param string $envPath Environment file path (defaults to CWD)
     * @param array $config Configuration settings [optional]
     * @param \Psecio\Gatekeeper\DataSource $datasource Custom datasource provider
     */
    public static function init($envPath = null, array $config = array(), \Psecio\Gatekeeper\DataSource $datasource = null)
    {
        $result = self::loadConfig($config, $envPath);
        if ($datasource === null) {
            $datasource = self::buildDataSource($config, $result);
        }
        self::$datasource = $datasource;

        if (isset($config['throttle']) && $config['throttle'] === false) {
            self::disableThrottle();
        }
    }

    /**
     * Get the configuration either from the config given or .env path
     *
     * @param array $config Configuration values
     * @param string $envPath Path to .env file
     * @return array Set of configuration values
     */
    public static function loadConfig(array $config, $envPath = null)
    {
        $envPath = ($envPath !== null) ? $envPath : getcwd();
        $result = self::loadDotEnv($envPath);

        // If the .env load failed, use the config given
        if ($result === false) {
            if (empty($config)) {
                throw new \InvalidArgumentException('Configuration values must be defined!');
            }
            $result = $config;
        }
        self::setConfig($result);
        return $result;
    }

    /**
     * Set the current configuration options
     *
     * @param array $config Set of configuration settings
     */
    public static function setConfig(array $config)
    {
        self::$config = $config;
    }

    /**
     * Get the current configuration information
     *     If an index is given, it tries to find it. If found,
     *     returns just that value. If not, returns null. Otherwise
     *     returns all config values
     *
     * @param string $index Index to locate [optional]
     * @return mixed Single value if index found, otherwise array of all
     */
    public static function getConfig($index = null)
    {
        if ($index !== null) {
            return (isset(self::$config[$index])) ? self::$config[$index] : null;
        } else {
            return self::$config;
        }
    }

    /**
     * Build a datasource object
     *
     * @param array $config Configuration settings
     * @param array $result Environment data
     * @throws \Exception If data source type is not valid
     * @return \Psecio\Gatekeeper\DataSource instance
     */
    public static function buildDataSource(array $config, $result)
    {
        $dsType = (isset($config['source'])) ? $config['source'] : 'mysql';
        $dsClass = '\\Psecio\\Gatekeeper\\DataSource\\'.ucwords($dsType);
        if (!class_exists($dsClass)) {
            throw new \InvalidArgumentException('Data source type "'.$dsType.'" not valid!');
        }

        try {
            $datasource = new $dsClass($result);
            return $datasource;
        } catch (\Exception $e) {
            throw new \Exception('Error creating data source "'.$dsType.'" ('.$e->getMessage().')');
        }
    }

    /**
     * Get the current datasource
     *
     * @return \Psecio\Gatekeeper\DataSource instance
     */
    public static function getDatasource()
    {
        return self::$datasource;
    }

    /**
     * Set the current data source to the one given
     *
     * @param \Psecio\Gatekeeper\DataSource $ds Data source instance
     */
    public static function setDatasource($ds)
    {
        self::$datasource = $ds;
    }

    /**
     * Get the current list of restrictions
     *
     * @return array Set of restrictions
     */
    public static function getRestrictions()
    {
        return self::$restrictions;
    }

    /**
     * Load the variables using the .env handling
     *
     * @param string $envPath Path to the .env file
     * @return array|boolean Array of data if found, false if load fails
     */
    protected static function loadDotEnv($envPath)
    {
        try {
            \Dotenv::load($envPath);
            $config = array(
                'username' => $_SERVER['DB_USER'],
                'password' => $_SERVER['DB_PASS'],
                'name' => $_SERVER['DB_NAME'],
                'type' => (isset($_SERVER['DB_TYPE'])) ? $_SERVER['DB_TYPE'] : 'mysql',
                'host' => $_SERVER['DB_HOST']
            );
            if (isset($_SERVER['DB_PREFIX'])) {
                $config['prefix'] = $_SERVER['DB_PREFIX'];
            }
            return $config;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * Create and setup a new model instance
     *
     * @param string $type Model class name
     * @throws \InvalidArgumentException If class requested is not valid
     * @return object Model instance
     */
    public static function modelFactory($type, array $data = array())
    {
        $class = '\\Psecio\\Gatekeeper\\'.$type;
        if (!class_exists($class)) {
            throw new \InvalidArgumentException('Model type "'.$class.'" does not exist!');
        }
        $model = new $class(self::$datasource, $data);
        return $model;
    }


    /**
     * Safer way to evaluate if hashes equal
     *
     * @param string $hash1 Hash #1
     * @param string $hash2 Hash #1
     * @return boolean Pass/fail on hash equality
     */
    public static function hash_equals($hash1, $hash2)
    {
        if (\function_exists('hash_equals')) {
            return \hash_equals($hash1, $hash2);
        }
        if (\strlen($hash1) !== \strlen($hash2)) {
            return false;
        }
        $res = 0;
        $len = \strlen($hash1);
        for ($i = 0; $i < $len; ++$i) {
            $res |= \ord($hash1[$i]) ^ \ord($hash2[$i]);
        }
        return $res === 0;
    }

    /**
     * Authenticate a user given the username/password credentials
     *
     * @param array $credentials Credential information (must include "username" and "password")
     * @param boolean $remember Flag to activate the "remember me" functionality
     * @return boolean Pass/fail of authentication
     */
    public static function authenticate(array $credentials, $remember = false)
    {
        $username = $credentials['username'];
        $user = new UserModel(self::$datasource);
        $user->findByUsername($username);

        // If they're inactive, they can't log in
        if ($user->status === UserModel::STATUS_INACTIVE) {
            throw new Exception\UserInactiveException('User "'.$username.'" is inactive and cannot log in.');
        }

        // Handle some throttle logic, if it's turned on
        if (self::$throttleStatus === true) {
            // Set up our default throttle restriction
            $instance = new \Psecio\Gatekeeper\Restrict\Throttle(array('userId' => $user->id));
            self::$restrictions[] = $instance;
        }

        // Check any restrictions
        if (!empty(self::$restrictions)) {
            foreach (self::$restrictions as $restriction) {
                if ($restriction->evaluate() === false) {
                    throw new Exception\RestrictionFailedException('Restriction '.get_class($restriction).' failed');
                }
            }
        }

        // Verify the password!
        $result = password_verify($credentials['password'], $user->password);

        if (self::$throttleStatus === true && $result === true) {
            $instance->model->allow();

            if ($remember === true) {
                self::rememberMe($user);
            }
        }

        return $result;
    }

    /**
     * Disable the throttling
     */
    public static function disableThrottle()
    {
        self::$throttleStatus = false;
    }

    /**
     * Enable the throttling feature
     */
    public static function enableThrottle()
    {
        self::$throttleStatus = true;
    }

    /**
     * Return the enabled/disabled status of the throttling
     *
     * @return boolean Throttle status
     */
    public static function throttleStatus()
    {
        return self::$throttleStatus;
    }

    /**
     * Get the user throttle information
     *     If not found, makes a new one
     *
     * @param integer $userId User ID
     * @return ThrottleModel instance
     */
    public static function getUserThrottle($userId)
    {
        try {
            $throttle = Gatekeeper::findThrottleByUserId($userId);
        } catch (Exception\ThrottleNotFoundException $e) {
            $data = array(
                'user_id' => $userId,
                'attempts' => 1,
                'status' => ThrottleModel::STATUS_ALLOWED,
                'last_attempt' => date('Y-m-d H:i:s'),
                'status_change' => date('Y-m-d H:i:s')
            );
            $throttle = Gatekeeper::modelFactory('ThrottleModel', $data);
        }
        return $throttle;
    }

    /**
     * Register a new user
     *
     * @param array $userData User data
     * @return boolean Success/fail of user create
     */
    public static function register(array $userData)
    {
        $user = new UserModel(self::$datasource, $userData);
        if (self::$datasource->save($user)  === false) {
            echo 'ERROR: '.$user->getLastError()."\n";
            return false;
        }
        return true;
    }

    /**
     * Handle undefined static function calls
     *
     * @param string $name Function name
     * @param arrya $args Arguments set
     * @return mixed Boolean false if function not matched, otherwise Model instance
     */
    public static function __callStatic($name, $args)
    {
        // find the action first
        $action = 'find';
        foreach (self::$actions as $a) {
            if (strstr($name, $a) !== false) {
                $action = $a;
            }
        }

        if ($action == 'find') {
            return self::handleFindBy($name, $args);
        } elseif ($action == 'create') {
            return self::handleCreate($name, $args);
        } elseif ($action == 'delete') {
            return self::handleDelete($name, $args);
        } elseif ($action == 'save') {
            return self::handleSave($name, $args);
        }
        return false;
    }

    /**
     * Handle the "findBy" calls for data
     *
     * @param string $name Function name called
     * @param array $args Arguments
     * @throws \Exception\ModelNotFoundException If model type is not found
     * @throws \Exception If Data could not be found
     * @return object Model instance
     */
    public static function handleFindBy($name, $args)
    {
        $action = 'find';
        $name = str_replace($action, '', $name);
        preg_match('/By(.+)/', $name, $matches);

        if (empty($matches) && strtolower(substr($name, -1)) === 's') {
            return self::handleFindByMultiple($name, $args, $matches);
        } else {
            return self::handleFindBySingle($name, $args, $matches);
        }

        return $instance;
    }

    /**
     * Handle the "find by" when a single record is requested
     *
     * @param string $name Name of function called
     * @param array $args Arguments list
     * @param array $matches Matches from regex
     * @return \Modler\Collection collection
     */
    public static function handleFindBySingle($name, $args, $matches)
    {
        $property = lcfirst($matches[1]);
        $model = str_replace($matches[0], '', $name);
        $data = array($property => $args[0]);

        $modelNs = '\\Psecio\\Gatekeeper\\'.$model.'Model';
        if (!class_exists($modelNs)) {
            throw new Exception\ModelNotFoundException('Model type '.$model.' could not be found');
        }
        $instance = new $modelNs(self::$datasource);
        $instance = self::$datasource->find($instance, $data);

        if ($instance->id === null) {
            $exception = '\\Psecio\\Gatekeeper\\Exception\\'.$model.'NotFoundException';
            throw new $exception($model.' could not be found for criteria');
        }

        return $instance;
    }

    /**
     * Handle the "find by" when multiple are requested
     *
     * @param string $name Name of function called
     * @param array $args Arguments list
     * @param array $matches Matches from regex
     * @return \Modler\Collection collection
     */
    public static function handleFindByMultiple($name, $args, $matches)
    {
        $data = (isset($args[0])) ? $args[0] : array();
        $model = substr($name, 0, strlen($name) - 1);
        $collectionNs = '\\Psecio\\Gatekeeper\\'.$model.'Collection';
        if (!class_exists($collectionNs)) {
            throw new Exception\ModelNotFoundException('Collection type '.$model.' could not be found');
        }
        $model = self::modelFactory($model.'Model');
        $collection = new $collectionNs(self::$datasource);
        $collection = self::$datasource->find($model, $data, true);

        return $collection;
    }

    /**
     * Handle the calls to create a new instance
     *
     * @param string $name Function name
     * @param array $args Argument set
     * @throws Exception\ModelNotFoundException If model type is not found
     * @return mixed Boolean false if method incorrect, model instance if created
     */
    public static function handleCreate($name, array $args)
    {
        $model = '\\Psecio\\Gatekeeper\\'.str_replace('create', '', $name).'Model';
        if (class_exists($model) === true) {
            $instance = new $model(self::$datasource, $args[0]);
            $instance = self::$datasource->save($instance);
            return $instance;
        } else {
            throw new Exception\ModelNotFoundException('Model type '.$model.' could not be found');
        }
        return false;
    }

    /**
     * Handle the delete requests
     *
     * @param string $name Function name called
     * @param array $args Arguments set
     * @return boolean Success/fail of delete request
     */
    public static function handleDelete($name, array $args)
    {
        $model = self::buildModel('delete', $name, $args);
        return self::$datasource->delete($model);
    }

    /**
     * Handle the saving of a model instance
     *
     * @param string $name Name of funciton called
     * @param array $args Arguments set
     * @return boolean Success/fail of save request
     */
    public static function handleSave($name, array $args)
    {
        return self::$datasource->save($args[0]);
    }

    /**
     * Build the model instance with data given
     *
     * @param string $action Action called (ex: "delete" or "create")
     * @param string $name Function nname
     * @param array $args Arguments set
     * @throws \Exception\ModelNotFoundException If model type is not found
     * @return object Model instance
     */
    protected static function buildModel($action = 'find', $name, array $args)
    {
        $name = str_replace($action, '', $name);
        preg_match('/By(.+)/', $name, $matches);

        if (empty($matches) && $args[0] instanceof \Modler\Model) {
            $model = $name;
            $data = $args[0]->toArray();
        } else {
            $property = lcfirst($matches[1]);
            $model = str_replace($matches[0], '', $name);
            $data = array($property => $args[0]);
        }

        $modelNs = '\\Psecio\\Gatekeeper\\'.$model.'Model';
        if (!class_exists($modelNs)) {
            throw new Exception\ModelNotFoundException('Model type '.$model.' could not be found');
        }

        $instance = new $modelNs(self::$datasource);
        $instance = self::$datasource->find($instance, $data);
        return $instance;
    }

    /**
     * Create a restriction and add it to be evaluated
     *
     * @param string $type Restriction type
     * @param array $config Restriction configuration
     */
    public static function restrict($type, array $config)
    {
        $classNs = '\\Psecio\\Gatekeeper\\Restrict\\'.ucwords(strtolower($type));
        if (!class_exists($classNs)) {
            throw new \InvalidArgumentException('Restriction type "'.$type.'" is invalid');
        }
        $instance = new $classNs($config);
        self::$restrictions[] = $instance;
    }

    /**
     * Enable and set up the "Remember Me" cookie token handling for the given user
     *
     * @param \Psecio\Gatekeeper\UserModel|string $user User model instance
     * @param array $config Set of configuration settings
     * @return boolean Success/fail of sesssion setup
     */
    public static function rememberMe($user, array $config = array())
    {
        if (is_string($user)) {
            $user = Gatekeeper::findUserByUsername($user);
        }

        $data = array_merge($_COOKIE, $config);
        $remember = new Session\RememberMe(self::$datasource, $data, $user);
        return $remember->setup();
    }

    /**
     * Check the "Remember Me" token information (if it exists)
     *
     * @return boolean|\Psecio\Gatekeeper\UserModel Success/fail of token validation or User model instance
     */
    public static function checkRememberMe()
    {
        $remember = new Session\RememberMe(self::$datasource, $_COOKIE);
        return $remember->verify();
    }

    /**
     * Evaluate the policy (found by name) against the data provided
     *
     * @param string $name Name of the policy
     * @param mixed $data Data to use in evaluation (single object or array)
     * @return boolean Pass/fail status of evaluation
     */
    public static function evaluatePolicy($name, $data)
    {
        $policy = Gatekeeper::findPolicyByName($name);
        return $policy->evaluate($data);
    }
}