/**
  * @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;
 }
 /**
  * Invoke middleware.
  *
  * @param ServerRequestInterface $request  request object
  * @param ResponseInterface      $response response object
  * @param callable               $next     next middleware
  *
  * @return ResponseInterface response object
  *
  * @SuppressWarnings(PHPMD.StaticAccess)
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $response = FigResponseCookies::modify($response, $this->cookieName, function (SetCookie $setCookie) use($request, $response) {
         return $this->modifyCookie($request, $response, $setCookie);
     });
     return $next($request, $response);
 }
 /**
  * {@inheritdoc}
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     $cookieName = 'cookie-test';
     $cookie = FigRequestCookies::get($request, $cookieName);
     $cookie = $cookie->getValue() ?: 'Not Found';
     $cookieTest = "\nCookie test: {$cookie}";
     $contents = str_replace('{cookie}', $cookieTest, self::HTML);
     $response->getBody()->write($contents);
     $responseCookie = SetCookie::create($cookieName, 'testing-' . \random_int(100, 200));
     return FigResponseCookies::set($response, $responseCookie);
 }
 /**
  * {@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);
     }
 }
 protected function withForgetCookie(ResponseInterface $response)
 {
     // Delete the cookie by setting it to an expiration date in the past
     return FigResponseCookies::set($response, SetCookie::create('flarum_remember')->withMaxAge(-2628000)->withPath('/')->withHttpOnly(true));
 }
Example #6
0
 public function forget(ResponseInterface $response)
 {
     return FigResponseCookies::expire($response, $this->cookieName);
 }
Example #7
0
 public function forget(ResponseInterface $response)
 {
     return FigResponseCookies::set($response, $this->createCookie()->withMaxAge(-2628000));
 }
Example #8
0
 private function withSessionCookie(Response $response, SessionInterface $session)
 {
     return FigResponseCookies::set($response, SetCookie::create($session->getName(), $session->getId())->withPath('/')->withHttpOnly(true));
 }
 /**
  * @param ResponseInterface $response
  *
  * @return SetCookie
  */
 private function getCookie(ResponseInterface $response, string $name = SessionMiddleware::DEFAULT_COOKIE) : SetCookie
 {
     return FigResponseCookies::get($response, $name);
 }
 /**
  * Removes authorization cookie, deauthorizing the user.
  *
  * @param ResponseInterface $response Response instance.
  * Used to unset the authorization cookie.
  *
  * @return \Psr\Http\Message\ResponseInterface Response instance with
  * authorization cookie removed.
  */
 public function deauthorizeUser(ResponseInterface $response)
 {
     return FigResponseCookies::remove($response, 'authorization');
 }
Example #11
0
 /**
  * @param ResponseInterface $response
  * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
  * @return ResponseInterface
  */
 protected function addCookieToResponse(ResponseInterface $response, $session)
 {
     $s = $session;
     if ($this->sessionIsPersistent($c = $this->manager->getSessionConfig())) {
         $secure = array_get($c, 'secure', false);
         $setCookie = SetCookie::create($s->getName())->withValue($s->getId())->withExpires($this->getCookieLifetime())->withDomain($c['domain'])->withPath($c['path'])->withHttpOnly(true)->withSecure($secure);
         $response = FigResponseCookies::set($response, $setCookie);
     }
     return $response;
 }
 /**
  * @param SessionInterface $sessionContainer
  * @param Response         $response
  * @param Token            $token
  *
  * @return Response
  *
  * @throws \InvalidArgumentException
  */
 private function appendToken(SessionInterface $sessionContainer, Response $response, Token $token = null) : Response
 {
     $sessionContainerChanged = $sessionContainer->hasChanged();
     $sessionContainerEmpty = $sessionContainer->isEmpty();
     if ($sessionContainerChanged && $sessionContainerEmpty) {
         return FigResponseCookies::set($response, $this->getExpirationCookie());
     }
     if ($sessionContainerChanged || !$sessionContainerEmpty && $token && $this->shouldTokenBeRefreshed($token)) {
         return FigResponseCookies::set($response, $this->getTokenCookie($sessionContainer));
     }
     return $response;
 }
 /**
  * Adds a CSRF cookie to given PSR-7 Response instance.
  *
  * @param ResponseInterface $response Response instance with CSRF cookie.
  */
 public function setResposneCookie(ResponseInterface $response)
 {
     $dateTime = new \DateTime("now");
     $dateTime->add(new \DateInterval("P1D"));
     return FigResponseCookies::set($response, SetCookie::create('csrf')->withValue($this->csrfToken)->withExpires($dateTime->format(\DateTime::COOKIE))->withPath('/'));
 }