-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp-simple-authentication.php
More file actions
75 lines (48 loc) · 1.39 KB
/
php-simple-authentication.php
File metadata and controls
75 lines (48 loc) · 1.39 KB
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
<?php
/**
* A way of simple authentication for a given user or simple password.
*/
/**
* Checks against an array of authenticated users. When a valid user is found, that user's
* information is set as readonly properties.
*/
class Php_Simple_Authentication {
/**
* Details of the authenticated user.
*
* @var string
*/
public readonly array $user;
/**
* Constructor for the class.
*
* @param array<int, array{username: string, password: string, privs: string}> $users Preconfigured usernames and passwords.
*/
public function __construct( array $config ) {
// Attempt to set the user.
$this->set_user( $config['users'] );
}
/**
* Set the current user type based on the current authenticated user.
*
* @return void
*/
private function set_user( array $users ) {
// Loop over each configured user type.
foreach ( $users as $user ) {
// Check if a password matches a configured user.
if ( isset( $_GET ) && array_key_exists( 'password', $_GET ) && in_array( $_GET['password'], $user ) ) {
// Do not store this since it is about to be set as a property of this object.
unset( $user['password'] );
// Set the matching user user.
$this->user = $user;
break;
}
}
// Check if a user was found.
if ( empty( $this->user ) ) {
// The loop finished with no found user. Set a fallback value.
$this->user = array();
}
}
}