/**
  * @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;
 }
示例#2
0
 public function remember(ResponseInterface $response, AccessToken $token, $session = false)
 {
     $cookie = $this->createCookie()->withValue($token->id);
     if (!$session) {
         $lifetime = 60 * 60 * 24 * 14;
         $token->lifetime = $lifetime;
         $token->save();
         $cookie = $cookie->withMaxAge($lifetime);
     }
     return FigResponseCookies::set($response, $cookie);
 }
 /**
  * {@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));
 }
示例#6
0
 public function forget(ResponseInterface $response)
 {
     return FigResponseCookies::set($response, $this->createCookie()->withMaxAge(-2628000));
 }
示例#7
0
 private function withSessionCookie(Response $response, SessionInterface $session)
 {
     return FigResponseCookies::set($response, SetCookie::create($session->getName(), $session->getId())->withPath('/')->withHttpOnly(true));
 }
 /**
  * Authorizes given Student entity.
  *
  * This method authorizes user using cookies. In details, it adds an
  * authorization cookie to the given response instance. Authorization
  * cookie contains an authorization token, which is unique for every student
  * entity.
  *
  * This method throws an InvalidArgumentException if given student entity does
  * not have an authorization token. You can use createAuthToken() method to generate
  * an auth token for a student entity.
  *
  * @param Student $student Student to authorize
  * @param ResponseInterface $response Response instance for authorization cookie.
  *
  * @throws \InvalidArgumentException If given Student instance does not have an
  * authorization token.
  *
  * @return \Psr\Http\Message\ResponseInterface Response instance with authorization cookie.
  */
 public function authorizeUser(Student $student, ResponseInterface $response)
 {
     if (empty($student->getToken())) {
         throw new \InvalidArgumentException('Student must have an authorization' . ' token in order to complete authorization.');
     }
     $dateTime = new \DateTime("now");
     $dateTime->add(new \DateInterval("P90D"));
     return FigResponseCookies::set($response, SetCookie::create('authorization')->withValue($student->getToken())->withExpires($dateTime->format(\DateTime::COOKIE))->withPath('/'));
 }
示例#9
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;
 }
示例#10
0
 /**
  * @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;
 }
示例#11
0
 public function remember(ResponseInterface $response, AccessToken $token)
 {
     $token->lifetime = 60 * 60 * 24 * 14;
     $token->save();
     return FigResponseCookies::set($response, $this->createCookie()->withValue($token->id)->withMaxAge(14 * 24 * 60 * 60));
 }
示例#12
0
 /**
  * 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('/'));
 }