1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-27 01:02:42 +01:00

Implement "auth.logout" Conduit API method

Summary:
Ref T7303. Ref T7673. This implements an "auth.logout" which:

  - terminates all web sessions;
  - terminates the current OAuth token if called via OAuth; and
  - may always be called via OAuth.

(Since it consumes an OAuth token, even a "malicious" OAuth application can't really be that much of a jerk with this: it can't continuously log you out, since calling the method once kills the token. The application would need to ask your permission again to get a fresh token.)

The primary goal here is to let Phacility instances call this against the Phacility upstream, so that when you log out of an instance it also logs you out of your Phacility account (possibly with a checkbox or something).

This also smooths over the session token code. Before this change, your sessions would get logged out but when you reloaded we'd tell you your session was invalid.

Instead, try to clear the invalid session before telling the user there's an issue. I think that ssentially 100% of invalid sessions are a result of something in this vein (e.g., forced logout via Settings) nowadays, since the session code is generally stable and sane and has been for a long time.

Test Plan:
  - Called `auth.logout` via console, got a reasonable logout experience.
  - Called `auth.logout` via OAuth.
    - Tried to make another call, verified OAuth token had been invalidated.
    - Verified web session had been invalidated.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T7303, T7673

Differential Revision: https://secure.phabricator.com/D15594
This commit is contained in:
epriestley 2016-04-03 09:06:26 -07:00
parent 60133b6fa5
commit e55522cade
5 changed files with 89 additions and 6 deletions

View file

