Skip to content

Commit

Permalink
3.1.2
Browse files Browse the repository at this point in the history
Code review
  • Loading branch information
nilsteampassnet committed Sep 14, 2024
1 parent 8549120 commit 958461e
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 97 deletions.
2 changes: 1 addition & 1 deletion includes/config/include.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

define('TP_VERSION', '3.1.2');
define("UPGRADE_MIN_DATE", "1724862801");
define('TP_VERSION_MINOR', '77');
define('TP_VERSION_MINOR', '78');
define('TP_TOOL_NAME', 'Teampass');
define('TP_ONE_DAY_SECONDS', 86400);
define('TP_ONE_WEEK_SECONDS', 604800);
Expand Down
4 changes: 2 additions & 2 deletions includes/tables_integrity.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
{
"table_name": "background_tasks_logs",
"structure_hash": "18a0d2bde6955715cc70fdc01c2d91cd61ee7608093d076fcc3f8d32e4be938f"
"structure_hash": "3895fed095075e05002fc7a9efdda430b2f7899172cfb2f0d04f743f7eef2012"
},
{
"table_name": "cache",
Expand Down Expand Up @@ -101,7 +101,7 @@
},
{
"table_name": "log_system",
"structure_hash": "7cf40d09bef89b6cc415df5abe2175e33893973afe651f8814ddf6d8b06721f8"
"structure_hash": "29e304570416adcbc09673d1023bf0ae3442ad4ac93b1eeac1de4db9ebc35187"
},
{
"table_name": "misc",
Expand Down
18 changes: 12 additions & 6 deletions pages/oauth.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,18 @@ function(data) {
*/
$(function() {
// Click on log in button with Azure Entra
$('#but_perform_setup').click(function() {
if (debugJavascript === true) {
console.log('User starts setup with Azure');
}
document.location.href="sources/oauth.php";
return false;
$('#oauth2_tenant_id').change(function(event) {
var tenantId = $(this).val();
var endpointUrl = $('#oauth2_client_endpoint').val();
var tokenUrl = $('#oauth2_client_token').val();

// Remplace l'identifiant de locataire dans l'URL du point de terminaison
endpointUrl = endpointUrl.replace(/([^\/]*)\/oauth2\/v2.0\/authorize/, tenantId + '/oauth2/v2.0/authorize');
$('#oauth2_client_endpoint').val(endpointUrl);

// Remplace l'identifiant de locataire dans l'URL du jeton
tokenUrl = tokenUrl.replace(/([^\/]*)\/oauth2\/v2.0\/token/, tenantId + '/oauth2/v2.0/token');
$('#oauth2_client_token').val(tokenUrl);
});
});

Expand Down
6 changes: 3 additions & 3 deletions pages/oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@
<form role='form-horizontal'>
<div class='card-body'>

<div class='row mb-5'>
<div class='row mb-2'>
<div class='col-12'>
<div class="alert alert-warning" role="alert">
<i class="fa-solid fa-flask-vial mr-3"></i>Expiremental feature. Use at your own risk.
</div>
</div>
</div>

<div class='row mb-5'>
<div class='row mb-2'>
<div class='col-10'>
<?php echo $lang->get('settings_oauth_mode'); ?>
<small id='passwordHelpBlock' class='form-text text-muted'>
Expand Down Expand Up @@ -194,7 +194,7 @@
</small>
</div>
<div class='col-7'>
<input type='text' class='form-control form-control-sm setting-oauth' id='oauth2_client_scopes' value='<?php echo $SETTINGS['oauth2_client_scopes'] ?? 'openid,email,profile'; ?>'>
<input type='text' class='form-control form-control-sm setting-oauth' id='oauth2_client_scopes' value='<?php echo $SETTINGS['oauth2_client_scopes'] ?? 'openid,profile,email,User.Read,Group.Read.All'; ?>'>
</div>
</div>

Expand Down
10 changes: 0 additions & 10 deletions sources/identify.php
Original file line number Diff line number Diff line change
Expand Up @@ -2163,16 +2163,6 @@ public function getUserInfo($login, $enable_ad_user_auto_creation, $oauth2_enabl
$data['ldap_user_to_be_created'] = $enable_ad_user_auto_creation === true && DB::count() === 0 && $oauth2LoginOngoing !== true ? true : false;
$data['oauth2_user_to_be_created'] = $oauth2_enabled === true && DB::count() === 0 && $oauth2LoginOngoing === true ? true : false;


// ensure user fonction_id is set to false if not existing
/*if (is_null($data['fonction_id']) === true) {
$data['fonction_id'] = '';
}*/

// Prepare user roles (fonction_id + roles_from_ad_groups)
// Disable this as this happend repeadetly and is not necessary when working with AD groups
//$data['fonction_id'] = is_null($data['roles_from_ad_groups']) === true ? $data['fonction_id'] : (empty($data['roles_from_ad_groups']) === true ? $data['fonction_id'] : $data['fonction_id'] . ';' . $data['roles_from_ad_groups']);

return $data;
}

Expand Down
209 changes: 134 additions & 75 deletions sources/import.queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,92 +499,151 @@
$new = simplexml_load_string($xmlfile);
$con = json_encode($new);
$newArr = json_decode($con, true);



/**
* Undocumented function
*
* @param array $array
* @param integer $previousFolder
* @param array $newItemsToAdd
* @param integer $level
* @return array
* Recursive function to process the Keepass XML structure.
*
* @param array $array The current array to process.
* @param string $previousFolder The parent folder ID.
* @param array $newItemsToAdd The new items to add to the database.
* @param int $level The current level of the recursion.
*
* @return array The new items to add to the database.
*/
function recursive($array, $previousFolder, $newItemsToAdd, $level) : array
{
// Manage entries
if (isset($array['Entry']) === true) {
foreach($array['Entry'] as $key => $value) {
if (isset($value['String']) === true) {
$itemDefinition = [];
$c = count($value['String']);
for ($i = 0; $i < $c; $i++) {
$itemDefinition[$value['String'][$i]['Key']] = is_array($value['String'][$i]['Value']) === false ? $value['String'][$i]['Value'] : '';
}
$itemDefinition['parentFolderId'] = $previousFolder;
isset($itemDefinition['Notes']) === false ? $itemDefinition['Notes'] = '' : '';
isset($itemDefinition['URL']) === false ? $itemDefinition['URL'] = '' : '';
isset($itemDefinition['Password']) === false ? $itemDefinition['Password'] = '' : '';
array_push(
$newItemsToAdd['items'],
$itemDefinition
);
continue;
}

if ($key === "String") {
array_push(
$newItemsToAdd['items'],
[
'Notes' => is_array($value[0]['Value']) === false ? $value[0]['Value'] : '',
'Title' => is_array($value[2]['Value']) === false ? $value[2]['Value'] : '',
'Password' => is_array($value[1]['Value']) === false ? $value[1]['Value'] : '',
'URL' => is_array($value[3]['Value']) === false ? $value[3]['Value'] : '',
'UserName' => is_array($value[4]['Value']) === false ? $value[4]['Value'] : '',
'parentFolderId' => $previousFolder,
]
);
}
}
// Handle entries (items)
if (isset($array['Entry'])) {
$newItemsToAdd = handleEntries($array['Entry'], $previousFolder, $newItemsToAdd);
}

// Manage GROUPS
if (isset($array['Group']) === true && is_array($array['Group'])=== true) {
$currentFolderId = $previousFolder;
if (isset($array['Group']['UUID']) === true) {
// build expect array format
$array['Group'] = [$array['Group']];
}
foreach($array['Group'] as $key => $value){
// Add this new folder
array_push(
$newItemsToAdd['folders'],
[
'folderName' => $value['Name'],
'uuid' => $value['UUID'],
'parentFolderId' => $previousFolder,
'level' => $level,
]
);
$previousFolder = $value['UUID'];

// recursive inside this entry
$newItemsToAdd = recursive(
array_merge(
['Entry' => isset($value['Entry']) === true ? $value['Entry'] : ''],
['Group' => isset($value['Group']) === true ? $value['Group'] : ''],
),
$previousFolder,
$newItemsToAdd,
$level + 1
);
// Handle groups (folders)
if (isset($array['Group']) && is_array($array['Group'])) {
$newItemsToAdd = handleGroups($array['Group'], $previousFolder, $newItemsToAdd, $level);
}

$previousFolder = $currentFolderId;
return $newItemsToAdd;
}

/**
* Handle entries (items) within the structure.
* It processes each entry and adds it to the new items list.
*
* @param array $entries The entries to process.
* @param string $previousFolder The parent folder ID.
* @param array $newItemsToAdd The new items to add to the database.
*
* @return array The new items to add to the database.
*/
function handleEntries(array $entries, string $previousFolder, array $newItemsToAdd) : array
{
foreach ($entries as $key => $value) {
// Check if the entry has a 'String' field and process it
if (isset($value['String'])) {
$newItemsToAdd['items'][] = buildItemDefinition($value['String'], $previousFolder);
}
// If it's a direct 'String' item, build a simple item
elseif ($key === 'String') {
$newItemsToAdd['items'][] = buildSimpleItem($value, $previousFolder);
}
}

return $newItemsToAdd;
}


/**
* Build an item definition from the 'String' fields.
* Converts the key-value pairs into a usable item format.
*
* @param array $strings The 'String' fields to process.
* @param string $previousFolder The parent folder ID.
*
* @return array The item definition.
*/
function buildItemDefinition(array $strings, string $previousFolder) : array
{
$itemDefinition = [];
// Loop through each 'String' entry and map keys and values
foreach ($strings as $entry) {
$itemDefinition[$entry['Key']] = is_array($entry['Value']) ? '' : $entry['Value'];
}

// Set the parent folder and ensure default values for certain fields
$itemDefinition['parentFolderId'] = $previousFolder;
$itemDefinition['Notes'] = $itemDefinition['Notes'] ?? '';
$itemDefinition['URL'] = $itemDefinition['URL'] ?? '';
$itemDefinition['Password'] = $itemDefinition['Password'] ?? '';

return $itemDefinition;
}

/**
* Build a simple item with predefined fields.
* This is used when there is no associated key, just ordered values.
*
* @param array $value The ordered values to process.
* @param string $previousFolder The parent folder ID.
*
* @return array The simple item definition.
*/
function buildSimpleItem(array $value, string $previousFolder) : array
{
return [
'Notes' => is_array($value[0]['Value']) ? '' : $value[0]['Value'],
'Title' => is_array($value[2]['Value']) ? '' : $value[2]['Value'],
'Password' => is_array($value[1]['Value']) ? '' : $value[1]['Value'],
'URL' => is_array($value[3]['Value']) ? '' : $value[3]['Value'],
'UserName' => is_array($value[4]['Value']) ? '' : $value[4]['Value'],
'parentFolderId' => $previousFolder,
];
}

/**
* Handle groups (folders) within the structure.
* It processes each group and recursively goes deeper into subgroups and subentries.
*
* @param array $groups The groups to process.
* @param string $previousFolder The parent folder ID.
* @param array $newItemsToAdd The new items to add to the database.
*
* @return array The new items to add to the database.
*/
function handleGroups($groups, string $previousFolder, array $newItemsToAdd, int $level) : array
{
// If a single group is found, wrap it into an array
if (isset($groups['UUID'])) {
$groups = [$groups];
}

// Save the current folder ID to restore it after recursion
$currentFolderId = $previousFolder;

foreach ($groups as $group) {
// Add the current group (folder) to the list
$newItemsToAdd['folders'][] = [
'folderName' => $group['Name'],
'uuid' => $group['UUID'],
'parentFolderId' => $previousFolder,
'level' => $level,
];

// Recursively process entries and subgroups inside this group
$newItemsToAdd = recursive(
[
'Entry' => $group['Entry'] ?? '',
'Group' => $group['Group'] ?? '',
],
$group['UUID'],
$newItemsToAdd,
$level + 1
);
}

return $newItemsToAdd;
}

// Start the recursive processing
$ret = recursive(
array_merge(
['Entry' => $newArr['Root']['Group']['Entry']],
Expand Down

0 comments on commit 958461e

Please sign in to comment.