Skip to content

Commit 9ae75a3

Browse files
authored
Merge pull request #21850 from nextcloud/techdebt/noid/register-alternative-logins
Allow to register AlternativeLogin on RegistrationContext
2 parents 84ca586 + 0dfcc13 commit 9ae75a3

File tree

6 files changed

+145
-2
lines changed

6 files changed

+145
-2
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
'OCP\\Authentication\\Events\\LoginFailedEvent' => $baseDir . '/lib/public/Authentication/Events/LoginFailedEvent.php',
8484
'OCP\\Authentication\\Exceptions\\CredentialsUnavailableException' => $baseDir . '/lib/public/Authentication/Exceptions/CredentialsUnavailableException.php',
8585
'OCP\\Authentication\\Exceptions\\PasswordUnavailableException' => $baseDir . '/lib/public/Authentication/Exceptions/PasswordUnavailableException.php',
86+
'OCP\\Authentication\\IAlternativeLogin' => $baseDir . '/lib/public/Authentication/IAlternativeLogin.php',
8687
'OCP\\Authentication\\IApacheBackend' => $baseDir . '/lib/public/Authentication/IApacheBackend.php',
8788
'OCP\\Authentication\\LoginCredentials\\ICredentials' => $baseDir . '/lib/public/Authentication/LoginCredentials/ICredentials.php',
8889
'OCP\\Authentication\\LoginCredentials\\IStore' => $baseDir . '/lib/public/Authentication/LoginCredentials/IStore.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
112112
'OCP\\Authentication\\Events\\LoginFailedEvent' => __DIR__ . '/../../..' . '/lib/public/Authentication/Events/LoginFailedEvent.php',
113113
'OCP\\Authentication\\Exceptions\\CredentialsUnavailableException' => __DIR__ . '/../../..' . '/lib/public/Authentication/Exceptions/CredentialsUnavailableException.php',
114114
'OCP\\Authentication\\Exceptions\\PasswordUnavailableException' => __DIR__ . '/../../..' . '/lib/public/Authentication/Exceptions/PasswordUnavailableException.php',
115+
'OCP\\Authentication\\IAlternativeLogin' => __DIR__ . '/../../..' . '/lib/public/Authentication/IAlternativeLogin.php',
115116
'OCP\\Authentication\\IApacheBackend' => __DIR__ . '/../../..' . '/lib/public/Authentication/IApacheBackend.php',
116117
'OCP\\Authentication\\LoginCredentials\\ICredentials' => __DIR__ . '/../../..' . '/lib/public/Authentication/LoginCredentials/ICredentials.php',
117118
'OCP\\Authentication\\LoginCredentials\\IStore' => __DIR__ . '/../../..' . '/lib/public/Authentication/LoginCredentials/IStore.php',

lib/private/AppFramework/Bootstrap/RegistrationContext.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class RegistrationContext {
6363
/** @var array[] */
6464
private $searchProviders = [];
6565

66+
/** @var array[] */
67+
private $alternativeLogins = [];
68+
6669
/** @var ILogger */
6770
private $logger;
6871

@@ -151,6 +154,13 @@ public function registerSearchProvider(string $class): void {
151154
$class
152155
);
153156
}
157+
158+
public function registerAlternativeLogin(string $class): void {
159+
$this->context->registerAlternativeLogin(
160+
$this->appId,
161+
$class
162+
);
163+
}
154164
};
155165
}
156166

@@ -223,6 +233,13 @@ public function registerSearchProvider(string $appId, string $class) {
223233
];
224234
}
225235

236+
public function registerAlternativeLogin(string $appId, string $class): void {
237+
$this->alternativeLogins[] = [
238+
'appId' => $appId,
239+
'class' => $class,
240+
];
241+
}
242+
226243
/**
227244
* @param App[] $apps
228245
*/
@@ -386,4 +403,11 @@ public function delegateMiddlewareRegistrations(array $apps): void {
386403
public function getSearchProviders(): array {
387404
return $this->searchProviders;
388405
}
406+
407+
/**
408+
* @return array[]
409+
*/
410+
public function getAlternativeLogins(): array {
411+
return $this->alternativeLogins;
412+
}
389413
}

lib/private/legacy/OC_App.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@
5252
*/
5353
use OC\App\DependencyAnalyzer;
5454
use OC\App\Platform;
55+
use OC\AppFramework\Bootstrap\Coordinator;
5556
use OC\DB\MigrationService;
5657
use OC\Installer;
5758
use OC\Repair;
5859
use OC\ServerNotAvailableException;
5960
use OCP\App\ManagerEvent;
61+
use OCP\AppFramework\QueryException;
62+
use OCP\Authentication\IAlternativeLogin;
6063
use OCP\ILogger;
6164

