Skip to content

Commit

Permalink
Merge pull request #642 from DiamondLightSource/pre-release/2023-R3.3
Browse files Browse the repository at this point in the history
Pre release/2023 r3.3
  • Loading branch information
MattPrit authored Aug 15, 2023
2 parents 5f7083b + 8415438 commit d0df7be
Show file tree
Hide file tree
Showing 34 changed files with 16,394 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,16 @@ jobs:
- name: Checkout code
uses: actions/checkout@v3

- name: Use Node.js 18
uses: actions/setup-node@v3
with:
node-version: 18.x
- name: JavaScript build, lint and test
working-directory: ./client
# hack the output from the linting steps to avoid these stopping the builds - we are not going to get
# to a clean output without considerable effort, but it's useful to see the output
run: |
node --version
npm ci
npm run build
npm run test
Expand Down
5 changes: 5 additions & 0 deletions api/config_sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
$isb = array('user' => 'user', 'pass' => 'pass', 'db' => 'localhost/ispyb');
$dbtype = 'mysql';

# Summary Database credentials
######### DELETE if not using connection.
$summarydbconfig = array('user' => 'user', 'pass' => 'pass', 'db' => 'localhost/ispyb');
$ifsummary = true;

# Encoded JWT key, used to sign and check validaty of jwt tokens
# - Create one of these using /api/authenticate/key
# This can be changed to invalidate all currently active tokens
Expand Down
12 changes: 10 additions & 2 deletions api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function setupApplication($mode): Slim
global $motd, $authentication_type, $cas_url, $cas_sso, $sso_url, $package_description,
$facility_courier_countries, $facility_courier_countries_nde,
$dhl_enable, $dhl_link, $scale_grid, $scale_grid_end_date, $preset_proposal, $timezone,
$valid_components, $enabled_container_types;
$valid_components, $enabled_container_types, $ifsummary;
$app->contentType('application/json');
$app->response()->body(json_encode(array(
'motd' => $motd,
Expand All @@ -87,7 +87,8 @@ function setupApplication($mode): Slim
'preset_proposal' => $preset_proposal,
'timezone' => $timezone,
'valid_components' => $valid_components,
'enabled_container_types' => $enabled_container_types
'enabled_container_types' => $enabled_container_types,
'ifsummary' => $ifsummary
)));
});
return $app;
Expand All @@ -102,6 +103,13 @@ function setupDependencyInjectionContainer($app)
return $db;
});

$app->container->singleton('dbsummary', function () use ($app): DatabaseParent {
$dbFactory = new DatabaseFactory(new DatabaseConnectionFactory());
$db = $dbFactory->get("summary");
$db->set_app($app);
return $db;
});

$app->container->singleton('authData', function () use ($app) {
return new AuthenticationData($app->container['db']);
});
Expand Down
29 changes: 20 additions & 9 deletions api/src/Authentication/Type/OIDC.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ function __construct() {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

$newProviderConfig = json_decode($response);
$newProviderConfig->b64ClientCreds = base64_encode(
$oidc_client_id . ":" . $oidc_client_secret
);

if($newProviderConfig == null) {
if(!$newProviderConfig
|| !isset($newProviderConfig->userinfo_endpoint)
|| !isset($newProviderConfig->authorization_endpoint)
|| !isset($newProviderConfig->token_endpoint)) {
error_log("OIDC Authentication provider replied with invalid JSON body");
return;
}
$newProviderConfig->b64ClientCreds = base64_encode(
$oidc_client_id . ":" . $oidc_client_secret
);

$this->providerConfig = $newProviderConfig;
}
Expand All @@ -43,9 +45,12 @@ private function getUser($token)
$response = curl_exec($ch);
curl_close($ch);

$fedid = json_decode($response)->id;
$response_json = json_decode($response);
if (!$response_json || !isset($response_json->id)) {
return false;
}

return $fedid;
return $response_json->id;
}

function authenticate($login, $password)
Expand Down Expand Up @@ -92,8 +97,14 @@ function authenticateByCode($code)
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . $this->providerConfig->b64ClientCreds));
$response = curl_exec($ch);
curl_close($ch);

