Skip to content

Commit

Permalink
Fixed OIDC Logout
Browse files Browse the repository at this point in the history
  • Loading branch information
joancyho committed Aug 29, 2023
1 parent e176aae commit 6b55104
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example.complete
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ OIDC_GROUPS_CLAIM=groups
OIDC_REMOVE_FROM_GROUPS=false
OIDC_EXTERNAL_ID_CLAIM=sub

# OIDC Logout Feature: Its value should be value of end_session_endpoint from <issuer>/.well-known/openid-configuration
OIDC_END_SESSION_ENDPOINT=null


# Disable default third-party services such as Gravatar and Draw.IO
# Service-specific options will override this option
DISABLE_EXTERNAL_SERVICES=false
Expand Down
14 changes: 14 additions & 0 deletions app/Access/Controllers/OidcController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,18 @@ public function callback(Request $request)

return redirect()->intended();
}

/**
* OIDC Logout Feature: Start the authorization logout flow via OIDC.
*/
public function logout()
{
try {
return $this->oidcService->logout();
} catch (OidcException $exception) {
$this->showErrorNotification($exception->getMessage());
return redirect('/logout');
}
}

}
39 changes: 39 additions & 0 deletions app/Access/Oidc/OidcService.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ protected function processAccessTokenCallback(OidcAccessToken $accessToken, Oidc
$settings->keys,
);

// OIDC Logout Feature: Temporarily save token in session
$access_token_for_logout = $idTokenText;
session()->put("oidctoken", $access_token_for_logout);



$returnClaims = Theme::dispatch(ThemeEvents::OIDC_ID_TOKEN_PRE_VALIDATE, $idToken->getAllClaims(), [
'access_token' => $accessToken->getToken(),
'expires_in' => $accessToken->getExpires(),
Expand Down Expand Up @@ -283,4 +289,37 @@ protected function shouldSyncGroups(): bool
{
return $this->config()['user_to_groups'] !== false;
}


/**
* OIDC Logout Feature: Initiate a logout flow.
*
* @throws OidcException
*
* @return string
*/
public function logout() {

$config = $this->config();
$app_url = env('APP_URL', null);
$end_session_endpoint = $config["end_session_endpoint"];

$oidctoken = session()->get("oidctoken");
session()->invalidate();

if (str_contains($app_url, 'https://')) {
$protocol = 'https://';
} else {
$protocol = 'http://';
}



return redirect($end_session_endpoint.'?id_token_hint='.$oidctoken."&post_logout_redirect_uri=".$protocol.$_SERVER['HTTP_HOST']."/");


}



}
5 changes: 5 additions & 0 deletions app/Config/oidc.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@
'groups_claim' => env('OIDC_GROUPS_CLAIM', 'groups'),
// When syncing groups, remove any groups that no longer match. Otherwise sync only adds new groups.
'remove_from_groups' => env('OIDC_REMOVE_FROM_GROUPS', false),

// OIDC Logout Feature: OAuth2 end_session_endpoint
'end_session_endpoint' => env('OIDC_END_SESSION_ENDPOINT', null),

];

14 changes: 14 additions & 0 deletions resources/views/common/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,22 @@ class="mobile-menu-toggle hide-over-l">@icon('more')</button>
</a>
</li>
<li>
<?php
// OIDC Logout Feature: Use /oidc/logout if authentication method is oidc.
if (config('auth.method') === 'oidc') {
?>
<form action="/oidc/logout"
method="get">
<?php
// OIDC Logout Feature: Use /oidc/logout if authentication method is oidc.
} else {
?>
<form action="{{ url(config('auth.method') === 'saml2' ? '/saml2/logout' : '/logout') }}"
method="post">
<?php
// OIDC Logout Feature: Use /oidc/logout if authentication method is oidc.
}
?>
{{ csrf_field() }}
<button class="icon-item" data-shortcut="logout">
@icon('logout')
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@
// OIDC routes
Route::post('/oidc/login', [AccessControllers\OidcController::class, 'login']);
Route::get('/oidc/callback', [AccessControllers\OidcController::class, 'callback']);
// OIDC Logout Feature: Added to cater OIDC logout
Route::get('/oidc/logout', [AccessControllers\OidcController::class, 'logout']);

// User invitation routes
Route::get('/register/invite/{token}', [AccessControllers\UserInviteController::class, 'showSetPassword']);
Expand Down

0 comments on commit 6b55104

Please sign in to comment.