Skip to content

Commit

Permalink
Refactor existing logic for consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Jan 3, 2024
1 parent 2de49b5 commit c3cff93
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
64 changes: 42 additions & 22 deletions module/VuFind/src/VuFind/ILS/Driver/Folio.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use DateTime;
use DateTimeZone;
use Exception;
use Laminas\Http\Response;
use VuFind\Exception\ILS as ILSException;
use VuFind\I18n\Translator\TranslatorAwareInterface;
use VuFindHttp\HttpServiceAwareInterface as HttpServiceAwareInterface;
Expand Down Expand Up @@ -250,21 +251,14 @@ public function preRequest(\Laminas\Http\Headers $headers, $params)
protected function renewTenantToken()
{
$this->token = null;
$auth = [
'username' => $this->config['API']['username'],
'password' => $this->config['API']['password'],
];
$response = $this->makeRequest(
method: 'POST',
path: '/authn/login',
params: json_encode($auth),
debugParams: '{"username":"...","password":"..."}'
$response = $this->performOkapiUsernamePasswordAuthentication(
$this->config['API']['username'],
$this->config['API']['password']
);
$this->token = $response->getHeaders()->get('X-Okapi-Token')
->getFieldValue();
$this->token = $this->extractTokenFromResponse($response);
$this->sessionCache->folio_token = $this->token;
$this->debug(
'Token renewed. Tenant: ' . $auth['username'] .
'Token renewed. Username: ' . $this->config['API']['username'] .
' Token: ' . substr($this->token, 0, 30) . '...'
);
}
Expand Down Expand Up @@ -870,6 +864,40 @@ protected function getDueDate($itemId, $showTime)
return '';
}

/**
* Support method to perform a username/password login to Okapi.
*
* @param string $username The patron username
* @param string $password The patron password
*
* @return Response
*/
protected function performOkapiUsernamePasswordAuthentication(string $username, string $password): Response
{
$tenant = $this->config['API']['tenant'];
$credentials = compact('tenant', 'username', 'password');
// Get token
return $this->makeRequest(
method: 'POST',
path: '/authn/login',
params: json_encode($credentials),
debugParams: '{"username":"...","password":"..."}'
);
}

/**
* Given a response from performOkapiUsernamePasswordAuthentication(),
* extract the token value.
*
* @param Response $response Response from performOkapiUsernamePasswordAuthentication().
*
* @return string
*/
protected function extractTokenFromResponse(Response $response): string
{
return $response->getHeaders()->get('X-Okapi-Token')->getFieldValue();
}