$token = json_decode($response)->access_token;

$response_json = json_decode($response);
if (!$response_json || !isset($response_json->access_token)) {
error_log("Invalid authentication attempt, provider returned invalid response");
return false;
}

$token = $response_json->access_token;

if(!$token) {
error_log("Invalid authentication attempt, provider returned no access token");
Expand Down
11 changes: 11 additions & 0 deletions api/src/Database/DatabaseConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class DatabaseConnectionFactory
public function get($databaseType)
{
global $isb;
global $summarydbconfig;

if (!$databaseType) {
error_log('Database type variable, dbtype, is not specified in config.php - defaulting to MySql.');
Expand All @@ -24,6 +25,16 @@ public function get($databaseType)
$conn = new \mysqli($host, $isb['user'], $isb['pass'], $dbn, $port);
$conn->set_charset("utf8mb4");
}
elseif ($databaseType == 'PureMySQL') {
$port = array_key_exists('port', $summarydbconfig) ? $summarydbconfig['port'] : null;
if (!$port) {
$port = ini_get("mysqli.default_port");
}
list($host, $dbn) = explode('/', $summarydbconfig['db']);
$conn = new \mysqli($host, $summarydbconfig['user'], $summarydbconfig['pass'], $dbn, $port);
$conn->set_charset("utf8mb4");
}


if ($conn == null) {
Utils::returnError("Database Configuration Error", "Database connection for type '$databaseType' does not exist.");
Expand Down
27 changes: 15 additions & 12 deletions api/src/Database/DatabaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class DatabaseFactory
// Value is class name in SynchWeb\Database\Type namespace.
// Key is lower case representation of class name.
public $database_types = array(
'mysql' => 'MySQL'
'mysql' => ["dbClassName" =>'MySQL', "dataConnectionName" => 'MySQL'],
'summary' => ["dbClassName" =>'PureMySQL', "dataConnectionName" => 'PureMySQL']
);

function __construct($databaseConnectionFactory)
Expand All @@ -19,25 +20,27 @@ function __construct($databaseConnectionFactory)

private $databaseConnectionFactory;

public function get()
{
// Global variable is named $dbtype in config.php.
global $dbtype;
$database_type = $dbtype;
public function get($databaseType = null)
{
if ( $databaseType == null) {
// Global variable is named $dbtype in config.php.
global $dbtype;
$databaseType = $dbtype;
}

if (!$database_type) {
if (!$databaseType) {
error_log('Database type variable, dbtype, is not specified in config.php - defaulting to MySql.');
$database_type = 'MySQL';
}

// Determine fully-qualified class name of database class corresponding to $database_type.
if (key_exists(strtolower($database_type), $this->database_types)) {
$dbClassName = $this->database_types[strtolower($database_type)];
if (key_exists(strtolower($databaseType), $this->database_types)) {
$dbType = $this->database_types[strtolower($databaseType)];

$full_class_name = 'SynchWeb\\Database\\Type\\' . $dbClassName;
$full_class_name = 'SynchWeb\\Database\\Type\\' . $dbType["dbClassName"];;

if (class_exists($full_class_name)) {
$conn = $this->databaseConnectionFactory->get($dbClassName);
$conn = $this->databaseConnectionFactory->get($dbType["dataConnectionName"]);
return new $full_class_name($conn);
}
else {
Expand All @@ -46,7 +49,7 @@ public function get()

}
else {
error_log("Database type '$database_type' not configured.");
error_log("Database type '$databaseType' not configured.");
}
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion api/src/Database/Type/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ class MySQL extends DatabaseParent {
'PhasingProgramAttachment',

// Xray Centring
'XrayCentring',
'XrayCentringResult',

'BeamCalendar',
Expand Down Expand Up @@ -551,4 +552,4 @@ function close()
if ($this->conn)
$this->conn->close();
}
}
}
Loading

0 comments on commit d0df7be

Please sign in to comment.