Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIMS-933: Attach auth header to shipping service requests #675

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/config_sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@
# Shipping service details
$use_shipping_service = null;
$use_shipping_service_incoming_shipments = null;
$shipping_service_url = null;
$shipping_service_api_url = null;
$shipping_service_api_user = null;
$shipping_service_api_password = null;
$shipping_service_app_url = null;
$shipping_service_links_in_emails = null;


Expand Down
6 changes: 3 additions & 3 deletions api/src/Page/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,10 +927,10 @@ function _dispatch_dewar_in_shipping_service($dispatch_info, $dewar)
global $facility_country;
global $facility_phone;
global $facility_contact;
global $shipping_service_url;
global $shipping_service_api_url;
global $facility_email;
if (!isset($shipping_service_url)) {
throw new Exception("Could not send request to shipping service: shipping_service_url not set");
if (!isset($shipping_service_api_url)) {
throw new Exception("Could not send request to shipping service: shipping_service_api_url not set");
}

# Create shipment
Expand Down
40 changes: 32 additions & 8 deletions api/src/Shipment/ShippingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,53 @@
class ShippingService
{
private $shipping_api_url;
private $headers;
private $shipping_app_url;
public const JOURNEY_TO_FACILITY = "TO_FACILITY";
public const JOURNEY_FROM_FACILITY = "FROM_FACILITY";

function __construct()
function _build_headers()
{
global $shipping_service_url;
$this->shipping_api_url = $shipping_service_url;
$this->headers = array(
global $cookie_key;
global $shipping_service_api_user;
global $shipping_service_api_password;

$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);

if (isset($_COOKIE[$cookie_key])) {
array_push($headers, "Authorization: Bearer {$_COOKIE[$cookie_key]}");
return $headers;
}

if (isset($shipping_service_api_user) && isset($shipping_service_api_password)) {
$basic_auth = base64_encode($shipping_service_api_user . ":" . $shipping_service_api_password);
array_push($headers, "Authorization: Basic {$basic_auth}");
return $headers;
}

throw new \Exception("Shipping service auth error: no cookie found and basic auth credentials unset");
}

function __construct()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constructing this object will fail if the setting are not set and might fail if they are null. Either:

  • make sure this is not constructed if the shipping service is not being used
  • make sure the constructor doesn't do anything if the shipping service is not used

{
global $shipping_service_api_url;
global $shipping_service_app_url;
$this->shipping_api_url = $shipping_service_api_url;
$this->shipping_app_url = $shipping_service_app_url;
}


function _send_request($url, $type, $data, $expected_status_code)
{
$ch = curl_init($url);
$base_headers = $this->_build_headers();
curl_setopt_array(
$ch,
array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => $this->headers,
CURLOPT_HTTPHEADER => $base_headers,
CURLOPT_TIMEOUT => 5
)
);
Expand All @@ -52,7 +76,7 @@ function _send_request($url, $type, $data, $expected_status_code)
$ch,
array(
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => array_merge($this->headers, array('Content-Length: ' . strlen(json_encode($data)))),
CURLOPT_HTTPHEADER => array_merge($base_headers, array('Content-Length: ' . strlen(json_encode($data)))),
CURLOPT_POSTFIELDS => json_encode($data),
)
);
Expand Down Expand Up @@ -125,6 +149,6 @@ function dispatch_shipment($shipment_id, $pickup_requested)

function get_awb_pdf_url($shipment_id)
{
return $this->shipping_api_url . '/shipments/' . $shipment_id . '/awb/pdf';
return $this->shipping_app_url . '/shipments/' . $shipment_id . '/awb';
}
}
Loading