2019-07-22 13:56:00 +02:00
< ? php
namespace Friendica\Module\Settings\TwoFactor ;
use Friendica\Core\Renderer ;
2019-12-15 22:34:11 +01:00
use Friendica\DI ;
2019-07-22 13:56:00 +02:00
use Friendica\Model\TwoFactor\AppSpecificPassword ;
2020-01-23 05:14:14 +01:00
use Friendica\Module\BaseSettings ;
2019-12-27 22:19:28 +01:00
use Friendica\Module\Security\Login ;
2019-07-22 13:56:00 +02:00
/**
* // Page 5: 2FA enabled, app-specific password generation
*
* @ package Friendica\Module\TwoFactor
*/
2020-01-23 05:14:14 +01:00
class AppSpecific extends BaseSettings
2019-07-22 13:56:00 +02:00
{
private static $appSpecificPassword = null ;
2019-11-05 22:48:54 +01:00
public static function init ( array $parameters = [])
2019-07-22 13:56:00 +02:00
{
if ( ! local_user ()) {
return ;
}
2020-01-18 16:50:57 +01:00
$verified = DI :: pConfig () -> get ( local_user (), '2fa' , 'verified' );
2019-07-22 13:56:00 +02:00
if ( ! $verified ) {
2019-12-16 00:28:31 +01:00
DI :: baseUrl () -> redirect ( 'settings/2fa' );
2019-07-22 13:56:00 +02:00
}
if ( ! self :: checkFormSecurityToken ( 'settings_2fa_password' , 't' )) {
2020-01-18 20:52:34 +01:00
notice ( DI :: l10n () -> t ( 'Please enter your password to access this page.' ));
2019-12-16 00:28:31 +01:00
DI :: baseUrl () -> redirect ( 'settings/2fa' );
2019-07-22 13:56:00 +02:00
}
}
2019-11-05 22:48:54 +01:00
public static function post ( array $parameters = [])
2019-07-22 13:56:00 +02:00
{
if ( ! local_user ()) {
return ;
}
if ( ! empty ( $_POST [ 'action' ])) {
self :: checkFormSecurityTokenRedirectOnError ( 'settings/2fa/app_specific' , 'settings_2fa_app_specific' );
switch ( $_POST [ 'action' ]) {
case 'generate' :
$description = $_POST [ 'description' ] ? ? '' ;
if ( empty ( $description )) {
2020-01-18 20:52:34 +01:00
notice ( DI :: l10n () -> t ( 'App-specific password generation failed: The description is empty.' ));
2019-12-16 00:28:31 +01:00
DI :: baseUrl () -> redirect ( 'settings/2fa/app_specific?t=' . self :: getFormSecurityToken ( 'settings_2fa_password' ));
2019-07-22 13:56:00 +02:00
} elseif ( AppSpecificPassword :: checkDuplicateForUser ( local_user (), $description )) {
2020-01-18 20:52:34 +01:00
notice ( DI :: l10n () -> t ( 'App-specific password generation failed: This description already exists.' ));
2019-12-16 00:28:31 +01:00
DI :: baseUrl () -> redirect ( 'settings/2fa/app_specific?t=' . self :: getFormSecurityToken ( 'settings_2fa_password' ));
2019-07-22 13:56:00 +02:00
} else {
self :: $appSpecificPassword = AppSpecificPassword :: generateForUser ( local_user (), $_POST [ 'description' ] ? ? '' );
2020-01-18 20:52:34 +01:00
notice ( DI :: l10n () -> t ( 'New app-specific password generated.' ));
2019-07-22 13:56:00 +02:00
}
break ;
case 'revoke_all' :
AppSpecificPassword :: deleteAllForUser ( local_user ());
2020-01-18 20:52:34 +01:00
notice ( DI :: l10n () -> t ( 'App-specific passwords successfully revoked.' ));
2019-12-16 00:28:31 +01:00
DI :: baseUrl () -> redirect ( 'settings/2fa/app_specific?t=' . self :: getFormSecurityToken ( 'settings_2fa_password' ));
2019-07-22 13:56:00 +02:00
break ;
}
}
if ( ! empty ( $_POST [ 'revoke_id' ])) {
self :: checkFormSecurityTokenRedirectOnError ( 'settings/2fa/app_specific' , 'settings_2fa_app_specific' );
if ( AppSpecificPassword :: deleteForUser ( local_user (), $_POST [ 'revoke_id' ])) {
2020-01-18 20:52:34 +01:00
notice ( DI :: l10n () -> t ( 'App-specific password successfully revoked.' ));
2019-07-22 13:56:00 +02:00
}
2019-12-16 00:28:31 +01:00
DI :: baseUrl () -> redirect ( 'settings/2fa/app_specific?t=' . self :: getFormSecurityToken ( 'settings_2fa_password' ));
2019-07-22 13:56:00 +02:00
}
}
2019-11-05 22:48:54 +01:00
public static function content ( array $parameters = [])
2019-07-22 13:56:00 +02:00
{
if ( ! local_user ()) {
return Login :: form ( 'settings/2fa/app_specific' );
}
2019-11-05 21:22:54 +01:00
parent :: content ( $parameters );
2019-07-22 13:56:00 +02:00
$appSpecificPasswords = AppSpecificPassword :: getListForUser ( local_user ());
return Renderer :: replaceMacros ( Renderer :: getMarkupTemplate ( 'settings/twofactor/app_specific.tpl' ), [
'$form_security_token' => self :: getFormSecurityToken ( 'settings_2fa_app_specific' ),
'$password_security_token' => self :: getFormSecurityToken ( 'settings_2fa_password' ),
2020-01-18 20:52:34 +01:00
'$title' => DI :: l10n () -> t ( 'Two-factor app-specific passwords' ),
'$help_label' => DI :: l10n () -> t ( 'Help' ),
'$message' => DI :: l10n () -> t ( '<p>App-specific passwords are randomly generated passwords used instead your regular password to authenticate your account on third-party applications that don\'t support two-factor authentication.</p>' ),
'$generated_message' => DI :: l10n () -> t ( 'Make sure to copy your new app-specific password now. You won’ t be able to see it again!' ),
2019-07-22 13:56:00 +02:00
'$generated_app_specific_password' => self :: $appSpecificPassword ,
2020-01-18 20:52:34 +01:00
'$description_label' => DI :: l10n () -> t ( 'Description' ),
'$last_used_label' => DI :: l10n () -> t ( 'Last Used' ),
'$revoke_label' => DI :: l10n () -> t ( 'Revoke' ),
'$revoke_all_label' => DI :: l10n () -> t ( 'Revoke All' ),
2019-07-22 13:56:00 +02:00
'$app_specific_passwords' => $appSpecificPasswords ,
2020-01-18 20:52:34 +01:00
'$generate_message' => DI :: l10n () -> t ( 'When you generate a new app-specific password, you must use it right away, it will be shown to you once after you generate it.' ),
'$generate_title' => DI :: l10n () -> t ( 'Generate new app-specific password' ),
'$description_placeholder_label' => DI :: l10n () -> t ( 'Friendiqa on my Fairphone 2...' ),
'$generate_label' => DI :: l10n () -> t ( 'Generate' ),
2019-07-22 13:56:00 +02:00
]);
}
}