The page you requested was not found.
'); $view = new PhabricatorStandardPageView(); $view->setTitle('404 Not Found'); $view->setRequest($this->getRequest()); $view->appendChild($failure); $response = new AphrontWebpageResponse(); $response->setContent($view->render()); $response->setHTTPResponseCode(404); return $response; } return $response; } public function build404Controller() { return array(new Phabricator404Controller($this->getRequest()), array()); } public function buildRedirectController($uri) { return array( new PhabricatorRedirectController($this->getRequest()), array( 'uri' => $uri, )); } private function renderStackTrace($trace) { $libraries = PhutilBootloader::getInstance()->getAllLibraries(); // TODO: Make this configurable? $host = 'https://secure.phabricator.com'; $browse = array( 'arcanist' => $host.'/diffusion/ARC/browse/origin:master/src/', 'phutil' => $host.'/diffusion/PHU/browse/origin:master/src/', 'phabricator' => $host.'/diffusion/P/browse/origin:master/src/', ); $rows = array(); $depth = count($trace); foreach ($trace as $part) { $lib = null; $file = idx($part, 'file'); $relative = $file; foreach ($libraries as $library) { $root = phutil_get_library_root($library); if (Filesystem::isDescendant($file, $root)) { $lib = $library; $relative = Filesystem::readablePath($file, $root); break; } } $where = ''; if (isset($part['class'])) { $where .= $part['class'].'::'; } if (isset($part['function'])) { $where .= $part['function'].'()'; } if ($file) { if (isset($browse[$lib])) { $file_name = phutil_render_tag( 'a', array( 'href' => $browse[$lib].$relative.'$'.$part['line'], 'title' => $file, 'target' => '_blank', ), phutil_escape_html($relative)); } else { $file_name = phutil_render_tag( 'span', array( 'title' => $file, ), phutil_escape_html($relative)); } $file_name = $file_name.' : '.(int)$part['line']; } else { $file_name = '(Internal)'; } $rows[] = array( $depth--, phutil_escape_html($lib), $file_name, phutil_escape_html($where), ); } $table = new AphrontTableView($rows); $table->setHeaders( array( 'Depth', 'Library', 'File', 'Where', )); $table->setColumnClasses( array( 'n', '', '', 'wide', )); return ''.$body.'
') ->appendChild( id(new AphrontFormSubmitControl()) ->setValue("{$button} \xC2\xBB")); $forms[$title] = $auth_form; } $panel = new AphrontPanelView(); $panel->setWidth(AphrontPanelView::WIDTH_FORM); foreach ($forms as $name => $form) { $panel->appendChild('The '.$provider_name.' account you just authorized '. 'is already linked to another Phabricator account. Before you can '. 'associate your '.$provider_name.' account with this Phabriactor '. 'account, you must unlink it from the Phabricator account it is '. 'currently linked to.
'); $dialog->addCancelButton('/settings/page/'.$provider_key.'/'); return id(new AphrontDialogResponse())->setDialog($dialog); } else { return id(new AphrontRedirectResponse()) ->setURI('/settings/page/'.$provider_key.'/'); } } $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( 'The account you are logged in with is already linked to a '. $provider_name.' account. Before you can link it to a different '. $provider_name.' account, you must unlink the old account.
'); $dialog->addCancelButton('/settings/page/'.$provider_key.'/'); return id(new AphrontDialogResponse())->setDialog($dialog); } if (!$request->isDialogFormPost()) { $dialog = new AphrontDialogView(); $dialog->setUser($current_user); $dialog->setTitle('Link '.$provider_name.' Account'); $dialog->appendChild( 'Link your '.$provider_name.' account to your Phabricator '. 'account?
'); $dialog->addHiddenInput('token', $provider->getAccessToken()); $dialog->addHiddenInput('expires', $oauth_info->getTokenExpires()); $dialog->addHiddenInput('state', $this->oauthState); $dialog->addSubmitButton('Link Accounts'); $dialog->addCancelButton('/settings/page/'.$provider_key.'/'); return id(new AphrontDialogResponse())->setDialog($dialog); } $oauth_info->setUserID($current_user->getID()); $this->saveOAuthInfo($oauth_info); return id(new AphrontRedirectResponse()) ->setURI('/settings/page/'.$provider_key.'/'); } $next_uri = $request->getCookie('next_uri', '/'); // Login with known auth. if ($oauth_info->getID()) { $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); $known_user = id(new PhabricatorUser())->load($oauth_info->getUserID()); $request->getApplicationConfiguration()->willAuthenticateUserWithOAuth( $known_user, $oauth_info, $provider); $session_key = $known_user->establishSession('web'); $this->saveOAuthInfo($oauth_info); $request->setCookie('phusr', $known_user->getUsername()); $request->setCookie('phsid', $session_key); $request->clearCookie('next_uri'); return id(new AphrontRedirectResponse()) ->setURI($next_uri); } $oauth_email = $provider->retrieveUserEmail(); if ($oauth_email) { $known_email = id(new PhabricatorUser()) ->loadOneWhere('email = %s', $oauth_email); if ($known_email) { $dialog = new AphrontDialogView(); $dialog->setUser($current_user); $dialog->setTitle('Already Linked to Another Account'); $dialog->appendChild( 'The '.$provider_name.' 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.
'); $dialog->addCancelButton('/login/'); return id(new AphrontDialogResponse())->setDialog($dialog); } } if (!$provider->isProviderRegistrationEnabled()) { $dialog = new AphrontDialogView(); $dialog->setUser($current_user); $dialog->setTitle('No Account Registration With '.$provider_name); $dialog->appendChild( 'You can not register a new account using '.$provider_name.'; '. 'you can only use your '.$provider_name.' account to log into an '. 'existing Phabricator account which you have registered through '. 'other means.
'); $dialog->addCancelButton('/login/'); return id(new AphrontDialogResponse())->setDialog($dialog); } $class = PhabricatorEnv::getEnvConfig('controller.oauth-registration'); PhutilSymbolLoader::loadClass($class); $controller = newv($class, array($this->getRequest())); $controller->setOAuthProvider($provider); $controller->setOAuthInfo($oauth_info); $controller->setOAuthState($this->oauthState); return $this->delegateToController($controller); } 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', )); } private function retrieveAccessToken(PhabricatorOAuthProvider $provider) { $request = $this->getRequest(); $token = $request->getStr('token'); if ($token) { $this->tokenExpires = $request->getInt('expires'); $this->accessToken = $token; $this->oauthState = $request->getStr('state'); return null; } $client_id = $provider->getClientID(); $client_secret = $provider->getClientSecret(); $redirect_uri = $provider->getRedirectURI(); $auth_uri = $provider->getTokenURI(); $code = $request->getStr('code'); $query_data = array( 'client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri, 'code' => $code, - ); + ) + $provider->getExtraTokenParameters(); $post_data = http_build_query($query_data); $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); } if ($response === false) { return $this->buildErrorResponse(new PhabricatorOAuthFailureView()); } - $data = array(); - parse_str($response, $data); + $data = $provider->decodeTokenResponse($response); $token = idx($data, 'access_token'); if (!$token) { return $this->buildErrorResponse(new PhabricatorOAuthFailureView()); } if (idx($data, 'expires')) { $this->tokenExpires = time() + $data['expires']; } $this->accessToken = $token; $this->oauthState = $request->getStr('state'); return null; } private function retrieveOAuthInfo(PhabricatorOAuthProvider $provider) { $oauth_info = id(new PhabricatorUserOAuthInfo())->loadOneWhere( 'oauthProvider = %s and oauthUID = %s', $provider->getProviderKey(), $provider->retrieveUserID()); if (!$oauth_info) { $oauth_info = new PhabricatorUserOAuthInfo(); $oauth_info->setOAuthProvider($provider->getProviderKey()); $oauth_info->setOAuthUID($provider->retrieveUserID()); } $oauth_info->setAccountURI($provider->retrieveUserAccountURI()); $oauth_info->setAccountName($provider->retrieveUserAccountName()); $oauth_info->setToken($provider->getAccessToken()); $oauth_info->setTokenStatus(PhabricatorUserOAuthInfo::TOKEN_STATUS_GOOD); // 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); return $oauth_info; } private function saveOAuthInfo(PhabricatorUserOAuthInfo $info) { // UNGUARDED WRITES: Logging-in users don't have their CSRF set up yet. $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); $info->save(); } } diff --git a/src/applications/auth/oauth/provider/base/PhabricatorOAuthProvider.php b/src/applications/auth/oauth/provider/base/PhabricatorOAuthProvider.php index f5124eaf81..a2065959eb 100644 --- a/src/applications/auth/oauth/provider/base/PhabricatorOAuthProvider.php +++ b/src/applications/auth/oauth/provider/base/PhabricatorOAuthProvider.php @@ -1,87 +1,121 @@ accessToken = $access_token; return $this; } final public function getAccessToken() { return $this->accessToken; } public static function newProvider($which) { switch ($which) { case self::PROVIDER_FACEBOOK: $class = 'PhabricatorOAuthProviderFacebook'; break; case self::PROVIDER_GITHUB: $class = 'PhabricatorOAuthProviderGithub'; break; + case self::PROVIDER_GOOGLE: + $class = 'PhabricatorOAuthProviderGoogle'; + break; default: throw new Exception('Unknown OAuth provider.'); } PhutilSymbolLoader::loadClass($class); return newv($class, array()); } public static function getAllProviders() { $all = array( self::PROVIDER_FACEBOOK, self::PROVIDER_GITHUB, + self::PROVIDER_GOOGLE, ); $providers = array(); foreach ($all as $provider) { $providers[$provider] = self::newProvider($provider); } return $providers; } } diff --git a/src/applications/auth/oauth/provider/facebook/PhabricatorOAuthProviderFacebook.php b/src/applications/auth/oauth/provider/facebook/PhabricatorOAuthProviderFacebook.php index ec97334a1f..aa305fe09f 100644 --- a/src/applications/auth/oauth/provider/facebook/PhabricatorOAuthProviderFacebook.php +++ b/src/applications/auth/oauth/provider/facebook/PhabricatorOAuthProviderFacebook.php @@ -1,106 +1,106 @@ userData = $data; + $this->userData = json_decode($data, true); return $this; } public function retrieveUserID() { return $this->userData['id']; } public function retrieveUserEmail() { return $this->userData['email']; } public function retrieveUserAccountName() { $matches = null; $link = $this->userData['link']; if (preg_match('@/([a-zA-Z0-9]+)$@', $link, $matches)) { return $matches[1]; } return null; } public function retrieveUserProfileImage() { $uri = 'https://graph.facebook.com/me/picture?access_token='; return @file_get_contents($uri.$this->getAccessToken()); } public function retrieveUserAccountURI() { return $this->userData['link']; } public function retrieveUserRealName() { return $this->userData['name']; } } diff --git a/src/applications/auth/oauth/provider/github/PhabricatorOAuthProviderGithub.php b/src/applications/auth/oauth/provider/github/PhabricatorOAuthProviderGithub.php index f6e9759d19..e41d7a76f3 100644 --- a/src/applications/auth/oauth/provider/github/PhabricatorOAuthProviderGithub.php +++ b/src/applications/auth/oauth/provider/github/PhabricatorOAuthProviderGithub.php @@ -1,109 +1,109 @@ userData = $data['user']; + $this->userData = idx(json_decode($data, true), 'user'); return $this; } public function retrieveUserID() { return $this->userData['id']; } public function retrieveUserEmail() { return idx($this->userData, 'email'); } public function retrieveUserAccountName() { return $this->userData['login']; } public function retrieveUserProfileImage() { $id = $this->userData['gravatar_id']; if ($id) { $uri = 'http://www.gravatar.com/avatar/'.$id.'?s=50'; return @file_get_contents($uri); } return null; } public function retrieveUserAccountURI() { $username = $this->retrieveUserAccountName(); if ($username) { return 'https://github.com/'.$username; } return null; } public function retrieveUserRealName() { return idx($this->userData, 'name'); } } diff --git a/src/applications/auth/oauth/provider/google/PhabricatorOAuthProviderGoogle.php b/src/applications/auth/oauth/provider/google/PhabricatorOAuthProviderGoogle.php new file mode 100644 index 0000000000..f9b3a57608 --- /dev/null +++ b/src/applications/auth/oauth/provider/google/PhabricatorOAuthProviderGoogle.php @@ -0,0 +1,136 @@ +id; + $this->userData = array( + 'id' => $id, + 'email' => (string)$xml->author[0]->email, + 'real' => (string)$xml->author[0]->name, + + // Guess account name from email address, this is just a hint anyway. + 'account' => head(explode('@', $id)), + ); + return $this; + } + + public function retrieveUserID() { + return $this->userData['id']; + } + + public function retrieveUserEmail() { + return $this->userData['email']; + } + + public function retrieveUserAccountName() { + return $this->userData['account']; + } + + public function retrieveUserProfileImage() { + // No apparent API access to Plus yet. + return null; + } + + public function retrieveUserAccountURI() { + // No apparent API access to Plus yet. + return null; + } + + public function retrieveUserRealName() { + return $this->userData['real']; + } + + public function getExtraAuthParameters() { + return array( + 'response_type' => 'code', + ); + } + + public function getExtraTokenParameters() { + return array( + 'grant_type' => 'authorization_code', + ); + + } + + public function decodeTokenResponse($response) { + return json_decode($response, true); + } + +} diff --git a/src/applications/auth/oauth/provider/google/__init__.php b/src/applications/auth/oauth/provider/google/__init__.php new file mode 100644 index 0000000000..92eeccc3bc --- /dev/null +++ b/src/applications/auth/oauth/provider/google/__init__.php @@ -0,0 +1,15 @@ +provider = $oauth_provider; return $this; } public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $provider = $this->provider; $notice = null; $provider_name = $provider->getProviderName(); $provider_key = $provider->getProviderKey(); $oauth_info = id(new PhabricatorUserOAuthInfo())->loadOneWhere( 'userID = %d AND oauthProvider = %s', $user->getID(), $provider->getProviderKey()); $form = new AphrontFormView(); $form ->setUser($user); $forms = array(); $forms[] = $form; if (!$oauth_info) { $form ->appendChild( 'There is currently no '. $provider_name.' account linked to your Phabricator account. You '. 'can link an account, which will allow you to use it to log into '. 'Phabricator.
'); switch ($provider_key) { case PhabricatorOAuthProvider::PROVIDER_GITHUB: $form->appendChild( 'Additionally, you must '. 'link your Github account before Phabricator can access any '. 'information about hosted repositories.
'); break; } $auth_uri = $provider->getAuthURI(); $client_id = $provider->getClientID(); $redirect_uri = $provider->getRedirectURI(); + $minimum_scope = $provider->getMinimumScope(); $form ->setAction($auth_uri) ->setMethod('GET') ->addHiddenInput('redirect_uri', $redirect_uri) ->addHiddenInput('client_id', $client_id) + ->addHiddenInput('scope', $minimum_scope); + + foreach ($provider->getExtraAuthParameters() as $key => $value) { + $form->addHiddenInput($key, $value); + } + + $form ->appendChild( id(new AphrontFormSubmitControl()) ->setValue('Link '.$provider_name." Account \xC2\xBB")); } else { $form ->appendChild( 'Your account is linked with '. 'a '.$provider_name.' account. You may use your '.$provider_name.' '. 'credentials to log into Phabricator.
') ->appendChild( id(new AphrontFormStaticControl()) ->setLabel($provider_name.' ID') ->setValue($oauth_info->getOAuthUID())) ->appendChild( id(new AphrontFormStaticControl()) ->setLabel($provider_name.' Name') ->setValue($oauth_info->getAccountName())) ->appendChild( id(new AphrontFormStaticControl()) ->setLabel($provider_name.' URI') ->setValue($oauth_info->getAccountURI())); if (!$provider->isProviderLinkPermanent()) { $unlink = 'Unlink '.$provider_name.' Account'; $unlink_form = new AphrontFormView(); $unlink_form ->setUser($user) ->appendChild( 'You may unlink this account '. 'from your '.$provider_name.' account. This will prevent you from '. 'logging in with your '.$provider_name.' credentials.
') ->appendChild( id(new AphrontFormSubmitControl()) ->addCancelButton('/oauth/'.$provider_key.'/unlink/', $unlink)); $forms['Unlink Account'] = $unlink_form; } $expires = $oauth_info->getTokenExpires(); if ($expires) { if ($expires <= time()) { $expires = "Expired"; } else { $expires = phabricator_format_timestamp($expires); } } else { $expires = 'No Information Available'; } $scope = $oauth_info->getTokenScope(); if (!$scope) { $scope = 'No Information Available'; } $status = $oauth_info->getTokenStatus(); $status = PhabricatorUserOAuthInfo::getReadableTokenStatus($status); $token_form = new AphrontFormView(); $token_form ->setUser($user) ->appendChild( 'insert rap about tokens
') ->appendChild( id(new AphrontFormStaticControl()) ->setLabel('Token Status') ->setValue($status)) ->appendChild( id(new AphrontFormStaticControl()) ->setLabel('Expires') ->setValue($expires)) ->appendChild( id(new AphrontFormStaticControl()) ->setLabel('Scope') ->setValue($scope)); $forms['Account Token Information'] = $token_form; } $panel = new AphrontPanelView(); $panel->setHeader($provider_name.' Account Settings'); $panel->setWidth(AphrontPanelView::WIDTH_FORM); foreach ($forms as $name => $form) { if ($name) { $panel->appendChild('