Providers
2 weeks ago
CallbackHandler.php
2 weeks ago
MfaApiClient.php
2 weeks ago
MfaFlowSendCode.php
2 weeks ago
MfaProviderRegistry.php
2 weeks ago
MfaRestApi.php
2 weeks ago
SessionStore.php
2 weeks ago
MfaRestApi.php
124 lines
| 1 | <?php |
| 2 | |
| 3 | namespace LLAR\Core\MfaFlow; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * REST API for MFA flow: send-code endpoint (fallback is AJAX via admin-ajax.php). |
| 11 | */ |
| 12 | class MfaRestApi { |
| 13 | |
| 14 | const REST_NAMESPACE = 'llar/v1'; |
| 15 | const SEND_CODE_ROUTE = 'mfa/send-code'; |
| 16 | |
| 17 | /** |
| 18 | * Register REST routes on rest_api_init. |
| 19 | */ |
| 20 | public static function register() { |
| 21 | add_action( 'rest_api_init', array( __CLASS__, 'register_routes' ) ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Register REST routes. |
| 26 | */ |
| 27 | public static function register_routes() { |
| 28 | register_rest_route( |
| 29 | self::REST_NAMESPACE, |
| 30 | self::SEND_CODE_ROUTE, |
| 31 | array( |
| 32 | 'methods' => 'POST', |
| 33 | 'callback' => array( __CLASS__, 'send_code_callback' ), |
| 34 | 'permission_callback' => '__return_true', |
| 35 | 'args' => array( |
| 36 | 'token' => array( |
| 37 | 'required' => true, |
| 38 | 'type' => 'string', |
| 39 | 'sanitize_callback' => 'sanitize_text_field', |
| 40 | ), |
| 41 | 'secret' => array( |
| 42 | 'required' => true, |
| 43 | 'type' => 'string', |
| 44 | 'sanitize_callback' => 'sanitize_text_field', |
| 45 | ), |
| 46 | 'code' => array( |
| 47 | 'required' => false, |
| 48 | 'type' => 'string', |
| 49 | 'sanitize_callback' => 'sanitize_text_field', |
| 50 | ), |
| 51 | 'ip' => array( |
| 52 | 'required' => false, |
| 53 | 'type' => 'string', |
| 54 | 'sanitize_callback' => 'sanitize_text_field', |
| 55 | ), |
| 56 | 'browser' => array( |
| 57 | 'required' => false, |
| 58 | 'type' => 'string', |
| 59 | 'sanitize_callback' => 'sanitize_text_field', |
| 60 | ), |
| 61 | 'location' => array( |
| 62 | 'required' => false, |
| 63 | 'type' => 'string', |
| 64 | 'sanitize_callback' => 'sanitize_text_field', |
| 65 | ), |
| 66 | ), |
| 67 | ) |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * REST callback: MFA send-code. POST only (token, secret, code in request body). |
| 73 | * |
| 74 | * @param \WP_REST_Request $request Request object. |
| 75 | * @return \WP_REST_Response |
| 76 | */ |
| 77 | public static function send_code_callback( $request ) { |
| 78 | $token = $request->get_param( 'token' ); |
| 79 | $secret = $request->get_param( 'secret' ); |
| 80 | $code = $request->get_param( 'code' ); |
| 81 | $code = is_string( $code ) ? $code : ''; |
| 82 | $ip = $request->get_param( 'ip' ); |
| 83 | $browser = $request->get_param( 'browser' ); |
| 84 | $location = $request->get_param( 'location' ); |
| 85 | $context = array( |
| 86 | 'ip' => is_string( $ip ) ? $ip : '', |
| 87 | 'browser' => is_string( $browser ) ? $browser : '', |
| 88 | 'location' => is_string( $location ) ? $location : '', |
| 89 | ); |
| 90 | |
| 91 | if ( '' === $token || '' === $secret ) { |
| 92 | return new \WP_REST_Response( |
| 93 | array( |
| 94 | 'success' => false, |
| 95 | 'message' => 'Forbidden', |
| 96 | ), |
| 97 | 403 |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | $result = MfaFlowSendCode::execute( $token, $secret, $code, $context ); |
| 102 | |
| 103 | $status = isset( $result['http_status'] ) ? (int) $result['http_status'] : 200; |
| 104 | $body = array( |
| 105 | 'success' => (bool) $result['success'], |
| 106 | ); |
| 107 | if ( ! empty( $result['message'] ) ) { |
| 108 | $body['message'] = $result['message']; |
| 109 | } |
| 110 | |
| 111 | return new \WP_REST_Response( $body, $status ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Build REST URL for send-code (POST). Used in handshake as primary send_email_url. |
| 116 | * No query args; the MFA app must POST token, secret, and code in the request body. |
| 117 | * |
| 118 | * @return string |
| 119 | */ |
| 120 | public static function get_send_code_rest_url() { |
| 121 | return rest_url( self::REST_NAMESPACE . '/' . self::SEND_CODE_ROUTE ); |
| 122 | } |
| 123 | } |
| 124 |