2017-05-05 00:46:59 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-12-07 15:43:22 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2017-05-05 00:46:59 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2017-05-05 00:46:59 +03:00
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-05 00:46:59 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\OAuth2\Controller;
|
|
|
|
|
|
|
|
use OCA\OAuth2\Db\ClientMapper;
|
2018-12-06 17:23:28 +03:00
|
|
|
use OCA\OAuth2\Exceptions\ClientNotFoundException;
|
2017-05-05 00:46:59 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
2018-12-06 17:23:28 +03:00
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use OCP\IL10N;
|
2017-05-05 00:46:59 +03:00
|
|
|
use OCP\IRequest;
|
2017-05-18 16:43:14 +03:00
|
|
|
use OCP\ISession;
|
2017-05-05 00:46:59 +03:00
|
|
|
use OCP\IURLGenerator;
|
|
|
|
|
|
|
|
class LoginRedirectorController extends Controller {
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
/** @var ClientMapper */
|
|
|
|
private $clientMapper;
|
2017-05-18 16:43:14 +03:00
|
|
|
/** @var ISession */
|
|
|
|
private $session;
|
2018-12-06 17:23:28 +03:00
|
|
|
/** @var IL10N */
|
|
|
|
private $l;
|
2017-05-05 00:46:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
* @param ClientMapper $clientMapper
|
2017-05-18 16:43:14 +03:00
|
|
|
* @param ISession $session
|
2018-12-06 17:23:28 +03:00
|
|
|
* @param IL10N $l
|
2017-05-05 00:46:59 +03:00
|
|
|
*/
|
2018-12-07 15:43:22 +03:00
|
|
|
public function __construct(string $appName,
|
2017-05-05 00:46:59 +03:00
|
|
|
IRequest $request,
|
|
|
|
IURLGenerator $urlGenerator,
|
2017-05-18 16:43:14 +03:00
|
|
|
ClientMapper $clientMapper,
|
2018-12-06 17:23:28 +03:00
|
|
|
ISession $session,
|
|
|
|
IL10N $l) {
|
2017-05-05 00:46:59 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->clientMapper = $clientMapper;
|
2017-05-18 16:43:14 +03:00
|
|
|
$this->session = $session;
|
2018-12-06 17:23:28 +03:00
|
|
|
$this->l = $l;
|
2017-05-05 00:46:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
2017-05-18 16:43:14 +03:00
|
|
|
* @UseSession
|
2017-05-05 00:46:59 +03:00
|
|
|
*
|
|
|
|
* @param string $client_id
|
|
|
|
* @param string $state
|
2018-05-16 12:24:48 +03:00
|
|
|
* @param string $response_type
|
2018-12-06 17:23:28 +03:00
|
|
|
* @return Response
|
2017-05-05 00:46:59 +03:00
|
|
|
*/
|
|
|
|
public function authorize($client_id,
|
2018-05-16 12:24:48 +03:00
|
|
|
$state,
|
2018-12-07 15:43:22 +03:00
|
|
|
$response_type): Response {
|
2018-12-06 17:23:28 +03:00
|
|
|
try {
|
|
|
|
$client = $this->clientMapper->getByIdentifier($client_id);
|
|
|
|
} catch (ClientNotFoundException $e) {
|
2020-03-15 13:52:06 +03:00
|
|
|
$params = [
|
2018-12-06 17:23:28 +03:00
|
|
|
'content' => $this->l->t('Your client is not authorized to connect. Please inform the administrator of your client.'),
|
2020-03-15 13:52:06 +03:00
|
|
|
];
|
|
|
|
return new TemplateResponse('core', '404', $params, 'guest');
|
2018-12-06 17:23:28 +03:00
|
|
|
}
|
2018-05-16 12:24:48 +03:00
|
|
|
|
|
|
|
if ($response_type !== 'code') {
|
|
|
|
//Fail
|
|
|
|
$url = $client->getRedirectUri() . '?error=unsupported_response_type&state=' . $state;
|
|
|
|
return new RedirectResponse($url);
|
|
|
|
}
|
|
|
|
|
2017-05-18 16:43:14 +03:00
|
|
|
$this->session->set('oauth.state', $state);
|
2017-05-05 00:46:59 +03:00
|
|
|
|
|
|
|
$targetUrl = $this->urlGenerator->linkToRouteAbsolute(
|
|
|
|
'core.ClientFlowLogin.showAuthPickerPage',
|
|
|
|
[
|
|
|
|
'clientIdentifier' => $client->getClientIdentifier(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
return new RedirectResponse($targetUrl);
|
|
|
|
}
|
|
|
|
}
|