From 51fde44c04b0606467fcd9c148c5a1bb5df25fbd Mon Sep 17 00:00:00 2001 From: Token2 Date: Mon, 25 Nov 2024 15:25:00 +0100 Subject: [PATCH] Add files via upload --- README.md | 104 +++++++++++++++++++- index.php | 276 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 378 insertions(+), 2 deletions(-) create mode 100644 index.php diff --git a/README.md b/README.md index aec3816..1bcbe41 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,102 @@ -# token_inventory -Entra ID - Hardware token inventory portal + +# Entra ID - Hardware token inventory portal + +## Overview +This is a simple, lightweight web application built in PHP to manage hardware OATH tokens using the Microsoft Graph API. It includes features such as listing tokens, importing token configurations via JSON files, and providing an intuitive and responsive interface with Bootstrap. + +--- + +## Features +- **Authentication**: + - Form-based password protection for secure access. +- **Token Management**: + - View a list of hardware OATH tokens. + - Includes key details like Serial Number, Device (Manufacturer/Model), Assigned User, Status, and Last Used. +- **Import Functionality**: + - Upload and import tokens using a JSON file. +- **Responsive Design**: + - Utilizes [Bootstrap](https://getbootstrap.com/) for a clean and responsive UI. +- **Interactive Table**: + - Integrated with [DataTables](https://datatables.net/) for sorting, searching, and pagination. + +--- + +## Installation +1. Clone the repository: + ```bash + git clone https://github.com/YOUR_USERNAME/hardware-token-management.git + cd hardware-token-management + ``` + +2. Update the configuration in `index.php`: + - Replace the following placeholders with your own values: + $tenantId = 'YOUR_TENANT_ID'; + $clientId = 'YOUR_CLIENT_ID'; + $clientSecret = 'YOUR_CLIENT_SECRET'; + + $valid_username = 'username to access this app'; // Set your username + $valid_password = 'password to access the app'; // Set your password + + + +3. Deploy the application on a PHP-compatible server (e.g., Apache, Nginx). + +4. Access the application in your browser. + +--- + +## Usage +1. **Login**: + - Access the portal using the predefined username and password (`admin/password123` by default). + - Update these credentials in `index.php`. + +2. **Import Tokens**: + - Use the **Import Tokens** button to upload a JSON file with the token details. This format is available in Token2's seed request form. + - Example JSON format: + ```json + [ + { + "serialNumber": "8659623852751", + "secretKey": "ABC1234567890DEF", + "manufacturer": "Token2", + "model": "C202", + "timeIntervalInSeconds": 30, + "hashFunction": "hmacsha1" + } + ] + ``` + +3. **View Tokens**: + - Explore the table with token details. + - Use search and sorting features provided by DataTables for quick navigation. + +--- + +## Screenshots +**Login Page** +[Insert Screenshot Here] + +**Token List** +[Insert Screenshot Here] + +**Import Modal** +[Insert Screenshot Here] + +--- + + +## License +This project is open-source and available under the [MIT License](LICENSE). + +--- + +## Acknowledgments +- [Microsoft Graph API](https://learn.microsoft.com/en-us/graph/overview) +- [Token2](https://www.token2.com) for hardware tokens +- [Bootstrap](https://getbootstrap.com/) for responsive design +- [DataTables](https://datatables.net/) for interactive table functionality + +--- + +## Contact +For any questions or suggestions, please contact me via support {at} token2.com or open an issue in this repository. diff --git a/index.php b/index.php new file mode 100644 index 0000000..0aeae15 --- /dev/null +++ b/index.php @@ -0,0 +1,276 @@ + + + + + + + Login + + + +
+

Please Log In


+
+
+ + +
+
+ + +
+ ' . (isset($error_message) ? '

' . htmlspecialchars($error_message) . '

' : '') . ' + +
+
+ + '; + exit; +} + + + + + +// Function to get an access token +function getAccessToken($tenantId, $clientId, $clientSecret) { + $url = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"; + $data = [ + 'grant_type' => 'client_credentials', + 'client_id' => $clientId, + 'client_secret' => $clientSecret, + 'scope' => 'https://graph.microsoft.com/.default', + ]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']); + $response = curl_exec($ch); + curl_close($ch); + + $response = json_decode($response, true); + return $response['access_token'] ?? null; +} + +// Function to fetch hardware tokens +function fetchHardwareTokens($accessToken) { + $url = "https://graph.microsoft.com/beta/directory/authenticationMethodDevices/hardwareOathDevices"; + $headers = [ + "Authorization: Bearer $accessToken", + "Content-Type: application/json", + ]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + $response = curl_exec($ch); + curl_close($ch); + + $responseData = json_decode($response, true); + + // Debugging: Save raw API response to debug.log + file_put_contents('debug.log', print_r($responseData, true)); + + return $responseData['value'] ?? []; +} + +// Function to import tokens via PATCH +function importTokens($accessToken, $importData) { + $url = "https://graph.microsoft.com/beta/directory/authenticationMethodDevices/hardwareOathDevices"; + $headers = [ + "Authorization: Bearer $accessToken", + "Content-Type: application/json", + ]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($importData)); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + curl_close($ch); + + return json_decode($response, true); +} + +// Get Access Token +$accessToken = getAccessToken($tenantId, $clientId, $clientSecret); +if (!$accessToken) { + die('Failed to authenticate with Microsoft Graph.'); +} + +// Handle Import Action +$message = ''; +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['importFile'])) { + $fileContent = file_get_contents($_FILES['importFile']['tmp_name']); + $importData = json_decode($fileContent, true); + + if ($importData) { + $result = importTokens($accessToken, $importData); + $message = $result ? 'Tokens successfully imported!' : 'Failed to import tokens.'; + } else { + $message = 'Invalid JSON file.'; + } +} + +// Fetch Hardware Tokens +$tokens = fetchHardwareTokens($accessToken); +?> + + + + + + Hardware Token Management + + + + + + + + + +
+
+ + + + log out + + +
+ + +
Entra ID - Hardware token inventory portal
+ +

+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
Serial NumberDeviceUser IDStatusLast Updated
/
+
+ + + +


+ + + + +