Auth.php
64 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * AAM JWT Authentication handler |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | * @since v5.9.2 |
| 16 | */ |
| 17 | class AAM_Core_Jwt_Auth { |
| 18 | |
| 19 | /** |
| 20 | * Authenticate user with username and password |
| 21 | * |
| 22 | * @param string $username |
| 23 | * @param string $password |
| 24 | * |
| 25 | * @return stdClass |
| 26 | * |
| 27 | * @access public |
| 28 | */ |
| 29 | public function authenticateWithCredentials($username, $password) { |
| 30 | $response = array('error' => true); |
| 31 | |
| 32 | // try to authenticate user with provided credentials |
| 33 | try { |
| 34 | $result = AAM_Core_Login::getInstance()->execute( |
| 35 | array( |
| 36 | 'user_login' => $username, |
| 37 | 'user_password' => $password |
| 38 | ), |
| 39 | false |
| 40 | ); |
| 41 | } catch (Exception $ex) { |
| 42 | $result = array( |
| 43 | 'status' => 'failure', |
| 44 | 'reason' => $ex->getMessage(), |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | if ($result['status'] === 'success') { // generate token |
| 49 | try { |
| 50 | $response = array( |
| 51 | 'status' => 'success', |
| 52 | 'user' => $result['user'] |
| 53 | ); |
| 54 | } catch (Exception $ex) { |
| 55 | $response['reason'] = $ex->getMessage(); |
| 56 | } |
| 57 | } else { |
| 58 | $response['reason'] = $result['reason']; |
| 59 | } |
| 60 | |
| 61 | return (object) $response; |
| 62 | } |
| 63 | |
| 64 | } |