@ -1807,6 +1807,7 @@ phutil_register_library_map(array(
'PhabricatorAuthListController' => 'applications/auth/controller/config/PhabricatorAuthListController.php', 'PhabricatorAuthListController' => 'applications/auth/controller/config/PhabricatorAuthListController.php',
'PhabricatorAuthLoginController' => 'applications/auth/controller/PhabricatorAuthLoginController.php', 'PhabricatorAuthLoginController' => 'applications/auth/controller/PhabricatorAuthLoginController.php',
'PhabricatorAuthLoginHandler' => 'applications/auth/handler/PhabricatorAuthLoginHandler.php', 'PhabricatorAuthLoginHandler' => 'applications/auth/handler/PhabricatorAuthLoginHandler.php',
'PhabricatorAuthLogoutConduitAPIMethod' => 'applications/auth/conduit/PhabricatorAuthLogoutConduitAPIMethod.php',
'PhabricatorAuthMainMenuBarExtension' => 'applications/auth/extension/PhabricatorAuthMainMenuBarExtension.php', 'PhabricatorAuthMainMenuBarExtension' => 'applications/auth/extension/PhabricatorAuthMainMenuBarExtension.php',
'PhabricatorAuthManagementCachePKCS8Workflow' => 'applications/auth/management/PhabricatorAuthManagementCachePKCS8Workflow.php', 'PhabricatorAuthManagementCachePKCS8Workflow' => 'applications/auth/management/PhabricatorAuthManagementCachePKCS8Workflow.php',
'PhabricatorAuthManagementLDAPWorkflow' => 'applications/auth/management/PhabricatorAuthManagementLDAPWorkflow.php', 'PhabricatorAuthManagementLDAPWorkflow' => 'applications/auth/management/PhabricatorAuthManagementLDAPWorkflow.php',
@ -6149,6 +6150,7 @@ phutil_register_library_map(array(
'PhabricatorAuthListController' => 'PhabricatorAuthProviderConfigController', 'PhabricatorAuthListController' => 'PhabricatorAuthProviderConfigController',
'PhabricatorAuthLoginController' => 'PhabricatorAuthController', 'PhabricatorAuthLoginController' => 'PhabricatorAuthController',
'PhabricatorAuthLoginHandler' => 'Phobject', 'PhabricatorAuthLoginHandler' => 'Phobject',
'PhabricatorAuthLogoutConduitAPIMethod' => 'PhabricatorAuthConduitAPIMethod',
'PhabricatorAuthMainMenuBarExtension' => 'PhabricatorMainMenuBarExtension', 'PhabricatorAuthMainMenuBarExtension' => 'PhabricatorMainMenuBarExtension',
'PhabricatorAuthManagementCachePKCS8Workflow' => 'PhabricatorAuthManagementWorkflow', 'PhabricatorAuthManagementCachePKCS8Workflow' => 'PhabricatorAuthManagementWorkflow',
'PhabricatorAuthManagementLDAPWorkflow' => 'PhabricatorAuthManagementWorkflow', 'PhabricatorAuthManagementLDAPWorkflow' => 'PhabricatorAuthManagementWorkflow',

View file

@ -0,0 +1,51 @@
<?php
final class PhabricatorAuthLogoutConduitAPIMethod
extends PhabricatorAuthConduitAPIMethod {
public function getAPIMethodName() {
return 'auth.logout';
}
public function getMethodSummary() {
return pht('Terminate all login sessions.');
}
public function getMethodDescription() {
return pht(
'Terminate all web login sessions. If called via OAuth, also terminate '.
'the current OAuth token.'.
"\n\n".
'WARNING: This method does what it claims on the label. If you call '.
'this method via the test console in the web UI, it will log you out!');
}
protected function defineParamTypes() {
return array();
}
protected function defineReturnType() {
return 'void';
}
public function getRequiredScope() {
return self::SCOPE_ALWAYS;
}
protected function execute(ConduitAPIRequest $request) {
$viewer = $request->getUser();
// Destroy all web sessions.
$engine = id(new PhabricatorAuthSessionEngine());
$engine->terminateLoginSessions($viewer);
// If we were called via OAuth, destroy the OAuth token.
$oauth_token = $request->getOAuthToken();
if ($oauth_token) {
$oauth_token->delete();
}
return null;
}
}

View file

@ -29,6 +29,7 @@ final class PhabricatorAuthStartController
// it and warn the user they may need to nuke their cookies. // it and warn the user they may need to nuke their cookies.
$session_token = $request->getCookie(PhabricatorCookies::COOKIE_SESSION); $session_token = $request->getCookie(PhabricatorCookies::COOKIE_SESSION);
$did_clear = $request->getStr('cleared');
if (strlen($session_token)) { if (strlen($session_token)) {
$kind = PhabricatorAuthSessionEngine::getSessionKindFromToken( $kind = PhabricatorAuthSessionEngine::getSessionKindFromToken(
@ -39,18 +40,34 @@ final class PhabricatorAuthStartController
// be logged in, so we can just continue. // be logged in, so we can just continue.
break; break;
default: default:
// The session cookie is invalid, so clear it. // The session cookie is invalid, so try to clear it.
$request->clearCookie(PhabricatorCookies::COOKIE_USERNAME); $request->clearCookie(PhabricatorCookies::COOKIE_USERNAME);
$request->clearCookie(PhabricatorCookies::COOKIE_SESSION); $request->clearCookie(PhabricatorCookies::COOKIE_SESSION);
return $this->renderError( // We've previously tried to clear the cookie but we ended up back
pht( // here, so it didn't work. Hard fatal instead of trying again.
'Your login session is invalid. Try reloading the page and '. if ($did_clear) {
'logging in again. If that does not work, clear your browser '. return $this->renderError(
'cookies.')); pht(
'Your login session is invalid, and clearing the session '.
'cookie was unsuccessful. Try clearing your browser cookies.'));
}
$redirect_uri = $request->getRequestURI();
$redirect_uri->setQueryParam('cleared', 1);
return id(new AphrontRedirectResponse())->setURI($redirect_uri);
} }
} }
// If we just cleared the session cookie and it worked, clean up after
// ourselves by redirecting to get rid of the "cleared" parameter. The
// the workflow will continue normally.
if ($did_clear) {
$redirect_uri = $request->getRequestURI();
$redirect_uri->setQueryParam('cleared', null);
return id(new AphrontRedirectResponse())->setURI($redirect_uri);
}
$providers = PhabricatorAuthProvider::getAllEnabledProviders(); $providers = PhabricatorAuthProvider::getAllEnabledProviders();
foreach ($providers as $key => $provider) { foreach ($providers as $key => $provider) {
if (!$provider->shouldAllowLogin()) { if (!$provider->shouldAllowLogin()) {

View file

@ -395,6 +395,8 @@ final class PhabricatorConduitAPIController
); );
} }
$api_request->setOAuthToken($token);
return $this->validateAuthenticatedUser( return $this->validateAuthenticatedUser(
$api_request, $api_request,
$user); $user);

View file

@ -5,6 +5,7 @@ final class ConduitAPIRequest extends Phobject {
protected $params; protected $params;
private $user; private $user;
private $isClusterRequest = false; private $isClusterRequest = false;
private $oauthToken;
public function __construct(array $params) { public function __construct(array $params) {
$this->params = $params; $this->params = $params;
@ -48,6 +49,16 @@ final class ConduitAPIRequest extends Phobject {
return $this->user; return $this->user;
} }
public function setOAuthToken(
PhabricatorOAuthServerAccessToken $oauth_token) {
$this->oauthToken = $oauth_token;
return $this;
}
public function getOAuthToken() {
return $this->oauthToken;
}
public function setIsClusterRequest($is_cluster_request) { public function setIsClusterRequest($is_cluster_request) {
$this->isClusterRequest = $is_cluster_request; $this->isClusterRequest = $is_cluster_request;
return $this; return $this;