2011-02-21 07:47:56 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
Validate logins, and simplify email password resets
Summary:
- There are some recent reports of login issues, see T755 and T754. I'm not
really sure what's going on, but this is an attempt at getting some more
information.
- When we login a user by setting 'phusr' and 'phsid', send them to
/login/validate/ to validate that the cookies actually got set.
- Do email password resets in two steps: first, log the user in. Redirect them
through validate, then give them the option to reset their password.
- Don't CSRF logged-out users. It technically sort of works most of the time
right now, but is silly. If we need logged-out CSRF we should generate it in
some more reliable way.
Test Plan:
- Logged in with username/password.
- Logged in with OAuth.
- Logged in with email password reset.
- Sent bad values to /login/validate/, got appropriate errors.
- Reset password.
- Verified next_uri still works.
Reviewers: btrahan, jungejason
Reviewed By: btrahan
CC: aran, btrahan, j3kuntz
Maniphest Tasks: T754, T755
Differential Revision: https://secure.phabricator.com/D1353
2012-01-10 23:42:07 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-02-21 07:47:56 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-03-10 00:46:25 +01:00
|
|
|
final class PhabricatorOAuthLoginController
|
|
|
|
extends PhabricatorAuthController {
|
2011-02-21 07:47:56 +01:00
|
|
|
|
|
|
|
private $provider;
|
|
|
|
private $userID;
|
2011-02-28 04:47:22 +01:00
|
|
|
|
2011-02-21 07:47:56 +01:00
|
|
|
private $accessToken;
|
2011-02-22 19:24:49 +01:00
|
|
|
private $tokenExpires;
|
2011-03-08 04:29:51 +01:00
|
|
|
private $oauthState;
|
2011-02-21 07:47:56 +01:00
|
|
|
|
|
|
|
public function shouldRequireLogin() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->provider = PhabricatorOAuthProvider::newProvider($data['provider']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$current_user = $this->getRequest()->getUser();
|
|
|
|
|
|
|
|
$provider = $this->provider;
|
|
|
|
if (!$provider->isProviderEnabled()) {
|
|
|
|
return new Aphront400Response();
|
|
|
|
}
|
|
|
|
|
2012-02-05 05:48:19 +01:00
|
|
|
$provider_name = $provider->getProviderName();
|
2011-02-22 07:51:34 +01:00
|
|
|
$provider_key = $provider->getProviderKey();
|
|
|
|
|
2011-02-21 07:47:56 +01:00
|
|
|
$request = $this->getRequest();
|
|
|
|
|
|
|
|
if ($request->getStr('error')) {
|
|
|
|
$error_view = id(new PhabricatorOAuthFailureView())
|
|
|
|
->setRequest($request);
|
|
|
|
return $this->buildErrorResponse($error_view);
|
|
|
|
}
|
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
$error_response = $this->retrieveAccessToken($provider);
|
|
|
|
if ($error_response) {
|
|
|
|
return $error_response;
|
2011-02-21 07:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$userinfo_uri = new PhutilURI($provider->getUserInfoURI());
|
2012-05-08 21:08:05 +02:00
|
|
|
$userinfo_uri->setQueryParam('access_token', $this->accessToken);
|
2011-02-21 07:47:56 +01:00
|
|
|
|
2012-05-01 11:51:40 +02:00
|
|
|
try {
|
|
|
|
$user_data = @file_get_contents($userinfo_uri);
|
|
|
|
$provider->setUserData($user_data);
|
|
|
|
} catch (PhabricatorOAuthProviderException $e) {
|
|
|
|
return $this->buildErrorResponse(new PhabricatorOAuthFailureView());
|
|
|
|
}
|
2011-02-28 04:47:22 +01:00
|
|
|
$provider->setAccessToken($this->accessToken);
|
2011-02-21 07:47:56 +01:00
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
$user_id = $provider->retrieveUserID();
|
|
|
|
$provider_key = $provider->getProviderKey();
|
2011-02-21 07:47:56 +01:00
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
$oauth_info = $this->retrieveOAuthInfo($provider);
|
2011-02-22 07:51:34 +01:00
|
|
|
|
|
|
|
if ($current_user->getPHID()) {
|
2011-02-28 04:47:22 +01:00
|
|
|
if ($oauth_info->getID()) {
|
|
|
|
if ($oauth_info->getUserID() != $current_user->getID()) {
|
2011-02-22 07:51:34 +01:00
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($current_user);
|
|
|
|
$dialog->setTitle('Already Linked to Another Account');
|
|
|
|
$dialog->appendChild(
|
2012-02-05 05:48:19 +01:00
|
|
|
hsprintf(
|
|
|
|
'<p>The %s account you just authorized is already linked to '.
|
|
|
|
'another Phabricator account. Before you can associate your %s '.
|
|
|
|
'account with this Phabriactor account, you must unlink it from '.
|
|
|
|
'the Phabricator account it is currently linked to.</p>',
|
|
|
|
$provider_name,
|
|
|
|
$provider_name));
|
2011-02-22 07:51:34 +01:00
|
|
|
$dialog->addCancelButton('/settings/page/'.$provider_key.'/');
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
} else {
|
2012-04-19 06:30:40 +02:00
|
|
|
$this->saveOAuthInfo($oauth_info); // Refresh token.
|
2011-02-22 07:51:34 +01:00
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI('/settings/page/'.$provider_key.'/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
$existing_oauth = id(new PhabricatorUserOAuthInfo())->loadOneWhere(
|
|
|
|
'userID = %d AND oauthProvider = %s',
|
|
|
|
$current_user->getID(),
|
|
|
|
$provider_key);
|
|
|
|
|
|
|
|
if ($existing_oauth) {
|
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($current_user);
|
|
|
|
$dialog->setTitle('Already Linked to an Account From This Provider');
|
|
|
|
$dialog->appendChild(
|
2012-02-05 05:48:19 +01:00
|
|
|
hsprintf(
|
|
|
|
'<p>The account you are logged in with is already linked to a %s '.
|
|
|
|
'account. Before you can link it to a different %s account, you '.
|
|
|
|
'must unlink the old account.</p>',
|
|
|
|
$provider_name,
|
|
|
|
$provider_name));
|
2011-02-28 04:47:22 +01:00
|
|
|
$dialog->addCancelButton('/settings/page/'.$provider_key.'/');
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
2011-02-22 07:51:34 +01:00
|
|
|
if (!$request->isDialogFormPost()) {
|
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($current_user);
|
2012-02-05 05:48:19 +01:00
|
|
|
$dialog->setTitle('Link '.$provider_name.' Account');
|
2011-02-22 07:51:34 +01:00
|
|
|
$dialog->appendChild(
|
2012-02-05 05:48:19 +01:00
|
|
|
hsprintf(
|
|
|
|
'<p>Link your %s account to your Phabricator account?</p>',
|
|
|
|
$provider_name));
|
2012-05-08 21:08:05 +02:00
|
|
|
$dialog->addHiddenInput('confirm_token', $provider->getAccessToken());
|
2011-02-28 04:47:22 +01:00
|
|
|
$dialog->addHiddenInput('expires', $oauth_info->getTokenExpires());
|
2011-03-08 04:29:51 +01:00
|
|
|
$dialog->addHiddenInput('state', $this->oauthState);
|
2012-05-08 21:08:05 +02:00
|
|
|
$dialog->addHiddenInput('scope', $oauth_info->getTokenScope());
|
2011-02-22 07:51:34 +01:00
|
|
|
$dialog->addSubmitButton('Link Accounts');
|
|
|
|
$dialog->addCancelButton('/settings/page/'.$provider_key.'/');
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
$oauth_info->setUserID($current_user->getID());
|
Create AphrontWriteGuard, a backup mechanism for CSRF validation
Summary:
Provide a catchall mechanism to find unprotected writes.
- Depends on D758.
- Similar to WriteOnHTTPGet stuff from Facebook's stack.
- Since we have a small number of storage mechanisms and highly structured
read/write pathways, we can explicitly answer the question "is this page
performing a write?".
- Never allow writes without CSRF checks.
- This will probably break some things. That's fine: they're CSRF
vulnerabilities or weird edge cases that we can fix. But don't push to Facebook
for a few days unless you're prepared to deal with this.
- **>>> MEGADERP: All Conduit write APIs are currently vulnerable to CSRF!
<<<**
Test Plan:
- Ran some scripts that perform writes (scripts/search indexers), no issues.
- Performed normal CSRF submits.
- Added writes to an un-CSRF'd page, got an exception.
- Executed conduit methods.
- Did login/logout (this works because the logged-out user validates the
logged-out csrf "token").
- Did OAuth login.
- Did OAuth registration.
Reviewers: pedram, andrewjcg, erling, jungejason, tuomaspelkonen, aran,
codeblock
Commenters: pedram
CC: aran, epriestley, pedram
Differential Revision: 777
2011-08-03 20:49:27 +02:00
|
|
|
|
|
|
|
$this->saveOAuthInfo($oauth_info);
|
2011-02-22 07:51:34 +01:00
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI('/settings/page/'.$provider_key.'/');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Login with known auth.
|
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
if ($oauth_info->getID()) {
|
2011-08-20 00:22:30 +02:00
|
|
|
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
|
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
$known_user = id(new PhabricatorUser())->load($oauth_info->getUserID());
|
2011-02-28 19:15:42 +01:00
|
|
|
|
|
|
|
$request->getApplicationConfiguration()->willAuthenticateUserWithOAuth(
|
|
|
|
$known_user,
|
|
|
|
$oauth_info,
|
|
|
|
$provider);
|
|
|
|
|
2011-02-21 07:47:56 +01:00
|
|
|
$session_key = $known_user->establishSession('web');
|
2011-02-22 19:24:49 +01:00
|
|
|
|
Create AphrontWriteGuard, a backup mechanism for CSRF validation
Summary:
Provide a catchall mechanism to find unprotected writes.
- Depends on D758.
- Similar to WriteOnHTTPGet stuff from Facebook's stack.
- Since we have a small number of storage mechanisms and highly structured
read/write pathways, we can explicitly answer the question "is this page
performing a write?".
- Never allow writes without CSRF checks.
- This will probably break some things. That's fine: they're CSRF
vulnerabilities or weird edge cases that we can fix. But don't push to Facebook
for a few days unless you're prepared to deal with this.
- **>>> MEGADERP: All Conduit write APIs are currently vulnerable to CSRF!
<<<**
Test Plan:
- Ran some scripts that perform writes (scripts/search indexers), no issues.
- Performed normal CSRF submits.
- Added writes to an un-CSRF'd page, got an exception.
- Executed conduit methods.
- Did login/logout (this works because the logged-out user validates the
logged-out csrf "token").
- Did OAuth login.
- Did OAuth registration.
Reviewers: pedram, andrewjcg, erling, jungejason, tuomaspelkonen, aran,
codeblock
Commenters: pedram
CC: aran, epriestley, pedram
Differential Revision: 777
2011-08-03 20:49:27 +02:00
|
|
|
$this->saveOAuthInfo($oauth_info);
|
2011-02-22 19:24:49 +01:00
|
|
|
|
2011-02-21 07:47:56 +01:00
|
|
|
$request->setCookie('phusr', $known_user->getUsername());
|
|
|
|
$request->setCookie('phsid', $session_key);
|
Validate logins, and simplify email password resets
Summary:
- There are some recent reports of login issues, see T755 and T754. I'm not
really sure what's going on, but this is an attempt at getting some more
information.
- When we login a user by setting 'phusr' and 'phsid', send them to
/login/validate/ to validate that the cookies actually got set.
- Do email password resets in two steps: first, log the user in. Redirect them
through validate, then give them the option to reset their password.
- Don't CSRF logged-out users. It technically sort of works most of the time
right now, but is silly. If we need logged-out CSRF we should generate it in
some more reliable way.
Test Plan:
- Logged in with username/password.
- Logged in with OAuth.
- Logged in with email password reset.
- Sent bad values to /login/validate/, got appropriate errors.
- Reset password.
- Verified next_uri still works.
Reviewers: btrahan, jungejason
Reviewed By: btrahan
CC: aran, btrahan, j3kuntz
Maniphest Tasks: T754, T755
Differential Revision: https://secure.phabricator.com/D1353
2012-01-10 23:42:07 +01:00
|
|
|
|
|
|
|
$uri = new PhutilURI('/login/validate/');
|
|
|
|
$uri->setQueryParams(
|
|
|
|
array(
|
|
|
|
'phusr' => $known_user->getUsername(),
|
|
|
|
));
|
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())->setURI((string)$uri);
|
2011-02-21 07:47:56 +01:00
|
|
|
}
|
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
$oauth_email = $provider->retrieveUserEmail();
|
2011-02-21 07:47:56 +01:00
|
|
|
if ($oauth_email) {
|
2012-05-07 19:29:33 +02:00
|
|
|
$known_email = id(new PhabricatorUserEmail())
|
|
|
|
->loadOneWhere('address = %s', $oauth_email);
|
2011-02-21 07:47:56 +01:00
|
|
|
if ($known_email) {
|
2011-02-22 19:24:49 +01:00
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($current_user);
|
|
|
|
$dialog->setTitle('Already Linked to Another Account');
|
|
|
|
$dialog->appendChild(
|
2012-02-05 05:48:19 +01:00
|
|
|
hsprintf(
|
|
|
|
'<p>The %s account you just authorized has an email address which '.
|
|
|
|
'is already in use by another Phabricator account. To link the '.
|
|
|
|
'accounts, log in to your Phabricator account and then go to '.
|
|
|
|
'Settings.</p>',
|
|
|
|
$provider_name));
|
2011-02-22 19:24:49 +01:00
|
|
|
$dialog->addCancelButton('/login/');
|
2011-02-21 07:47:56 +01:00
|
|
|
|
2011-02-22 19:24:49 +01:00
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
2011-02-21 07:47:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
if (!$provider->isProviderRegistrationEnabled()) {
|
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($current_user);
|
2012-02-05 05:48:19 +01:00
|
|
|
$dialog->setTitle('No Account Registration With '.$provider_name);
|
2011-02-28 04:47:22 +01:00
|
|
|
$dialog->appendChild(
|
2012-02-05 05:48:19 +01:00
|
|
|
hsprintf(
|
|
|
|
'<p>You can not register a new account using %s; you can only use '.
|
|
|
|
'your %s account to log into an existing Phabricator account which '.
|
|
|
|
'you have registered through other means.</p>',
|
|
|
|
$provider_name,
|
|
|
|
$provider_name));
|
2011-02-28 04:47:22 +01:00
|
|
|
$dialog->addCancelButton('/login/');
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
2011-02-21 07:47:56 +01:00
|
|
|
}
|
|
|
|
|
2012-03-22 21:43:50 +01:00
|
|
|
$controller = PhabricatorEnv::newObjectFromConfig(
|
2012-03-26 19:03:32 +02:00
|
|
|
'controller.oauth-registration',
|
2012-03-22 21:43:50 +01:00
|
|
|
array($this->getRequest()));
|
2011-02-21 07:47:56 +01:00
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
$controller->setOAuthProvider($provider);
|
|
|
|
$controller->setOAuthInfo($oauth_info);
|
2011-03-08 04:29:51 +01:00
|
|
|
$controller->setOAuthState($this->oauthState);
|
2011-02-21 07:47:56 +01:00
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
return $this->delegateToController($controller);
|
2011-02-21 07:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function buildErrorResponse(PhabricatorOAuthFailureView $view) {
|
|
|
|
$provider = $this->provider;
|
|
|
|
|
|
|
|
$provider_name = $provider->getProviderName();
|
|
|
|
$view->setOAuthProvider($provider);
|
|
|
|
|
|
|
|
return $this->buildStandardPageResponse(
|
|
|
|
$view,
|
|
|
|
array(
|
|
|
|
'title' => $provider_name.' Auth Failed',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
private function retrieveAccessToken(PhabricatorOAuthProvider $provider) {
|
|
|
|
$request = $this->getRequest();
|
2011-02-21 07:47:56 +01:00
|
|
|
|
2012-05-08 21:08:05 +02:00
|
|
|
$token = $request->getStr('confirm_token');
|
2011-02-28 04:47:22 +01:00
|
|
|
if ($token) {
|
|
|
|
$this->tokenExpires = $request->getInt('expires');
|
2012-05-08 21:08:05 +02:00
|
|
|
$this->accessToken = $token;
|
|
|
|
$this->oauthState = $request->getStr('state');
|
2011-02-28 04:47:22 +01:00
|
|
|
return null;
|
|
|
|
}
|
2011-02-21 07:47:56 +01:00
|
|
|
|
2012-05-08 21:08:05 +02:00
|
|
|
$client_id = $provider->getClientID();
|
|
|
|
$client_secret = $provider->getClientSecret();
|
|
|
|
$redirect_uri = $provider->getRedirectURI();
|
|
|
|
$auth_uri = $provider->getTokenURI();
|
2011-02-28 04:47:22 +01:00
|
|
|
|
|
|
|
$code = $request->getStr('code');
|
|
|
|
$query_data = array(
|
|
|
|
'client_id' => $client_id,
|
|
|
|
'client_secret' => $client_secret,
|
|
|
|
'redirect_uri' => $redirect_uri,
|
|
|
|
'code' => $code,
|
Add Google as an OAuth2 provider (BETA)
Summary:
This is pretty straightforward, except:
- We need to request read/write access to the address book to get the account
ID (which we MUST have) and real name, email and account name (which we'd like
to have). This is way more access than we should need, but there's apparently no
"get_loggedin_user_basic_information" type of call in the Google API suite (or,
at least, I couldn't find one).
- We can't get the profile picture or profile URI since there's no Plus API
access and Google users don't have meaningful public pages otherwise.
- Google doesn't save the fact that you've authorized the app, so every time
you want to login you need to reaffirm that you want to give us silly amounts of
access. Phabricator sessions are pretty long-duration though so this shouldn't
be a major issue.
Test Plan:
- Registered, logged out, and logged in with Google.
- Registered, logged out, and logged in with Facebook / Github to make sure I
didn't break anything.
- Linked / unlinked Google accounts.
Reviewers: Makinde, jungejason, nh, tuomaspelkonen, aran
Reviewed By: aran
CC: aran, epriestley, Makinde
Differential Revision: 916
2011-09-09 01:37:22 +02:00
|
|
|
) + $provider->getExtraTokenParameters();
|
2011-02-28 04:47:22 +01:00
|
|
|
|
2012-04-19 23:22:59 +02:00
|
|
|
$post_data = http_build_query($query_data, '', '&');
|
2011-02-28 04:47:22 +01:00
|
|
|
$post_length = strlen($post_data);
|
|
|
|
|
|
|
|
$stream_context = stream_context_create(
|
|
|
|
array(
|
|
|
|
'http' => array(
|
|
|
|
'method' => 'POST',
|
|
|
|
'header' =>
|
|
|
|
"Content-Type: application/x-www-form-urlencoded\r\n".
|
|
|
|
"Content-Length: {$post_length}\r\n",
|
|
|
|
'content' => $post_data,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
|
|
|
|
$stream = fopen($auth_uri, 'r', false, $stream_context);
|
|
|
|
|
|
|
|
$response = false;
|
|
|
|
$meta = null;
|
|
|
|
if ($stream) {
|
|
|
|
$meta = stream_get_meta_data($stream);
|
|
|
|
$response = stream_get_contents($stream);
|
|
|
|
fclose($stream);
|
2011-02-21 07:47:56 +01:00
|
|
|
}
|
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
if ($response === false) {
|
|
|
|
return $this->buildErrorResponse(new PhabricatorOAuthFailureView());
|
2011-02-21 07:47:56 +01:00
|
|
|
}
|
|
|
|
|
Add Google as an OAuth2 provider (BETA)
Summary:
This is pretty straightforward, except:
- We need to request read/write access to the address book to get the account
ID (which we MUST have) and real name, email and account name (which we'd like
to have). This is way more access than we should need, but there's apparently no
"get_loggedin_user_basic_information" type of call in the Google API suite (or,
at least, I couldn't find one).
- We can't get the profile picture or profile URI since there's no Plus API
access and Google users don't have meaningful public pages otherwise.
- Google doesn't save the fact that you've authorized the app, so every time
you want to login you need to reaffirm that you want to give us silly amounts of
access. Phabricator sessions are pretty long-duration though so this shouldn't
be a major issue.
Test Plan:
- Registered, logged out, and logged in with Google.
- Registered, logged out, and logged in with Facebook / Github to make sure I
didn't break anything.
- Linked / unlinked Google accounts.
Reviewers: Makinde, jungejason, nh, tuomaspelkonen, aran
Reviewed By: aran
CC: aran, epriestley, Makinde
Differential Revision: 916
2011-09-09 01:37:22 +02:00
|
|
|
$data = $provider->decodeTokenResponse($response);
|
2011-02-28 04:47:22 +01:00
|
|
|
|
|
|
|
$token = idx($data, 'access_token');
|
|
|
|
if (!$token) {
|
|
|
|
return $this->buildErrorResponse(new PhabricatorOAuthFailureView());
|
2011-02-22 19:24:49 +01:00
|
|
|
}
|
2011-02-28 04:47:22 +01:00
|
|
|
|
2012-05-08 21:08:05 +02:00
|
|
|
$this->tokenExpires = $provider->getTokenExpiryFromArray($data);
|
|
|
|
$this->accessToken = $token;
|
|
|
|
$this->oauthState = $request->getStr('state');
|
2011-02-28 04:47:22 +01:00
|
|
|
|
2011-02-22 19:24:49 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
private function retrieveOAuthInfo(PhabricatorOAuthProvider $provider) {
|
2011-02-21 07:47:56 +01:00
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
$oauth_info = id(new PhabricatorUserOAuthInfo())->loadOneWhere(
|
|
|
|
'oauthProvider = %s and oauthUID = %s',
|
|
|
|
$provider->getProviderKey(),
|
|
|
|
$provider->retrieveUserID());
|
2011-02-22 19:24:49 +01:00
|
|
|
|
2012-05-08 21:08:05 +02:00
|
|
|
$scope = $this->getRequest()->getStr('scope');
|
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
if (!$oauth_info) {
|
|
|
|
$oauth_info = new PhabricatorUserOAuthInfo();
|
|
|
|
$oauth_info->setOAuthProvider($provider->getProviderKey());
|
|
|
|
$oauth_info->setOAuthUID($provider->retrieveUserID());
|
2012-05-08 21:08:05 +02:00
|
|
|
// some providers don't tell you what scope you got, so default
|
|
|
|
// to the minimum Phabricator requires rather than assuming no scope
|
|
|
|
if (!$scope) {
|
|
|
|
$scope = $provider->getMinimumScope();
|
|
|
|
}
|
2011-02-28 04:47:22 +01:00
|
|
|
}
|
2011-02-22 19:24:49 +01:00
|
|
|
|
2011-02-28 04:47:22 +01:00
|
|
|
$oauth_info->setAccountURI($provider->retrieveUserAccountURI());
|
|
|
|
$oauth_info->setAccountName($provider->retrieveUserAccountName());
|
|
|
|
$oauth_info->setToken($provider->getAccessToken());
|
2011-02-22 19:24:49 +01:00
|
|
|
$oauth_info->setTokenStatus(PhabricatorUserOAuthInfo::TOKEN_STATUS_GOOD);
|
2012-05-08 21:08:05 +02:00
|
|
|
$oauth_info->setTokenScope($scope);
|
2011-02-22 19:24:49 +01:00
|
|
|
|
|
|
|
// If we have out-of-date expiration info, just clear it out. Then replace
|
|
|
|
// it with good info if the provider gave it to us.
|
|
|
|
$expires = $oauth_info->getTokenExpires();
|
|
|
|
if ($expires <= time()) {
|
|
|
|
$expires = null;
|
|
|
|
}
|
|
|
|
if ($this->tokenExpires) {
|
|
|
|
$expires = $this->tokenExpires;
|
|
|
|
}
|
|
|
|
$oauth_info->setTokenExpires($expires);
|
2011-02-28 04:47:22 +01:00
|
|
|
|
|
|
|
return $oauth_info;
|
2011-02-22 19:24:49 +01:00
|
|
|
}
|
|
|
|
|
Create AphrontWriteGuard, a backup mechanism for CSRF validation
Summary:
Provide a catchall mechanism to find unprotected writes.
- Depends on D758.
- Similar to WriteOnHTTPGet stuff from Facebook's stack.
- Since we have a small number of storage mechanisms and highly structured
read/write pathways, we can explicitly answer the question "is this page
performing a write?".
- Never allow writes without CSRF checks.
- This will probably break some things. That's fine: they're CSRF
vulnerabilities or weird edge cases that we can fix. But don't push to Facebook
for a few days unless you're prepared to deal with this.
- **>>> MEGADERP: All Conduit write APIs are currently vulnerable to CSRF!
<<<**
Test Plan:
- Ran some scripts that perform writes (scripts/search indexers), no issues.
- Performed normal CSRF submits.
- Added writes to an un-CSRF'd page, got an exception.
- Executed conduit methods.
- Did login/logout (this works because the logged-out user validates the
logged-out csrf "token").
- Did OAuth login.
- Did OAuth registration.
Reviewers: pedram, andrewjcg, erling, jungejason, tuomaspelkonen, aran,
codeblock
Commenters: pedram
CC: aran, epriestley, pedram
Differential Revision: 777
2011-08-03 20:49:27 +02:00
|
|
|
private function saveOAuthInfo(PhabricatorUserOAuthInfo $info) {
|
|
|
|
// UNGUARDED WRITES: Logging-in users don't have their CSRF set up yet.
|
|
|
|
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
|
|
|
|
$info->save();
|
|
|
|
}
|
2011-02-21 07:47:56 +01:00
|
|
|
}
|