πŸ” 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 code from Microsoft OAuth redirect.
  • Exchange authorization code for access and refresh tokens.
  • Refresh the access token using the refresh token.
  • Store tokens in the admin_users table.
  • 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 πŸš€

  1. Check for code.
    if ($_REQUEST['code'] != '') { … }
    
  2. 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)
    ];
    
  3. Send cURL POST to the token endpoint.
  4. Decode JSON response and extract $access_token and $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 code is missing or token exchange fails, the script echoes β€œAuthentication Error!!” and exits.

Dependencies

  • PHP cURL extension
  • mysqli driver
  • Valid Azure AD app with redirect URI set to /auth.php
  • auth_v1.php: Legacy OAuth flow variant
  • auth_graph.php: Uses Microsoft Graph’s User.Read scope
  • 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.