I am currently running a custom plugin with an action on the wp_login hook. However even though i got a token granted with client credentials through the php sdk on top of the wordpress plugin to work, it of course was not linked to the logged in user. How do i get an encrypted jwt for the logged in user with the sdk? I tried the getAccessToken function but that gives me a JWE token.
Here is my current code using client credential.
add_action('wp_login', 'update_roles', 10, 2);
function update_roles($user_login, $user) {
// Define the API endpoint
$api_url = [REDACTED];
// Check if the user login action is triggering
error_log('update_roles function triggered for user: ' . $user_login);
$plugin = wpAuth0(); // Returns an instance of Auth0\WordPress\Plugin
$sdk = $plugin->getSdk(); // Returns an instance of Auth0\SDK\Auth0
try {
// Request the OAuth token
$jwtRequest = $sdk->authentication()->oauthToken('client_credentials', array('audience' => [REDACTED]));
// Check if the response contains the token
if (is_object($jwtRequest) && method_exists($jwtRequest, 'getBody')) {
$response_body = json_decode($jwtRequest->getBody(), true);
// Print the response body for debugging
error_log('Token response body: ' . print_r($response_body, true));
// Ensure the access_token exists in the response body
if (isset($response_body['access_token'])) {
$token = $response_body['access_token'];
} else {
error_log('No access_token found in the response.');
return;
}
} else {
error_log('Invalid response object returned.');
return;
}
} catch (ArgumentException $e) {
error_log('ArgumentException: ' . $e->getMessage());
} catch (ConfigurationException $e) {
error_log('ConfigurationException: ' . $e->getMessage());
} catch (NetworkException $e) {
error_log('NetworkException: ' . $e->getMessage());
}
// Log the token for debugging
if ($token) {
error_log('Token retrieved: ' . $token);
} else {
error_log('No token found');
}
}
// Code for updating roles based on external api response
I am running the newest version of the auth0 wp plugin.