PHP code example of alancting / oauth2-microsoft-openid
1. Go to this page and download the library: Download alancting/oauth2-microsoft-openid 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/ */
alancting / oauth2-microsoft-openid example snippets
namespace App\Controller;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
class AdfsController extends AbstractController
{
/**
* After going to microsoft, you're redirected back here
* because this is the "redirect_route" you configured
* in config/packages/knpu_oauth2_client.yaml
*
* @Route("/adfs/connect", name="microsoft_openid_connect")
*/
public function connectCheckAction(Request $request, ClientRegistry $clientRegistry)
{
return new Response();
}
/**
* After going to microsoft, you're redirected back here
* because this is the "redirect_route" you configured
* in config/packages/knpu_oauth2_client.yaml
*
* @Route("/adfs/logout", name="microsoft_openid_logout")
*/
public function logoutAction(Request $request, ClientRegistry $clientRegistry)
{
return new Response();
}
}
namespace App\Controller;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
class AzureAdController extends AbstractController
{
/**
* After going to microsoft, you're redirected back here
* because this is the "redirect_route" you configured
* in config/packages/knpu_oauth2_client.yaml
*
* @Route("/azure_ad/connect", name="microsoft_openid_connect")
*/
public function connectCheckAction(Request $request, ClientRegistry $clientRegistry)
{
return new Response();
}
/**
* After going to microsoft, you're redirected back here
* because this is the "redirect_route" you configured
* in config/packages/knpu_oauth2_client.yaml
*
* @Route("/azure_ad/logout", name="microsoft_openid_logout")
*/
public function logoutAction(Request $request, ClientRegistry $clientRegistry)
{
return new Response();
}
}
use Alancting\OAuth2\OpenId\Client\Client\AdfsClient;
public index(AdfsClient $adfsClient)
{
/**
* Get credential for main scope
*/
$mainScopeCredential = $adfsClient->getOAuthCredential();
// Get access token
$accessToken = $mainScopeCredential->getAccessToken();
// Get id token
$idTokenJWT = $mainScopeCredential->getIdTokenJWT();
// Get id token payload
$idTokenPayload = $idTokenJWT->getPayload();
// Get value for a specific attr from id token payload
$idTokenPayloadAttr1 = $idTokenJWT->get('attr1');
/**
* If have other resource scopes, you can loop to fetch credentials for other scopes
*/
$otherScopeCredentials = [];
foreach ($mainScopeCredential->getOtherResourceCredentials() as $scope => $credential) {
$otherScopeCredentials[$scope] = $credential;
}
/**
* You can also get the credential from scope name by
*/
$otherScopeCredential = $mainScopeCredential->getOtherResourceCredential('other_scope_name');
}
use Alancting\OAuth2\OpenId\Client\Client\AzureAdClient;
public index(AzureAdClient $azureAdClient)
{
/**
* Get credential for main scope
*/
$mainScopeCredential = $azureAdClient->getOAuthCredential();
// Get access token
$accessToken = $mainScopeCredential->getAccessToken();
// Get id token
$idTokenJWT = $mainScopeCredential->getIdTokenJWT();
// Get id token payload
$idTokenPayload = $idTokenJWT->getPayload();
// Get value for a specific attr from id token payload
$idTokenPayloadAttr1 = $idTokenJWT->get('attr1');
/**
* If have other resource scopes, you can loop to fetch credentials for other scopes
*/
$otherScopeCredentials = [];
foreach ($mainScopeCredential->getOtherResourceCredentials() as $scope => $credential) {
$otherScopeCredentials[$scope] = $credential;
}
/**
* You can also get the credential from scope name by
*/
$otherScopeCredential = $mainScopeCredential->getOtherResourceCredential('other_scope_name');
}
// Logout url for Adfs
$logoutUrl = $adfsClient->getLogoutUrl();
// Logout url for Azure Ad
$logoutUrl = $azureAdClient->getLogoutUrl();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.