0
0
Wordpressframework~20 mins

Authentication for API in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Authentication Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main purpose of using OAuth2 in WordPress REST API authentication?

OAuth2 is a common method for API authentication. What does it mainly help with in WordPress REST API?

AIt allows users to authenticate without sharing their passwords directly with the client app.
BIt automatically encrypts all API data sent between client and server.
CIt disables all other authentication methods to improve security.
DIt stores user passwords in a special encrypted database.
Attempts:
2 left
💡 Hint

Think about how OAuth2 helps users keep their passwords safe when using third-party apps.

component_behavior
intermediate
2:00remaining
What happens when you send a REST API request to WordPress without authentication for a protected route?

Consider a WordPress REST API route that requires authentication. What will the server respond if you call it without any authentication?

AThe server crashes and stops responding.
BThe server returns the requested data without any restrictions.
CThe server returns a 401 Unauthorized error response.
DThe server returns a 404 Not Found error response.
Attempts:
2 left
💡 Hint

Think about what HTTP status code means 'you need to log in first'.

📝 Syntax
advanced
2:30remaining
Which code snippet correctly adds Basic Authentication support to WordPress REST API?

Choose the code snippet that properly enables Basic Authentication for WordPress REST API requests.

Aadd_filter('rest_authentication_errors', function($result) { if (empty($_SERVER['PHP_AUTH_USER'])) { return new WP_Error('rest_not_logged_in', 'You are not logged in.', array('status' => 401)); } return $result; });
Badd_action('rest_authentication_errors', function($result) { if (!empty($_SERVER['PHP_AUTH_USER'])) { return true; } return $result; });
Cadd_filter('rest_authentication_errors', function($result) { if (!empty($_SERVER['PHP_AUTH_USER'])) { return true; } return $result; });
Dadd_filter('rest_authentication_errors', function() { return true; });
Attempts:
2 left
💡 Hint

Look for the filter hook that checks if the PHP_AUTH_USER is empty and returns an error if so.

state_output
advanced
2:00remaining
What is the output of this WordPress REST API authentication check code?

Given the following code snippet inside a REST API callback, what will be the output if the user is not logged in?

Wordpress
<?php
function my_api_callback() {
  if (!is_user_logged_in()) {
    return new WP_Error('rest_forbidden', 'You must be logged in.', array('status' => 403));
  }
  return array('message' => 'Welcome!');
}
?>
APHP Fatal error: Call to undefined function is_user_logged_in()
B{"message":"Welcome!"}
Cnull
D{"code":"rest_forbidden","message":"You must be logged in.","data":{"status":403}}
Attempts:
2 left
💡 Hint

Think about what happens when the user is not logged in and the function returns a WP_Error.

🔧 Debug
expert
3:00remaining
Why does this WordPress REST API authentication code always allow access even without credentials?

Examine the code below. It is intended to restrict access to authenticated users only. Why does it fail to do so?

add_filter('rest_authentication_errors', function($result) {
  if (!empty($_SERVER['PHP_AUTH_USER'])) {
    return true;
  }
  return $result;
});
AThe filter should return false to block unauthenticated users, not true.
BReturning true from 'rest_authentication_errors' means no error, so unauthenticated users are allowed access.
CReturning true from 'rest_authentication_errors' means authentication passed, so unauthenticated users are blocked correctly.
DThe code is missing a check for $_SERVER['PHP_AUTH_PW'], so it always fails.
Attempts:
2 left
💡 Hint

Check what returning true or an error from this filter means for access control.