3 final class PhabricatorAuthStartController
4 extends PhabricatorAuthController
{
6 public function shouldRequireLogin() {
10 public function handleRequest(AphrontRequest
$request) {
11 $viewer = $request->getUser();
13 if ($viewer->isLoggedIn()) {
14 // Kick the user home if they are already logged in.
15 return id(new AphrontRedirectResponse())->setURI('/');
18 if ($request->isAjax()) {
19 return $this->processAjaxRequest();
22 if ($request->isConduit()) {
23 return $this->processConduitRequest();
26 // If the user gets this far, they aren't logged in, so if they have a
27 // user session token we can conclude that it's invalid: if it was valid,
28 // they'd have been logged in above and never made it here. Try to clear
29 // it and warn the user they may need to nuke their cookies.
31 $session_token = $request->getCookie(PhabricatorCookies
::COOKIE_SESSION
);
32 $did_clear = $request->getStr('cleared');
34 if (strlen($session_token)) {
35 $kind = PhabricatorAuthSessionEngine
::getSessionKindFromToken(
38 case PhabricatorAuthSessionEngine
::KIND_ANONYMOUS
:
39 // If this is an anonymous session. It's expected that they won't
40 // be logged in, so we can just continue.
43 // The session cookie is invalid, so try to clear it.
44 $request->clearCookie(PhabricatorCookies
::COOKIE_USERNAME
);
45 $request->clearCookie(PhabricatorCookies
::COOKIE_SESSION
);
47 // We've previously tried to clear the cookie but we ended up back
48 // here, so it didn't work. Hard fatal instead of trying again.
50 return $this->renderError(
52 'Your login session is invalid, and clearing the session '.
53 'cookie was unsuccessful. Try clearing your browser cookies.'));
56 $redirect_uri = $request->getRequestURI();
57 $redirect_uri->replaceQueryParam('cleared', 1);
58 return id(new AphrontRedirectResponse())->setURI($redirect_uri);
62 // If we just cleared the session cookie and it worked, clean up after
63 // ourselves by redirecting to get rid of the "cleared" parameter. The
64 // the workflow will continue normally.
66 $redirect_uri = $request->getRequestURI();
67 $redirect_uri->removeQueryParam('cleared');
68 return id(new AphrontRedirectResponse())->setURI($redirect_uri);
71 $providers = PhabricatorAuthProvider
::getAllEnabledProviders();
72 foreach ($providers as $key => $provider) {
73 if (!$provider->shouldAllowLogin()) {
74 unset($providers[$key]);
79 foreach ($providers as $provider) {
80 $configs[] = $provider->getProviderConfig();
84 if ($this->isFirstTimeSetup()) {
85 // If this is a fresh install, let the user register their admin
87 return id(new AphrontRedirectResponse())
88 ->setURI($this->getApplicationURI('/register/'));
91 return $this->renderError(
93 'This server is not configured with any enabled authentication '.
94 'providers which can be used to log in. If you have accidentally '.
95 'locked yourself out by disabling all providers, you can use `%s` '.
96 'to recover access to an account.',
97 './bin/auth recover <username>'));
100 $next_uri = $request->getStr('next');
101 if (!strlen($next_uri)) {
102 if ($this->getDelegatingController()) {
103 // Only set a next URI from the request path if this controller was
104 // delegated to, which happens when a user tries to view a page which
105 // requires them to login.
107 // If this controller handled the request directly, we're on the main
108 // login page, and never want to redirect the user back here after they
110 $next_uri = (string)$this->getRequest()->getRequestURI();
114 if (!$request->isFormPost()) {
115 if (strlen($next_uri)) {
116 PhabricatorCookies
::setNextURICookie($request, $next_uri);
118 PhabricatorCookies
::setClientIDCookie($request);
121 $auto_response = $this->tryAutoLogin($providers);
122 if ($auto_response) {
123 return $auto_response;
126 $invite = $this->loadInvite();
128 $not_buttons = array();
129 $are_buttons = array();
130 $providers = msort($providers, 'getLoginOrder');
131 foreach ($providers as $provider) {
133 $form = $provider->buildInviteForm($this);
135 $form = $provider->buildLoginForm($this);
137 if ($provider->isLoginFormAButton()) {
138 $are_buttons[] = $form;
140 $not_buttons[] = $form;
145 $out[] = $not_buttons;
147 require_celerity_resource('auth-css');
149 foreach ($are_buttons as $key => $button) {
150 $are_buttons[$key] = phutil_tag(
153 'class' => 'phabricator-login-button mmb',
158 // If we only have one button, add a second pretend button so that we
159 // always have two columns. This makes it easier to get the alignments
160 // looking reasonable.
161 if (count($are_buttons) == 1) {
162 $are_buttons[] = null;
165 $button_columns = id(new AphrontMultiColumnView())
166 ->setFluidLayout(true);
167 $are_buttons = array_chunk($are_buttons, ceil(count($are_buttons) / 2));
168 foreach ($are_buttons as $column) {
169 $button_columns->addColumn($column);
175 'class' => 'phabricator-login-buttons',
180 $invite_message = null;
182 $invite_message = $this->renderInviteHeader($invite);
185 $custom_message = $this->newCustomStartMessage();
187 $email_login = $this->newEmailLoginView($configs);
189 $crumbs = $this->buildApplicationCrumbs();
190 $crumbs->addTextCrumb(pht('Login'));
191 $crumbs->setBorder(true);
193 $title = pht('Login');
201 return $this->newPage()
204 ->appendChild($view);
208 private function processAjaxRequest() {
209 $request = $this->getRequest();
210 $viewer = $request->getUser();
212 // We end up here if the user clicks a workflow link that they need to
213 // login to use. We give them a dialog saying "You need to login...".
215 if ($request->isDialogFormPost()) {
216 return id(new AphrontRedirectResponse())->setURI(
217 $request->getRequestURI());
220 // Often, users end up here by clicking a disabled action link in the UI
221 // (for example, they might click "Edit Subtasks" on a Maniphest task
222 // page). After they log in we want to send them back to that main object
223 // page if we can, since it's confusing to end up on a standalone page with
224 // only a dialog (particularly if that dialog is another error,
225 // like a policy exception).
227 $via_header = AphrontRequest
::getViaHeaderName();
228 $via_uri = AphrontRequest
::getHTTPHeader($via_header);
229 if (strlen($via_uri)) {
230 PhabricatorCookies
::setNextURICookie($request, $via_uri, $force = true);
233 return $this->newDialog()
234 ->setTitle(pht('Login Required'))
235 ->appendParagraph(pht('You must log in to take this action.'))
236 ->addSubmitButton(pht('Log In'))
237 ->addCancelButton('/');
241 private function processConduitRequest() {
242 $request = $this->getRequest();
243 $viewer = $request->getUser();
245 // A common source of errors in Conduit client configuration is getting
246 // the request path wrong. The client will end up here, so make some
247 // effort to give them a comprehensible error message.
249 $request_path = $this->getRequest()->getPath();
250 $conduit_path = '/api/<method>';
251 $example_path = '/api/conduit.ping';
254 'ERROR: You are making a Conduit API request to "%s", but the correct '.
255 'HTTP request path to use in order to access a Conduit method is "%s" '.
256 '(for example, "%s"). Check your configuration.',
261 return id(new AphrontPlainTextResponse())->setContent($message);
264 protected function renderError($message) {
265 return $this->renderErrorPage(
266 pht('Authentication Failure'),
270 private function tryAutoLogin(array $providers) {
271 $request = $this->getRequest();
273 // If the user just logged out, don't immediately log them in again.
274 if ($request->getURIData('loggedout')) {
278 // If we have more than one provider, we can't autologin because we
279 // don't know which one the user wants.
280 if (count($providers) != 1) {
284 $provider = head($providers);
285 if (!$provider->supportsAutoLogin()) {
289 $config = $provider->getProviderConfig();
290 if (!$config->getShouldAutoLogin()) {
294 $auto_uri = $provider->getAutoLoginURI($request);
296 return id(new AphrontRedirectResponse())
297 ->setIsExternal(true)
301 private function newEmailLoginView(array $configs) {
302 assert_instances_of($configs, 'PhabricatorAuthProviderConfig');
304 // Check if password auth is enabled. If it is, the password login form
305 // renders a "Forgot password?" link, so we don't need to provide a
306 // supplemental link.
308 $has_password = false;
309 foreach ($configs as $config) {
310 $provider = $config->getProvider();
311 if ($provider instanceof PhabricatorPasswordAuthProvider
) {
312 $has_password = true;
321 pht('Trouble logging in?'),
326 'href' => '/login/email/',
328 pht('Send a login link to your email address.')),
334 'class' => 'auth-custom-message',