Skip to content

Commit

Permalink
MDL-65518 badges: Retry auth test
Browse files Browse the repository at this point in the history
When testing the site backpack login credentials, don't rely on a previous login
because the username and password could have changed.
  • Loading branch information
Damyon Wiese committed May 9, 2019
1 parent c659b1c commit 2fe3453
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions badges/classes/backpack_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,21 @@ public function put_issuer($data) {
return $this->curl_request('issuers', null, null, $data);
}

/**
* Delete any user access tokens in the session so we will attempt to get new ones.
*
* @return void
*/
public function clear_system_user_session() {
global $SESSION;

$useridkey = $this->get_token_key(BADGE_USER_ID_TOKEN);
unset($SESSION->$useridkey);

$expireskey = $this->get_token_key(BADGE_EXPIRES_TOKEN);
unset($SESSION->$expireskey);
}

/**
* Authenticate using the stored email and password and save the valid access tokens.
*
Expand Down
7 changes: 6 additions & 1 deletion lib/badgeslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,8 @@ function badge_assemble_notification(stdClass $badge) {

/**
* Attempt to authenticate with the site backpack credentials and return an error
* if the authentication fails.
* if the authentication fails. If external backpacks are not enabled, this will
* not perform any test.
*
* @return string
*/
Expand All @@ -1189,6 +1190,10 @@ function badges_verify_site_backpack() {
if (empty($backpack->apiversion) || ($backpack->apiversion == OPEN_BADGES_V2)) {
$backpackapi = new \core_badges\backpack_api($backpack);

// Clear any cached access tokens in the session.
$backpackapi->clear_system_user_session();

// Now attempt a login with these credentials.
$result = $backpackapi->authenticate();
if ($result === false || !empty($result->error)) {
$warning = $backpackapi->get_authentication_error();
Expand Down

0 comments on commit 2fe3453

Please sign in to comment.