π linuxfront-v2-logistics/auth.php
This script handles the OAuth2 callback for Microsoft Outlook integration. It exchanges the authorization code for access and refresh tokens, then stores them in the database. Finally, it notifies the parent window of completion.
Purpose and Flow
- Receive the
codefrom Microsoft OAuth redirect. - Exchange authorization code for access and refresh tokens.
- Refresh the access token using the refresh token.
- Store tokens in the
admin_userstable. - Notify the calling window via
postMessage.
Key Configuration
| Constant / Variable | Description |
|---|---|
$client_id |
App (client) ID registered in Azure AD π |
$client_secret |
App secret in Azure AD π |
$redirect_uri |
Callback URL: https://www.techlogistics.com/auth.php |
$token_endpoint |
https://login.microsoftonline.com/common/oauth2/v2.0/token |
OAuth2 Code Exchange π
- Check for
code.if ($_REQUEST['code'] != '') { β¦ } - Build authorization-code request
$fields = [ 'client_id' => urlencode($client_id), 'scope' => urlencode('https://outlook.office.com/mail.read ...'), 'redirect_uri' => urlencode($redirect_uri), 'grant_type' => urlencode('authorization_code'), 'client_secret' => urlencode($client_secret), 'code' => urlencode($code) ]; - Send cURL POST to the token endpoint.
- Decode JSON response and extract
$access_tokenand$refresh_token.
Access Token Refresh π
After obtaining initial tokens, the script immediately requests a new access token:
$fields = [
'client_id' => urlencode($client_id),
'scope' => urlencode('https://outlook.office.com/mail.read ...'),
'redirect_uri' => urlencode($redirect_uri),
'refresh_token' => urlencode($refresh_token),
'grant_type' => urlencode('refresh_token'),
'client_secret' => urlencode($client_secret)
];
// β¦ same cURL POST flow
Database Storage πΎ
The script connects directly to MySQL and updates the admin_users table:
$con = mysqli_connect(
"db1.freightanywhere.com",
"hashetms",
"Hashe1234",
"techdb_win"
);
$ins_sql = "
UPDATE admin_users
SET outlook_api = '$access_token',
refresh_token = '$refresh_token'
WHERE id = " . $_REQUEST['state'] . "
";
mysqli_query($con, $ins_sql);
- DB host:
db1.freightanywhere.com - DB user:
hashetms - Database:
techdb_win
Parent Window Notification π¨
Upon success, the script outputs HTML and JavaScript:
<h1>Success, please close this tab and open inbox in TMS</h1>
<script>
window.parent.postMessage({
status: 'Y',
location: window.location.href
}, "*");
</script>
This informs the embedding page that authentication completed.
Error Handling
- If
codeis missing or token exchange fails, the script echoes βAuthentication Error!!β and exits.
Dependencies
- PHP cURL extension
mysqlidriver- Valid Azure AD app with redirect URI set to
/auth.php
Related Files π
- auth_v1.php: Legacy OAuth flow variant
- auth_graph.php: Uses Microsoft Graphβs
User.Readscope - gmi_callback.php: Gmail/Mail integration prototype
- api_and.php: General API gateway in this module
These variants share similar token-exchange logic but target different services or scopes.
Auth.php centralizes Outlook OAuth2 in the linuxfront-v2-logistics module, enabling secure token management for email, calendar, and task features.