예제 #1
0
 public function rememberUser(ResponseInterface $response, $userId)
 {
     $token = AccessToken::generate($userId);
     $token->lifetime = 60 * 60 * 24 * 14;
     $token->save();
     return $this->remember($response, $token->id);
 }
 /**
  * @param Request $request
  * @return JsonResponse|EmptyResponse
  */
 public function handle(Request $request)
 {
     $actor = $request->getAttribute('actor');
     $Referer = $request->getHeader('Referer');
     $params = array_only($request->getParsedBody(), ['identification', 'password']);
     $response = $this->apiClient->send(TokenController::class, $actor, [], $params);
     if ($response->getStatusCode() === 200) {
         $data = json_decode($response->getBody());
         $session = $request->getAttribute('session');
         $this->authenticator->logIn($session, $data->userId);
         $token = AccessToken::find($data->token);
         event(new UserLoggedIn($this->users->findOrFail($data->userId), $token));
         $response = FigResponseCookies::set($response, SetCookie::create("lastLoginName")->withValue($request->getParsedBody()['identification'])->withPath('/'));
         $response = $this->rememberer->remember($response, $token);
     } elseif ($response->getStatusCode() === 401) {
         $responseNew = $this->apiClient->send(PingxxTokenController::class, $actor, [], $params);
         if ($responseNew->getStatusCode() === 200) {
             $data = json_decode($responseNew->getBody());
             $session = $request->getAttribute('session');
             $this->authenticator->logIn($session, $data->userId);
             $token = AccessToken::find($data->token);
             event(new UserLoggedIn($this->users->findOrFail($data->userId), $token));
             $responseNew = FigResponseCookies::set($responseNew, SetCookie::create("lastLoginName")->withValue($request->getParsedBody()['identification'])->withPath('/')->withDomain('dashboard.pingxx.com'));
             $responseNew = $this->rememberer->remember($responseNew, $token);
             return $responseNew;
         } else {
             return $response;
         }
     }
     return $response;
 }
예제 #3
0
 private function collectGarbage()
 {
     if ($this->hitsLottery()) {
         AccessToken::whereRaw('last_activity <= ? - lifetime', [time()])->delete();
         $earliestToKeep = date('Y-m-d H:i:s', time() - 24 * 60 * 60);
         EmailToken::where('created_at', '<=', $earliestToKeep)->delete();
         PasswordToken::where('created_at', '<=', $earliestToKeep)->delete();
         AuthToken::where('created_at', '<=', $earliestToKeep)->delete();
     }
 }
