1
0
mirror of https://github.com/delight-im/PHP-Auth.git synced 2025-08-07 16:46:29 +02:00

211 Commits

Author SHA1 Message Date
Marco
0b67f3d1e2 Document new method 'logOutButKeepSession' from class 'Auth' 2017-09-18 16:10:47 +02:00
Marco
16bcfa85ef Add tests for new method 'logOutButKeepSession' in class 'Auth' 2017-09-18 16:10:19 +02:00
Marco
404739634d Compose 'logout' using new 'logOutButKeepSession' and 'destroySession' 2017-09-18 16:08:32 +02:00
Marco
82a24fbbca Implement method 'destroySession' in class 'Auth' 2017-09-18 16:07:05 +02:00
Marco
1a195adf39 Implement method 'logOutButKeepSession' in class 'Auth' 2017-09-18 16:01:47 +02:00
Marco
5e4d4fd072 Improve language 2017-09-18 15:53:59 +02:00
Marco
6162092618 Catch undefined IP address in rare use cases such as CLI usage 2017-09-18 14:54:39 +02:00
Marco
f142dd91dc Emphasize primary line of project description in README 2017-08-25 07:57:47 +02:00
Marco
05567acc7c Remove exception from tests that cannot be thrown with specified call 2017-08-19 00:47:42 +02:00
Marco
3d8c583823 Remove exception from PHPDoc that cannot reasonably appear in practice 2017-08-19 00:46:38 +02:00
Marco
546a57cbf9 Document 'throttle' method for throttling or rate limiting in README 2017-08-19 00:45:27 +02:00
Marco
52ba03248d Make 'throttle' method for throttling or rate limiting a public method 2017-08-19 00:42:53 +02:00
Marco
c5ed53898e Explain changes to interface of internal throttling in migration guide 2017-08-19 00:40:42 +02:00
Marco
a66312bbcf Re-implement internal throttling or rate limiting from scratch 2017-08-19 00:22:21 +02:00
Marco
c1bb10f58d Fix language in migration guide 2017-08-07 23:30:35 +02:00
Marco
4fd37f079b Describe required changes to SQLite schema in migration guide 2017-08-07 23:20:34 +02:00
Marco
8ff3776e75 Completely rewrite SQLite schema for table 'users_throttling' 2017-08-07 23:19:51 +02:00
Marco
b24979ae26 Describe required changes to MySQL schema in migration guide 2017-08-07 23:18:53 +02:00
Marco
30b2f30aec Completely rewrite MySQL schema for table 'users_throttling' 2017-08-07 23:17:12 +02:00
Marco
b3d37ada86 Document methods for re-sending confirmation requests in class 'Auth' 2017-08-07 21:27:20 +02:00
Marco
27adc9fa91 Add tests for re-sending confirmation requests with class 'Auth' 2017-08-07 21:09:31 +02:00
Marco
c9a4e28c7b Implement methods for re-sending confirmation requests in class 'Auth' 2017-08-07 21:08:06 +02:00
Marco
f83ac969d4 Add class 'ConfirmationRequestNotFound' 2017-08-07 19:36:13 +02:00
Marco
0bbf9d32b1 Describe required changes to SQLite schema in migration guide 2017-08-07 19:30:44 +02:00
Marco
381e05f102 Update SQLite schema to index on 'user_id' in 'users_confirmations' 2017-08-07 19:28:49 +02:00
Marco
2839743c46 Describe required changes to MySQL schema in migration guide 2017-08-07 19:26:25 +02:00
Marco
d86d7ffd25 Update MySQL schema to index on 'user_id' in 'users_confirmations' 2017-08-07 19:23:58 +02:00
Marco
e3873f2d15 Use alternative 'LIMIT' syntax with wider compatibility in SQL query 2017-08-07 18:52:36 +02:00
Marco
b7a47fc707 Extract TTL in seconds of (email) confirmation requests into constant 2017-08-07 18:51:21 +02:00
Marco
91f50a80bb Document method 'changePasswordWithoutOldPassword' from class 'Auth' 2017-08-04 00:45:41 +02:00
Marco
7272fbb9a8 Add tests for method 'changePasswordWithoutOldPassword' from 'Auth' 2017-08-04 00:43:17 +02:00
Marco
62c5fab1ad Re-implement 'changePassword' method using two existing methods
Make use of 'reconfirmPassword' and 'changePasswordWithoutOldPassword'
2017-08-04 00:35:50 +02:00
Marco
1800525b51 Implement new method 'changePasswordWithoutOldPassword' in 'Auth' 2017-08-04 00:31:35 +02:00
Marco
e4f8673eab Remove documentation on half-baked support for multi-factor auth 2017-08-03 22:02:09 +02:00
Marco
59cd626bd0 Document method 'changeEmail' from class 'Auth' 2017-07-30 21:09:57 +02:00
Marco
3809b9d5d5 Add tests for method 'changeEmail' from class 'Auth' 2017-07-30 21:01:41 +02:00
Marco
3329c6a985 Let signed-in users perform email confirmation as well in 'tests' 2017-07-30 20:58:40 +02:00
Marco
7b98993bf8 Extract form for email verification into separate method in 'tests' 2017-07-30 20:57:13 +02:00
Marco
d5ae78a418 Hint at related methods for email confirmation where required 2017-07-30 20:53:18 +02:00
Marco
e925a73ef8 Implement method 'changeEmail' in class 'Auth' 2017-07-30 20:51:58 +02:00
Marco
39f9b00b45 Reflect changed email address in same session immediately 2017-07-30 20:24:19 +02:00
Marco
ea67c66bd1 Explain new exception of email confirmation methods in migration guide 2017-07-30 20:17:41 +02:00
Marco
7b4c4bf0e1 Document new exception for 'confirmEmail' and 'confirmEmailAndSignIn' 2017-07-30 20:15:58 +02:00
Marco
f13302b014 Update tests for 'confirmEmail' and its wrapper to catch new exception 2017-07-30 20:13:28 +02:00
Marco
af5ce5a0b4 Allow 'confirmEmail' to be used additionally to change email addresses 2017-07-30 20:04:08 +02:00
Marco
15f73567b6 Update accounts by ID instead of email after confirming email address 2017-07-30 19:59:09 +02:00
Marco
90c621aeb0 Store affected user ID when creating new email confirmation requests 2017-07-30 19:46:45 +02:00
Marco
28979925d7 Let 'Auth' access 'createConfirmationRequest' from 'UserManager' 2017-07-30 19:41:27 +02:00
Marco
b2e6f68a22 Describe required changes to SQLite schema in migration guide 2017-07-30 19:38:20 +02:00
Marco
d14d929bc3 Update SQLite schema to include 'user_id' in 'users_confirmations' 2017-07-30 19:36:29 +02:00
Marco
f962008fc4 Describe required changes to MySQL schema in migration guide 2017-07-30 19:35:52 +02:00
Marco
ec8e9eab4e Update MySQL schema to include 'user_id' in 'users_confirmations' 2017-07-30 19:34:30 +02:00
Marco
65b4f812c0 Document two methods that let users enable or disable password resets 2017-07-30 17:02:59 +02:00
Marco
b8e04e3c6a Add tests for methods that let users enable or disable password resets 2017-07-30 16:45:54 +02:00
Marco
5c92d026c9 Pass 'Auth' instance to 'showAuthenticatedUserForm' in 'tests' 2017-07-30 16:37:34 +02:00
Marco
2247c2781c Allow for users to enable or disable password resets on their own 2017-07-30 16:34:29 +02:00
Marco
72b2468aa3 Explain new exception from password reset methods in migration guide 2017-07-30 16:22:34 +02:00
Marco
7cc27b814e Add tests for new exception from 'forgotPassword' and 'resetPassword' 2017-07-30 16:21:21 +02:00
Marco
dbc463c95e Document new exception for 'forgotPassword' and 'resetPassword' 2017-07-30 16:17:04 +02:00
Marco
4b6afc7c48 Fail with exception in 'resetPassword' if password reset is disabled 2017-07-30 16:12:57 +02:00
Marco
a3a28af2aa Fail with exception in 'forgotPassword' if password reset is disabled 2017-07-30 16:12:10 +02:00
Marco
c842fa9792 Add class 'ResetDisabledException' 2017-07-30 15:48:19 +02:00
Marco
a599771bd5 Describe required changes to SQLite schema in migration guide 2017-07-30 14:42:21 +02:00
Marco
e73f29eec0 Update SQLite schema to include 'resettable' column in 'users' table 2017-07-30 14:41:53 +02:00
Marco
c118116a52 Describe required changes to MySQL schema in migration guide 2017-07-30 14:41:28 +02:00
Marco
0e969ccd8d Update MySQL schema to include 'resettable' column in 'users' table 2017-07-30 14:40:11 +02:00
Marco
aae0bfb5ab Document method 'confirmEmailAndSignIn' from class 'Auth' 2017-07-30 14:21:33 +02:00
Marco
fb982cee6a Add tests for method 'confirmEmailAndSignIn' from class 'Auth' 2017-07-30 14:20:31 +02:00
Marco
838c6edf66 Implement method 'confirmEmailAndSignIn' in class 'Auth' 2017-07-30 14:19:07 +02:00
Marco
ad5784364d Return confirmed email address from 'confirmEmail' in class 'Auth' 2017-07-30 14:16:52 +02:00
Marco
d8f21a35fc Add documentation for method 'reconfirmPassword' from class 'Auth' 2017-07-30 01:17:16 +02:00
Marco
79ecb85bb6 Add tests for method 'reconfirmPassword' from class 'Auth' 2017-07-30 00:57:38 +02:00
Marco
f56e7e6871 Implement method 'reconfirmPassword' in class 'Auth' 2017-07-30 00:54:06 +02:00
Marco
83f2ab0a9c Document optional prefix for the names of all database tables 2017-07-30 00:11:10 +02:00
Marco
5274dd5f8e Support optional prefix for the names of all database tables 2017-07-30 00:04:48 +02:00
Marco
b93d9616d0 Fix URL fragment for internal link in README 2017-07-29 23:31:46 +02:00
Marco
0af55ad77c Document features related to roles in 'Administration' interface 2017-07-29 23:21:57 +02:00
Marco
7b6287a7dc Document features related to roles in 'Auth' interface 2017-07-29 23:18:38 +02:00
Marco
cf7493d87e Fix response on exception in tests 2017-07-29 22:59:09 +02:00
Marco
f68d29000e Remove tests for 'onBeforeSuccess' callback of login methods 2017-07-29 20:43:31 +02:00
Marco
cd3469c137 Add tests for checking roles for users via 'Administration' class 2017-07-29 20:28:18 +02:00
Marco
bc44a08b1b Allow for roles to be checked for users via 'Administration' class 2017-07-29 20:24:24 +02:00
Marco
8ff4242f8f Add tests for taking roles away from users via 'Administration' class 2017-07-29 19:09:04 +02:00
Marco
1a4041ea60 Allow for roles to be taken away from users via 'Administration' class 2017-07-29 19:06:13 +02:00
Marco
b7e6ca6dee Add tests for assigning roles to users via 'Administration' class 2017-07-29 19:02:55 +02:00
Marco
f2074e1537 Allow for roles to be assigned to users via 'Administration' class 2017-07-29 18:55:15 +02:00
Marco
9c63c30cd9 Add method in 'tests' that creates list of roles for HTML 'select' 2017-07-29 18:52:18 +02:00
Marco
8a1140a485 Add private methods to 'Administration' for modifying users' roles 2017-07-29 18:47:32 +02:00
Marco
23b172055b Add tests for read access to user's roles via 'Auth' interface 2017-07-29 18:21:27 +02:00
Marco
c25b74d405 Provide read access to user's roles via 'Auth' interface 2017-07-29 18:19:00 +02:00
Marco
2278b86fba Read user's roles from database and maintain value in session data 2017-07-29 18:15:17 +02:00
Marco
4eca6bb151 Merge notes about 'Base64' class in migration guide 2017-07-29 18:09:04 +02:00
Marco
db4c99e729 Include general guide for any update in migration notes 2017-07-29 18:02:59 +02:00
Marco
d6bc8c6492 Describe required changes to SQLite schema in migration guide 2017-07-29 18:01:51 +02:00
Marco
b577322939 Update SQLite schema to include 'roles_mask' column in 'users' table 2017-07-29 18:01:04 +02:00
Marco
6cf955ed52 Describe required changes to MySQL schema in migration guide 2017-07-29 17:59:14 +02:00
Marco
8c2c32f9dc Update MySQL schema to include 'roles_mask' column in 'users' table 2017-07-29 17:50:43 +02:00
Marco
2d7ad74c44 Explain in migration guide that the database schema will have changed 2017-07-26 16:19:28 +02:00
Marco
a91cde706d Improve formatting 2017-07-26 16:18:50 +02:00
Marco
8feda0ae58 Update dependencies 2017-07-26 16:16:20 +02:00
Marco
78b7fb4169 Add warning to 'tests' explaining that files are not to be re-used 2017-07-24 23:28:26 +02:00
Marco
499fbb6542 Explain why login attempts may (confusingly) be cancelled in 'tests' 2017-07-24 23:26:30 +02:00
Marco
50b9c48f8d Improve note on 'Base64' class in migration guide 2017-07-24 22:10:10 +02:00
Marco
fcbace0aec Add another note regarding 'Base64' class to migration guide 2017-07-24 22:00:22 +02:00
Marco
c2ab825354 Extract class 'Base64' into external library 2017-07-24 21:56:35 +02:00
Marco
b1ac859fd2 Update dependencies 2017-07-24 21:21:44 +02:00
Marco
0d9be76f8b Add note regarding 'Base64' class to migration guide 2017-07-23 23:50:22 +02:00
Marco
64d15263ae Prepare migration guide for next major version 2017-07-23 23:49:29 +02:00
Marco
854bc2b62b Swap positions of hyphen and underscore characters in URL-safe Base64
This ensures compatibility with RFC 4648 and the example from the
appendix of RFC 7515, aside from the padding character that is used.
2017-07-23 23:18:28 +02:00
Marco
01a52b76bc Switch characters in URL-safe Base64 to use tilde (~) for padding
The tilde character is less familiar to most users and harder to type
on most keyboards (compared to the hyphen and underscore characters).
2017-07-23 22:56:28 +02:00
Marco
ad88c1c6ab Use tilde character (~) instead of dot (.) for URL-safe Base64 coding
The dot character is excluded from auto-linking in most email clients
and is ambiguous in all other contexts when occurring at the end of a
URL. The tilde character, being the only unreserved character for use
in URLs that remains, as per RFC 3986, is thus a good alternative.
2017-07-23 22:16:13 +02:00
Marco
449e1c69ee Remove obsolete 'pre-check' and 'post-check' for 'Cache-Control' 2017-07-21 06:20:30 +02:00
Marco
63734fc5ee Add 'Role' class with constants for individual roles or groups 2017-07-10 20:59:45 +02:00
Marco
6e3728a918 Include help link for Composer in README 2017-07-08 23:32:48 +02:00
Marco
0909291cf1 Support multi-factor authentication via 'onBeforeSuccess' callback 2017-07-02 23:12:36 +02:00
Marco
6aa3f58059 Add 'AttemptCancelledException' 2017-07-02 22:17:43 +02:00
Marco
6156b1c135 Explain how to achieve interoperability with other session-based libs 2017-07-02 21:13:23 +02:00
Marco
829d5614ed Explain how to allow framing or embedding on third-party sites 2017-06-22 22:06:20 +02:00
Marco
47afa1c411 Remove enforcement of hard dependency on 'mysqlnd' in code 2017-06-20 02:19:46 +02:00
Marco
26cb41e992 Document support of SQLite 2017-06-12 20:35:07 +02:00
Marco
ee485f99ab Ensure compatibility with SQLite which does not cast to native types 2017-06-12 20:29:58 +02:00
Marco
8fc0b98493 Remove superfluous blank line 2017-06-12 20:28:47 +02:00
prometeusweb
45553afaea Add database schema for SQLite 2017-06-12 20:26:14 +02:00
Marco
7834455e16 Add 'What about password hashing?' to FAQ in README 2017-04-24 21:06:06 +02:00
Marco
e49adf0150 Move 'Custom password requirements' to FAQ in README 2017-04-24 20:58:18 +02:00
Marco
0fb653d6e0 Add section 'Custom password requirements' to README 2017-03-24 17:07:26 +01:00
Marco
dc233d9d46 Remove 'Features' section in README 2017-03-24 16:49:37 +01:00
Marco
7c842f903e Add 'MySQL Native Driver' as full name of 'mysqlnd' driver in README 2017-03-18 22:35:44 +01:00
Marco
0e2279ecda Document what is required to make library work with other databases 2017-03-18 22:31:30 +01:00
Marco
79db94f500 Add 'mysqlnd' driver for PDO as platform dependency in README 2017-03-18 22:30:19 +01:00
Marco
f38d7bd62c Add PDO as platform dependency in README 2017-03-18 22:25:23 +01:00
Marco
04a2e8ef4e Throw error if 'libmysqlclient' driver is used instead of 'mysqlnd' 2017-03-18 22:21:23 +01:00
Marco
59505479a5 Add class 'WrongMysqlDatabaseDriverError' 2017-03-18 22:13:28 +01:00
Marco
fdcfd6f78c Add class 'DatabaseDriverError' 2017-03-18 22:12:49 +01:00
Marco
20606bc507 Update dependencies 2017-03-18 22:11:05 +01:00
Marco
89a7af17fe Add documentation on how to retrieve status information 2017-02-26 14:15:26 +01:00
Marco
4c084150c4 Update migration guide 2017-02-26 13:49:20 +01:00
Marco
dd51d2c07d Add tests for 'getStatus' and related methods in 'Auth' class 2017-02-26 13:45:26 +01:00
Marco
93477e4e7e Add shorthands for 'getStatus' in 'Auth' class 2017-02-26 13:45:00 +01:00
Marco
d59ac83d13 Refactor array definition in 'authenticateUserInternal' 2017-02-26 13:13:37 +01:00
Marco
9a0036b8a8 Add 'Status' class with constants 2017-02-26 13:05:37 +01:00
Marco
a05d277a2c Read status from 'users' table and provide read access in session 2017-02-26 13:03:52 +01:00
Marco
0839beefcb Improve readability of README 2017-02-26 11:54:41 +01:00
Marco
bf5db38361 Link to 'Migration' from README 2017-02-25 19:13:56 +01:00
Marco
d9be7a4c22 Add table of contents to 'Migration' 2017-02-25 18:59:15 +01:00
Marco
e9bae4a346 Reverse chronological order in 'Migration.md' 2017-02-25 18:49:27 +01:00
Marco
2317423550 Explain that constructor of 'Administration' is for internal use only 2017-02-25 18:40:49 +01:00
Marco
d9dccf8100 Add 'status' column to 'users' table 2017-02-25 18:13:55 +01:00
Marco
26ca48c3b9 Improve language 2017-02-25 18:07:02 +01:00
Marco
9ec74b3b2d Deprecate boolean format for parameter 'rememberDuration' with login 2017-02-25 18:06:07 +01:00
Marco
9c60acec0d Improve code style 2017-02-25 17:58:29 +01:00
Marco
94eeb9dbe0 Fix anchors in README 2017-02-25 17:35:38 +01:00
Marco
4dca8439d1 Add tests for method 'deleteUserById' and related methods 2017-02-25 17:34:36 +01:00
Marco
81bdd79906 Add method 'deleteUserById' and similar methods for email and username 2017-02-25 17:32:35 +01:00
Marco
63144d4dc0 Add private method 'deleteUsersByColumnValue' to 'Administration' 2017-02-25 17:14:24 +01:00
Marco
f06af42f87 Move method 'getUserDataByUsername' from 'Auth' to 'UserManager' 2017-02-25 16:18:51 +01:00
Marco
6c6f34935c Add tests for method 'createUser' in class 'Administration' 2017-02-25 15:45:58 +01:00
Marco
293c231003 Do not offer email verification when creating users as admin 2017-02-25 15:44:37 +01:00
Marco
05d72a849b Improve language 2017-02-21 10:02:49 +01:00
Marco
cf41c9a105 Add methods 'createUser' and 'createUserWithUniqueUsername' 2017-02-21 10:02:03 +01:00
Marco
da4bb583bf Add component for administrative tasks as class 'Administration' 2017-02-21 09:43:30 +01:00
Marco
d99979f270 Move method 'createUserInternal' from class 'Auth' to 'UserManager' 2017-02-21 09:28:42 +01:00
Marco
22872d55bd Import class 'IntegrityConstraintViolationException' in 'UserManager' 2017-02-21 09:27:54 +01:00
Marco
ff6d78942a Move method 'createConfirmationRequest' from 'Auth' to 'UserManager' 2017-02-21 09:26:10 +01:00
Marco
d27005df10 Import class 'Error' in 'UserManager' 2017-02-21 09:22:40 +01:00
Marco
ad2aa84e4a Move method 'validatePassword' from class 'Auth' to 'UserManager' 2017-02-21 09:19:09 +01:00
Marco
f7d50d53ea Move method 'validateEmailAddress' from class 'Auth' to 'UserManager' 2017-02-21 09:17:08 +01:00
Marco
e916c3d07e Move method 'createRandomString' from class 'Auth' to 'UserManager' 2017-02-21 09:13:39 +01:00
Marco
fdeff8a792 Emphasize that class 'UserManager' is for internal use only 2017-02-21 09:03:55 +01:00
Marco
43fa612d67 Move method 'throttle' and its constants from 'Auth' to 'UserManager' 2017-02-21 08:55:10 +01:00
Marco
0b0258f29a Manually require file 'Exceptions.php' in parent class as well 2017-02-21 08:45:27 +01:00
Marco
9252bee030 Let parent class 'UserManager' manage database connection for 'Auth' 2017-02-21 08:40:30 +01:00
Marco
6a15679238 Make class 'Base64' final 2017-02-21 08:28:14 +01:00
Marco
8ab08f41e1 Let autoloader fetch class 'Base64' instead of including it manually 2017-02-21 08:27:19 +01:00
Marco
83464c0be7 Improve description of 'Auth' class 2017-02-21 08:07:38 +01:00
Marco
b5c853388c Make class 'Auth' final 2017-02-21 08:01:41 +01:00
Marco
5585623e08 Let class 'Auth' extend abstract class 'UserManager' 2017-02-21 08:00:26 +01:00
Marco
a7d640154c Add abstract class 'UserManager' 2017-02-21 07:59:49 +01:00
Marco
8acd3a9779 Add tests for method 'loginWithUsername' in class 'Auth' 2017-02-20 21:46:36 +01:00
Marco
374f27176b Add tests for method 'registerWithUniqueUsername' in class 'Auth' 2017-02-20 21:42:48 +01:00
Marco
3cb2284870 Add public method 'loginWithUsername' to class 'Auth' 2017-02-20 21:36:45 +01:00
Marco
690485ba6d Add support for sign in via username to 'authenticateUserInternal' 2017-02-20 21:32:45 +01:00
Marco
495a87d499 No need to check for uniqueness of username if none has been provided 2017-02-20 20:52:02 +01:00
Marco
784030139b Treat empty string or whitespace-only string as non-existent username 2017-02-20 20:48:03 +01:00
Marco
fb6f3d31b8 Add private method 'getUserDataByUsername' to class 'Auth' 2017-02-20 19:57:23 +01:00
Marco
370ecc4933 Add class 'AmbiguousUsernameException' 2017-02-16 09:48:55 +01:00
Marco
da2d282648 Add class 'UnknownUsernameException' 2017-02-16 09:48:04 +01:00
Marco
4aaf85e3cf Add class 'EmailOrUsernameRequiredError' 2017-02-16 09:47:26 +01:00
Marco
f2561a1932 Re-use 'getUserDataByEmailAddress' in 'authenticateUserInternal' 2017-02-16 08:56:44 +01:00
Marco
8cc54473e3 Improve language of parameter name 2017-02-16 08:30:24 +01:00
Marco
f26f2209cd Store email address in session data as found in the database 2017-02-16 08:25:14 +01:00
Marco
188086f2e4 Do not validate password earlier than necessary 2017-02-16 08:18:48 +01:00
Marco
c6213a6081 Change order of parameters in 'authenticateUserInternal' 2017-02-16 08:15:48 +01:00
Marco
c55250c572 Refactor body of 'login' method into new 'authenticateUserInternal' 2017-02-15 18:29:15 +01:00
Marco
dac2850aba Add method 'registerWithUniqueUsername' 2017-02-15 17:19:16 +01:00
Marco
4268e3fcd5 Add support for unique username constraint in 'createUserInternal' 2017-02-15 17:11:56 +01:00
Marco
d579179494 Add class 'DuplicateUsernameException' 2017-02-15 17:06:08 +01:00
Marco
bd02e08f83 Refactor body of 'register' method into new 'createUserInternal' 2017-02-15 16:41:38 +01:00
Marco
d4fe11b844 Improve language 2017-02-15 16:39:17 +01:00
Marco
09fabd4c91 Improve notes in 'General advice' section of README 2017-01-31 23:49:36 +01:00
Marco
4dcf491ad9 Fix language in README 2017-01-30 19:33:04 +01:00
Marco
4f5ff151ef Improve installation instructions by excluding steps for manual setup 2017-01-30 19:31:45 +01:00
Marco
f5027c09e9 Add table of contents to 'Usage' section in README 2017-01-30 19:10:28 +01:00
Marco
6db82d1f65 Add guide on how to store and access additional user information 2017-01-30 19:04:11 +01:00
Marco
f944067aff Set 'ignore_user_abort' to 'true' in method 'register' 2017-01-30 18:48:23 +01:00
Marco
a640e8a5ad Define link to external documentation more precisely 2017-01-30 18:39:21 +01:00
Marco
2aee8a662e Move 'Reading and writing session data' below 'Utilities' in README 2017-01-30 18:37:47 +01:00
Marco
36ef710480 Group several sections in README under 'Accessing user information' 2017-01-30 18:24:44 +01:00
Marco
9187840767 Improve language in README 2017-01-30 18:23:30 +01:00
Marco
6bfa298836 Prevent usage of password reset if email has not been verified yet 2016-12-12 20:58:37 +01:00
Marco
6be456a27a Change 'getUserIdByEmailAddress' to 'getUserDataByEmailAddress' 2016-12-12 20:38:49 +01:00
14 changed files with 3171 additions and 542 deletions

