PHP code example of oshco / adfs

1. Go to this page and download the library: Download oshco/adfs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oshco / adfs example snippets


use Oshco\Adfs\ADFSUser;

class AppUser implements ADFSUser {
    private int $id;
    private string $email;

    public function __construct(int $id, string $email) {
        $this->id = $id;
        $this->email = $email;
    }

    public function getId() {
        return $this->id;
    }
}

use Oshco\Adfs\SAMLRequest;

$request = new SAMLRequest();
$request->setDestination('https://adfs.example.com/adfs/ls');
$request->setAppID('My Application');
$request->setAppURL('https://myapp.example.com');
$request->setIssueInstant(gmdate('Y-m-d\TH:i:s\Z'));

$encoded = $request->encode();

// Redirect user to ADFS with the encoded SAML request
header('Location: https://adfs.example.com/adfs/ls?SAMLRequest=' . urlencode($encoded));

use Oshco\Adfs\ADFSUser;
use Oshco\Adfs\ADFSVerificationService;

class MyVerificationService extends ADFSVerificationService {

    public function __construct() {
        parent::__construct('adfs-verify', 'https://myapp.example.com/login-failed');
    }

    public function getUser(string $username): ?ADFSUser {
        // Look up the user in your database by username/email
        // Return an ADFSUser instance or null if not found
    }

    public function onSuccess(ADFSUser $user) {
        // User authenticated successfully
        // Set session, redirect to dashboard, etc.
    }
}

use Oshco\Adfs\ADFSResponse;

$response = new ADFSResponse($_POST['SAMLResponse']);

$response->isSuccess();    // true if ADFS authentication succeeded
$response->getUserName();  // authenticated username (lowercased)
$response->getAppName();   // application name from ADFS
$response->getXMLString(); // raw XML of the SAML response
$response->storeResponse(); // save response to file for debugging