-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_apps.inc
154 lines (126 loc) · 3.32 KB
/
security_apps.inc
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
<?php
class access_apps
{
public static function access($page)
{
if( preg_match("/^https?:/", $page) )
return true;
if( strlen($page) > 1 )
$page = rtrim($page, "/");
$a = array();
$a['/forbidden'] = array('SELF_ACCESS');
if( !is_array($page) )
$page = array($page);
foreach( $page as $p )
{
if( isset($a[$p]) )
{
foreach( $a[$p] as $g )
if( !security::hasGrant($g) )
return false;
}
}
return true;
}
}
class security_apps extends security
{
public function login($user='', $pass='', $save=false)
{
if( $user == 'admin' )
throw new SiteException('Invalid creditentials', 403, "Invalid username : {$user}");
if( !preg_match("/^[a-fA-F0-9]{32}$/", $pass) )
{
// this is a password so get a list of tokens
$tokens = api::send('system/token/list', array('auth'=>'', 'user'=>$user, 'pass'=>$pass));
if( count($tokens) < 1 )
throw new SiteException("Forbidden", 403, "No available token");
if( !$GLOBALS['CONFIG']['FORCE_FIRST_TOKEN'] )
{
// redirect to the login page
foreach( $tokens as $t )
$_SESSION['LOGIN_TOKENS'][] = array('token'=>$t['token_value'], 'name'=>$t['name']);
$_SESSION['LOGIN_USER'] = $user;
template::redirect($GLOBALS['CONFIG']['LOGIN_PAGE']);
}
else
{
// force login with the first token
$pass = $tokens[0]['token_value'];
}
}
// if reaching here, it means the $pass is a real token
// so proceed to real authentication
$this->directLogin($user.':'.$pass);
if( $save )
$this->save();
if( isset($_SESSION['LOGIN_REDIRECT']) )
{
$r = $_SESSION['LOGIN_REDIRECT'];
template::redirect($r);
}
else
template::redirect($GLOBALS['CONFIG']['PANEL_PAGE']);
}
public function directLogin($auth)
{
security::set('API_AUTH', $auth);
$expl = explode(':', $auth);
security::set('USER', $expl[0]);
security::set('TOKEN', $expl[1]);
$this->loadGrants();
}
public function loadGrants()
{
security::clearGrant();
$grants = api::send('self/system/grant/token/list', array('token'=>security::get('TOKEN')));
foreach( $grants as $g )
security::setGrant($g['grant_name']);
}
public function logout()
{
parent::logout();
if( isset($_COOKIE['BUSITPIE']) )
setcookie('BUSITPIE', '', time()-60*60*24*30, '/');
}
private function save($auth=null)
{
if( $auth == null )
$auth = security::get('API_AUTH');
$crypt = '';
for( $i = 0; $i < strlen($auth); $i++ )
$crypt .= base_convert(ord($auth[$i]), 10, 36);
setcookie('BUSITPIE', base64_encode($crypt), mktime(0,0,0,12,31,2030), '/');
}
private function load()
{
if( !isset($_COOKIE['BUSITPIE']) )
return null;
$crypt = base64_decode($_COOKIE['BUSITPIE']);
$auth = '';
for( $i = 0; $i < strlen($crypt); $i += 2 )
$auth .= chr(base_convert(substr($crypt,$i,2), 36, 10));
$this->directLogin($auth);
}
public function hasAccess($page)
{
if( !access_apps::access($page) )
{
// load cookie if the user is not logged yet
if( !access_apps::access('/') )
{
$this->load();
return access_apps::access($page);
}
else
return false;
}
return true;
}
public function requireAccess($page)
{
if( !$this->hasAccess($page) )
throw new SiteException('Unsufficient permission', 403, 'Not all grants available to see page : ' . $page);
}
}
?>