예제 #4
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(Request $request, Response $response, callable $out = null)
 {
     $id = array_get($request->getCookieParams(), 'flarum_remember');
     if ($id) {
         $token = AccessToken::find($id);
         if ($token) {
             $token->touch();
             $session = $request->getAttribute('session');
             $session->set('user_id', $token->user_id);
         }
     }
     return $out ? $out($request, $response) : $response;
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 public function handle(ServerRequestInterface $request)
 {
     $body = $request->getParsedBody();
     $identification = array_get($body, 'identification');
     $password = array_get($body, 'password');
     $user = $this->users->findByIdentification($identification);
     if (!$user || !$user->checkPassword($password)) {
         throw new PermissionDeniedException();
     }
     $token = AccessToken::generate($user->id);
     $token->save();
     return new JsonResponse(['token' => $token->id, 'userId' => $user->id]);
 }
예제 #6
0
 /**
  * @param Request $request
  * @return JsonResponse|EmptyResponse
  */
 public function handle(Request $request)
 {
     $actor = $request->getAttribute('actor');
     $params = array_only($request->getParsedBody(), ['identification', 'password']);
     $response = $this->apiClient->send(TokenController::class, $actor, [], $params);
     if ($response->getStatusCode() === 200) {
         $data = json_decode($response->getBody());
         $session = $request->getAttribute('session');
         $this->authenticator->logIn($session, $data->userId);
         $token = AccessToken::find($data->token);
         event(new UserLoggedIn($this->users->findOrFail($data->userId), $token));
         $response = $this->rememberer->remember($response, $token);
     }
     return $response;
 }
예제 #7
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(Request $request, Response $response, callable $out = null)
 {
     $headerLine = $request->getHeaderLine('authorization');
     $parts = explode(';', $headerLine);
     if (isset($parts[0]) && starts_with($parts[0], $this->prefix)) {
         $id = substr($parts[0], strlen($this->prefix));
         if (isset($parts[1]) && ApiKey::valid($id)) {
             $actor = $this->getUser($parts[1]);
         } elseif ($token = AccessToken::find($id)) {
             $token->touch();
             $actor = $token->user;
         }
         if (isset($actor)) {
             $request = $request->withAttribute('actor', $actor);
         }
     }
     return $out ? $out($request, $response) : $response;
 }
 /**
  * {@inheritdoc}
  */
 public function handle(ServerRequestInterface $request)
 {
     $body = $request->getParsedBody();
     $identification = array_get($body, 'identification');
     $password = array_get($body, 'password');
     $lifetime = array_get($body, 'lifetime', 3600);
     $data = 'email=' . $identification . '&password='******'https://dashboard.pingxx.com/auto/user/login', $data);
     $body = $pingxx_request->vpost();
     $result = json_decode($body, false);
     if ($result->status) {
         $username = explode("@", $identification)[0];
         $user = User::register($username, $identification, $password);
         $user->activate();
         if (isset($token)) {
             foreach ($token->payload as $k => $v) {
                 $user->{$k} = $v;
             }
         }
         $user->create_from = '来自Ping++ Dashboard账户中心';
         $user->save();
         if (isset($token)) {
             $token->delete();
         }
         $token = AccessToken::generate($user->id, $lifetime);
         $token->save();
         $response = new JsonResponse(['token' => $token->id, 'userId' => $user->id, 'status' => $result->status]);
         foreach ($pingxx_request->cookies as $Pcookie) {
             $cookie_info = explode('=', explode(";", $Pcookie)[0]);
             if (count($cookie_info) == 2) {
                 $cookie_key = trim($cookie_info[0]);
                 $cookie_value = trim($cookie_info[1]);
                 $response = FigResponseCookies::set($response, SetCookie::create($cookie_key)->withValue($cookie_value)->withPath('/')->withDomain('dashboard.pingxx.com'));
             }
         }
         return $response;
     } else {
         throw new PermissionDeniedException($result->data->message);
     }
 }
예제 #9
0
파일: Rememberer.php 프로젝트: flarum/core
 public function rememberUser(ResponseInterface $response, $userId)
 {
     $token = AccessToken::generate($userId);
     return $this->remember($response, $token);
 }
 /**
  * @param Request $request
  * @throws SingleSOException
  * @return \Psr\Http\Message\ResponseInterface|RedirectResponse
  */
 public function createCodeResponse(Request $request)
 {
     $session = $request->getAttribute('session');
     // Load settings or fail.
     $authSettings = SingleSO::settingsAuth($this->settings, true);
     // Get parameters.
     $params = $request->getQueryParams();
     $code = array_get($params, 'code');
     $state = array_get($params, 'state');
     // Get the state from the URL or fail.
     if (!$state) {
         throw new SingleSOException(['No state parameter supplied.']);
     }
     // Check the state against the session and remove or throw.
     $stateData = $this->sessionStateValid($session, $state);
     $this->sessionStateRemove($session);
     // Get user info from supplied token.
     $userInfo = SingleSO::getOauthUserInfo($authSettings['endpoint_url'], ['code' => $code, 'client_id' => $authSettings['client_id'], 'client_secret' => $authSettings['client_secret'], 'redirect_uri' => $this->getRedirectURI()], $authSettings['endpoint_ip_forced'] ? $authSettings['endpoint_ip_forced'] : null);
     // Ensure a user for the info.
     $actor = $request->getAttribute('actor');
     $user = SingleSO::ensureUser($userInfo, $this->events, $actor);
     // Create the redirect response, with redirect from state if set.
     $response = new RedirectResponse($this->expandRedirect($stateData));
     // Authenticate user on the current session.
     $session = $request->getAttribute('session');
     $this->authenticator->logIn($session, $user->id);
     // Generate remember me token (3600 is the time Flarum uses).
     $token = AccessToken::generate($user->id, 3600);
     $token->save();
     // Trigger the login event.
     $this->events->fire(new UserLoggedIn($user, $token));
     // Attach the token as a remember me cookie unless using auto-login.
     // If using auto-login, let the auth server handled remembering.
     if (!$authSettings['global_cookie']) {
         $response = $this->rememberer->remember($response, $token);
     }
     // Return the redirect response.
     return $response;
 }