linuxfront-v2-logistics/auth_graph.php

This script serves as the OAuth2 callback handler for Microsoft Graph. It exchanges the authorization code for access and refresh tokens, persists them in the database, and signals the parent application to refresh its state before closing the popup window.


🔍 Overview

  • Handles the Authorization Code Grant and Refresh Token Grant flows.
  • Stores tokens in the cs_me_users table of the customersupport MySQL database.
  • Relies on the state parameter to map tokens to a specific user.

⚙️ Configuration

Variable Description
$client_id Application (client) ID from Azure AD.
$client_secret Client secret corresponding to the above ID.
redirect_uri Must match the Azure AD app’s redirect URL.
$client_id     = "2821e8a2-06fd-4385-a8db-36676ed424e4";
$client_secret = "4xPN6S.jmer1Y6OvYd_BvvvU_~Rej76sR.";
$redirect_uri  = "https://www.techlogistics.com/auth_graph.php";

🔄 Flowchart

sequenceDiagram
    participant B as Browser
    participant S as Server (auth_graph.php)
    participant M as Microsoft OAuth2
    participant DB as MySQL Database

    B->>S: GET /auth_graph.php?code=&state=
    S->>M: POST /common/oauth2/v2.0/token (authorization_code)  
    M-->>S: {access_token, refresh_token}
    S->>M: POST /common/oauth2/v2.0/token (refresh_token)  
    M-->>S: {access_token, refresh_token}
    S->>DB: SELECT/INSERT/UPDATE cs_me_users  
    S-->>B: <script>window.close()</script>

📑 Step-by-Step Logic

  1. Validate Callback
    • Check for non-empty $_REQUEST['code'].
  2. Exchange Authorization Code
    • Build POST fields for grant_type=authorization_code.
    • cURL to Azure AD token endpoint.
  3. Refresh Token Request
    • Use obtained refresh_token to request a fresh access token.
  4. Persist Tokens
    • Connect to customersupport DB.
    • Query cs_me_users by user_id = $_REQUEST['state'].
    • UPDATE existing record or INSERT a new one.
  5. Notify & Close
    • Emit JavaScript to close the popup and prompt the parent to refresh.

💾 Database Interaction

Table: cs_me_users

Column Type Description
user_id VARCHAR The application’s user identifier.
token TEXT Current Microsoft Graph access token.
refresh_token TEXT Refresh token for long-lived sessions.

🛡️ Error Handling

  • If code is missing, the script outputs “Authentication Error!!” and exits.
  • If MySQL connection fails, execution halts with a descriptive error.

🔗 Integration Points

  • Initiation: Another part of the application constructs an OAuth2 authorization URL pointing to
    https://login.microsoftonline.com/common/oauth2/v2.0/authorize
    with redirect_uri=https://www.techlogistics.com/auth_graph.php and state=<user_id>.
  • Usage: Once tokens are stored, downstream code can query cs_me_users to call Graph APIs on behalf of users.

📌 Best Practices & Notes

  • Ensure HTTPS is configured to protect tokens in transit.
  • Rotate client_secret periodically in Azure AD and here.
  • Validate the state parameter to mitigate CSRF attacks.
  • Consider using a library (e.g., [league/oauth2-client]) for more robust error handling.

Example cURL Exchange

curl -X POST https://login.microsoftonline.com/common/oauth2/v2.0/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "client_id=2821e8a2-06fd-4385-a8db-36676ed424e4&\
scope=User.Read%20offline_access&\
redirect_uri=https%3A%2F%2Fwww.techlogistics.com%2Fauth_graph.php&\
grant_type=authorization_code&\
client_secret=4xPN6S.jmer1Y6OvYd_BvvvU_~Rej76sR.&\
code=<AUTH_CODE>"

This returns a JSON payload containing access_token and refresh_token.


✨ End of Documentation ✨