PHP code example of virgantara / unida-apps-support
1. Go to this page and download the library: Download virgantara/unida-apps-support 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/ */
virgantara / unida-apps-support example snippets
return [
'oauth' => [
'client_id' => 'your-client-id',
'client_secret' => 'your-client-secret',
'baseurl' => 'https://your-sso-server.com',
'redirectUri' => 'https://your-app.com/site/auth-callback',
'scope' => 'read write',
],
];
return [
'oauth' => [
'client_id' => 'elitabmas',
'client_secret' => 'your-secret',
'baseurl' => 'https://sso.unida.gontor.ac.id',
'redirectUri' => 'https://elitabmas.unida.gontor.ac.id/site/auth-callback',
'scope' => 'read write',
],
];
'components' => [
// Other components...
'tokenService' => [
'class' => 'virgantara\components\TokenService',
],
'aplikasi' => [
'class' => 'virgantara\components\AplikasiAuth',
'baseurl' => $params['oauth']['baseurl'],
],
'tokenManager' => [
'class' => 'virgantara\components\TokenManager',
],
'oauth2' => [
'class' => 'virgantara\components\OAuth2Client',
'tokenValidationUrl' => $params['oauth']['baseurl'],
'tokenRefreshUrl' => $params['oauth']['baseurl'],
'client_id' => $params['oauth']['client_id'],
'client_secret' => $params['oauth']['client_secret'],
],
],
public function actionStartSso()
{
$session = Yii::$app->session;
if (!$session->isActive) {
$session->open();
}
$state = Yii::$app->security->generateRandomString(40);
$session->set('oauth_state', $state);
$params = [
'client_id' => Yii::$app->params['oauth']['client_id'],
'redirect_uri' => Yii::$app->params['oauth']['redirectUri'],
'response_type' => 'code',
'scope' => Yii::$app->params['oauth']['scope'] ?? 'read write',
'state' => $state,
];
$authUrl = rtrim(Yii::$app->params['oauth']['baseurl'], '/')
. '/oauth/authorize?'
. http_build_query($params);
return $this->redirect($authUrl);
}
public function actionAuthCallback()
{
try {
$session = Yii::$app->session;
if (!$session->isActive) {
$session->open();
}
$sessionState = $session->get('oauth_state');
$receivedState = Yii::$app->request->get('state');
if (empty($sessionState) || empty($receivedState) || $sessionState !== $receivedState) {
throw new \yii\web\BadRequestHttpException('Invalid OAuth state.');
}
$authCode = Yii::$app->request->get('code');
if (empty($authCode)) {
throw new \yii\web\BadRequestHttpException('Authorization code not received.');
}
$response = Yii::$app->tokenManager->fetchAccessTokenWithAuthCode($authCode);
if (empty($response['access_token'])) {
throw new \yii\web\UnauthorizedHttpException('Failed to get access token.');
}
$accessToken = $response['access_token'];
$refreshToken = $response['refresh_token'] ?? null;
$userinfo = Yii::$app->tokenManager->getUserinfo($accessToken);
if (empty($userinfo) || empty($userinfo['email'])) {
throw new \yii\web\UnauthorizedHttpException('Failed to get user info.');
}
$session->set('access_token', $accessToken);
if (!empty($refreshToken)) {
$session->set('refresh_token', $refreshToken);
}
/*
* TODO:
* Implement local user login here.
*
* Example:
* - Find local user by email
* - Create user if allowed
* - Login using Yii::$app->user->login($identity)
*/
return $this->redirect(['site/index']);
} catch (\Throwable $e) {
return $this->handleException($e);
}
}
protected function handleException($e)
{
Yii::error($e->getMessage(), __METHOD__);
Yii::$app->session->setFlash('danger', $e->getMessage());
return $this->redirect(['site/index']);
}
$apps = Yii::$app->aplikasi->getRenderedAllowedAppsList();
use yii\widgets\Menu;
echo Menu::widget([
'items' => Yii::$app->aplikasi->getRenderedAllowedAppsList(),
'encodeLabels' => false,
]);
public function actionLogout()
{
Yii::$app->user->logout();
Yii::$app->session->remove('access_token');
Yii::$app->session->remove('refresh_token');
$logoutUrl = rtrim(Yii::$app->params['oauth']['baseurl'], '/')
. '/oauth/logout?'
. http_build_query([
'client_id' => Yii::$app->params['oauth']['client_id'],
]);
return $this->redirect($logoutUrl);
}
text
https://app.example.com/callback?access_token=xxx&refresh_token=yyy
text
Unida-Apps-Support/
├── composer.json
├── README.md
├── src/
│ └── components/
│ ├── AplikasiAuth.php
│ ├── OAuth2Client.php
│ ├── TokenManager.php
│ └── TokenService.php
└── LICENSE