Skip to content

Commit

Permalink
Implement new authentication mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Jan 3, 2024
1 parent c3cff93 commit adb31e8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions config/vufind/Folio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ debug_get_requests = false
; any real/sensitive data before including it in tests!
;json_log_file = "/usr/local/vufind/local/cache/folio.json"

; If set to true, the driver will use the legacy /authn/login method instead of the
; more secure /auth/login-with-expiry method introduced in the Poppy release.
legacy_authentication = true

[IDs]
; Which FOLIO ID is VuFind using as its internal bibliographic ID?
; Options:
Expand Down
27 changes: 25 additions & 2 deletions module/VuFind/src/VuFind/ILS/Driver/Folio.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,16 @@ protected function getDueDate($itemId, $showTime)
return '';
}

/**
* Should we use the legacy authentication mechanism?
*
* @return bool
*/
protected function useLegacyAuthentication(): bool
{
return $this->config['API']['legacy_authentication'] ?? false;
}

/**
* Support method to perform a username/password login to Okapi.
*
Expand All @@ -874,12 +884,13 @@ protected function getDueDate($itemId, $showTime)
*/
protected function performOkapiUsernamePasswordAuthentication(string $username, string $password): Response
{
$newMethod = !($this->config['API']['legacy_authentication'] ?? false);
$tenant = $this->config['API']['tenant'];
$credentials = compact('tenant', 'username', 'password');
// Get token
return $this->makeRequest(
method: 'POST',
path: '/authn/login',
path: $this->useLegacyAuthentication() ? '/authn/login' : '/authn/login-with-expiry',
params: json_encode($credentials),
debugParams: '{"username":"...","password":"..."}'
);
Expand All @@ -895,7 +906,19 @@ protected function performOkapiUsernamePasswordAuthentication(string $username,
*/
protected function extractTokenFromResponse(Response $response): string
{
return $response->getHeaders()->get('X-Okapi-Token')->getFieldValue();
if ($this->useLegacyAuthentication()) {
return $response->getHeaders()->get('X-Okapi-Token')->getFieldValue();
}
$folioUrl = $this->config['API']['base_url'];
$cookies = new \Laminas\Http\Cookies();
$cookies->addCookiesFromResponse($response, $folioUrl);
$results = $cookies->getAllCookies();
foreach ($results as $cookie) {
if ($cookie->getName() == 'folioAccessToken') {
return $cookie->getValue();
}
}
throw new \Exception('Could not find token in response');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class FolioTest extends \PHPUnit\Framework\TestCase
'tenant' => 'config_tenant',
'username' => 'config_username',
'password' => 'config_password',
'legacy_authentication' => 'true',
],
];

Expand Down

0 comments on commit adb31e8

Please sign in to comment.