/**
  * @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);
         $loginName = $sessionToken->getLoginName();
         try {
             $password = $this->tokenProvider->getPassword($sessionToken, $sessionId);
         } catch (PasswordlessTokenException $ex) {
             $password = null;
         }
     } catch (InvalidTokenException $ex) {
         $resp = new JSONResponse();
         $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
         return $resp;
     }
     $token = $this->generateRandomDeviceToken();
     $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN);
     return ['token' => $token, 'deviceToken' => $deviceToken];
 }
Exemple #2
0
 /**
  * Create a new session token for the given user credentials
  *
  * @param IRequest $request
  * @param string $uid user UID
  * @param string $loginName login name
  * @param string $password
  * @return boolean
  */
 public function createSessionToken(IRequest $request, $uid, $loginName, $password = null)
 {
     if (is_null($this->manager->get($uid))) {
         // User does not exist
         return false;
     }
     $name = isset($request->server['HTTP_USER_AGENT']) ? $request->server['HTTP_USER_AGENT'] : 'unknown browser';
     try {
         $sessionId = $this->session->getId();
         $pwd = $this->getPassword($password);
         $this->tokenProvider->generateToken($sessionId, $uid, $loginName, $pwd, $name);
         return true;
     } catch (SessionNotAvailableException $ex) {
         // This can happen with OCC, where a memory session is used
         // if a memory session is used, we shouldn't create a session token anyway
         return false;
     }
 }
 /**
  * @NoAdminRequired
  * @NoSubadminRequired
  *
  * @return JSONResponse
  */
 public function create($name)
 {
     try {
         $sessionId = $this->session->getId();
     } catch (SessionNotAvailableException $ex) {
         return $this->getServiceNotAvailableResponse();
     }
     try {
         $sessionToken = $this->tokenProvider->getToken($sessionId);
         $loginName = $sessionToken->getLoginName();
         try {
             $password = $this->tokenProvider->getPassword($sessionToken, $sessionId);
         } catch (PasswordlessTokenException $ex) {
             $password = null;
         }
     } catch (InvalidTokenException $ex) {
         return $this->getServiceNotAvailableResponse();
     }
     $token = $this->generateRandomDeviceToken();
     $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN);
     return ['token' => $token, 'loginName' => $loginName, 'deviceToken' => $deviceToken];
 }
 /**
  * Generate a new access token clients can authenticate with
  *
  * @PublicPage
  * @NoCSRFRequired
  *
  * @param string $user
  * @param string $password
  * @param string $name the name of the client
  * @return JSONResponse
  */
 public function generateToken($user, $password, $name = 'unknown client')
 {
     if (is_null($user) || is_null($password)) {
         $response = new JSONResponse();
         $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
         return $response;
     }
     $loginName = $user;
     $user = $this->userManager->checkPassword($loginName, $password);
     if ($user === false) {
         $response = new JSONResponse();
         $response->setStatus(Http::STATUS_UNAUTHORIZED);
         return $response;
     }
     if ($this->twoFactorAuthManager->isTwoFactorAuthenticated($user)) {
         $resp = new JSONResponse();
         $resp->setStatus(Http::STATUS_UNAUTHORIZED);
         return $resp;
     }
     $token = $this->secureRandom->generate(128);
     $this->tokenProvider->generateToken($token, $user->getUID(), $loginName, $password, $name, IToken::PERMANENT_TOKEN);
     return ['token' => $token];
 }