Làm việc với AI

Dùng bản tóm tắt tối ưu cho ChatGPT, Claude, Grok hoặc Cursor.

Mở bản Markdown cho AI
Hãy đọc docs/AI-CONTEXT.md trước và tích hợp ứng dụng hiện tại với PHP SSO Service theo OAuth 2.0 Authorization Code Flow và OpenID Connect. Kiểm tra framework và session system của ứng dụng client; triển khai login route, callback route, state validation, PKCE nếu là public client, token exchange, UserInfo request và tạo session nội bộ. Dùng biến môi trường cho SSO_BASE_URL, CLIENT_ID, CLIENT_SECRET và REDIRECT_URI. Không sửa source code của SSO Service, không commit hoặc log secret/token, không thay đổi OAuth contract, và bổ sung hướng dẫn cấu hình cùng kiểm thử.
Tài liệu Tích Hợp PHP

Hướng Dẫn Tích Hợp PHP

Hướng dẫn chi tiết tích hợp PHP vào hệ thống SSO.

Yêu Cầu

  • PHP 8.2 trở lên
  • Mở rộng cURL
  • Hỗ trợ Sessions
  • Không cần thư viện bên ngoài

Bước 1: Tạo Nút Đăng Nhập

<?php
// login.php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['sso_login'])) {
    $state = bin2hex(random_bytes(16));
    $_SESSION['oauth_state'] = $state;
    
    $auth_url = 'https://accounts.lequangkhai.id.vn/authorize.php?' . http_build_query([
        'client_id' => 'YOUR_CLIENT_ID',
        'response_type' => 'code',
        'redirect_uri' => 'https://yourapp.com/callback.php',
        'scope' => 'openid profile email',
        'state' => $state
    ]);
    
    header('Location: ' . $auth_url);
    exit;
}
?>
<form method="POST">
    <button type="submit" name="sso_login" class="btn btn-primary">
        Đăng Nhập bằng SSO
    </button>
</form>

Bước 2: Xử Lý Callback

<?php
// callback.php
session_start();

$code = $_GET['code'] ?? null;
$state = $_GET['state'] ?? null;
$error = $_GET['error'] ?? null;

if ($error) die('Lỗi: ' . htmlspecialchars($error));
if (!$state || $state !== $_SESSION['oauth_state']) die('CSRF detected');
if (!$code) die('Code missing');

// Trao đổi code lấy token
$ch = curl_init('https://accounts.lequangkhai.id.vn/api/token');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'grant_type' => 'authorization_code',
    'code' => $code,
    'client_id' => 'YOUR_CLIENT_ID',
    'client_secret' => 'YOUR_CLIENT_SECRET',
    'redirect_uri' => 'https://yourapp.com/callback.php'
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$token_data = json_decode($response, true);

$_SESSION['access_token'] = $token_data['access_token'];
$_SESSION['refresh_token'] = $token_data['refresh_token'] ?? null;
$_SESSION['token_expires_at'] = time() + ($token_data['expires_in'] ?? 3600);

header('Location: /dashboard.php');
?>

Bước 3: Lấy Thông Tin Người Dùng

<?php
// includes/auth.php
function getAuthenticatedUser() {
    session_start();
    
    if (!isset($_SESSION['access_token'])) return null;
    
    if (time() >= $_SESSION['token_expires_at']) {
        refreshAccessToken();
    }
    
    $ch = curl_init('https://accounts.lequangkhai.id.vn/api/userinfo');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . $_SESSION['access_token']
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    return json_decode($response, true);
}

function isLoggedIn() {
    session_start();
    return isset($_SESSION['access_token']) && time() < ($_SESSION['token_expires_at'] ?? 0);
}

function logout() {
    session_start();
    session_destroy();
    header('Location: /');
}
?>

Bước 4: Bảo Vệ Các Trang

<?php
// dashboard.php
require_once 'includes/auth.php';

session_start();

if (!isLoggedIn()) {
    header('Location: /login.php');
    exit;
}

$user = getAuthenticatedUser();
?>
<h1>Chào <?php echo htmlspecialchars($user['name']); ?>!</h1>
<p>Email: <?php echo htmlspecialchars($user['email']); ?></p>
<a href="/logout.php">Đăng Xuất</a>

Bước 5: Cấu Hình Biến Môi Trường

SSO_CLIENT_ID=your_client_id
SSO_CLIENT_SECRET=your_client_secret
SSO_AUTHORIZATION_ENDPOINT=https://sso.example.com/authorize.php
SSO_TOKEN_ENDPOINT=https://sso.example.com/api/token
SSO_USERINFO_ENDPOINT=https://sso.example.com/api/userinfo

Kiểm Tra

  • Truy cập login.php, thấy nút "Đăng Nhập"
  • Nhấp nút, chuyển hướng tới SSO
  • Đăng nhập thành công
  • Quay lại dashboard, thấy thông tin người dùng
  • Đăng xuất hoạt động

Khắc Phục Sự Cố →

Cập nhật gần nhất: Tháng 10, 2025
Lên đầu trang