View File

@@ -12,7 +12,10 @@ CREATE TABLE IF NOT EXISTS `users` (
`email` varchar(249) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`username` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` tinyint(2) unsigned NOT NULL DEFAULT '0',
`verified` tinyint(1) unsigned NOT NULL DEFAULT '0',
`resettable` tinyint(1) unsigned NOT NULL DEFAULT '1',
`roles_mask` int(10) unsigned NOT NULL DEFAULT '0',
`registered` int(10) unsigned NOT NULL,
`last_login` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
@@ -21,13 +24,15 @@ CREATE TABLE IF NOT EXISTS `users` (
CREATE TABLE IF NOT EXISTS `users_confirmations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL,
`email` varchar(249) COLLATE utf8mb4_unicode_ci NOT NULL,
`selector` varchar(16) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`token` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`expires` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `selector` (`selector`),
KEY `email_expires` (`email`,`expires`)
KEY `email_expires` (`email`,`expires`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `users_remembered` (
@@ -53,13 +58,12 @@ CREATE TABLE IF NOT EXISTS `users_resets` (
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `users_throttling` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`action_type` enum('login','register','confirm_email') COLLATE utf8mb4_unicode_ci NOT NULL,
`selector` varchar(44) CHARACTER SET latin1 COLLATE latin1_general_cs DEFAULT NULL,
`time_bucket` int(10) unsigned NOT NULL,
`attempts` mediumint(8) unsigned NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `action_type_selector_time_bucket` (`action_type`,`selector`,`time_bucket`)
`bucket` varchar(44) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`tokens` float unsigned NOT NULL,
`replenished_at` int(10) unsigned NOT NULL,
`expires_at` int(10) unsigned NOT NULL,
PRIMARY KEY (`bucket`),
KEY `expires_at` (`expires_at`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

59
Database/SQLite.sql Normal file
View File

@@ -0,0 +1,59 @@
-- PHP-Auth (https://github.com/delight-im/PHP-Auth)
-- Copyright (c) delight.im (https://www.delight.im/)
-- Licensed under the MIT License (https://opensource.org/licenses/MIT)
PRAGMA foreign_keys = OFF;
CREATE TABLE "users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK ("id" >= 0),
"email" VARCHAR(249) NOT NULL,
"password" VARCHAR(255) NOT NULL,
"username" VARCHAR(100) DEFAULT NULL,
"status" INTEGER NOT NULL CHECK ("status" >= 0) DEFAULT "0",
"verified" INTEGER NOT NULL CHECK ("verified" >= 0) DEFAULT "0",
"resettable" INTEGER NOT NULL CHECK ("resettable" >= 0) DEFAULT "1",
"roles_mask" INTEGER NOT NULL CHECK ("roles_mask" >= 0) DEFAULT "0",
"registered" INTEGER NOT NULL CHECK ("registered" >= 0),
"last_login" INTEGER CHECK ("last_login" >= 0) DEFAULT NULL,
CONSTRAINT "email" UNIQUE ("email")
);
CREATE TABLE "users_confirmations" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK ("id" >= 0),
"user_id" INTEGER NOT NULL CHECK ("user_id" >= 0),
"email" VARCHAR(249) NOT NULL,
"selector" VARCHAR(16) NOT NULL,
"token" VARCHAR(255) NOT NULL,
"expires" INTEGER NOT NULL CHECK ("expires" >= 0),
CONSTRAINT "selector" UNIQUE ("selector")
);
CREATE INDEX "users_confirmations.email_expires" ON "users_confirmations" ("email", "expires");
CREATE INDEX "users_confirmations.user_id" ON "users_confirmations" ("user_id");
CREATE TABLE "users_remembered" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK ("id" >= 0),
"user" INTEGER NOT NULL CHECK ("user" >= 0),
"selector" VARCHAR(24) NOT NULL,
"token" VARCHAR(255) NOT NULL,
"expires" INTEGER NOT NULL CHECK ("expires" >= 0),
CONSTRAINT "selector" UNIQUE ("selector")
);
CREATE INDEX "users_remembered.user" ON "users_remembered" ("user");
CREATE TABLE "users_resets" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK ("id" >= 0),
"user" INTEGER NOT NULL CHECK ("user" >= 0),
"selector" VARCHAR(20) NOT NULL,
"token" VARCHAR(255) NOT NULL,
"expires" INTEGER NOT NULL CHECK ("expires" >= 0),
CONSTRAINT "selector" UNIQUE ("selector")
);
CREATE INDEX "users_resets.user_expires" ON "users_resets" ("user", "expires");
CREATE TABLE "users_throttling" (
"bucket" VARCHAR(44) PRIMARY KEY NOT NULL,
"tokens" REAL NOT NULL CHECK ("tokens" >= 0),
"replenished_at" INTEGER NOT NULL CHECK ("replenished_at" >= 0),
"expires_at" INTEGER NOT NULL CHECK ("expires_at" >= 0)
);
CREATE INDEX "users_throttling.expires_at" ON "users_throttling" ("expires_at");

View File

@@ -1,5 +1,112 @@
# Migration
* [General](#general)
* [From `v5.x.x` to `v6.x.x`](#from-v5xx-to-v6xx)
* [From `v4.x.x` to `v5.x.x`](#from-v4xx-to-v5xx)
* [From `v3.x.x` to `v4.x.x`](#from-v3xx-to-v4xx)
* [From `v2.x.x` to `v3.x.x`](#from-v2xx-to-v3xx)
* [From `v1.x.x` to `v2.x.x`](#from-v1xx-to-v2xx)
## General
Update your version of this library via Composer [[?]](https://github.com/delight-im/Knowledge/blob/master/Composer%20(PHP).md):
```
$ composer update delight-im/auth
```
## From `v5.x.x` to `v6.x.x`
* The database schema has changed.
* The MySQL database schema has changed. Use the statements below to update your database:
```sql
ALTER TABLE users
ADD COLUMN roles_mask INT(10) UNSIGNED NOT NULL DEFAULT 0 AFTER verified,
ADD COLUMN resettable TINYINT(1) UNSIGNED NOT NULL DEFAULT 1 AFTER verified;
ALTER TABLE users_confirmations
ADD COLUMN user_id INT(10) UNSIGNED NULL DEFAULT NULL AFTER id;
UPDATE users_confirmations SET user_id = (
SELECT id FROM users WHERE email = users_confirmations.email
) WHERE user_id IS NULL;
ALTER TABLE users_confirmations
CHANGE COLUMN user_id user_id INT(10) UNSIGNED NOT NULL;
ALTER TABLE users_confirmations
ADD INDEX user_id (user_id ASC);
DROP TABLE users_throttling;
CREATE TABLE users_throttling (
bucket varchar(44) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
tokens float unsigned NOT NULL,
replenished_at int(10) unsigned NOT NULL,
expires_at int(10) unsigned NOT NULL,
PRIMARY KEY (bucket),
KEY expires_at (expires_at)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```
* The SQLite database schema has changed. Use the statements below to update your database:
```sql
ALTER TABLE users
ADD COLUMN "roles_mask" INTEGER NOT NULL CHECK ("roles_mask" >= 0) DEFAULT "0",
ADD COLUMN "resettable" INTEGER NOT NULL CHECK ("resettable" >= 0) DEFAULT "1";
ALTER TABLE users_confirmations
ADD COLUMN "user_id" INTEGER CHECK ("user_id" >= 0);
UPDATE users_confirmations SET user_id = (
SELECT id FROM users WHERE email = users_confirmations.email
) WHERE user_id IS NULL;
CREATE INDEX "users_confirmations.user_id" ON "users_confirmations" ("user_id");
DROP TABLE users_throttling;
CREATE TABLE "users_throttling" (
"bucket" VARCHAR(44) PRIMARY KEY NOT NULL,
"tokens" REAL NOT NULL CHECK ("tokens" >= 0),
"replenished_at" INTEGER NOT NULL CHECK ("replenished_at" >= 0),
"expires_at" INTEGER NOT NULL CHECK ("expires_at" >= 0)
);
CREATE INDEX "users_throttling.expires_at" ON "users_throttling" ("expires_at");
```
* The method `setThrottlingOptions` has been removed.
* The method `changePassword` may now throw an additional `\Delight\Auth\TooManyRequestsException` if too many attempts have been made without the correct old password.
* The two methods `confirmEmail` and `confirmEmailAndSignIn` may now throw an additional `\Delight\Auth\UserAlreadyExistsException` if an attempt has been made to change the email address to an address that has become occupied in the meantime.
* The two methods `forgotPassword` and `resetPassword` may now throw an additional `\Delight\Auth\ResetDisabledException` if the user has disabled password resets for their account.
* The `Base64` class is now an external module and has been moved from the namespace `Delight\Auth` to the namespace `Delight\Base64`. The interface and the return values are not compatible with those from previous versions anymore.
## From `v4.x.x` to `v5.x.x`
* The MySQL database schema has changed. Use the statement below to update your database:
```sql
ALTER TABLE `users` ADD COLUMN `status` TINYINT(2) UNSIGNED NOT NULL DEFAULT 0 AFTER `username`;
```
* The two classes `Auth` and `Base64` are now `final`, i.e. they can't be extended anymore, which has never been a good idea, anyway. If you still need to wrap your own methods around these classes, consider [object composition instead of class inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance).
## From `v3.x.x` to `v4.x.x`
* PHP 5.6.0 or higher is now required.
## From `v2.x.x` to `v3.x.x`
* The license has been changed from the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0) to the [MIT License](https://opensource.org/licenses/MIT).
## From `v1.x.x` to `v2.x.x`
* The MySQL schema has been changed from charset `utf8` to charset `utf8mb4` and from collation `utf8_general_ci` to collation `utf8mb4_unicode_ci`. Use the statements below to update the database schema:
@@ -34,11 +141,3 @@
REPAIR TABLE users_throttling;
OPTIMIZE TABLE users_throttling;
```
## From `v2.x.x` to `v3.x.x`
* The license has been changed from the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0) to the [MIT License](https://opensource.org/licenses/MIT).
## From `v3.x.x` to `v4.x.x`
* PHP 5.6.0 or higher is now required.

728
README.md
View File

@@ -1,6 +1,6 @@
# Auth
Authentication for PHP. Simple, lightweight and secure.
**Authentication for PHP. Simple, lightweight and secure.**
Written once, to be used everywhere.
@@ -17,35 +17,85 @@ Completely framework-agnostic and database-agnostic.
## Requirements
* PHP 5.6.0+
* PDO (PHP Data Objects) extension (`pdo`)
* MySQL Native Driver (`mysqlnd`) **or** SQLite driver (`sqlite`)
* OpenSSL extension (`openssl`)
* MySQL 5.5.3+ **or** MariaDB 5.5.23+
* MySQL 5.5.3+ **or** MariaDB 5.5.23+ **or** SQLite 3.14.1+ **or** other SQL databases that you create the [schema](Database) for
## Installation
* Set up the PHP library
* Install via [Composer](https://getcomposer.org/) (recommended)
1. Include the library via Composer [[?]](https://github.com/delight-im/Knowledge/blob/master/Composer%20(PHP).md):
`$ composer require delight-im/auth`
```
$ composer require delight-im/auth
```
Include the Composer autoloader:
1. Include the Composer autoloader:
`require __DIR__.'/vendor/autoload.php';`
```php
require __DIR__ . '/vendor/autoload.php';
```
* or
* Install manually
* Copy the contents of the [`src`](src) directory to a subfolder of your project
* Include the files in your code via `require` or `require_once`
* Set up a database and create the required tables
* [MySQL](Database/MySQL.sql)
1. Set up a database and create the required tables:
* [MySQL](Database/MySQL.sql)
* [SQLite](Database/SQLite.sql)
## Upgrading
Migrating from an earlier version of this project? See our [upgrade guide](Migration.md) for help.
## Usage
### Create a new instance
* [Creating a new instance](#creating-a-new-instance)
* [Registration (sign up)](#registration-sign-up)
* [Login (sign in)](#login-sign-in)
* [Email verification](#email-verification)
* [Keeping the user logged in](#keeping-the-user-logged-in)
* [Password reset ("forgot password")](#password-reset-forgot-password)
* [Changing the current user's password](#changing-the-current-users-password)
* [Changing the current user's email address](#changing-the-current-users-email-address)
* [Re-sending confirmation requests](#re-sending-confirmation-requests)
* [Logout](#logout)
* [Accessing user information](#accessing-user-information)
* [Login state](#login-state)
* [User ID](#user-id)
* [Email address](#email-address)
* [Display name](#display-name)
* [Checking whether the user was "remembered"](#checking-whether-the-user-was-remembered)
* [IP address](#ip-address)
* [Additional user information](#additional-user-information)
* [Reconfirming the user's password](#reconfirming-the-users-password)
* [Roles (or groups)](#roles-or-groups)
* [Checking roles](#checking-roles)
* [Available roles](#available-roles)
* [Permissions (or access rights, privileges or capabilities)](#permissions-or-access-rights-privileges-or-capabilities)
* [Custom role names](#custom-role-names)
* [Enabling or disabling password resets](#enabling-or-disabling-password-resets)
* [Throttling or rate limiting](#throttling-or-rate-limiting)
* [Administration (managing users)](#administration-managing-users)
* [Creating new users](#creating-new-users)
* [Deleting users](#deleting-users)
* [Assigning roles to users](#assigning-roles-to-users)
* [Taking roles away from users](#taking-roles-away-from-users)
* [Checking roles](#checking-roles-1)
* [Utilities](#utilities)
* [Creating a random string](#creating-a-random-string)
* [Creating a UUID v4 as per RFC 4122](#creating-a-uuid-v4-as-per-rfc-4122)
* [Reading and writing session data](#reading-and-writing-session-data)
### Creating a new instance
```php
// $db = new PDO('mysql:dbname=my-database;host=localhost;charset=utf8mb4', 'my-username', 'my-password');
// or
// $db = new PDO('sqlite:../Databases/my-database.sqlite');
// or
// $db = new \Delight\Db\PdoDsn('mysql:dbname=my-database;host=localhost;charset=utf8mb4', 'my-username', 'my-password');
// or
// $db = new \Delight\Db\PdoDsn('sqlite:../Databases/my-database.sqlite');
$auth = new \Delight\Auth\Auth($db);
```
@@ -58,7 +108,9 @@ Only in the very rare case that you need access to your cookies from JavaScript,
If your web server is behind a proxy server and `$_SERVER['REMOTE_ADDR']` only contains the proxy's IP address, you must pass the user's real IP address to the constructor in the fourth argument. The default is `null`.
### Registration (sign up a new user)
Should your database tables for this library need a common prefix, e.g. `my_users` instead of `users` (and likewise for the other tables), pass the prefix (e.g. `my_`) as the fifth parameter to the constructor. This is optional and the prefix is empty by default.
### Registration (sign up)
```php
try {
@@ -82,7 +134,9 @@ catch (\Delight\Auth\TooManyRequestsException $e) {
}
```
The username in the third parameter is optional. You can pass `null` here if you don't want to manage usernames.
The username in the third parameter is optional. You can pass `null` there if you don't want to manage usernames.
If you want to enforce unique usernames, on the other hand, simply call `registerWithUniqueUsername` instead of `register`, and be prepared to catch the `DuplicateUsernameException`.
For email verification, you should build an URL with the selector and token and send it to the user, e.g.:
@@ -92,7 +146,7 @@ $url = 'https://www.example.com/verify_email?selector='.urlencode($selector).'&t
If you don't want to perform email verification, just omit the last parameter to `Auth#register`. The new user will be active immediately, then.
### Login (sign in an existing user)
### Login (sign in)
```php
try {
@@ -114,6 +168,8 @@ catch (\Delight\Auth\TooManyRequestsException $e) {
}
```
If you want to sign in with usernames on the other hand, either in addition to the login via email address or as a replacement, that's possible as well. Simply call the method `loginWithUsername` instead of method `login`. Then, instead of catching `InvalidEmailException`, make sure to catch both `UnknownUsernameException` and `AmbiguousUsernameException`. You may also want to read the notes about the uniqueness of usernames in the section that explains how to [sign up new users](#registration-sign-up).
### Email verification
Extract the selector and token from the URL that the user clicked on in the verification email.
@@ -130,11 +186,16 @@ catch (\Delight\Auth\InvalidSelectorTokenPairException $e) {
catch (\Delight\Auth\TokenExpiredException $e) {
// token expired
}
catch (\Delight\Auth\UserAlreadyExistsException $e) {
// email address already exists
}
catch (\Delight\Auth\TooManyRequestsException $e) {
// too many requests
}
```
If you want the user to be automatically signed in after successful confirmation, just call `confirmEmailAndSignIn` instead of `confirmEmail`. That alternative method also supports [persistent logins](#keeping-the-user-logged-in) via its optional third parameter.
### Keeping the user logged in
The third parameter to the `Auth#login` method controls whether the login is persistent with a long-lived cookie. With such a persistent login, users may stay authenticated for a long time, even when the browser session has already been closed and the session cookies have expired. Typically, you'll want to keep the user logged in for weeks or months with this feature, which is known as "remember me" or "keep me logged in". Many users will find this more convenient, but it may be less secure if they leave their devices unattended.
@@ -173,6 +234,12 @@ try {
catch (\Delight\Auth\InvalidEmailException $e) {
// invalid email address
}
catch (\Delight\Auth\EmailNotVerifiedException $e) {
// email not verified
}
catch (\Delight\Auth\ResetDisabledException $e) {
// password reset is disabled
}
catch (\Delight\Auth\TooManyRequestsException $e) {
// too many requests
}
@@ -211,6 +278,9 @@ catch (\Delight\Auth\InvalidSelectorTokenPairException $e) {
catch (\Delight\Auth\TokenExpiredException $e) {
// token expired
}
catch (\Delight\Auth\ResetDisabledException $e) {
// password reset is disabled
}
catch (\Delight\Auth\InvalidPasswordException $e) {
// invalid password
}
@@ -235,17 +305,121 @@ catch (\Delight\Auth\NotLoggedInException $e) {
catch (\Delight\Auth\InvalidPasswordException $e) {
// invalid password(s)
}
catch (\Delight\Auth\TooManyRequestsException $e) {
// too many requests
}
```
Asking the user for their current (and soon *old*) password and requiring it for verification is the recommended way to handle password changes. This is shown above.
If youre sure that you dont need that confirmation, however, you may use the following method instead:
```php
try {
$auth->changePasswordWithoutOldPassword($_POST['newPassword']);
// password has been changed
}
catch (\Delight\Auth\NotLoggedInException $e) {
// not logged in
}
catch (\Delight\Auth\InvalidPasswordException $e) {
// invalid password
}
```
### Changing the current user's email address
If a user is currently logged in, they may change their email address.
```php
try {
$auth->changeEmail($_POST['newEmail'], function ($selector, $token) {
// send `$selector` and `$token` to the user (e.g. via email)
});
// the change will take effect as soon as the email address has been confirmed
}
catch (\Delight\Auth\InvalidEmailException $e) {
// invalid email address
}
catch (\Delight\Auth\UserAlreadyExistsException $e) {
// email address already exists
}
catch (\Delight\Auth\EmailNotVerifiedException $e) {
// account not verified
}
catch (\Delight\Auth\NotLoggedInException $e) {
// not logged in
}
catch (\Delight\Auth\TooManyRequestsException $e) {
// too many requests
}
```
For email verification, you should build an URL with the selector and token and send it to the user, e.g.:
```php
$url = 'https://www.example.com/verify_email?selector='.urlencode($selector).'&token='.urlencode($token);
```
### Re-sending confirmation requests
If an earlier confirmation request could not be delivered to the user, or if the user missed that request, or if they just dont want to wait any longer, you may re-send an earlier request like this:
```php
try {
$auth->resendConfirmationForEmail($_POST['email'], function ($selector, $token) {
// send `$selector` and `$token` to the user (e.g. via email)
});
// the user may now respond to the confirmation request (usually by clicking a link)
}
catch (\Delight\Auth\ConfirmationRequestNotFound $e) {
// no earlier request found that could be re-sent
}
catch (\Delight\Auth\TooManyRequestsException $e) {
// there have been too many requests -- try again later
}
```
If you want to specify the user by their ID instead of by their email address, this is possible as well:
```php
try {
$auth->resendConfirmationForUserId($_POST['userId'], function ($selector, $token) {
// send `$selector` and `$token` to the user (e.g. via email)
});
// the user may now respond to the confirmation request (usually by clicking a link)
}
catch (\Delight\Auth\ConfirmationRequestNotFound $e) {
// no earlier request found that could be re-sent
}
catch (\Delight\Auth\TooManyRequestsException $e) {
// there have been too many requests -- try again later
}
```
Usually, you should build an URL with the selector and token and send it to the user, e.g. as follows:
```php
$url = 'https://www.example.com/verify_email?selector=' . urlencode($selector) . '&token=' . urlencode($token);
```
### Logout
```php
$auth->logOutButKeepSession();
// or
$auth->logout();
// user has been signed out
```
### Checking if the user is signed in
### Accessing user information
#### Login state
```php
if ($auth->isLoggedIn()) {
@@ -258,7 +432,7 @@ else {
A shorthand/alias for this method is `$auth->check()`.
### Getting the user's ID
#### User ID
```php
$id = $auth->getUserId();
@@ -268,7 +442,7 @@ If the user is not currently signed in, this returns `null`.
A shorthand/alias for this method is `$auth->id()`.
### Getting the user's email address
#### Email address
```php
$email = $auth->getEmail();
@@ -276,7 +450,7 @@ $email = $auth->getEmail();
If the user is not currently signed in, this returns `null`.
### Getting the user's display name
#### Display name
```php
$email = $auth->getUsername();
@@ -286,7 +460,35 @@ Remember that usernames are optional and there is only a username if you supplie
If the user is not currently signed in, this returns `null`.
### Checking if the user was "remembered"
#### Status information
```php
if ($auth->isNormal()) {
// user is in default state
}
if ($auth->isArchived()) {
// user has been archived
}
if ($auth->isBanned()) {
// user has been banned
}
if ($auth->isLocked()) {
// user has been locked
}
if ($auth->isPendingReview()) {
// user is pending review
}
if ($auth->isSuspended()) {
// user has been suspended
}
```
#### Checking whether the user was "remembered"
```php
if ($auth->isRemembered()) {
@@ -299,15 +501,397 @@ else {
If the user is not currently signed in, this returns `null`.
### Getting the user's IP address
#### IP address
```php
$ip = $auth->getIpAddress();
```
### Reading and writing session data
#### Additional user information
For detailed information on how to read and write session data conveniently, please refer to [the documentation of the session library](https://github.com/delight-im/PHP-Cookie), which is included by default.
In order to preserve this library's suitability for all purposes as well as its full re-usability, it doesn't come with additional bundled columns for user information. But you don't have to do without additional user information, of course:
Here's how to use this library with your own tables for custom user information in a maintainable and re-usable way:
1. Add any number of custom database tables where you store custom user information, e.g. a table named `profiles`.
1. Whenever you call the `register` method (which returns the new user's ID), add your own logic afterwards that fills your custom database tables.
1. If you need the custom user information only rarely, you may just retrieve it as needed. If you need it more frequently, however, you'd probably want to have it in your session data. The following method is how you can load and access your data in a reliable way:
```php
function getUserInfo(\Delight\Auth\Auth $auth) {
if (!$auth->isLoggedIn()) {
return null;
}
if (!isset($_SESSION['_internal_user_info'])) {
// TODO: load your custom user information and assign it to the session variable below
// $_SESSION['_internal_user_info'] = ...
}
return $_SESSION['_internal_user_info'];
}
```
### Reconfirming the user's password
Whenever you want to confirm the users identity again, e.g. before the user is allowed to perform some “dangerous” action, you should verify their password again to confirm that they actually are who they claim to be.
For example, when a user has been remembered by a long-lived cookie and thus `Auth#isRemembered` returns `true`, this means that the user probably has not entered their password for quite some time anymore. You may want to reconfirm their password in that case.
```php
try {
if ($auth->reconfirmPassword($_POST['password'])) {
// the user really seems to be who they claim to be
}
else {
// we can't say if the user is who they claim to be
}
}
catch (\Delight\Auth\NotLoggedInException $e) {
// the user is not signed in
}
catch (\Delight\Auth\TooManyRequestsException $e) {
// too many requests
}
```
### Roles (or groups)
Every user can have any number of roles, which you can use to implement authorization and to refine your access controls.
Users may have no role at all (which they do by default), exactly one role, or any arbitrary combination of roles.
#### Checking roles
```php
if ($auth->hasRole(\Delight\Auth\Role::SUPER_MODERATOR)) {
// the user is a super moderator
}
// or
if ($auth->hasAnyRole(\Delight\Auth\Role::DEVELOPER, \Delight\Auth\Role::MANAGER)) {
// the user is either a developer, or a manager, or both
}
// or
if ($auth->hasAllRoles(\Delight\Auth\Role::DEVELOPER, \Delight\Auth\Role::MANAGER)) {
// the user is both a developer and a manager
}
```
While the method `hasRole` takes exactly one role as its argument, the two methods `hasAnyRole` and `hasAllRoles` can take any number of roles that you would like to check for.
#### Available roles
```php
\Delight\Auth\Role::ADMIN;
\Delight\Auth\Role::AUTHOR;
\Delight\Auth\Role::COLLABORATOR;
\Delight\Auth\Role::CONSULTANT;
\Delight\Auth\Role::CONSUMER;
\Delight\Auth\Role::CONTRIBUTOR;
\Delight\Auth\Role::COORDINATOR;
\Delight\Auth\Role::CREATOR;
\Delight\Auth\Role::DEVELOPER;
\Delight\Auth\Role::DIRECTOR;
\Delight\Auth\Role::EDITOR;
\Delight\Auth\Role::EMPLOYEE;
\Delight\Auth\Role::MAINTAINER;
\Delight\Auth\Role::MANAGER;
\Delight\Auth\Role::MODERATOR;
\Delight\Auth\Role::PUBLISHER;
\Delight\Auth\Role::REVIEWER;
\Delight\Auth\Role::SUBSCRIBER;
\Delight\Auth\Role::SUPER_ADMIN;
\Delight\Auth\Role::SUPER_EDITOR;
\Delight\Auth\Role::SUPER_MODERATOR;
\Delight\Auth\Role::TRANSLATOR;
```
You can use any of these roles and ignore those that you don't need.
#### Permissions (or access rights, privileges or capabilities)
The permissions of each user are encoded in the way that role requirements are specified throughout your code base. If those requirements are evaluated with a specific user's set of roles, implicitly checked permissions are the result.
For larger projects, it is often recommended to maintain the definition of permissions in a single place. You then dont check for *roles* in your business logic, but you check for *individual permissions*. You could implement that concept as follows:
```php
function canEditArticle(\Delight\Auth\Auth $auth) {
return $auth->hasAnyRole(
\Delight\Auth\Role::MODERATOR,
\Delight\Auth\Role::SUPER_MODERATOR,
\Delight\Auth\Role::ADMIN,
\Delight\Auth\Role::SUPER_ADMIN
);
}
// ...
if (canEditArticle($app->auth())) {
// the user can edit articles here
}
// ...
if (canEditArticle($app->auth())) {
// ... and here
}
// ...
if (canEditArticle($app->auth())) {
// ... and here
}
```
As you can see, the permission of whether a certain user can edit an article is stored at a central location. This implementation has two major advantages:
If you *want to know* which users can edit articles, you dont have to check your business logic in various places, but you only have to look where the specific permission is defined. And if you want to *change* who can edit an article, you only have to do this in one single place as well, not throughout your whole code base.
But this also comes with slightly more overhead when implementing the access restrictions for the first time, which may or may not be worth it for your project.
#### Custom role names
If the names of the included roles dont work for you, you can alias any number of roles using your own identifiers, e.g. like this:
```php
namespace My\Namespace;
final class MyRole {
const CUSTOMER_SERVICE_AGENT = \Delight\Auth\Role::REVIEWER;
const FINANCIAL_DIRECTOR = \Delight\Auth\Role::COORDINATOR;
private function __construct() {}
}
```
The example above would allow you to use
```php
\My\Namespace\MyRole::CUSTOMER_SERVICE_AGENT;
// and
\My\Namespace\MyRole::FINANCIAL_DIRECTOR;
```
instead of
```php
\Delight\Auth\Role::REVIEWER;
// and
\Delight\Auth\Role::COORDINATOR;
```
Just remember *not* to alias a *single* included role to *multiple* roles with custom names.
### Enabling or disabling password resets
While password resets via email are a convenient feature that most users find helpful from time to time, the availability of this feature implies that accounts on your service are only ever as secure as the users associated email account.
You may provide security-conscious (and experienced) users with the possibility to disable password resets for their accounts (and to enable them again later) for enhanced security:
```php
try {
$auth->setPasswordResetEnabled($_POST['enabled'] == 1);
// the settings have been changed
}
catch (\Delight\Auth\NotLoggedInException $e) {
// the user is not signed in
}
```
In order to check the current value of this setting, use the return value from
```php
$auth->isPasswordResetEnabled();
```
for the correct default option in your user interface. You dont need to check this value for restrictions of the feature, which are enforced automatically.
### Throttling or rate limiting
All methods provided by this library are *automatically* protected against excessive numbers of requests from clients.
If you would like to throttle or rate limit *external* features or methods as well, e.g. those in your own code, you can make use of the built-in helper method for throttling and rate limiting:
```php
try {
// throttle the specified resource or feature to *3* requests per *60* seconds
$auth->throttle([ 'my-resource-name' ], 3, 60);
// do something with the resource or feature
}
catch (\Delight\Auth\TooManyRequestsException $e) {
// operation cancelled
\http_response_code(429);
exit;
}
```
If the protection of the resource or feature should additionally depend on another attribute, e.g. to track something separately per IP address, just add more data to the resource description, such as:
```php
[ 'my-resource-name', $_SERVER['REMOTE_ADDR'] ]
// instead of
// [ 'my-resource-name' ]
```
Allowing short bursts of activity during peak demand is possible by specifying a burst factor as the fourth argument. A value of `5`, for example, would permit temporary bursts of fivefold activity, compared to the generally accepted level.
In some cases, you may just want to *simulate* the throttling or rate limiting. This lets you check whether an action would be permitted without actually modifying the activity tracker. To do so, simply pass `true` as the fifth argument.
### Administration (managing users)
The administrative interface is available via `$auth->admin()`. You can call various method on this interface, as documented below.
Do not forget to implement secure access control before exposing access to this interface. For example, you may provide access to this interface to logged in users with the administrator role only, or use the interface in private scripts only.
#### Creating new users
```php
try {
$userId = $auth->admin()->createUser($_POST['email'], $_POST['password'], $_POST['username']);
// we have signed up a new user with the ID `$userId`
}
catch (\Delight\Auth\InvalidEmailException $e) {
// invalid email address
}
catch (\Delight\Auth\InvalidPasswordException $e) {
// invalid password
}
catch (\Delight\Auth\UserAlreadyExistsException $e) {
// user already exists
}
```
The username in the third parameter is optional. You can pass `null` there if you don't want to manage usernames.
If you want to enforce unique usernames, on the other hand, simply call `createUserWithUniqueUsername` instead of `createUser`, and be prepared to catch the `DuplicateUsernameException`.
#### Deleting users
Deleting users by their ID:
```php
try {
$auth->admin()->deleteUserById($_POST['id']);
}
catch (\Delight\Auth\UnknownIdException $e) {
// unknown ID
}
```
Deleting users by their email address:
```php
try {
$auth->admin()->deleteUserByEmail($_POST['email']);
}
catch (\Delight\Auth\InvalidEmailException $e) {
// unknown email address
}
```
Deleting users by their username:
```php
try {
$auth->admin()->deleteUserByUsername($_POST['username']);
}
catch (\Delight\Auth\UnknownUsernameException $e) {
// unknown username
}
catch (\Delight\Auth\AmbiguousUsernameException $e) {
// ambiguous username
}
```
#### Assigning roles to users
```php
try {
$auth->admin()->addRoleForUserById($userId, \Delight\Auth\Role::ADMIN);
}
catch (\Delight\Auth\UnknownIdException $e) {
// unknown user ID
}
// or
try {
$auth->admin()->addRoleForUserByEmail($userEmail, \Delight\Auth\Role::ADMIN);
}
catch (\Delight\Auth\InvalidEmailException $e) {
// unknown email address
}
// or
try {
$auth->admin()->addRoleForUserByUsername($username, \Delight\Auth\Role::ADMIN);
}
catch (\Delight\Auth\UnknownUsernameException $e) {
// unknown username
}
catch (\Delight\Auth\AmbiguousUsernameException $e) {
// ambiguous username
}
```
#### Taking roles away from users
```php
try {
$auth->admin()->removeRoleForUserById($userId, \Delight\Auth\Role::ADMIN);
}
catch (\Delight\Auth\UnknownIdException $e) {
// unknown user ID
}
// or
try {
$auth->admin()->removeRoleForUserByEmail($userEmail, \Delight\Auth\Role::ADMIN);
}
catch (\Delight\Auth\InvalidEmailException $e) {
// unknown email address
}
// or
try {
$auth->admin()->removeRoleForUserByUsername($username, \Delight\Auth\Role::ADMIN);
}
catch (\Delight\Auth\UnknownUsernameException $e) {
// unknown username
}
catch (\Delight\Auth\AmbiguousUsernameException $e) {
// ambiguous username
}
```
#### Checking roles
```php
try {
if ($auth->admin()->doesUserHaveRole($userId, \Delight\Auth\Role::ADMIN)) {
// the specified user is an administrator
}
else {
// the specified user is *not* an administrator
}
}
catch (\Delight\Auth\UnknownIdException $e) {
// unknown user ID
}
```
### Utilities
@@ -324,41 +908,57 @@ $randomStr = \Delight\Auth\Auth::createRandomString($length);
$uuid = \Delight\Auth\Auth::createUuid();
```
## Features
### Reading and writing session data
* registration
* secure password storage using the bcrypt algorithm
* email verification through message with confirmation link
* assurance of unique email addresses
* customizable password requirements and enforcement
* optional usernames with customizable restrictions
* login
* keeping the user logged in for a long time (beyond expiration of browser session) via secure long-lived token ("remember me")
* account management
* change password
* tracking the time of sign up and last login
* check if user has been logged in via "remember me" cookie
* logout
* full and reliable destruction of session
* session management
* protection against session hijacking via cross-site scripting (XSS)
* do *not* permit script-based access to cookies
* restrict cookies to HTTPS to prevent session hijacking via non-secure HTTP
* protection against session fixation attacks
* protection against cross-site request forgery (CSRF)
* works automatically (i.e. no need for CSRF tokens everywhere)
* do *not* use HTTP `GET` requests for "dangerous" operations
* throttling
* per IP address
* per account
* enhanced HTTP security
* prevents clickjacking
* prevent content sniffing (MIME sniffing)
* disables caching of potentially sensitive data
* miscellaneous
* ready for both IPv4 and IPv6
* works behind proxy servers as well
* privacy-friendly (e.g. does *not* save readable IP addresses)
For detailed information on how to read and write session data conveniently, please refer to [the documentation of the session library](https://github.com/delight-im/PHP-Cookie#reading-and-writing-session-data), which is included by default.
## Frequently asked questions
### What about password hashing?
Any password or authentication token is automatically hashed using the ["bcrypt"](https://en.wikipedia.org/wiki/Bcrypt) function, which is based on the ["Blowfish" cipher](https://en.wikipedia.org/wiki/Blowfish_(cipher)) and (still) considered one of the strongest password hash functions today. "bcrypt" is used with 1,024 iterations, i.e. a "cost" factor of 10. A random ["salt"](https://en.wikipedia.org/wiki/Salt_(cryptography)) is applied automatically as well.
You can verify this configuration by looking at the hashes in your database table `users`. If the above is true with your setup, all password hashes in your `users` table should start with the prefix `$2$10$`, `$2a$10$` or `$2y$10$`.
When new algorithms (such as [Argon2](https://en.wikipedia.org/wiki/Argon2)) may be introduced in the future, this library will automatically take care of "upgrading" your existing password hashes whenever a user signs in or changes their password.
### How can I implement custom password requirements?
Enforcing a minimum length for passwords is usually a good idea. Apart from that, you may want to look up whether a potential password is in some blacklist, which you could manage in a database or in a file, in order to prevent dictionary words or commonly used passwords from being used in your application.
To allow for maximum flexibility and ease of use, this library has been designed so that it does *not* contain any further checks for password requirements itself, but instead allows you to wrap your own checks around the relevant calls to library methods. Example:
```php
function isPasswordAllowed($password) {
if (strlen($password) < 8) {
return false;
}
$blacklist = [ 'password1', '123456', 'qwerty' ];
if (in_array($password, $blacklist)) {
return false;
}
return true;
}
if (isPasswordAllowed($password)) {
$auth->register($email, $password);
}
```
### Why are there problems when using other libraries that work with sessions?
You might try loading this library first, and creating the `Auth` instance first, *before* loading the other libraries. Apart from that, there's probably not much we can do here.
### Why are other sites not able to frame or embed my site?
If you want to let others include your site in a `<frame>`, `<iframe>`, `<object>`, `<embed>` or `<applet>` element, you have to disable the default clickjacking prevention:
```php
\header_remove('X-Frame-Options');
```
## Exceptions
@@ -369,12 +969,12 @@ This library throws two types of exceptions to indicate problems:
## General advice
* Both serving the authentication pages (e.g. login and registration) and submitting the data entered by the user should only be done over TLS (HTTPS).
* You should enforce a minimum length for passwords, e.g. 10 characters, but *no* maximum length. Moreover, you should not restrict the set of allowed characters.
* Whenever a user was remembered ("remember me") and did not log in by entering their password, you should require re-authentication for critical features.
* Serve *all* pages over HTTPS only, i.e. using SSL/TLS for every single request.
* You should enforce a minimum length for passwords, e.g. 10 characters, but *never* any maximum length, at least not anywhere below 100 characters. Moreover, you should *not* restrict the set of allowed characters.
* Whenever a user was remembered through the "remember me" feature enabled or disabled during sign in, which means that they did not log in by typing their password, you should require re-authentication for critical features.
* Encourage users to use pass*phrases*, i.e. combinations of words or even full sentences, instead of single pass*words*.
* Do not prevent users' password managers from working correctly. Thus please use the standard form fields only and do not prevent copy and paste.
* Before executing sensitive account operations (e.g. changing a user's email address, deleting a user's account), you should always require re-authentication, i.e. require the user to sign in once more.
* Do not prevent users' password managers from working correctly. Thus, use the standard form fields only and do not prevent copy and paste.
* Before executing sensitive account operations (e.g. changing a user's email address, deleting a user's account), you should always require re-authentication, i.e. require the user to verify their login credentials once more.
* You should not offer an online password reset feature ("forgot password") for high-security applications.
* For high-security applications, you should not use email addresses as identifiers. Instead, choose identifiers that are specific to the application and secret, e.g. an internal customer number.

View File

@@ -4,8 +4,9 @@
"require": {
"php": ">=5.6.0",
"ext-openssl": "*",
"delight-im/base64": "^1.0",
"delight-im/cookie": "^2.1",
"delight-im/db": "^1.0"
"delight-im/db": "^1.2"
},
"type": "library",
"keywords": [ "auth", "authentication", "login", "security" ],

72
composer.lock generated
View File

@@ -4,26 +4,66 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "bd80e3e52b8bd8a4a0c74c7cf9f5bf5e",
"content-hash": "3f836c43e0ff2293051f2ccb739d23cf",
"content-hash": "8ab7c9ad8ef2bc7d9a6beb27f9bf4df5",
"packages": [
{
"name": "delight-im/cookie",
"version": "v2.1.0",
"name": "delight-im/base64",
"version": "v1.0.0",
"source": {
"type": "git",
"url": "https://github.com/delight-im/PHP-Cookie.git",
"reference": "3e41e0d44959b59de98722b5b1b1fb83f9f528f3"
"url": "https://github.com/delight-im/PHP-Base64.git",
"reference": "687b2a49f663e162030a8d27b32838bbe7f91c78"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/delight-im/PHP-Cookie/zipball/3e41e0d44959b59de98722b5b1b1fb83f9f528f3",
"reference": "3e41e0d44959b59de98722b5b1b1fb83f9f528f3",
"url": "https://api.github.com/repos/delight-im/PHP-Base64/zipball/687b2a49f663e162030a8d27b32838bbe7f91c78",
"reference": "687b2a49f663e162030a8d27b32838bbe7f91c78",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Delight\\Base64\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Simple and convenient Base64 encoding and decoding for PHP",
"homepage": "https://github.com/delight-im/PHP-Base64",
"keywords": [
"URL-safe",
"base-64",
"base64",
"decode",
"decoding",
"encode",
"encoding",
"url"
],
"time": "2017-07-24T18:59:51+00:00"
},
{
"name": "delight-im/cookie",
"version": "v2.1.3",
"source": {
"type": "git",
"url": "https://github.com/delight-im/PHP-Cookie.git",
"reference": "a66c8a02aa4776c4b7d3d04c695411f73e04e1eb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/delight-im/PHP-Cookie/zipball/a66c8a02aa4776c4b7d3d04c695411f73e04e1eb",
"reference": "a66c8a02aa4776c4b7d3d04c695411f73e04e1eb",
"shasum": ""
},
"require": {
"delight-im/http": "^2.0",
"php": ">=5.3.0"
"php": ">=5.4.0"
},
"type": "library",
"autoload": {
@@ -46,20 +86,20 @@
"samesite",
"xss"
],
"time": "2016-11-23 20:09:42"
"time": "2017-07-26T14:03:38+00:00"
},
{
"name": "delight-im/db",
"version": "v1.0.2",
"version": "v1.2.0",
"source": {
"type": "git",
"url": "https://github.com/delight-im/PHP-DB.git",
"reference": "c8d1eba6583007471d55bf7d88eb3c9d87ea849d"
"reference": "df99ef7c2e86c7ce206647ffe8ba74447c075b57"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/delight-im/PHP-DB/zipball/c8d1eba6583007471d55bf7d88eb3c9d87ea849d",
"reference": "c8d1eba6583007471d55bf7d88eb3c9d87ea849d",
"url": "https://api.github.com/repos/delight-im/PHP-DB/zipball/df99ef7c2e86c7ce206647ffe8ba74447c075b57",
"reference": "df99ef7c2e86c7ce206647ffe8ba74447c075b57",
"shasum": ""
},
"require": {
@@ -87,7 +127,7 @@
"sql",
"sqlite"
],
"time": "2016-12-01 12:40:36"
"time": "2017-03-18T20:51:59+00:00"
},
{
"name": "delight-im/http",
@@ -123,7 +163,7 @@
"http",
"https"
],
"time": "2016-07-21 15:05:01"
"time": "2016-07-21T15:05:01+00:00"
}
],
"packages-dev": [],

407
src/Administration.php Normal file
View File

@@ -0,0 +1,407 @@
<?php
/*
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
* Copyright (c) delight.im (https://www.delight.im/)
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
*/
namespace Delight\Auth;
use Delight\Db\PdoDatabase;
use Delight\Db\Throwable\Error;
require_once __DIR__ . '/Exceptions.php';
/** Component that can be used for administrative tasks by privileged and authorized users */
final class Administration extends UserManager {
/**
* @internal
*
* @param PdoDatabase $databaseConnection the database connection to operate on
* @param string|null $dbTablePrefix (optional) the prefix for the names of all database tables used by this component
*/
public function __construct(PdoDatabase $databaseConnection, $dbTablePrefix = null) {
parent::__construct($databaseConnection, $dbTablePrefix);
}
/**
* Creates a new user
*
* @param string $email the email address to register
* @param string $password the password for the new account
* @param string|null $username (optional) the username that will be displayed
* @return int the ID of the user that has been created (if any)
* @throws InvalidEmailException if the email address was invalid
* @throws InvalidPasswordException if the password was invalid
* @throws UserAlreadyExistsException if a user with the specified email address already exists
* @throws AuthError if an internal problem occurred (do *not* catch)
*/
public function createUser($email, $password, $username = null) {
return $this->createUserInternal(false, $email, $password, $username, null);
}
/**
* Creates a new user while ensuring that the username is unique
*
* @param string $email the email address to register
* @param string $password the password for the new account
* @param string|null $username (optional) the username that will be displayed
* @return int the ID of the user that has been created (if any)
* @throws InvalidEmailException if the email address was invalid
* @throws InvalidPasswordException if the password was invalid
* @throws UserAlreadyExistsException if a user with the specified email address already exists
* @throws DuplicateUsernameException if the specified username wasn't unique
* @throws AuthError if an internal problem occurred (do *not* catch)
*/
public function createUserWithUniqueUsername($email, $password, $username = null) {
return $this->createUserInternal(true, $email, $password, $username, null);
}
/**
* Deletes the user with the specified ID
*
* This action cannot be undone
*
* @param int $id the ID of the user to delete
* @throws UnknownIdException if no user with the specified ID has been found
* @throws AuthError if an internal problem occurred (do *not* catch)
*/
public function deleteUserById($id) {
$numberOfDeletedUsers = $this->deleteUsersByColumnValue('id', (int) $id);
if ($numberOfDeletedUsers === 0) {
throw new UnknownIdException();
}
}
/**
* Deletes the user with the specified email address
*
* This action cannot be undone
*
* @param string $email the email address of the user to delete
* @throws InvalidEmailException if no user with the specified email address has been found
* @throws AuthError if an internal problem occurred (do *not* catch)
*/
public function deleteUserByEmail($email) {
$email = self::validateEmailAddress($email);
$numberOfDeletedUsers = $this->deleteUsersByColumnValue('email', $email);
if ($numberOfDeletedUsers === 0) {
throw new InvalidEmailException();
}
}
/**
* Deletes the user with the specified username
*
* This action cannot be undone
*
* @param string $username the username of the user to delete
* @throws UnknownUsernameException if no user with the specified username has been found
* @throws AmbiguousUsernameException if multiple users with the specified username have been found
* @throws AuthError if an internal problem occurred (do *not* catch)
*/
public function deleteUserByUsername($username) {
$userData = $this->getUserDataByUsername(
trim($username),
[ 'id' ]
);
$this->deleteUsersByColumnValue('id', (int) $userData['id']);
}
/**
* Assigns the specified role to the user with the given ID
*
* A user may have any number of roles (i.e. no role at all, a single role, or any combination of roles)
*
* @param int $userId the ID of the user to assign the role to
* @param int $role the role as one of the constants from the {@see Role} class
* @throws UnknownIdException if no user with the specified ID has been found
*
* @see Role
*/
public function addRoleForUserById($userId, $role) {
$userFound = $this->addRoleForUserByColumnValue(
'id',
(int) $userId,
$role
);
if ($userFound === false) {
throw new UnknownIdException();
}
}
/**
* Assigns the specified role to the user with the given email address
*
* A user may have any number of roles (i.e. no role at all, a single role, or any combination of roles)
*
* @param string $userEmail the email address of the user to assign the role to
* @param int $role the role as one of the constants from the {@see Role} class
* @throws InvalidEmailException if no user with the specified email address has been found
*
* @see Role
*/
public function addRoleForUserByEmail($userEmail, $role) {
$userEmail = self::validateEmailAddress($userEmail);
$userFound = $this->addRoleForUserByColumnValue(
'email',
$userEmail,
$role
);
if ($userFound === false) {
throw new InvalidEmailException();
}
}
/**
* Assigns the specified role to the user with the given username
*
* A user may have any number of roles (i.e. no role at all, a single role, or any combination of roles)
*
* @param string $username the username of the user to assign the role to
* @param int $role the role as one of the constants from the {@see Role} class
* @throws UnknownUsernameException if no user with the specified username has been found
* @throws AmbiguousUsernameException if multiple users with the specified username have been found
*
* @see Role
*/
public function addRoleForUserByUsername($username, $role) {
$userData = $this->getUserDataByUsername(
\trim($username),
[ 'id' ]
);
$this->addRoleForUserByColumnValue(
'id',
(int) $userData['id'],
$role
);
}
/**
* Takes away the specified role from the user with the given ID
*
* A user may have any number of roles (i.e. no role at all, a single role, or any combination of roles)
*
* @param int $userId the ID of the user to take the role away from
* @param int $role the role as one of the constants from the {@see Role} class
* @throws UnknownIdException if no user with the specified ID has been found
*
* @see Role
*/
public function removeRoleForUserById($userId, $role) {
$userFound = $this->removeRoleForUserByColumnValue(
'id',
(int) $userId,
$role
);
if ($userFound === false) {
throw new UnknownIdException();
}
}
/**
* Takes away the specified role from the user with the given email address
*
* A user may have any number of roles (i.e. no role at all, a single role, or any combination of roles)
*
* @param string $userEmail the email address of the user to take the role away from
* @param int $role the role as one of the constants from the {@see Role} class
* @throws InvalidEmailException if no user with the specified email address has been found
*
* @see Role
*/
public function removeRoleForUserByEmail($userEmail, $role) {
$userEmail = self::validateEmailAddress($userEmail);
$userFound = $this->removeRoleForUserByColumnValue(
'email',
$userEmail,
$role
);
if ($userFound === false) {
throw new InvalidEmailException();
}
}
/**
* Takes away the specified role from the user with the given username
*
* A user may have any number of roles (i.e. no role at all, a single role, or any combination of roles)
*
* @param string $username the username of the user to take the role away from
* @param int $role the role as one of the constants from the {@see Role} class
* @throws UnknownUsernameException if no user with the specified username has been found
* @throws AmbiguousUsernameException if multiple users with the specified username have been found
*
* @see Role
*/
public function removeRoleForUserByUsername($username, $role) {
$userData = $this->getUserDataByUsername(
\trim($username),
[ 'id' ]
);
$this->removeRoleForUserByColumnValue(
'id',
(int) $userData['id'],
$role
);
}
/**
* Returns whether the user with the given ID has the specified role
*
* @param int $userId the ID of the user to check the roles for
* @param int $role the role as one of the constants from the {@see Role} class
* @return bool
* @throws UnknownIdException if no user with the specified ID has been found
*
* @see Role
*/
public function doesUserHaveRole($userId, $role) {
$userId = (int) $userId;
$role = (int) $role;
$rolesBitmask = $this->db->selectValue(
'SELECT roles_mask FROM ' . $this->dbTablePrefix . 'users WHERE id = ?',
[ $userId ]
);
if ($rolesBitmask === null) {
throw new UnknownIdException();
}
return ($rolesBitmask & $role) === $role;
}
/**
* Deletes all existing users where the column with the specified name has the given value
*
* You must never pass untrusted input to the parameter that takes the column name
*
* @param string $columnName the name of the column to filter by
* @param mixed $columnValue the value to look for in the selected column
* @return int the number of deleted users
* @throws AuthError if an internal problem occurred (do *not* catch)
*/
private function deleteUsersByColumnValue($columnName, $columnValue) {
try {
return $this->db->delete(
$this->dbTablePrefix . 'users',
[
$columnName => $columnValue
]
);
}
catch (Error $e) {
throw new DatabaseError();
}
}
/**
* Modifies the roles for the user where the column with the specified name has the given value
*
* You must never pass untrusted input to the parameter that takes the column name
*
* @param string $columnName the name of the column to filter by
* @param mixed $columnValue the value to look for in the selected column
* @param callable $modification the modification to apply to the existing bitmask of roles
* @return bool whether any user with the given column constraints has been found
* @throws AuthError if an internal problem occurred (do *not* catch)
*
* @see Role
*/
private function modifyRolesForUserByColumnValue($columnName, $columnValue, callable $modification) {
try {
$userData = $this->db->selectRow(
'SELECT id, roles_mask FROM ' . $this->dbTablePrefix . 'users WHERE ' . $columnName . ' = ?',
[ $columnValue ]
);
}
catch (Error $e) {
throw new DatabaseError();
}
if ($userData === null) {
return false;
}
$newRolesBitmask = $modification($userData['roles_mask']);
try {
$this->db->exec(
'UPDATE ' . $this->dbTablePrefix . 'users SET roles_mask = ? WHERE id = ?',
[
$newRolesBitmask,
(int) $userData['id']
]
);
return true;
}
catch (Error $e) {
throw new DatabaseError();
}
}
/**
* Assigns the specified role to the user where the column with the specified name has the given value
*
* You must never pass untrusted input to the parameter that takes the column name
*
* @param string $columnName the name of the column to filter by
* @param mixed $columnValue the value to look for in the selected column
* @param int $role the role as one of the constants from the {@see Role} class
* @return bool whether any user with the given column constraints has been found
*
* @see Role
*/
private function addRoleForUserByColumnValue($columnName, $columnValue, $role) {
$role = (int) $role;
return $this->modifyRolesForUserByColumnValue(
$columnName,
$columnValue,
function ($oldRolesBitmask) use ($role) {
return $oldRolesBitmask | $role;
}
);
}
/**
* Takes away the specified role from the user where the column with the specified name has the given value
*
* You must never pass untrusted input to the parameter that takes the column name
*
* @param string $columnName the name of the column to filter by
* @param mixed $columnValue the value to look for in the selected column
* @param int $role the role as one of the constants from the {@see Role} class
* @return bool whether any user with the given column constraints has been found
*
* @see Role
*/
private function removeRoleForUserByColumnValue($columnName, $columnValue, $role) {
$role = (int) $role;
return $this->modifyRolesForUserByColumnValue(
$columnName,
$columnValue,
function ($oldRolesBitmask) use ($role) {
return $oldRolesBitmask & ~$role;
}
);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,34 +0,0 @@
<?php
/*
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
* Copyright (c) delight.im (https://www.delight.im/)
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
*/
namespace Delight\Auth;
class Base64 {
const SPECIAL_CHARS_ORIGINAL = '+/=';
const SPECIAL_CHARS_SAFE = '._-';
public static function encode($data, $safeChars = false) {
$result = base64_encode($data);
if ($safeChars) {
$result = strtr($result, self::SPECIAL_CHARS_ORIGINAL, self::SPECIAL_CHARS_SAFE);
}
return $result;
}
public static function decode($data) {
$data = strtr($data, self::SPECIAL_CHARS_SAFE, self::SPECIAL_CHARS_ORIGINAL);
$result = base64_decode($data, true);
return $result;
}
}

View File

@@ -10,8 +10,12 @@ namespace Delight\Auth;
class AuthException extends \Exception {}
class UnknownIdException extends AuthException {}
class InvalidEmailException extends AuthException {}
class UnknownUsernameException extends AuthException {}
class InvalidPasswordException extends AuthException {}
class EmailNotVerifiedException extends AuthException {}
@@ -26,10 +30,24 @@ class TokenExpiredException extends AuthException {}
class TooManyRequestsException extends AuthException {}
class DuplicateUsernameException extends AuthException {}
class AmbiguousUsernameException extends AuthException {}
class AttemptCancelledException extends AuthException {}
class ResetDisabledException extends AuthException {}
class ConfirmationRequestNotFound extends AuthException {}
class AuthError extends \Exception {}
class DatabaseError extends AuthError {}
class DatabaseDriverError extends DatabaseError {}
class MissingCallbackError extends AuthError {}
class HeadersAlreadySentError extends AuthError {}
class EmailOrUsernameRequiredError extends AuthError {}

46
src/Role.php Normal file
View File

@@ -0,0 +1,46 @@
<?php
/*
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
* Copyright (c) delight.im (https://www.delight.im/)
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
*/
namespace Delight\Auth;
final class Role {
const ADMIN = 1;
const AUTHOR = 2;
const COLLABORATOR = 4;
const CONSULTANT = 8;
const CONSUMER = 16;
const CONTRIBUTOR = 32;
const COORDINATOR = 64;
const CREATOR = 128;
const DEVELOPER = 256;
const DIRECTOR = 512;
const EDITOR = 1024;
const EMPLOYEE = 2048;
const MAINTAINER = 4096;
const MANAGER = 8192;
const MODERATOR = 16384;
const PUBLISHER = 32768;
const REVIEWER = 65536;
const SUBSCRIBER = 131072;
const SUPER_ADMIN = 262144;
const SUPER_EDITOR = 524288;
const SUPER_MODERATOR = 1048576;
const TRANSLATOR = 2097152;
// const XXX = 4194304;
// const XXX = 8388608;
// const XXX = 16777216;
// const XXX = 33554432;
// const XXX = 67108864;
// const XXX = 134217728;
// const XXX = 268435456;
// const XXX = 536870912;
private function __construct() {}
}

20
src/Status.php Normal file
View File

@@ -0,0 +1,20 @@
<?php
/*
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
* Copyright (c) delight.im (https://www.delight.im/)
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
*/
namespace Delight\Auth;
final class Status {
const NORMAL = 0;
const ARCHIVED = 1;
const BANNED = 2;
const LOCKED = 3;
const PENDING_REVIEW = 4;
const SUSPENDED = 5;
}

297
src/UserManager.php Normal file
View File

@@ -0,0 +1,297 @@
<?php
/*
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
* Copyright (c) delight.im (https://www.delight.im/)
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
*/
namespace Delight\Auth;
use Delight\Base64\Base64;
use Delight\Db\PdoDatabase;
use Delight\Db\PdoDsn;
use Delight\Db\Throwable\Error;
use Delight\Db\Throwable\IntegrityConstraintViolationException;
require_once __DIR__ . '/Exceptions.php';
/**
* Abstract base class for components implementing user management
*
* @internal
*/
abstract class UserManager {
const CONFIRMATION_REQUESTS_TTL_IN_SECONDS = 60 * 60 * 24;
/** @var PdoDatabase the database connection to operate on */
protected $db;
/** @var string the prefix for the names of all database tables used by this component */
protected $dbTablePrefix;
/**
* Creates a random string with the given maximum length
*
* With the default parameter, the output should contain at least as much randomness as a UUID
*
* @param int $maxLength the maximum length of the output string (integer multiple of 4)
* @return string the new random string
*/
public static function createRandomString($maxLength = 24) {
// calculate how many bytes of randomness we need for the specified string length
$bytes = floor(intval($maxLength) / 4) * 3;
// get random data
$data = openssl_random_pseudo_bytes($bytes);
// return the Base64-encoded result
return Base64::encodeUrlSafe($data);
}
/**
* @param PdoDatabase|PdoDsn|\PDO $databaseConnection the database connection to operate on
* @param string|null $dbTablePrefix (optional) the prefix for the names of all database tables used by this component
*/
protected function __construct($databaseConnection, $dbTablePrefix = null) {
if ($databaseConnection instanceof PdoDatabase) {
$this->db = $databaseConnection;
}
elseif ($databaseConnection instanceof PdoDsn) {
$this->db = PdoDatabase::fromDsn($databaseConnection);
}
elseif ($databaseConnection instanceof \PDO) {
$this->db = PdoDatabase::fromPdo($databaseConnection, true);
}
else {
$this->db = null;
throw new \InvalidArgumentException('The database connection must be an instance of either `PdoDatabase`, `PdoDsn` or `PDO`');
}
$this->dbTablePrefix = (string) $dbTablePrefix;
}
/**
* Creates a new user
*
* If you want the user's account to be activated by default, pass `null` as the callback
*
* If you want to make the user verify their email address first, pass an anonymous function as the callback
*
* The callback function must have the following signature:
*
* `function ($selector, $token)`
*
* Both pieces of information must be sent to the user, usually embedded in a link
*
* When the user wants to verify their email address as a next step, both pieces will be required again
*
* @param bool $requireUniqueUsername whether it must be ensured that the username is unique
* @param string $email the email address to register
* @param string $password the password for the new account
* @param string|null $username (optional) the username that will be displayed
* @param callable|null $callback (optional) the function that sends the confirmation email to the user
* @return int the ID of the user that has been created (if any)
* @throws InvalidEmailException if the email address has been invalid
* @throws InvalidPasswordException if the password has been invalid
* @throws UserAlreadyExistsException if a user with the specified email address already exists
* @throws DuplicateUsernameException if it was specified that the username must be unique while it was *not*
* @throws AuthError if an internal problem occurred (do *not* catch)
*
* @see confirmEmail
* @see confirmEmailAndSignIn
*/
protected function createUserInternal($requireUniqueUsername, $email, $password, $username = null, callable $callback = null) {
ignore_user_abort(true);
$email = self::validateEmailAddress($email);
$password = self::validatePassword($password);
$username = isset($username) ? trim($username) : null;
// if the supplied username is the empty string or has consisted of whitespace only
if ($username === '') {
// this actually means that there is no username
$username = null;
}
// if the uniqueness of the username is to be ensured
if ($requireUniqueUsername) {
// if a username has actually been provided
if ($username !== null) {
// count the number of users who do already have that specified username
$occurrencesOfUsername = $this->db->selectValue(
'SELECT COUNT(*) FROM ' . $this->dbTablePrefix . 'users WHERE username = ?',
[ $username ]
);
// if any user with that username does already exist
if ($occurrencesOfUsername > 0) {
// cancel the operation and report the violation of this requirement
throw new DuplicateUsernameException();
}
}
}
$password = password_hash($password, PASSWORD_DEFAULT);
$verified = isset($callback) && is_callable($callback) ? 0 : 1;
try {
$this->db->insert(
$this->dbTablePrefix . 'users',
[
'email' => $email,
'password' => $password,
'username' => $username,
'verified' => $verified,
'registered' => time()
]
);
}
// if we have a duplicate entry
catch (IntegrityConstraintViolationException $e) {
throw new UserAlreadyExistsException();
}
catch (Error $e) {
throw new DatabaseError();
}
$newUserId = (int) $this->db->getLastInsertId();
if ($verified === 0) {
$this->createConfirmationRequest($newUserId, $email, $callback);
}
return $newUserId;
}
/**
* Returns the requested user data for the account with the specified username (if any)
*
* You must never pass untrusted input to the parameter that takes the column list
*
* @param string $username the username to look for
* @param array $requestedColumns the columns to request from the user's record
* @return array the user data (if an account was found unambiguously)
* @throws UnknownUsernameException if no user with the specified username has been found
* @throws AmbiguousUsernameException if multiple users with the specified username have been found
* @throws AuthError if an internal problem occurred (do *not* catch)
*/
protected function getUserDataByUsername($username, array $requestedColumns) {
try {
$projection = implode(', ', $requestedColumns);
$users = $this->db->select(
'SELECT ' . $projection . ' FROM ' . $this->dbTablePrefix . 'users WHERE username = ? LIMIT 2 OFFSET 0',
[ $username ]
);
}
catch (Error $e) {
throw new DatabaseError();
}
if (empty($users)) {
throw new UnknownUsernameException();
}
else {
if (count($users) === 1) {
return $users[0];
}
else {
throw new AmbiguousUsernameException();
}
}
}
/**
* Validates an email address
*
* @param string $email the email address to validate
* @return string the sanitized email address
* @throws InvalidEmailException if the email address has been invalid
*/
protected static function validateEmailAddress($email) {
if (empty($email)) {
throw new InvalidEmailException();
}
$email = trim($email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidEmailException();
}
return $email;
}
/**
* Validates a password
*
* @param string $password the password to validate
* @return string the sanitized password
* @throws InvalidPasswordException if the password has been invalid
*/
protected static function validatePassword($password) {
if (empty($password)) {
throw new InvalidPasswordException();
}
$password = trim($password);
if (strlen($password) < 1) {
throw new InvalidPasswordException();
}
return $password;
}
/**
* Creates a request for email confirmation
*
* The callback function must have the following signature:
*
* `function ($selector, $token)`
*
* Both pieces of information must be sent to the user, usually embedded in a link
*
* When the user wants to verify their email address as a next step, both pieces will be required again
*
* @param int $userId the user's ID
* @param string $email the email address to verify
* @param callable $callback the function that sends the confirmation email to the user
* @throws AuthError if an internal problem occurred (do *not* catch)
*/
protected function createConfirmationRequest($userId, $email, callable $callback) {
$selector = self::createRandomString(16);
$token = self::createRandomString(16);
$tokenHashed = password_hash($token, PASSWORD_DEFAULT);
// the request shall be valid for one day
$expires = time() + self::CONFIRMATION_REQUESTS_TTL_IN_SECONDS;
try {
$this->db->insert(
$this->dbTablePrefix . 'users_confirmations',
[
'user_id' => (int) $userId,
'email' => $email,
'selector' => $selector,
'token' => $tokenHashed,
'expires' => $expires
]
);
}
catch (Error $e) {
throw new DatabaseError();
}
if (isset($callback) && is_callable($callback)) {
$callback($selector, $token);
}
else {
throw new MissingCallbackError();
}
}
}

View File

@@ -6,6 +6,14 @@
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
*/
/*
* WARNING:
*
* Do *not* use these files from the `tests` directory as the foundation
* for the usage of this library in your own code. Instead, please follow
* the `README.md` file in the root directory of this project.
*/
// enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 'stdout');
@@ -20,6 +28,8 @@ header('Content-type: text/html; charset=utf-8');
require __DIR__.'/../vendor/autoload.php';
$db = new PDO('mysql:dbname=php_auth;host=127.0.0.1;charset=utf8mb4', 'root', 'monkey');
// or
// $db = new PDO('sqlite:../Databases/php_auth.sqlite');
$auth = new \Delight\Auth\Auth($db);
@@ -28,7 +38,7 @@ $result = processRequestData($auth);
showDebugData($auth, $result);
if ($auth->check()) {
showAuthenticatedUserForm();
showAuthenticatedUserForm($auth);
}
else {
showGuestUserForm();
@@ -48,13 +58,27 @@ function processRequestData(\Delight\Auth\Auth $auth) {
}
try {
$auth->login($_POST['email'], $_POST['password'], $rememberDuration);
if (isset($_POST['email'])) {
$auth->login($_POST['email'], $_POST['password'], $rememberDuration);
}
elseif (isset($_POST['username'])) {
$auth->loginWithUsername($_POST['username'], $_POST['password'], $rememberDuration);
}
else {
return 'either email address or username required';
}
return 'ok';
}
catch (\Delight\Auth\InvalidEmailException $e) {
return 'wrong email address';
}
catch (\Delight\Auth\UnknownUsernameException $e) {
return 'unknown username';
}
catch (\Delight\Auth\AmbiguousUsernameException $e) {
return 'ambiguous username';
}
catch (\Delight\Auth\InvalidPasswordException $e) {
return 'wrong password';
}
@@ -86,7 +110,16 @@ function processRequestData(\Delight\Auth\Auth $auth) {
$callback = null;
}
return $auth->register($_POST['email'], $_POST['password'], $_POST['username'], $callback);
if (!isset($_POST['require_unique_username'])) {
$_POST['require_unique_username'] = '0';
}
if ($_POST['require_unique_username'] == 0) {
return $auth->register($_POST['email'], $_POST['password'], $_POST['username'], $callback);
}
else {
return $auth->registerWithUniqueUsername($_POST['email'], $_POST['password'], $_POST['username'], $callback);
}
}
catch (\Delight\Auth\InvalidEmailException $e) {
return 'invalid email address';
@@ -95,7 +128,10 @@ function processRequestData(\Delight\Auth\Auth $auth) {
return 'invalid password';
}
catch (\Delight\Auth\UserAlreadyExistsException $e) {
return 'user already exists';
return 'email address already exists';
}
catch (\Delight\Auth\DuplicateUsernameException $e) {
return 'username already exists';
}
catch (\Delight\Auth\TooManyRequestsException $e) {
return 'too many requests';
@@ -103,7 +139,20 @@ function processRequestData(\Delight\Auth\Auth $auth) {
}
else if ($_POST['action'] === 'confirmEmail') {
try {
$auth->confirmEmail($_POST['selector'], $_POST['token']);
if (isset($_POST['login']) && $_POST['login'] > 0) {
if ($_POST['login'] == 2) {
// keep logged in for one year
$rememberDuration = (int) (60 * 60 * 24 * 365.25);
}
else {
// do not keep logged in after session ends
$rememberDuration = null;
}
$auth->confirmEmailAndSignIn($_POST['selector'], $_POST['token'], $rememberDuration);
}
else {
$auth->confirmEmail($_POST['selector'], $_POST['token']);
}
return 'ok';
}
@@ -113,6 +162,59 @@ function processRequestData(\Delight\Auth\Auth $auth) {
catch (\Delight\Auth\TokenExpiredException $e) {
return 'token expired';
}
catch (\Delight\Auth\UserAlreadyExistsException $e) {
return 'email address already exists';
}
catch (\Delight\Auth\TooManyRequestsException $e) {
return 'too many requests';
}
}
else if ($_POST['action'] === 'resendConfirmationForEmail') {
try {
$auth->resendConfirmationForEmail($_POST['email'], function ($selector, $token) {
echo '<pre>';
echo 'Email confirmation';
echo "\n";
echo ' > Selector';
echo "\t\t\t\t";
echo htmlspecialchars($selector);
echo "\n";
echo ' > Token';
echo "\t\t\t\t";
echo htmlspecialchars($token);
echo '</pre>';
});
return 'ok';
}
catch (\Delight\Auth\ConfirmationRequestNotFound $e) {
return 'no request found';
}
catch (\Delight\Auth\TooManyRequestsException $e) {
return 'too many requests';
}
}
else if ($_POST['action'] === 'resendConfirmationForUserId') {
try {
$auth->resendConfirmationForUserId($_POST['userId'], function ($selector, $token) {
echo '<pre>';
echo 'Email confirmation';
echo "\n";
echo ' > Selector';
echo "\t\t\t\t";
echo htmlspecialchars($selector);
echo "\n";
echo ' > Token';
echo "\t\t\t\t";
echo htmlspecialchars($token);
echo '</pre>';
});
return 'ok';
}
catch (\Delight\Auth\ConfirmationRequestNotFound $e) {
return 'no request found';
}
catch (\Delight\Auth\TooManyRequestsException $e) {
return 'too many requests';
}
@@ -138,6 +240,12 @@ function processRequestData(\Delight\Auth\Auth $auth) {
catch (\Delight\Auth\InvalidEmailException $e) {
return 'invalid email address';
}
catch (\Delight\Auth\EmailNotVerifiedException $e) {
return 'email not verified';
}
catch (\Delight\Auth\ResetDisabledException $e) {
return 'password reset disabled';
}
catch (\Delight\Auth\TooManyRequestsException $e) {
return 'too many requests';
}
@@ -154,6 +262,9 @@ function processRequestData(\Delight\Auth\Auth $auth) {
catch (\Delight\Auth\TokenExpiredException $e) {
return 'token expired';
}
catch (\Delight\Auth\ResetDisabledException $e) {
return 'password reset disabled';
}
catch (\Delight\Auth\InvalidPasswordException $e) {
return 'invalid password';
}
@@ -161,6 +272,17 @@ function processRequestData(\Delight\Auth\Auth $auth) {
return 'too many requests';
}
}
else if ($_POST['action'] === 'reconfirmPassword') {
try {
return $auth->reconfirmPassword($_POST['password']) ? 'correct' : 'wrong';
}
catch (\Delight\Auth\NotLoggedInException $e) {
return 'not logged in';
}
catch (\Delight\Auth\TooManyRequestsException $e) {
return 'too many requests';
}
}
else if ($_POST['action'] === 'changePassword') {
try {
$auth->changePassword($_POST['oldPassword'], $_POST['newPassword']);
@@ -173,12 +295,233 @@ function processRequestData(\Delight\Auth\Auth $auth) {
catch (\Delight\Auth\InvalidPasswordException $e) {
return 'invalid password(s)';
}
catch (\Delight\Auth\TooManyRequestsException $e) {
return 'too many requests';
}
}
else if ($_POST['action'] === 'changePasswordWithoutOldPassword') {
try {
$auth->changePasswordWithoutOldPassword($_POST['newPassword']);
return 'ok';
}
catch (\Delight\Auth\NotLoggedInException $e) {
return 'not logged in';
}
catch (\Delight\Auth\InvalidPasswordException $e) {
return 'invalid password';
}
}
else if ($_POST['action'] === 'changeEmail') {
try {
$auth->changeEmail($_POST['newEmail'], function ($selector, $token) {
echo '<pre>';
echo 'Email confirmation';
echo "\n";
echo ' > Selector';
echo "\t\t\t\t";
echo htmlspecialchars($selector);
echo "\n";
echo ' > Token';
echo "\t\t\t\t";
echo htmlspecialchars($token);
echo '</pre>';
});
return 'ok';
}
catch (\Delight\Auth\InvalidEmailException $e) {
return 'invalid email address';
}
catch (\Delight\Auth\UserAlreadyExistsException $e) {
return 'email address already exists';
}
catch (\Delight\Auth\EmailNotVerifiedException $e) {
return 'account not verified';
}
catch (\Delight\Auth\NotLoggedInException $e) {
return 'not logged in';
}
catch (\Delight\Auth\TooManyRequestsException $e) {
return 'too many requests';
}
}
else if ($_POST['action'] === 'setPasswordResetEnabled') {
try {
$auth->setPasswordResetEnabled($_POST['enabled'] == 1);
return 'ok';
}
catch (\Delight\Auth\NotLoggedInException $e) {
return 'not logged in';
}
}
else if ($_POST['action'] === 'logOutButKeepSession') {
$auth->logOutButKeepSession();
return 'ok';
}
else if ($_POST['action'] === 'logout') {
$auth->logout();
return 'ok';
}
else if ($_POST['action'] === 'admin.createUser') {
try {
if (!isset($_POST['require_unique_username'])) {
$_POST['require_unique_username'] = '0';
}
if ($_POST['require_unique_username'] == 0) {
return $auth->admin()->createUser($_POST['email'], $_POST['password'], $_POST['username']);
}
else {
return $auth->admin()->createUserWithUniqueUsername($_POST['email'], $_POST['password'], $_POST['username']);
}
}
catch (\Delight\Auth\InvalidEmailException $e) {
return 'invalid email address';
}
catch (\Delight\Auth\InvalidPasswordException $e) {
return 'invalid password';
}
catch (\Delight\Auth\UserAlreadyExistsException $e) {
return 'email address already exists';
}
catch (\Delight\Auth\DuplicateUsernameException $e) {
return 'username already exists';
}
}
else if ($_POST['action'] === 'admin.deleteUser') {
if (isset($_POST['id'])) {
try {
$auth->admin()->deleteUserById($_POST['id']);
}
catch (\Delight\Auth\UnknownIdException $e) {
return 'unknown ID';
}
}
elseif (isset($_POST['email'])) {
try {
$auth->admin()->deleteUserByEmail($_POST['email']);
}
catch (\Delight\Auth\InvalidEmailException $e) {
return 'unknown email address';
}
}
elseif (isset($_POST['username'])) {
try {
$auth->admin()->deleteUserByUsername($_POST['username']);
}
catch (\Delight\Auth\UnknownUsernameException $e) {
return 'unknown username';
}
catch (\Delight\Auth\AmbiguousUsernameException $e) {
return 'ambiguous username';
}
}
else {
return 'either ID, email or username required';
}
return 'ok';
}
else if ($_POST['action'] === 'admin.addRole') {
if (isset($_POST['role'])) {
if (isset($_POST['id'])) {
try {
$auth->admin()->addRoleForUserById($_POST['id'], $_POST['role']);
}
catch (\Delight\Auth\UnknownIdException $e) {
return 'unknown ID';
}
}
elseif (isset($_POST['email'])) {
try {
$auth->admin()->addRoleForUserByEmail($_POST['email'], $_POST['role']);
}
catch (\Delight\Auth\InvalidEmailException $e) {
return 'unknown email address';
}
}
elseif (isset($_POST['username'])) {
try {
$auth->admin()->addRoleForUserByUsername($_POST['username'], $_POST['role']);
}
catch (\Delight\Auth\UnknownUsernameException $e) {
return 'unknown username';
}
catch (\Delight\Auth\AmbiguousUsernameException $e) {
return 'ambiguous username';
}
}
else {
return 'either ID, email or username required';
}
}
else {
return 'role required';
}
return 'ok';
}
else if ($_POST['action'] === 'admin.removeRole') {
if (isset($_POST['role'])) {
if (isset($_POST['id'])) {
try {
$auth->admin()->removeRoleForUserById($_POST['id'], $_POST['role']);
}
catch (\Delight\Auth\UnknownIdException $e) {
return 'unknown ID';
}
}
elseif (isset($_POST['email'])) {
try {
$auth->admin()->removeRoleForUserByEmail($_POST['email'], $_POST['role']);
}
catch (\Delight\Auth\InvalidEmailException $e) {
return 'unknown email address';
}
}
elseif (isset($_POST['username'])) {
try {
$auth->admin()->removeRoleForUserByUsername($_POST['username'], $_POST['role']);
}
catch (\Delight\Auth\UnknownUsernameException $e) {
return 'unknown username';
}
catch (\Delight\Auth\AmbiguousUsernameException $e) {
return 'ambiguous username';
}
}
else {
return 'either ID, email or username required';
}
}
else {
return 'role required';
}
return 'ok';
}
else if ($_POST['action'] === 'admin.hasRole') {
if (isset($_POST['id'])) {
if (isset($_POST['role'])) {
try {
return $auth->admin()->doesUserHaveRole($_POST['id'], $_POST['role']) ? 'yes' : 'no';
}
catch (\Delight\Auth\UnknownIdException $e) {
return 'unknown ID';
}
}
else {
return 'role required';
}
}
else {
return 'ID required';
}
}
else {
throw new Exception('Unexpected action: '.$_POST['action']);
}
@@ -213,6 +556,25 @@ function showDebugData(\Delight\Auth\Auth $auth, $result) {
var_dump($auth->getEmail());
echo '$auth->getUsername()'."\t\t\t";
var_dump($auth->getUsername());
echo '$auth->getStatus()'."\t\t\t";
echo convertStatusToText($auth);
echo ' / ';
var_dump($auth->getStatus());
echo "\n";
echo 'Roles (super moderator)'."\t\t\t";
var_dump($auth->hasRole(\Delight\Auth\Role::SUPER_MODERATOR));
echo 'Roles (developer *or* manager)'."\t\t";
var_dump($auth->hasAnyRole(\Delight\Auth\Role::DEVELOPER, \Delight\Auth\Role::MANAGER));
echo 'Roles (developer *and* manager)'."\t\t";
var_dump($auth->hasAllRoles(\Delight\Auth\Role::DEVELOPER, \Delight\Auth\Role::MANAGER));
echo "\n";
echo '$auth->isRemembered()'."\t\t\t";
var_dump($auth->isRemembered());
echo '$auth->getIpAddress()'."\t\t\t";
@@ -227,15 +589,51 @@ function showDebugData(\Delight\Auth\Auth $auth, $result) {
echo '</pre>';
}
function convertStatusToText(\Delight\Auth\Auth $auth) {
if ($auth->isLoggedIn() === true) {
if ($auth->getStatus() === \Delight\Auth\Status::NORMAL && $auth->isNormal()) {
return 'normal';
}
elseif ($auth->getStatus() === \Delight\Auth\Status::ARCHIVED && $auth->isArchived()) {
return 'archived';
}
elseif ($auth->getStatus() === \Delight\Auth\Status::BANNED && $auth->isBanned()) {
return 'banned';
}
elseif ($auth->getStatus() === \Delight\Auth\Status::LOCKED && $auth->isLocked()) {
return 'locked';
}
elseif ($auth->getStatus() === \Delight\Auth\Status::PENDING_REVIEW && $auth->isPendingReview()) {
return 'pending review';
}
elseif ($auth->getStatus() === \Delight\Auth\Status::SUSPENDED && $auth->isSuspended()) {
return 'suspended';
}
}
elseif ($auth->isLoggedIn() === false) {
if ($auth->getStatus() === null) {
return 'none';
}
}
throw new Exception('Invalid status `' . $auth->getStatus() . '`');
}
function showGeneralForm() {
echo '<form action="" method="get" accept-charset="utf-8">';
echo '<button type="submit">Refresh</button>';
echo '</form>';
}
function showAuthenticatedUserForm() {
function showAuthenticatedUserForm(\Delight\Auth\Auth $auth) {
showGeneralForm();
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="reconfirmPassword" />';
echo '<input type="text" name="password" placeholder="Password" /> ';
echo '<button type="submit">Reconfirm password</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="changePassword" />';
echo '<input type="text" name="oldPassword" placeholder="Old password" /> ';
@@ -243,15 +641,45 @@ function showAuthenticatedUserForm() {
echo '<button type="submit">Change password</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="changePasswordWithoutOldPassword" />';
echo '<input type="text" name="newPassword" placeholder="New password" /> ';
echo '<button type="submit">Change password without old password</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="changeEmail" />';
echo '<input type="text" name="newEmail" placeholder="New email address" /> ';
echo '<button type="submit">Change email address</button>';
echo '</form>';
showConfirmEmailForm();
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="setPasswordResetEnabled" />';
echo '<select name="enabled" size="1">';
echo '<option value="0"' . ($auth->isPasswordResetEnabled() ? '' : ' selected="selected"') . '>Disabled</option>';
echo '<option value="1"' . ($auth->isPasswordResetEnabled() ? ' selected="selected"' : '') . '>Enabled</option>';
echo '</select> ';
echo '<button type="submit">Control password resets</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="logOutButKeepSession" />';
echo '<button type="submit">Log out but keep session</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="logout" />';
echo '<button type="submit">Logout</button>';
echo '<button type="submit">Log out</button>';
echo '</form>';
}
function showGuestUserForm() {
showGeneralForm();
echo '<h1>Public</h1>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="login" />';
echo '<input type="text" name="email" placeholder="Email" /> ';
@@ -260,7 +688,18 @@ function showGuestUserForm() {
echo '<option value="0">Remember (keep logged in)? — No</option>';
echo '<option value="1">Remember (keep logged in)? — Yes</option>';
echo '</select> ';
echo '<button type="submit">Login</button>';
echo '<button type="submit">Log in with email address</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="login" />';
echo '<input type="text" name="username" placeholder="Username" /> ';
echo '<input type="text" name="password" placeholder="Password" /> ';
echo '<select name="remember" size="1">';
echo '<option value="0">Remember (keep logged in)? — No</option>';
echo '<option value="1">Remember (keep logged in)? — Yes</option>';
echo '</select> ';
echo '<button type="submit">Log in with username</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
@@ -272,15 +711,14 @@ function showGuestUserForm() {
echo '<option value="0">Require email confirmation? — No</option>';
echo '<option value="1">Require email confirmation? — Yes</option>';
echo '</select> ';
echo '<select name="require_unique_username" size="1">';
echo '<option value="0">Username — Any</option>';
echo '<option value="1">Username — Unique</option>';
echo '</select> ';
echo '<button type="submit">Register</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="confirmEmail" />';
echo '<input type="text" name="selector" placeholder="Selector" /> ';
echo '<input type="text" name="token" placeholder="Token" /> ';
echo '<button type="submit">Confirm email</button>';
echo '</form>';
showConfirmEmailForm();
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="forgotPassword" />';
@@ -295,4 +733,123 @@ function showGuestUserForm() {
echo '<input type="text" name="password" placeholder="New password" /> ';
echo '<button type="submit">Reset password</button>';
echo '</form>';
echo '<h1>Administration</h1>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="admin.createUser" />';
echo '<input type="text" name="email" placeholder="Email" /> ';
echo '<input type="text" name="password" placeholder="Password" /> ';
echo '<input type="text" name="username" placeholder="Username (optional)" /> ';
echo '<select name="require_unique_username" size="1">';
echo '<option value="0">Username — Any</option>';
echo '<option value="1">Username — Unique</option>';
echo '</select> ';
echo '<button type="submit">Create user</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="admin.deleteUser" />';
echo '<input type="text" name="id" placeholder="ID" /> ';
echo '<button type="submit">Delete user by ID</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="admin.deleteUser" />';
echo '<input type="text" name="email" placeholder="Email" /> ';
echo '<button type="submit">Delete user by email</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="admin.deleteUser" />';
echo '<input type="text" name="username" placeholder="Username" /> ';
echo '<button type="submit">Delete user by username</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="admin.addRole" />';
echo '<input type="text" name="id" placeholder="ID" /> ';
echo '<select name="role">' . createRolesOptions() . '</select>';
echo '<button type="submit">Add role for user by ID</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="admin.addRole" />';
echo '<input type="text" name="email" placeholder="Email" /> ';
echo '<select name="role">' . createRolesOptions() . '</select>';
echo '<button type="submit">Add role for user by email</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="admin.addRole" />';
echo '<input type="text" name="username" placeholder="Username" /> ';
echo '<select name="role">' . createRolesOptions() . '</select>';
echo '<button type="submit">Add role for user by username</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="admin.removeRole" />';
echo '<input type="text" name="id" placeholder="ID" /> ';
echo '<select name="role">' . createRolesOptions() . '</select>';
echo '<button type="submit">Remove role for user by ID</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="admin.removeRole" />';
echo '<input type="text" name="email" placeholder="Email" /> ';
echo '<select name="role">' . createRolesOptions() . '</select>';
echo '<button type="submit">Remove role for user by email</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="admin.removeRole" />';
echo '<input type="text" name="username" placeholder="Username" /> ';
echo '<select name="role">' . createRolesOptions() . '</select>';
echo '<button type="submit">Remove role for user by username</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="admin.hasRole" />';
echo '<input type="text" name="id" placeholder="ID" /> ';
echo '<select name="role">' . createRolesOptions() . '</select>';
echo '<button type="submit">Does user have role?</button>';
echo '</form>';
}
function showConfirmEmailForm() {
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="confirmEmail" />';
echo '<input type="text" name="selector" placeholder="Selector" /> ';
echo '<input type="text" name="token" placeholder="Token" /> ';
echo '<select name="login" size="1">';
echo '<option value="0">Sign in automatically? — No</option>';
echo '<option value="1">Sign in automatically? — Yes</option>';
echo '<option value="2">Sign in automatically? — Yes (and remember)</option>';
echo '</select> ';
echo '<button type="submit">Confirm email</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="resendConfirmationForEmail" />';
echo '<input type="text" name="email" placeholder="Email" /> ';
echo '<button type="submit">Re-send confirmation</button>';
echo '</form>';
echo '<form action="" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="action" value="resendConfirmationForUserId" />';
echo '<input type="text" name="userId" placeholder="User ID" /> ';
echo '<button type="submit">Re-send confirmation</button>';
echo '</form>';
}
function createRolesOptions() {
$roleReflection = new ReflectionClass(\Delight\Auth\Role::class);
$out = '';
foreach ($roleReflection->getConstants() as $roleName => $roleValue) {
$out .= '<option value="' . $roleValue . '">' . $roleName . '</option>';
}
return $out;
}