6265
/**
@@ -149,8 +152,8 @@ public static function loadApp(string $app) {
149152
// in case someone calls loadApp() directly
150153
self::registerAutoloading($app, $appPath);
151154

152-
/** @var \OC\AppFramework\Bootstrap\Coordinator $coordinator */
153-
$coordinator = \OC::$server->query(\OC\AppFramework\Bootstrap\Coordinator::class);
155+
/** @var Coordinator $coordinator */
156+
$coordinator = \OC::$server->query(Coordinator::class);
154157
$isBootable = $coordinator->isBootable($app);
155158

156159
$hasAppPhpFile = is_file($appPath . '/appinfo/app.php');
@@ -672,15 +675,58 @@ public static function registerPersonal(string $app, string $page) {
672675

673676
/**
674677
* @param array $entry
678+
* @deprecated 20.0.0 Please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface
675679
*/
676680
public static function registerLogIn(array $entry) {
681+
\OC::$server->getLogger()->debug('OC_App::registerLogIn() is deprecated, please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface');
677682
self::$altLogin[] = $entry;
678683
}
679684

680685
/**
681686
* @return array
682687
*/
683688
public static function getAlternativeLogIns(): array {
689+
/** @var Coordinator $bootstrapCoordinator */
690+
$bootstrapCoordinator = \OC::$server->query(Coordinator::class);
691+
692+
foreach ($bootstrapCoordinator->getRegistrationContext()->getAlternativeLogins() as $registration) {
693+
if (!in_array(IAlternativeLogin::class, class_implements($registration['class']), true)) {
694+
\OC::$server->getLogger()->error('Alternative login option {option} does not implement {interface} and is therefore ignored.', [
695+
'option' => $registration['class'],
696+
'interface' => IAlternativeLogin::class,
697+
'app' => $registration['app'],
698+
]);
699+
continue;
700+
}
701+
702+
try {
703+
/** @var IAlternativeLogin $provider */
704+
$provider = \OC::$server->query($registration['class']);
705+
} catch (QueryException $e) {
706+
\OC::$server->getLogger()->logException($e, [
707+
'message' => 'Alternative login option {option} can not be initialised.',
708+
'option' => $registration['class'],
709+
'app' => $registration['app'],
710+
]);
711+
}
712+
713+
try {
714+
$provider->load();
715+
716+
self::$altLogin[] = [
717+
'name' => $provider->getLabel(),
718+
'href' => $provider->getLink(),
719+
'style' => $provider->getClass(),
720+
];
721+
} catch (Throwable $e) {
722+
\OC::$server->getLogger()->logException($e, [
723+
'message' => 'Alternative login option {option} had an error while loading.',
724+
'option' => $registration['class'],
725+
'app' => $registration['app'],
726+
]);
727+
}
728+
}
729+
684730
return self::$altLogin;
685731
}
686732

lib/public/AppFramework/Bootstrap/IRegistrationContext.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,17 @@ public function registerMiddleware(string $class): void;
139139
* @since 20.0.0
140140
*/
141141
public function registerSearchProvider(string $class): void;
142+
143+
/**
144+
* Register an alternative login option
145+
*
146+
* It is allowed to register more than one option per app.
147+
*
148+
* @param string $class
149+
*
150+
* @return void
151+
*
152+
* @since 20.0.0
153+
*/
154+
public function registerAlternativeLogin(string $class): void;
142155
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2020 Joas Schilling <[email protected]>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OCP\Authentication;
26+
27+
/**
28+
* @since 20.0.0
29+
*/
30+
interface IAlternativeLogin {
31+
32+
/**
33+
* Label shown on the login option
34+
* @return string
35+
* @since 20.0.0
36+
*/
37+
public function getLabel(): string;
38+
39+
/**
40+
* Relative link to the login option
41+
* @return string
42+
* @since 20.0.0
43+
*/
44+
public function getLink(): string;
45+
46+
/**
47+
* CSS classes added to the alternative login option on the login screen
48+
* @return string
49+
* @since 20.0.0
50+
*/
51+
public function getClass(): string;
52+
53+
/**
54+
* Load necessary resources to present the login option, e.g. style-file to style the getClass()
55+
* @since 20.0.0
56+
*/
57+
public function load(): void;
58+
}

0 commit comments

Comments
 (0)