Current Location: Home> Latest Articles> Build a Token-Based Authentication System with PHP and OAuth

Build a Token-Based Authentication System with PHP and OAuth

M66 2025-07-13

Build a Token-Based Authentication System with PHP and OAuth

In today's internet applications, ensuring the security of user identities is crucial. OAuth, as an open authorization protocol, is widely used across many platforms, allowing users to authorize applications to access their resources without sharing their password. This article will guide you through the process of building a token-based authentication system with PHP and OAuth.

Installing the PHP OAuth Extension

First, you need to install the OAuth extension in your PHP environment. You can install it via the following command in the terminal:

<span class="fun">sudo apt-get install php-oauth</span>

After installation, you can proceed to write PHP code to implement OAuth authentication.

Configure the OAuth Client

Before building the OAuth authentication system, you need to register for an OAuth client ID and client secret on a relevant developer platform (such as Google, Facebook, Twitter, etc.). These credentials are used in the authentication process.

Write OAuth Authentication Code

Next, create a file named oauth.php. Below is the code to implement the OAuth authentication flow:

<?php

// Define OAuth client ID and client secret
$clientId = "YOUR_CLIENT_ID";
$clientSecret = "YOUR_CLIENT_SECRET";

// Define OAuth callback URL
$callbackUrl = "http://yourwebsite.com/callback.php";

// Initialize OAuth client
$oauth = new OAuth($clientId, $clientSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);

// Request authorization
$requestToken = $oauth->getRequestToken("https://api.example.com/oauth/request_token", $callbackUrl);

// Save the temporary token in the session
$_SESSION['oauth_token'] = $requestToken['oauth_token'];
$_SESSION['oauth_token_secret'] = $requestToken['oauth_token_secret'];

// Generate authorization URL
$authorizeUrl = $oauth->getAuthorizeURL($requestToken['oauth_token']);

// Redirect user to the authorization URL
header("Location: " . $authorizeUrl);
?>

In the above code, we first define the OAuth client ID, client secret, and callback URL. We then initialize an OAuth client object and request a temporary token. Finally, we generate the authorization URL and redirect the user to it.

Handle OAuth Callback

Once the user authorizes the application, the system will receive a callback. At this point, we need to handle the authorization response and exchange the temporary token for an access token. Here is the PHP code to handle the callback:

<?php

// Check if the temporary token exists
if (!isset($_SESSION['oauth_token']) || !isset($_SESSION['oauth_token_secret'])) {
    die("Temporary token not found.");
}

// Get the temporary token
$oauthToken = $_SESSION['oauth_token'];
$oauthTokenSecret = $_SESSION['oauth_token_secret'];

// Initialize OAuth client
$oauth = new OAuth($clientId, $clientSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$oauth->setToken($oauthToken, $oauthTokenSecret);

// Get the access token
$accessToken = $oauth->getAccessToken("https://api.example.com/oauth/access_token");

// Output the access token
var_dump($accessToken);
?>

In the callback handling code, we first check if the temporary token exists. We then use these tokens to initialize the OAuth client and retrieve the access token, which is printed out.

Conclusion

With the above example code, you can easily implement a token-based authentication system using PHP and OAuth. You can further customize and extend the system according to your specific security requirements.