2017-05-04 23:46:59 +02:00
< ? php
2019-12-03 19:57:53 +01:00
2018-06-13 21:25:21 +02:00
declare ( strict_types = 1 );
2019-12-03 19:57:53 +01:00
2017-05-04 23:46:59 +02:00
/**
* @ copyright Copyright ( c ) 2017 Lukas Reschke < lukas @ statuscode . ch >
*
2019-12-03 19:57:53 +01:00
* @ author Bjoern Schiessle < bjoern @ schiessle . org >
2020-04-29 11:57:22 +02:00
* @ author Christoph Wurst < christoph @ winzerhof - wurst . at >
2019-12-03 19:57:53 +01:00
* @ author Lukas Reschke < lukas @ statuscode . ch >
* @ author Patrik Kernstock < info @ pkern . at >
* @ author rakekniven < mark . ziegler @ rakekniven . de >
* @ author Roeland Jago Douma < roeland @ famdouma . nl >
*
2017-05-04 23:46:59 +02: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
2021-06-04 21:52:51 +02:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2017-05-04 23:46:59 +02:00
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
2019-12-03 19:57:53 +01:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2017-05-04 23:46:59 +02:00
*
*/
namespace OCA\OAuth2\Controller ;
2017-05-12 16:14:32 +02:00
use OCA\OAuth2\Db\AccessTokenMapper ;
2017-05-04 23:46:59 +02:00
use OCA\OAuth2\Db\Client ;
use OCA\OAuth2\Db\ClientMapper ;
use OCP\AppFramework\Controller ;
2018-06-26 15:27:20 +02:00
use OCP\AppFramework\Http ;
2018-06-08 09:52:27 +02:00
use OCP\AppFramework\Http\JSONResponse ;
2023-11-23 10:22:34 +01:00
use OCP\Authentication\Token\IProvider as IAuthTokenProvider ;
2018-06-26 15:27:20 +02:00
use OCP\IL10N ;
2017-05-04 23:46:59 +02:00
use OCP\IRequest ;
2022-11-11 13:16:14 +05:45
use OCP\IUser ;
use OCP\IUserManager ;
2023-05-22 15:39:56 +02:00
use OCP\Security\ICrypto ;
2017-05-04 23:46:59 +02:00
use OCP\Security\ISecureRandom ;
class SettingsController extends Controller {
2023-05-22 15:39:56 +02:00
2020-04-10 16:54:27 +02:00
public const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
2017-05-04 23:46:59 +02:00
2023-05-22 15:39:56 +02:00
public function __construct (
string $appName ,
IRequest $request ,
private ClientMapper $clientMapper ,
private ISecureRandom $secureRandom ,
private AccessTokenMapper $accessTokenMapper ,
private IL10N $l ,
private IAuthTokenProvider $tokenProvider ,
private IUserManager $userManager ,
private ICrypto $crypto
2017-05-12 16:14:32 +02:00
) {
2017-05-04 23:46:59 +02:00
parent :: __construct ( $appName , $request );
}
2018-06-08 09:52:27 +02:00
public function addClient ( string $name ,
2023-11-23 10:22:34 +01:00
string $redirectUri ) : JSONResponse {
2018-09-04 00:58:44 +02:00
if ( filter_var ( $redirectUri , FILTER_VALIDATE_URL ) === false ) {
2018-06-30 08:49:44 +02:00
return new JSONResponse ([ 'message' => $this -> l -> t ( 'Your redirect URL needs to be a full URL for example: https://yourdomain.com/path' )], Http :: STATUS_BAD_REQUEST );
2018-06-26 15:27:20 +02:00
}
2017-05-04 23:46:59 +02:00
$client = new Client ();
$client -> setName ( $name );
$client -> setRedirectUri ( $redirectUri );
2023-05-22 15:39:56 +02:00
$secret = $this -> secureRandom -> generate ( 64 , self :: validChars );
2024-08-29 17:28:01 +02:00
$hashedSecret = bin2hex ( $this -> crypto -> calculateHMAC ( $secret ));
$client -> setSecret ( $hashedSecret );
2017-05-04 23:46:59 +02:00
$client -> setClientIdentifier ( $this -> secureRandom -> generate ( 64 , self :: validChars ));
2018-06-08 09:52:27 +02:00
$client = $this -> clientMapper -> insert ( $client );
$result = [
'id' => $client -> getId (),
'name' => $client -> getName (),
'redirectUri' => $client -> getRedirectUri (),
'clientId' => $client -> getClientIdentifier (),
2023-05-22 15:39:56 +02:00
'clientSecret' => $secret ,
2018-06-08 09:52:27 +02:00
];
return new JSONResponse ( $result );
2017-05-04 23:46:59 +02:00
}
2018-06-08 09:52:27 +02:00
public function deleteClient ( int $id ) : JSONResponse {
2017-05-12 16:14:32 +02:00
$client = $this -> clientMapper -> getByUid ( $id );
2022-11-11 13:16:14 +05:45
$this -> userManager -> callForAllUsers ( function ( IUser $user ) use ( $client ) {
2022-11-21 17:28:21 +05:45
$this -> tokenProvider -> invalidateTokensOfUser ( $user -> getUID (), $client -> getName ());
2022-11-11 13:16:14 +05:45
});
2017-05-12 16:14:32 +02:00
$this -> accessTokenMapper -> deleteByClientId ( $id );
2017-05-04 23:46:59 +02:00
$this -> clientMapper -> delete ( $client );
2018-06-08 09:52:27 +02:00
return new JSONResponse ([]);
}
2017-05-04 23:46:59 +02:00
}