friendica/src/Module/Security/Login.php

239 lines
6.9 KiB
PHP
Raw Normal View History

2018-01-11 01:06:00 +01:00
<?php
2018-01-21 19:33:59 +01:00
/**
* @copyright Copyright (C) 2010-2024, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
2018-01-21 19:33:59 +01:00
*/
namespace Friendica\Module\Security;
2018-01-11 01:06:00 +01:00
use Friendica\App;
2018-01-11 01:06:00 +01:00
use Friendica\BaseModule;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\DI;
use Friendica\Module\Register;
use Friendica\Module\Response;
use Friendica\Security\Authentication;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
2018-01-11 01:06:00 +01:00
/**
* Login module
*/
class Login extends BaseModule
{
/** @var Authentication */
private $auth;
/** @var IManageConfigValues */
private $config;
/** @var IHandleUserSessions */
private $session;
public function __construct(Authentication $auth, IManageConfigValues $config, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
{
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->auth = $auth;
$this->config = $config;
$this->session = $session;
}
protected function content(array $request = []): string
2018-01-11 01:06:00 +01:00
{
$return_path = $request['return_path'] ?? $this->session->pop('return_path', '') ;
2021-05-11 08:30:20 +02:00
if ($this->session->getLocalUserId()) {
$this->baseUrl->redirect($return_path);
2018-01-11 01:06:00 +01:00
}
return self::form($return_path, \Friendica\Module\Register::getPolicy() !== \Friendica\Module\Register::CLOSED);
2018-01-11 01:06:00 +01:00
}
protected function post(array $request = [])
2018-01-11 01:06:00 +01:00
{
$this->session->clear();
2019-01-13 19:03:13 +01:00
2018-01-11 01:06:00 +01:00
// OpenId Login
if (
empty($request['password'])
&& (!empty($request['openid_url'])
|| !empty($request['username']))
2018-01-11 01:06:00 +01:00
) {
$openid_url = trim(($request['openid_url'] ?? '') ?: $request['username']);
2018-01-11 01:06:00 +01:00
$this->auth->withOpenId($openid_url, !empty($request['remember']));
}
2018-01-11 01:06:00 +01:00
if (!empty($request['auth-params']) && $request['auth-params'] === 'login') {
$this->auth->withPassword(
DI::app(),
trim($request['username']),
trim($request['password']),
!empty($request['remember']),
$request['return_path'] ?? ''
);
}
}
2018-01-11 01:06:00 +01:00
/**
2020-01-19 07:05:23 +01:00
* Wrapper for adding a login box.
2018-01-11 01:06:00 +01:00
*
* @param string|null $return_path The path relative to the base the user should be sent back to after login completes.
* @param bool $register If $register == true provide a registration link.
* This will almost always depend on the value of config.register_policy.
2018-01-11 01:06:00 +01:00
*
* @return string Returns the complete html for inserting into the page
*
2019-01-06 22:06:53 +01:00
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \Friendica\Network\HTTPException\ServiceUnavailableException
2018-01-11 01:06:00 +01:00
* @hooks 'login_hook' string $o
*/
public static function form(string $return_path = null, bool $register = false): string
2018-01-11 01:06:00 +01:00
{
$noid = DI::config()->get('system', 'no_openid');
2019-10-24 22:23:26 +02:00
if ($noid) {
DI::session()->remove('openid_identity');
DI::session()->remove('openid_attributes');
2019-10-24 22:23:26 +02:00
}
2018-01-11 01:06:00 +01:00
$reg = false;
if ($register && Register::getPolicy() !== Register::CLOSED) {
$reg = [
'title' => DI::l10n()->t('Create a New Account'),
'desc' => DI::l10n()->t('Register'),
2019-10-24 22:23:26 +02:00
'url' => self::getRegisterURL()
];
2018-01-11 01:06:00 +01:00
}
if (DI::userSession()->getLocalUserId()) {
$tpl = Renderer::getMarkupTemplate('logout.tpl');
2018-01-11 01:06:00 +01:00
} else {
DI::page()['htmlhead'] .= Renderer::replaceMacros(
Renderer::getMarkupTemplate('login_head.tpl'),
2018-01-11 01:06:00 +01:00
[
]
);
$tpl = Renderer::getMarkupTemplate('login.tpl');
2018-01-11 01:06:00 +01:00
}
if (!empty(DI::session()->get('openid_identity'))) {
$openid_title = DI::l10n()->t('Your OpenID: ');
2019-10-24 22:23:26 +02:00
$openid_readonly = true;
$identity = DI::session()->get('openid_identity');
$username_desc = DI::l10n()->t('Please enter your username and password to add the OpenID to your existing account.');
2019-10-24 22:23:26 +02:00
} else {
$openid_title = DI::l10n()->t('Or login using OpenID: ');
2019-10-24 22:23:26 +02:00
$openid_readonly = false;
$identity = '';
$username_desc = '';
}
$o = Renderer::replaceMacros(
2018-01-11 01:06:00 +01:00
$tpl,
[
'$dest_url' => DI::baseUrl() . '/login',
'$logout' => DI::l10n()->t('Logout'),
'$login' => DI::l10n()->t('Login'),
2018-01-11 01:06:00 +01:00
'$lname' => ['username', DI::l10n()->t('Nickname or Email: '), '', $username_desc],
'$lpassword' => ['password', DI::l10n()->t('Password: '), '', ''],
'$lremember' => ['remember', DI::l10n()->t('Remember me'), 0, ''],
2018-01-11 01:06:00 +01:00
'$openid' => !$noid,
2019-10-24 22:23:26 +02:00
'$lopenid' => ['openid_url', $openid_title, $identity, '', $openid_readonly],
2018-01-11 01:06:00 +01:00
'$hiddens' => ['return_path' => $return_path ?? DI::args()->getQueryString()],
2018-01-11 01:06:00 +01:00
'$register' => $reg,
'$lostpass' => DI::l10n()->t('Forgot your password?'),
'$lostlink' => DI::l10n()->t('Password Reset'),
2018-01-11 01:06:00 +01:00
'$tostitle' => DI::l10n()->t('Website Terms of Service'),
'$toslink' => DI::l10n()->t('terms of service'),
2018-01-11 01:06:00 +01:00
'$privacytitle' => DI::l10n()->t('Website Privacy Policy'),
'$privacylink' => DI::l10n()->t('privacy policy'),
2018-01-11 01:06:00 +01:00
]
);
Hook::callAll('login_hook', $o);
2018-01-11 01:06:00 +01:00
return $o;
}
2019-10-24 22:23:26 +02:00
/**
* Get the URL to the register page and add OpenID parameters to it
*/
private static function getRegisterURL(): string
2019-10-24 22:23:26 +02:00
{
if (empty(DI::session()->get('openid_identity'))) {
2019-10-24 22:23:26 +02:00
return 'register';
}
2019-10-28 21:38:53 +01:00
$args = [];
$attr = DI::session()->get('openid_attributes', []);
2019-10-24 22:23:26 +02:00
if (is_array($attr) && count($attr)) {
foreach ($attr as $k => $v) {
if ($k === 'namePerson/friendly') {
2021-11-06 21:25:21 +01:00
$nick = trim($v);
2019-10-24 22:23:26 +02:00
}
if ($k === 'namePerson/first') {
2021-11-06 21:25:21 +01:00
$first = trim($v);
2019-10-24 22:23:26 +02:00
}
if ($k === 'namePerson') {
2021-11-06 21:25:21 +01:00
$args['username'] = trim($v);
2019-10-24 22:23:26 +02:00
}
if ($k === 'contact/email') {
2021-11-06 21:25:21 +01:00
$args['email'] = trim($v);
2019-10-24 22:23:26 +02:00
}
if ($k === 'media/image/aspect11') {
$photosq = bin2hex(trim($v));
}
if ($k === 'media/image/default') {
$photo = bin2hex(trim($v));
}
}
}
if (!empty($nick)) {
2019-10-28 21:38:53 +01:00
$args['nickname'] = $nick;
2019-10-24 22:23:26 +02:00
} elseif (!empty($first)) {
2019-10-28 21:38:53 +01:00
$args['nickname'] = $first;
2019-10-24 22:23:26 +02:00
}
if (!empty($photosq)) {
2019-10-28 21:38:53 +01:00
$args['photo'] = $photosq;
2019-10-24 22:23:26 +02:00
} elseif (!empty($photo)) {
2019-10-28 21:38:53 +01:00
$args['photo'] = $photo;
2019-10-24 22:23:26 +02:00
}
$args['openid_url'] = trim(DI::session()->get('openid_identity'));
2019-10-24 22:23:26 +02:00
2019-10-28 21:38:53 +01:00
return 'register?' . http_build_query($args);
2019-10-24 22:23:26 +02:00
}
2018-01-11 01:06:00 +01:00
}