🔐 Authentication Callback (auth_v1.php)
This PHP script serves as the OAuth 2.0 callback for Microsoft’s identity platform. It handles the exchange of an authorization code for access and refresh tokens, immediately refreshes the access token, and stores both tokens in the admin_users table. Finally, it notifies the parent window of success.
⚙️ Configuration
| Parameter | Value |
|---|---|
| Client ID | 83153367-cee4-4748-92d5-ed7596710762 |
| Client Secret | YHT7Q~y7~nvu19PVQpofQvDo4Es-Mn.Vr-Nau |
| Redirect URI | https://www.techlogistics.com/auth_v1.php |
| Token Endpoint | https://login.microsoftonline.com/common/oauth2/v2.0/token |
| Database Host | db1.freightanywhere.com |
| Database Name | techdb_win |
| DB Credentials | user: hashetms, password: Hashe1234 |
All values are hard-coded in the script.
📄 Code Breakdown
1. Request Validation
The script checks if the code parameter exists. If missing, it outputs an error and halts.
if ($_REQUEST['code'] != '') {
// proceed…
} else {
echo "Authentication Error!!";
exit;
}
2. Exchange Authorization Code
Builds a POST payload to request tokens via the authorization_code grant.
$fields = [
'client_id' => urlencode($client_id),
'scope' => urlencode(''),
'redirect_uri' => urlencode('https://www.techlogistics.com/auth_v1.php'),
'grant_type' => urlencode('authorization_code'),
'client_secret' => urlencode($client_secret),
'code' => urlencode($code)
];
$ch = curl_init("https://login.microsoftonline.com/common/oauth2/v2.0/token");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$response = json_decode($result, true);
$access_token = $response['access_token'];
$refresh_token = $response['refresh_token'];
3. Refresh Access Token
Immediately uses the obtained refresh_token to get a fresh access_token.
$fields = [
'client_id' => urlencode($client_id),
'scope' => urlencode(''),
'redirect_uri' => urlencode('https://www.techlogistics.com/auth_v1.php'),
'refresh_token' => urlencode($refresh_token),
'grant_type' => urlencode('refresh_token'),
'client_secret' => urlencode($client_secret)
];
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
$result = curl_exec($ch);
$response = json_decode($result, true);
$access_token = $response['access_token'];
4. Store Tokens in Database 🗄️
Connects to MySQL and updates the admin_users record identified by state.
if (!empty($access_token)) {
$con = mysqli_connect("db1.freightanywhere.com","hashetms","Hashe1234","techdb_win");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$ins_sql = "
UPDATE admin_users
SET team_api = '{$access_token}',
team_refresh_token = '{$refresh_token}'
WHERE id = {$_REQUEST['state']}
";
echo '<h1>Success, please close this tab and open inbox in TMS</h1>';
mysqli_query($con, $ins_sql);
}
5. Notify Parent Window
Sends a postMessage to the opener or parent frame to signal completion.
<script>
window.parent.postMessage(
{ status: 'Y', location: window.location.href },
"*"
);
</script>
🕸️ Sequence Diagram
sequenceDiagram
participant Browser
participant Azure as Microsoft OAuth2
participant Server as auth_v1.php
participant DB as MySQL
Browser->>Azure: Request authorization (with state)
Azure-->>Browser: Redirect with code & state
Browser->>Server: GET /auth_v1.php?code&state
Server->>Azure: POST /token (authorization_code)
Azure-->>Server: access_token + refresh_token
Server->>Azure: POST /token (refresh_token)
Azure-->>Server: new access_token
Server->>DB: UPDATE admin_users SET tokens
Server-->>Browser: HTML + postMessage('Y')
📌 Notes & Best Practices
- Secure Secrets: Move
client_secretinto environment variables or a secrets vault. - State Validation: Validate the
stateparameter to prevent CSRF. - Error Handling: Expand error checks on cURL responses and JSON decoding.
- Token Security: Encrypt tokens at rest and handle expiry gracefully.
This document details every critical step in auth_v1.php, clarifying its role in Microsoft OAuth integration.