queue = $queue; $this->limiter = $limiter; } /** * {@inheritdoc} */ public function register_routes(): void { register_rest_route( $this->namespace, '/check-in', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'check_in' ), 'permission_callback' => '__return_true', // Public on purpose: candidates have no account. Rate limited and matched against the application record. 'args' => array( 'session_id' => array( 'type' => 'integer', 'required' => true, 'minimum' => 1, 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', ), 'name' => array( 'type' => 'string', 'required' => true, 'maxLength' => 190, 'validate_callback' => 'rest_validate_request_arg', ), 'email' => array( 'type' => 'string', 'required' => true, 'maxLength' => 190, 'validate_callback' => 'rest_validate_request_arg', ), 'phone' => array( 'type' => 'string', 'required' => true, 'maxLength' => 30, 'validate_callback' => 'rest_validate_request_arg', ), ), ) ); foreach ( array( '/state' => array( WP_REST_Server::READABLE, 'state' ), '/requeue' => array( WP_REST_Server::CREATABLE, 'requeue' ), '/join' => array( WP_REST_Server::CREATABLE, 'join' ), ) as $path => $route ) { register_rest_route( $this->namespace, $path, array( 'methods' => $route[0], 'callback' => array( $this, $route[1] ), 'permission_callback' => '__return_true', // Public on purpose: protected by the candidate token header. ) ); } } /** * POST /check-in * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function check_in( WP_REST_Request $request ) { if ( $this->limiter->blocked( 'checkin_failure', self::FAILURE_LIMIT, self::WINDOW ) || ! $this->limiter->hit( 'checkin_all', self::CHECKIN_LIMIT, self::WINDOW ) ) { return $this->error( 'rate_limited', __( 'Too many verification attempts. Please try again in 15 minutes or contact the recruitment team.', 'interviewnea' ), 429 ); } $name = sanitize_text_field( (string) $request->get_param( 'name' ) ); $email = sanitize_email( (string) $request->get_param( 'email' ) ); $phone = sanitize_text_field( (string) $request->get_param( 'phone' ) ); if ( ! Identity::valid_name( $name ) || '' === $email || ! is_email( $email ) || ! Identity::phone_valid( $phone ) ) { return $this->error( 'invalid_fields', __( 'Enter your valid full name, application email and phone number.', 'interviewnea' ), 400 ); } $result = $this->queue->check_in( absint( $request->get_param( 'session_id' ) ), $name, $email, $phone ); if ( is_wp_error( $result ) && 'interviewnea_verification_failed' === $result->get_error_code() ) { $this->limiter->record( 'checkin_failure', self::WINDOW ); } return $this->respond( $result ); } /** * GET /state * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function state( WP_REST_Request $request ) { return $this->with_token( $request, array( $this->queue, 'state' ) ); } /** * POST /requeue * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function requeue( WP_REST_Request $request ) { return $this->with_token( $request, array( $this->queue, 'requeue' ) ); } /** * POST /join * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function join( WP_REST_Request $request ) { return $this->with_token( $request, array( $this->queue, 'join' ) ); } /** * Runs a token route, counting unknown tokens against the visitor. * * @param WP_REST_Request $request Request. * @param callable $callback Queue method taking the token. * @return \WP_REST_Response|\WP_Error */ private function with_token( WP_REST_Request $request, callable $callback ) { if ( $this->limiter->blocked( 'bad_token', self::BAD_TOKEN_LIMIT, self::WINDOW ) ) { return $this->error( 'rate_limited', __( 'Too many requests. Please wait a few minutes and try again.', 'interviewnea' ), 429 ); } $token = $request->get_header( self::TOKEN_HEADER ); $token = is_string( $token ) ? trim( $token ) : ''; $result = call_user_func( $callback, $token ); if ( is_wp_error( $result ) && 'interviewnea_unauthorized' === $result->get_error_code() ) { $this->limiter->record( 'bad_token', self::WINDOW ); } return $this->respond( $result ); } }