/**
* Support method for patronLogin(): authenticate the patron with an Okapi
* login attempt. Returns a CQL query for retrieving more information about
Expand All @@ -882,22 +910,14 @@ protected function getDueDate($itemId, $showTime)
*/
protected function patronLoginWithOkapi($username, $password)
{
$tenant = $this->config['API']['tenant'];
$credentials = compact('tenant', 'username', 'password');
// Get token
$response = $this->makeRequest(
'POST',
'/authn/login',
json_encode($credentials)
);
$response = $this->performOkapiUsernamePasswordAuthentication($username, $password);
$debugMsg = 'User logged in. User: ' . $username . '.';
// We've authenticated the user with Okapi, but we only have their
// username; set up a query to retrieve full info below.
$query = 'username == ' . $username;
// Replace admin with user as tenant if configured to do so:
if ($this->config['User']['use_user_token'] ?? false) {
$this->token = $response->getHeaders()->get('X-Okapi-Token')
->getFieldValue();
$this->token = $this->extractTokenFromResponse($response);
$debugMsg .= ' Token: ' . substr($this->token, 0, 30) . '...';
}
$this->debug($debugMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"comment": "send new token",
"expectedMethod": "POST",
"expectedPath": "/authn/login",
"expectedParams": "{\"username\":\"config_username\",\"password\":\"config_password\"}",
"expectedParams": "{\"tenant\":\"config_tenant\",\"username\":\"config_username\",\"password\":\"config_password\"}",
"headers": { "X-Okapi-Token": "x-okapi-token-after-invalid" }
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"query": "id == \"foo\""
},
"status": 200,
"body": "{\n \"users\": [\n{\"username\":\"fake@example.com\",\"id\":\"foo\",\"externalSystemId\":\"fakefake\",\"barcode\":\"fakefakefake\",\"active\":true,\"patronGroup\":\"fakegroup\",\"departments\":[],\"proxyFor\":[],\"personal\":{\"lastName\":\"User\",\"firstName\":\"Test\",\"middleName\":\"\",\"email\":\"fake@example.com\",\"phone\":\"0123456789\",\"mobilePhone\":\"1234567890\",\"addresses\":[{\"addressLine1\": \"street\", \"city\": \"city\", \"countryId\": \"country\", \"postalCode\": \"12345\"}],\"preferredContactTypeId\":\"002\"},\"expirationDate\":\"2030-05-30T00:00:00.000+00:00\",\"createdDate\":\"2023-02-06T15:05:26.941+00:00\",\"updatedDate\":\"2023-02-06T15:05:26.941+00:00\",\"metadata\":{\"createdDate\":\"2021-03-15T20:41:34.806+00:00\",\"createdByUserId\":\"not-real\",\"updatedDate\":\"2023-02-06T15:05:26.935+00:00\",\"updatedByUserId\":\"also-not-real\"},\"customFields\":{\"employeeClass\":\"Staff\",\"title\":\"Mr.\",\"employeeStatus\":\"Full Time\",\"employeeDepartment\":\"Someplace\"}}],\n \"totalRecords\": 1,\n \"resultInfo\": {\"totalRecords\":1,\"facets\":[],\"diagnostics\":[]}\n}"
"body": "{\n \"users\": [\n{\"tenant\":\"config_tenant\",\"username\":\"fake@example.com\",\"id\":\"foo\",\"externalSystemId\":\"fakefake\",\"barcode\":\"fakefakefake\",\"active\":true,\"patronGroup\":\"fakegroup\",\"departments\":[],\"proxyFor\":[],\"personal\":{\"lastName\":\"User\",\"firstName\":\"Test\",\"middleName\":\"\",\"email\":\"fake@example.com\",\"phone\":\"0123456789\",\"mobilePhone\":\"1234567890\",\"addresses\":[{\"addressLine1\": \"street\", \"city\": \"city\", \"countryId\": \"country\", \"postalCode\": \"12345\"}],\"preferredContactTypeId\":\"002\"},\"expirationDate\":\"2030-05-30T00:00:00.000+00:00\",\"createdDate\":\"2023-02-06T15:05:26.941+00:00\",\"updatedDate\":\"2023-02-06T15:05:26.941+00:00\",\"metadata\":{\"createdDate\":\"2021-03-15T20:41:34.806+00:00\",\"createdByUserId\":\"not-real\",\"updatedDate\":\"2023-02-06T15:05:26.935+00:00\",\"updatedByUserId\":\"also-not-real\"},\"customFields\":{\"employeeClass\":\"Staff\",\"title\":\"Mr.\",\"employeeStatus\":\"Full Time\",\"employeeDepartment\":\"Someplace\"}}],\n \"totalRecords\": 1,\n \"resultInfo\": {\"totalRecords\":1,\"facets\":[],\"diagnostics\":[]}\n}"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"query": "id == \"fakeid\""
},
"status": 200,
"body": "{\n \"users\": [\n{\"username\":\"fake@example.com\",\"id\":\"fakeid\",\"externalSystemId\":\"fake\",\"barcode\":\"fakefakefake\",\"active\":true,\"patronGroup\":\"fakegroup\",\"departments\":[],\"proxyFor\":[],\"personal\":{\"lastName\":\"Lastname\",\"firstName\":\"Proxity\",\"middleName\":\"P.\",\"email\":\"fake@example.com\",\"phone\":\"0123456789\",\"addresses\":[],\"preferredContactTypeId\":\"002\"},\"expirationDate\":\"2030-05-30T00:00:00.000+00:00\",\"createdDate\":\"2023-02-08T20:44:02.690+00:00\",\"updatedDate\":\"2023-02-08T20:44:02.690+00:00\",\"metadata\":{\"createdDate\":\"2021-12-14T19:23:51.863+00:00\",\"createdByUserId\":\"zzz\",\"updatedDate\":\"2023-02-08T20:44:02.681+00:00\",\"updatedByUserId\":\"yyy\"},\"customFields\":{\"employeeClass\":\"Staff\",\"title\":\"Mr.\",\"employeeStatus\":\"Full Time\",\"employeeDepartment\":\"Somewhere\",\"studentGraduationStatus\":\"AW\"}}],\n \"totalRecords\": 1,\n \"resultInfo\": {\"totalRecords\":1,\"facets\":[],\"diagnostics\":[]}\n}"
"body": "{\n \"users\": [\n{\"tenant\":\"config_tenant\",\"username\":\"fake@example.com\",\"id\":\"fakeid\",\"externalSystemId\":\"fake\",\"barcode\":\"fakefakefake\",\"active\":true,\"patronGroup\":\"fakegroup\",\"departments\":[],\"proxyFor\":[],\"personal\":{\"lastName\":\"Lastname\",\"firstName\":\"Proxity\",\"middleName\":\"P.\",\"email\":\"fake@example.com\",\"phone\":\"0123456789\",\"addresses\":[],\"preferredContactTypeId\":\"002\"},\"expirationDate\":\"2030-05-30T00:00:00.000+00:00\",\"createdDate\":\"2023-02-08T20:44:02.690+00:00\",\"updatedDate\":\"2023-02-08T20:44:02.690+00:00\",\"metadata\":{\"createdDate\":\"2021-12-14T19:23:51.863+00:00\",\"createdByUserId\":\"zzz\",\"updatedDate\":\"2023-02-08T20:44:02.681+00:00\",\"updatedByUserId\":\"yyy\"},\"customFields\":{\"employeeClass\":\"Staff\",\"title\":\"Mr.\",\"employeeStatus\":\"Full Time\",\"employeeDepartment\":\"Somewhere\",\"studentGraduationStatus\":\"AW\"}}],\n \"totalRecords\": 1,\n \"resultInfo\": {\"totalRecords\":1,\"facets\":[],\"diagnostics\":[]}\n}"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"query": "id == \"foo\""
},
"status": 200,
"body": "{\n \"users\": [\n{\"username\":\"fake@example.com\",\"id\":\"foo\",\"externalSystemId\":\"fake\",\"barcode\":\"fakefakefake\",\"active\":true,\"patronGroup\":\"fakegroup\",\"departments\":[],\"proxyFor\":[],\"personal\":{\"lastName\":\"Lastname\",\"firstName\":\"Proxity\",\"middleName\":\"P.\",\"email\":\"fake@example.com\",\"phone\":\"0123456789\",\"addresses\":[],\"preferredContactTypeId\":\"002\"},\"expirationDate\":\"2030-05-30T00:00:00.000+00:00\",\"createdDate\":\"2023-02-08T20:44:02.690+00:00\",\"updatedDate\":\"2023-02-08T20:44:02.690+00:00\",\"metadata\":{\"createdDate\":\"2021-12-14T19:23:51.863+00:00\",\"createdByUserId\":\"zzz\",\"updatedDate\":\"2023-02-08T20:44:02.681+00:00\",\"updatedByUserId\":\"yyy\"},\"customFields\":{\"employeeClass\":\"Staff\",\"title\":\"Mr.\",\"employeeStatus\":\"Full Time\",\"employeeDepartment\":\"Somewhere\",\"studentGraduationStatus\":\"AW\"}}],\n \"totalRecords\": 1,\n \"resultInfo\": {\"totalRecords\":1,\"facets\":[],\"diagnostics\":[]}\n}"
"body": "{\n \"users\": [\n{\"tenant\":\"config_tenant\",\"username\":\"fake@example.com\",\"id\":\"foo\",\"externalSystemId\":\"fake\",\"barcode\":\"fakefakefake\",\"active\":true,\"patronGroup\":\"fakegroup\",\"departments\":[],\"proxyFor\":[],\"personal\":{\"lastName\":\"Lastname\",\"firstName\":\"Proxity\",\"middleName\":\"P.\",\"email\":\"fake@example.com\",\"phone\":\"0123456789\",\"addresses\":[],\"preferredContactTypeId\":\"002\"},\"expirationDate\":\"2030-05-30T00:00:00.000+00:00\",\"createdDate\":\"2023-02-08T20:44:02.690+00:00\",\"updatedDate\":\"2023-02-08T20:44:02.690+00:00\",\"metadata\":{\"createdDate\":\"2021-12-14T19:23:51.863+00:00\",\"createdByUserId\":\"zzz\",\"updatedDate\":\"2023-02-08T20:44:02.681+00:00\",\"updatedByUserId\":\"yyy\"},\"customFields\":{\"employeeClass\":\"Staff\",\"title\":\"Mr.\",\"employeeStatus\":\"Full Time\",\"employeeDepartment\":\"Somewhere\",\"studentGraduationStatus\":\"AW\"}}],\n \"totalRecords\": 1,\n \"resultInfo\": {\"totalRecords\":1,\"facets\":[],\"diagnostics\":[]}\n}"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"comment": "Initial request for token",
"expectedMethod": "POST",
"expectedParams": "{\"username\":\"config_username\",\"password\":\"config_password\"}",
"expectedParams": "{\"tenant\":\"config_tenant\",\"username\":\"config_username\",\"password\":\"config_password\"}",
"expectedPath": "/authn/login",
"expectedHeaders": { "X-Okapi-Tenant": "config_tenant" },
"headers": { "X-Okapi-Token": "x-okapi-token-config-tenant" }
Expand Down

0 comments on commit c3cff93

Please sign in to comment.