diff options
author | Christoph Wurst <christoph@owncloud.com> | 2016-05-18 18:25:05 +0200 |
---|---|---|
committer | Christoph Wurst <christoph@owncloud.com> | 2016-05-23 09:11:12 +0200 |
commit | 6495534bcdbbda8aa2748cc9f5d94dcb2bc7a04a (patch) | |
tree | b000c3f7bc3b5dc2cc96b80b7f9ece2c51e9ba01 /settings/Controller/AuthSettingsController.php | |
parent | 12431aa3997154aaea4eec11c2dd65f9e5dbe179 (diff) | |
download | nextcloud-server-6495534bcdbbda8aa2748cc9f5d94dcb2bc7a04a.tar.gz nextcloud-server-6495534bcdbbda8aa2748cc9f5d94dcb2bc7a04a.zip |
add button to add new device tokens
Diffstat (limited to 'settings/Controller/AuthSettingsController.php')
-rw-r--r-- | settings/Controller/AuthSettingsController.php | 71 |
1 files changed, 67 insertions, 4 deletions
diff --git a/settings/Controller/AuthSettingsController.php b/settings/Controller/AuthSettingsController.php index 1d874193d36..71868b7688d 100644 --- a/settings/Controller/AuthSettingsController.php +++ b/settings/Controller/AuthSettingsController.php @@ -22,41 +22,56 @@ namespace OC\Settings\Controller; +use OC\AppFramework\Http; +use OC\Authentication\Exceptions\InvalidTokenException; use OC\Authentication\Token\IProvider; +use OC\Authentication\Token\IToken; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; +use OCP\ISession; use OCP\IUserManager; +use OCP\Security\ISecureRandom; +use OCP\Session\Exceptions\SessionNotAvailableException; class AuthSettingsController extends Controller { /** @var IProvider */ private $tokenProvider; - /** - * @var IUserManager - */ + /** @var IUserManager */ private $userManager; + /** @var ISession */ + private $session; + /** @var string */ private $uid; + /** @var ISecureRandom */ + private $random; + /** * @param string $appName * @param IRequest $request * @param IProvider $tokenProvider * @param IUserManager $userManager + * @param ISession $session + * @param ISecureRandom $random * @param string $uid */ - public function __construct($appName, IRequest $request, IProvider $tokenProvider, IUserManager $userManager, $uid) { + public function __construct($appName, IRequest $request, IProvider $tokenProvider, IUserManager $userManager, ISession $session, ISecureRandom $random, $uid) { parent::__construct($appName, $request); $this->tokenProvider = $tokenProvider; $this->userManager = $userManager; $this->uid = $uid; + $this->session = $session; + $this->random = $random; } /** * @NoAdminRequired + * @NoSubadminRequired * * @return JSONResponse */ @@ -68,4 +83,52 @@ class AuthSettingsController extends Controller { return $this->tokenProvider->getTokenByUser($user); } + /** + * @NoAdminRequired + * @NoSubadminRequired + * + * @return JSONResponse + */ + public function create($name) { + try { + $sessionId = $this->session->getId(); + } catch (SessionNotAvailableException $ex) { + $resp = new JSONResponse(); + $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); + return $resp; + } + + try { + $sessionToken = $this->tokenProvider->getToken($sessionId); + $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); + } catch (InvalidTokenException $ex) { + $resp = new JSONResponse(); + $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); + return $resp; + } + + $token = $this->generateRandomDeviceToken(); + $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $password, $name, IToken::PERMANENT_TOKEN); + + return [ + 'token' => $token, + 'deviceToken' => $deviceToken + ]; + } + + /** + * Return a 20 digit device password + * + * Example: ABCDE-FGHIJ-KLMNO-PQRST + * + * @return string + */ + private function generateRandomDeviceToken() { + $groups = []; + for ($i = 0; $i < 4; $i++) { + $groups[] = $this->random->generate(5, implode('', range('A', 'Z'))); + } + return implode('-', $groups); + } + } |