PHP code example of socialiteproviders / okta
1. Go to this page and download the library: Download socialiteproviders/okta 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/ */
socialiteproviders / okta example snippets
'okta' => [
'base_url' => env('OKTA_BASE_URL'),
'client_id' => env('OKTA_CLIENT_ID'),
'client_secret' => env('OKTA_CLIENT_SECRET'),
'redirect' => env('OKTA_REDIRECT_URI')
],
$config = new \SocialiteProviders\Manager\Config(
'client_id',
'client_secret',
route('okta.callback'),
[
'base_url' => 'https://1234.okta.com',
]
);
\Laravel\Socialite\Facades\Socialite::driver('okta')
->setConfig($config)
->redirect();
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
$event->extendSocialite('okta', \SocialiteProviders\Okta\Provider::class);
});
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// ... other providers
\SocialiteProviders\Okta\OktaExtendSocialite::class.'@handle',
],
];
return Socialite::driver('okta')->redirect();
public function handleProviderCallback(\Illuminate\Http\Request $request)
{
$user = Socialite::driver('okta')->user();
$localUser = User::updateOrCreate(['email' => $user->email], [
'email' => $user->email,
'name' => $user->name,
'token' => $user->token,
'id_token' => $user->id_token,
'refresh_token' => $user->refreshToken,
]);
try {
Auth::login($localUser);
}
catch (\Throwable $e) {
return redirect('/login-okta');
}
return redirect('/home');
}
public function logout(\Illuminate\Http\Request $request)
{
$idToken = $request->user()->id_token;
$logoutUrl = Socialite::driver('okta')->getLogoutUrl($idToken, URL::to('/'));
Auth::logout();
return redirect($logoutUrl);
}
$localUser = Auth::user();
$response = (object) Socialite::driver('okta')
->setScopes(['offline_access'])
->getRefreshTokenResponse($localUser->refresh_token);
$localUser->token = $response->access_token;
$localUser->refresh_token = $response->refresh_token;
$localUser->save();
Auth::setUser($localUser);
$response = (object) Socialite::driver('okta')->getClientAccessTokenResponse();
$token = $response->access_token;
$repo = Socialite::driver('okta');
$repo->revokeToken($token, 'access_token');
// verify against introspection endpoint
$state = $repo->introspectToken($token, 'access_token');
if($state['active']){...};