-
Notifications
You must be signed in to change notification settings - Fork 1
/
TokenMiddleware.php
132 lines (118 loc) · 3.85 KB
/
TokenMiddleware.php
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
<?php
class TokenMiddleware extends Prefab {
protected $app, $routes;
public function __construct() {
$this->app = Base::instance();
$this->routes = array();
}
public function protect($pattern, $handler) {
$bak = $this->app->ROUTES;
$this->app->ROUTES=array();
$this->app->route($pattern, $handler);
$this->routes = (count($this->routes)) ? $this->app->extend('ROUTES',$this->routes) : $this->app->ROUTES;
$this->app->ROUTES=$bak;
}
public function run() {
if (!count($this->routes))
return;
$paths=[];
foreach ($keys=array_keys($this->routes) as $key) {
$path=preg_replace('/@\w+/','*@',$key);
if (substr($path,-1)!='*')
$path.='+';
$paths[]=$path;
}
$vals=array_values($this->routes);
array_multisort($paths,SORT_DESC,$keys,$vals);
$this->routes=array_combine($keys,$vals);
// Convert to BASE-relative URL
$req=urldecode($this->app['PATH']);
foreach ($this->routes as $pattern=>$routes) {
if (!$args=$this->app->mask($pattern,$req))
continue;
ksort($args);
$route=NULL;
$ptr=$this->app->CLI?\Base::REQ_CLI:$this->app->AJAX+1;
if (isset($routes[$ptr][$this->app->VERB]) ||
isset($routes[$ptr=0]))
$route=$routes[$ptr];
if (!$route)
continue;
if ($this->app->VERB!='OPTIONS' &&
isset($route[$this->app->VERB])) {
if ($this->app['VERB']=='GET' &&
preg_match('/.+\/$/',$this->app['PATH']))
$this->app->reroute(substr($this->app['PATH'],0,-1).
($this->app['QUERY']?('?'.$this->app['QUERY']):''));
$handler=$route[$this->app->VERB][0];
$alias=$route[$this->app->VERB][3];
if (is_string($handler)) {
// Replace route pattern tokens in handler if any
$handler=preg_replace_callback('/({)?@(\w+\b)(?(1)})/',
function($id) use($args) {
$pid=count($id)>2?2:1;
return isset($args[$id[$pid]])?
$args[$id[$pid]]:
$id[0];
},
$handler
);
if (preg_match('/(.+)\h*(?:->|::)/',$handler,$match) &&
!class_exists($match[1]))
$this->app->error(500,'PreRoute handler not found');
}
if (!$this->app['RAW'] && !$this->app['BODY'])
$this->app['BODY']=file_get_contents('php://input');
return $this->validate($handler, $args, $alias) !== FALSE;
}
}
return true;
}
protected function validate($handler, $args, $alias) {
$authHeader = null;
$type = strtoupper($this->app->get('TOKEN.TYPE'));
if($type === 'HEADER') {
$authHeader = $this->app->get('HEADERS.' . $this->app->get('TOKEN.KEY'));
} else if($type === 'QUERY') {
$verb = $this->app->get('VERB');
$authHeader = $this->app->get($verb . '.' . $this->app->get('TOKEN.KEY'));
}
$startsWith = $this->app->get('TOKEN.STARTS_WITH');
if(!$authHeader || (($type === 'HEADER' && $startsWith) && !$this->startsWith($authHeader, $startsWith))) {
$this->app->call($handler, array($this->app, $args, $alias));
return false;
}
$authToken = $authHeader;
if($startsWith && $type === 'HEADER') {
$_ex = explode($startsWith . ' ', $authHeader);
$authToken = isset($_ex[1]) ? $_ex[1] : null;
}
if(!$authToken) {
$this->app->call($handler, array($this->app, $args, $alias));
return false;
}
$model = $this->app->get('TOKEN.TABLE');
if(!class_exists($model)) {
throw new Exception($model . ' class not exists!');
return false;
}
$model = new $model();
$model->load([$this->app->get('TOKEN.TABLE_KEY') . ' = ?', $authToken]);
if($model->dry()) {
$this->app->call($handler, array($this->app, $args, $alias));
return false;
}
$this->app->set('TOKEN.token', $authToken);
if(isset($model->user)) {
$this->app->set('TOKEN.user', $model->user);
}
}
private function startsWith($haystack, $needles) {
foreach ((array) $needles as $needle) {
if ($needle !== '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
return true;
}
}
return false;
}
}