From 981eb0d01458cebf032955ef788c8c88a3a99428 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Mon, 22 Jun 2015 20:17:30 -0400 Subject: [PATCH] upgrade login/password/session/auth handling --- admin.html | 2 +- admin.php | 7 +--- admin_admins.html | 69 ------------------------------ admin_admins.php | 114 -------------------------------------------------- admin_admins.sql | 12 ------ admin_files.php | 7 +--- admin_images.php | 5 +-- admin_login.html | 26 ------------ admin_login.php | 48 --------------------- admin_pages.php | 5 +-- config.php | 1 + inc/cms.php | 2 +- inc/session_auth.php | 87 ++++++++++++++++++++++++++++++++++++++ login.html | 24 +++++++++++ login.php | 67 +++++++++++++++++++++++++++++ 15 files changed, 185 insertions(+), 291 deletions(-) delete mode 100644 admin_admins.html delete mode 100644 admin_admins.php delete mode 100644 admin_admins.sql delete mode 100644 admin_login.html delete mode 100644 admin_login.php create mode 100644 inc/session_auth.php create mode 100644 login.html create mode 100644 login.php diff --git a/admin.html b/admin.html index c60e53f..cce5a90 100644 --- a/admin.html +++ b/admin.html @@ -15,7 +15,7 @@

Manage (downloadable) files

-

Manage administrators (passwords, etc.)

+

Manage accounts (admin passwords, etc.)

Log out

diff --git a/admin.php b/admin.php index f78b84b..ed40e7a 100644 --- a/admin.php +++ b/admin.php @@ -1,10 +1,5 @@ - - - - - <!--~$title show {~-->Accounts<!--~}~--> - - - - - - - -

Add a new accountEdit account "~username html~"

- -
- -
Name (optional)
-
- -
Username (required)
-
This is used to log in, and is case sensitive, so you may want to stick with all lowercase
-
- -
Password (case sensitive)
-
If this is blank, the user will be unable to log in.
-
Below you'll see only the encrypted version of the password. This is the only thing that's stored on the server, so if somebody has forgotten their password, the only thing that can be done about it is setting a new password using this field. If you do not edit this field, their password is unchanged.
-
- -
Role
-
Set to "None" to disable the account. This is useful if you might want to enable it again with the same password, since (unlike deleting the account) the password is preserved in the database.
-
- -
 
-
- -
- -
 
-
CancelCancel
- - - -

Accounts Listing

- - -

[Add a new account]

- - - - - - - - - - -
NameUsernameRole 
~name html~(blank)~username html~(blank)~privs html~(blank)[delete this account]
- - -

No accounts in database.

- - -

[Add a new account]

- - - - - diff --git a/admin_admins.php b/admin_admins.php deleted file mode 100644 index 6a8f0c4..0000000 --- a/admin_admins.php +++ /dev/null @@ -1,114 +0,0 @@ - session_generate_key() # [a-zA-Z0-9]{16} - ); - } - - tem_set('form', $data); -} diff --git a/admin_admins.sql b/admin_admins.sql deleted file mode 100644 index b276d69..0000000 --- a/admin_admins.sql +++ /dev/null @@ -1,12 +0,0 @@ -drop table if exists admins; -create table admins ( - id int unique auto_increment, - name varchar(100) not null default "", - username varchar(50) not null default "", - password varchar(50) not null default "", - privs varchar(100) not null default "" -); -insert into admins (username,password,privs) values ( - 'fixme', - '98fd71615b073b75810f4ed40d4538198c6450cc', /* sha1("fixme") */ - 'admin'); diff --git a/admin_files.php b/admin_files.php index 98c98e3..1531d9f 100644 --- a/admin_files.php +++ b/admin_files.php @@ -44,12 +44,7 @@ function admin_files_get_fields() { function admin_files_main() { - if(logged_in_as_admin()) { - tem_set('admin_privs'); - } else { - $_REQUEST['url'] = this_url(); - return 'admin_login'; - } + session_auth_must('manage_files'); $id = _REQUEST_cut('edit_id'); if($id) { diff --git a/admin_images.php b/admin_images.php index 9930889..05ff2ce 100644 --- a/admin_images.php +++ b/admin_images.php @@ -55,10 +55,7 @@ function admin_images_get_fields() { function admin_images_main() { - if(!logged_in_as_admin()) { - $_REQUEST['url'] = this_url(); - return 'admin_login'; - } + session_auth_must('admin_images'); $id = _REQUEST_cut('edit_id'); if($id) { diff --git a/admin_login.html b/admin_login.html deleted file mode 100644 index 4dc10b7..0000000 --- a/admin_login.html +++ /dev/null @@ -1,26 +0,0 @@ - - - - - <!--~$title show {~-->~$host~ Admin Login<!--~}~--> - - - - - -

~$host~ Admin Login

- -
-
Username (case sensitive)
-
- -
Password (case sensitive)
-
- -
 
-
-
- - - - diff --git a/admin_login.php b/admin_login.php deleted file mode 100644 index e68f86e..0000000 --- a/admin_login.php +++ /dev/null @@ -1,48 +0,0 @@ - null, + 'role' => null, + 'name' => null, + 'username' => null, + 'last_active' => null, + 'password_reset' => null + ]; + + if ($id) { + $user = db_get_assoc('users', 'role,name,username', 'where id=%i', $id); + $now = time(); + db_update('users', 'last_active', $now, 'where id=%i', $id); + $GLOBALS['wfpl_session_auth']['id'] = $id; + $GLOBALS['wfpl_session_auth']['role'] = $user['role']; + $GLOBALS['wfpl_session_auth']['name'] = $user['name']; + $GLOBALS['wfpl_session_auth']['username'] = $user['username']; + $GLOBALS['wfpl_session_auth']['last_active'] = $now; + } + + if ($password_reset) { + $GLOBALS['wfpl_session_auth']['password_reset'] = true; + $GLOBALS['wfpl_session_auth']['id'] = session_get('auth_password_reset_id'); + } +} + +# return an assoc containing info about the authenticated user, see session_auth_init +function session_auth() { + if (!isset($GLOBALS['wfpl_session_auth'])) { + $id = false; + $reset = false; + if (session_exists()) { + $id = session_get('auth_id'); + if (!$id) { + $r = session_get('auth_password_reset'); + if (strlen($r)) { + $r = (int) format_int_0($r); + if (time() < $r) { + $reset = true; + } else { + message('Oops, your temporary access (to change your password) has expired'); + session_clear('auth_password_reset'); + } + } + } + } + session_auth_init($id, $reset); + } + return $GLOBALS['wfpl_session_auth']; +} + +# return true if the logged in user is allowed to $priv +# (false if they are not logged in, or aren't alowed to $priv) +function session_auth_can($priv) { + $s = session_auth(); + if ($s['role'] === 'admin') { + return true; + } + return false; +} + +# return ONLY IF the currently logged in user can $priv +# otherwise, it displays the login page, and exit early +function session_auth_must($priv) { + if (session_auth_can($priv)) { + return; + } + if (!isset($_REQUEST['after_login'])) { + $_REQUEST['after_login_url'] = this_url(); + } + wfpl_main('login'); + exit(); +} diff --git a/login.html b/login.html new file mode 100644 index 0000000..6a3fbcf --- /dev/null +++ b/login.html @@ -0,0 +1,24 @@ + + + + + + + + + + +
+
Username
+
+ +
Password (case sensitive)
+
+ +
 
+
+
+ + + + diff --git a/login.php b/login.php new file mode 100644 index 0000000..4ec1344 --- /dev/null +++ b/login.php @@ -0,0 +1,67 @@ +