stolfam/ms-azure-sso-php

Easy-to-use PHP connector for Microsoft Azure SSO authentication

1.0.6 2024-11-07 12:11 UTC

This package is auto-updated.

Last update: 2024-11-07 12:12:07 UTC


README

Install

composer require stolfam/ms-azure-sso-php

Nette

Neon config:

parameters:
    microsoft:
        azure:
            loginBaseUri: https://login.microsoftonline.com
            apiBaseUri: https://login.windows.net
            appId: xxx
            clientSecret: xxx
            tenantId: xxx
            redirectUri: http://localhost
            refreshTokenRotationTime: 60
            refreshTokenKey: storageKey
               
services:
    - Stolfam\MS\Azure\Client(%microsoft.azure%)

Use

Redirect to Login URL:

$client = new Client($arrayArgs);
$state = "abc123";
$loginUrl = $client->getLoginUrl($state)
// redirect to $loginUrl to invoke user authentication with MS AZure

Handle returned Authorization Code:

$code = $_GET['code'];
$state = $_GET['state']; // abc123

// set callback
$client->onAuthSuccess[] = function(UserProfile $userProfile) {
    // authentication successful
    // persist user data where you need
    echo $userProfile->id;
    echo $userProfile->name;
    echo $userProfile->email;
};

if($client->authorize($code)) {
    // success
    // i.e. redirect back to original page before login invoked
}

Handle expired login:

if(!$client->isSessionValid()) {
    // session expired
    // try to re-authorize
    $client->invokeReAuthorization();
}