2017-04-24 22:11:48 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2017-04-24 22:11:48 +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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Core\Controller;
|
|
|
|
|
|
|
|
use OC\Authentication\Exceptions\InvalidTokenException;
|
|
|
|
use OC\Authentication\Exceptions\PasswordlessTokenException;
|
|
|
|
use OC\Authentication\Token\IProvider;
|
|
|
|
use OC\Authentication\Token\IToken;
|
2017-05-05 00:46:59 +03:00
|
|
|
use OCA\OAuth2\Db\AccessToken;
|
|
|
|
use OCA\OAuth2\Db\AccessTokenMapper;
|
|
|
|
use OCA\OAuth2\Db\ClientMapper;
|
2017-04-24 22:11:48 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use OCP\Defaults;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUserSession;
|
2017-05-05 00:46:59 +03:00
|
|
|
use OCP\Security\ICrypto;
|
2017-04-24 22:11:48 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
use OCP\Session\Exceptions\SessionNotAvailableException;
|
|
|
|
|
|
|
|
class ClientFlowLoginController extends Controller {
|
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
|
|
|
/** @var Defaults */
|
|
|
|
private $defaults;
|
|
|
|
/** @var ISession */
|
|
|
|
private $session;
|
|
|
|
/** @var IProvider */
|
|
|
|
private $tokenProvider;
|
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $random;
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
2017-05-05 00:46:59 +03:00
|
|
|
/** @var ClientMapper */
|
|
|
|
private $clientMapper;
|
|
|
|
/** @var AccessTokenMapper */
|
|
|
|
private $accessTokenMapper;
|
|
|
|
/** @var ICrypto */
|
|
|
|
private $crypto;
|
2017-04-24 22:11:48 +03:00
|
|
|
|
|
|
|
const stateName = 'client.flow.state.token';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param IUserSession $userSession
|
|
|
|
* @param IL10N $l10n
|
|
|
|
* @param Defaults $defaults
|
|
|
|
* @param ISession $session
|
|
|
|
* @param IProvider $tokenProvider
|
|
|
|
* @param ISecureRandom $random
|
|
|
|
* @param IURLGenerator $urlGenerator
|
2017-05-05 00:46:59 +03:00
|
|
|
* @param ClientMapper $clientMapper
|
|
|
|
* @param AccessTokenMapper $accessTokenMapper
|
|
|
|
* @param ICrypto $crypto
|
2017-04-24 22:11:48 +03:00
|
|
|
*/
|
|
|
|
public function __construct($appName,
|
|
|
|
IRequest $request,
|
|
|
|
IUserSession $userSession,
|
|
|
|
IL10N $l10n,
|
|
|
|
Defaults $defaults,
|
|
|
|
ISession $session,
|
|
|
|
IProvider $tokenProvider,
|
|
|
|
ISecureRandom $random,
|
2017-05-05 00:46:59 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
ClientMapper $clientMapper,
|
|
|
|
AccessTokenMapper $accessTokenMapper,
|
|
|
|
ICrypto $crypto) {
|
2017-04-24 22:11:48 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->l10n = $l10n;
|
|
|
|
$this->defaults = $defaults;
|
|
|
|
$this->session = $session;
|
|
|
|
$this->tokenProvider = $tokenProvider;
|
|
|
|
$this->random = $random;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2017-05-05 00:46:59 +03:00
|
|
|
$this->clientMapper = $clientMapper;
|
|
|
|
$this->accessTokenMapper = $accessTokenMapper;
|
|
|
|
$this->crypto = $crypto;
|
2017-04-24 22:11:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getClientName() {
|
2017-05-18 17:31:14 +03:00
|
|
|
$userAgent = $this->request->getHeader('USER_AGENT');
|
2018-01-12 16:15:12 +03:00
|
|
|
return $userAgent !== '' ? $userAgent : 'unknown';
|
2017-04-24 22:11:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $stateToken
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isValidToken($stateToken) {
|
|
|
|
$currentToken = $this->session->get(self::stateName);
|
|
|
|
if(!is_string($stateToken) || !is_string($currentToken)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return hash_equals($currentToken, $stateToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
|
|
|
private function stateTokenForbiddenResponse() {
|
|
|
|
$response = new TemplateResponse(
|
|
|
|
$this->appName,
|
|
|
|
'403',
|
|
|
|
[
|
2019-01-18 11:17:26 +03:00
|
|
|
'message' => $this->l10n->t('State token does not match'),
|
2017-04-24 22:11:48 +03:00
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
$response->setStatus(Http::STATUS_FORBIDDEN);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @UseSession
|
|
|
|
*
|
2017-05-05 00:46:59 +03:00
|
|
|
* @param string $clientIdentifier
|
|
|
|
*
|
2017-04-24 22:11:48 +03:00
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
2017-05-18 16:43:14 +03:00
|
|
|
public function showAuthPickerPage($clientIdentifier = '') {
|
2017-05-05 00:46:59 +03:00
|
|
|
$clientName = $this->getClientName();
|
2017-05-12 13:44:22 +03:00
|
|
|
$client = null;
|
2017-05-05 00:46:59 +03:00
|
|
|
if($clientIdentifier !== '') {
|
|
|
|
$client = $this->clientMapper->getByIdentifier($clientIdentifier);
|
|
|
|
$clientName = $client->getName();
|
|
|
|
}
|
|
|
|
|
2017-05-18 16:43:14 +03:00
|
|
|
// No valid clientIdentifier given and no valid API Request (APIRequest header not set)
|
|
|
|
$clientRequest = $this->request->getHeader('OCS-APIREQUEST');
|
|
|
|
if ($clientRequest !== 'true' && $client === null) {
|
2017-05-12 13:44:22 +03:00
|
|
|
return new TemplateResponse(
|
|
|
|
$this->appName,
|
|
|
|
'error',
|
2017-05-18 16:43:14 +03:00
|
|
|
[
|
|
|
|
'errors' =>
|
2017-05-12 13:44:22 +03:00
|
|
|
[
|
2017-05-18 16:43:14 +03:00
|
|
|
[
|
|
|
|
'error' => 'Access Forbidden',
|
|
|
|
'hint' => 'Invalid request',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'guest'
|
2017-05-12 13:44:22 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$stateToken = $this->random->generate(
|
|
|
|
64,
|
|
|
|
ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS
|
|
|
|
);
|
|
|
|
$this->session->set(self::stateName, $stateToken);
|
|
|
|
|
2017-04-24 22:11:48 +03:00
|
|
|
return new TemplateResponse(
|
|
|
|
$this->appName,
|
|
|
|
'loginflow/authpicker',
|
|
|
|
[
|
2017-05-05 00:46:59 +03:00
|
|
|
'client' => $clientName,
|
|
|
|
'clientIdentifier' => $clientIdentifier,
|
2017-04-24 22:11:48 +03:00
|
|
|
'instanceName' => $this->defaults->getName(),
|
|
|
|
'urlGenerator' => $this->urlGenerator,
|
|
|
|
'stateToken' => $stateToken,
|
2018-11-01 01:06:08 +03:00
|
|
|
'serverHost' => $this->getServerPath(),
|
2018-04-06 23:09:20 +03:00
|
|
|
'oauthState' => $this->session->get('oauth.state'),
|
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
2018-12-17 14:50:32 +03:00
|
|
|
* @NoSameSiteCookieRequired
|
2018-04-06 23:09:20 +03:00
|
|
|
* @UseSession
|
|
|
|
*
|
|
|
|
* @param string $stateToken
|
|
|
|
* @param string $clientIdentifier
|
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
|
|
|
public function grantPage($stateToken = '',
|
|
|
|
$clientIdentifier = '') {
|
|
|
|
if(!$this->isValidToken($stateToken)) {
|
|
|
|
return $this->stateTokenForbiddenResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$clientName = $this->getClientName();
|
|
|
|
$client = null;
|
|
|
|
if($clientIdentifier !== '') {
|
|
|
|
$client = $this->clientMapper->getByIdentifier($clientIdentifier);
|
|
|
|
$clientName = $client->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new TemplateResponse(
|
|
|
|
$this->appName,
|
|
|
|
'loginflow/grant',
|
|
|
|
[
|
|
|
|
'client' => $clientName,
|
|
|
|
'clientIdentifier' => $clientIdentifier,
|
|
|
|
'instanceName' => $this->defaults->getName(),
|
|
|
|
'urlGenerator' => $this->urlGenerator,
|
|
|
|
'stateToken' => $stateToken,
|
2018-11-01 01:06:08 +03:00
|
|
|
'serverHost' => $this->getServerPath(),
|
2017-08-24 13:15:26 +03:00
|
|
|
'oauthState' => $this->session->get('oauth.state'),
|
2017-04-24 22:11:48 +03:00
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @UseSession
|
|
|
|
*
|
|
|
|
* @param string $stateToken
|
2017-05-05 00:46:59 +03:00
|
|
|
* @param string $clientIdentifier
|
2017-04-24 22:11:48 +03:00
|
|
|
* @return Http\RedirectResponse|Response
|
|
|
|
*/
|
2017-05-05 00:46:59 +03:00
|
|
|
public function generateAppPassword($stateToken,
|
2017-05-18 16:43:14 +03:00
|
|
|
$clientIdentifier = '') {
|
2017-04-24 22:11:48 +03:00
|
|
|
if(!$this->isValidToken($stateToken)) {
|
2017-04-25 10:51:00 +03:00
|
|
|
$this->session->remove(self::stateName);
|
2017-04-24 22:11:48 +03:00
|
|
|
return $this->stateTokenForbiddenResponse();
|
|
|
|
}
|
|
|
|
|
2017-04-25 10:51:00 +03:00
|
|
|
$this->session->remove(self::stateName);
|
|
|
|
|
2017-04-24 22:11:48 +03:00
|
|
|
try {
|
|
|
|
$sessionId = $this->session->getId();
|
|
|
|
} catch (SessionNotAvailableException $ex) {
|
|
|
|
$response = new Response();
|
|
|
|
$response->setStatus(Http::STATUS_FORBIDDEN);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$sessionToken = $this->tokenProvider->getToken($sessionId);
|
|
|
|
$loginName = $sessionToken->getLoginName();
|
|
|
|
try {
|
|
|
|
$password = $this->tokenProvider->getPassword($sessionToken, $sessionId);
|
|
|
|
} catch (PasswordlessTokenException $ex) {
|
|
|
|
$password = null;
|
|
|
|
}
|
|
|
|
} catch (InvalidTokenException $ex) {
|
|
|
|
$response = new Response();
|
|
|
|
$response->setStatus(Http::STATUS_FORBIDDEN);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2017-05-12 17:41:12 +03:00
|
|
|
$clientName = $this->getClientName();
|
2017-05-18 17:31:14 +03:00
|
|
|
$client = false;
|
2017-05-12 17:41:12 +03:00
|
|
|
if($clientIdentifier !== '') {
|
|
|
|
$client = $this->clientMapper->getByIdentifier($clientIdentifier);
|
|
|
|
$clientName = $client->getName();
|
|
|
|
}
|
|
|
|
|
2017-05-05 01:40:23 +03:00
|
|
|
$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
|
2017-05-05 00:46:59 +03:00
|
|
|
$uid = $this->userSession->getUser()->getUID();
|
|
|
|
$generatedToken = $this->tokenProvider->generateToken(
|
2017-04-24 22:11:48 +03:00
|
|
|
$token,
|
2017-05-05 00:46:59 +03:00
|
|
|
$uid,
|
2017-04-24 22:11:48 +03:00
|
|
|
$loginName,
|
|
|
|
$password,
|
2017-05-12 17:41:12 +03:00
|
|
|
$clientName,
|
2017-04-24 22:11:48 +03:00
|
|
|
IToken::PERMANENT_TOKEN,
|
|
|
|
IToken::DO_NOT_REMEMBER
|
|
|
|
);
|
|
|
|
|
2017-05-18 17:31:14 +03:00
|
|
|
if($client) {
|
2018-05-16 16:09:35 +03:00
|
|
|
$code = $this->random->generate(128, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
|
2017-05-05 00:46:59 +03:00
|
|
|
$accessToken = new AccessToken();
|
|
|
|
$accessToken->setClientId($client->getId());
|
|
|
|
$accessToken->setEncryptedToken($this->crypto->encrypt($token, $code));
|
|
|
|
$accessToken->setHashedCode(hash('sha512', $code));
|
|
|
|
$accessToken->setTokenId($generatedToken->getId());
|
|
|
|
$this->accessTokenMapper->insert($accessToken);
|
|
|
|
|
|
|
|
$redirectUri = sprintf(
|
|
|
|
'%s?state=%s&code=%s',
|
|
|
|
$client->getRedirectUri(),
|
2017-05-18 16:43:14 +03:00
|
|
|
urlencode($this->session->get('oauth.state')),
|
2017-05-05 00:46:59 +03:00
|
|
|
urlencode($code)
|
|
|
|
);
|
2017-05-18 16:43:14 +03:00
|
|
|
$this->session->remove('oauth.state');
|
2017-05-05 00:46:59 +03:00
|
|
|
} else {
|
2018-11-01 01:06:08 +03:00
|
|
|
$redirectUri = 'nc://login/server:' . $this->getServerPath() . '&user:' . urlencode($loginName) . '&password:' . urlencode($token);
|
2017-11-09 02:29:34 +03:00
|
|
|
|
2018-11-01 01:06:08 +03:00
|
|
|
// Clear the token from the login here
|
|
|
|
$this->tokenProvider->invalidateToken($sessionId);
|
|
|
|
}
|
2017-11-09 02:29:34 +03:00
|
|
|
|
2018-11-01 01:06:08 +03:00
|
|
|
return new Http\RedirectResponse($redirectUri);
|
|
|
|
}
|
2018-01-12 10:57:51 +03:00
|
|
|
|
2019-01-04 22:30:37 +03:00
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
*/
|
|
|
|
public function apptokenRedirect(string $stateToken, string $user, string $password) {
|
|
|
|
if (!$this->isValidToken($stateToken)) {
|
|
|
|
return $this->stateTokenForbiddenResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$redirectUri = 'nc://login/server:' . $this->getServerPath() . '&user:' . urlencode($user) . '&password:' . urlencode($password);
|
|
|
|
return new Http\RedirectResponse($redirectUri);
|
|
|
|
}
|
|
|
|
|
2018-11-01 01:06:08 +03:00
|
|
|
private function getServerPath(): string {
|
|
|
|
$serverPostfix = '';
|
2018-01-12 10:57:51 +03:00
|
|
|
|
2018-11-01 01:06:08 +03:00
|
|
|
if (strpos($this->request->getRequestUri(), '/index.php') !== false) {
|
|
|
|
$serverPostfix = substr($this->request->getRequestUri(), 0, strpos($this->request->getRequestUri(), '/index.php'));
|
|
|
|
} else if (strpos($this->request->getRequestUri(), '/login/flow') !== false) {
|
|
|
|
$serverPostfix = substr($this->request->getRequestUri(), 0, strpos($this->request->getRequestUri(), '/login/flow'));
|
|
|
|
}
|
2018-01-12 10:57:51 +03:00
|
|
|
|
2018-11-01 01:06:08 +03:00
|
|
|
$protocol = $this->request->getServerProtocol();
|
2017-04-24 22:11:48 +03:00
|
|
|
|
2018-11-01 01:06:08 +03:00
|
|
|
if ($protocol !== "https") {
|
|
|
|
$xForwardedProto = $this->request->getHeader('X-Forwarded-Proto');
|
|
|
|
$xForwardedSSL = $this->request->getHeader('X-Forwarded-Ssl');
|
|
|
|
if ($xForwardedProto === 'https' || $xForwardedSSL === 'on') {
|
|
|
|
$protocol = 'https';
|
|
|
|
}
|
2018-09-06 09:30:52 +03:00
|
|
|
}
|
2018-01-04 16:30:40 +03:00
|
|
|
|
2018-11-01 01:06:08 +03:00
|
|
|
return $protocol . "://" . $this->request->getServerHost() . $serverPostfix;
|
2017-05-05 00:46:59 +03:00
|
|
|
}
|
2017-04-24 22:11:48 +03:00
|
|
|
}
|