diff --git a/src/Session/HttpSession.php b/src/Session/HttpSession.php index d65258b..3313f1a 100644 --- a/src/Session/HttpSession.php +++ b/src/Session/HttpSession.php @@ -62,7 +62,11 @@ public function __construct() $this->response = getDeepContextValueByClassName(Response::class); if($this->config->getSessionUsage() == SessionConfig::USAGE_COOKIE) { $this->id = $this->request->getCookieParams()[$this->config->getSessionName()] ?? null; - }else{ + } elseif ($this->config->getSessionUsage() == SessionConfig::USEAGE_HEADER) { + /** @var array $_sesionIdentify */ + $_sesionIdentify = $this->request->getHeader(SessionConfig::HEADER_IDENTIFY); + $this->id = !empty($_sesionIdentify[0]) ? $_sesionIdentify[0] : null; + } else{ $authorization = explode(' ',$this->request->getHeaderLine('authorization')); if(isset($authorization[1])){ $this->id = $authorization[1]; @@ -146,6 +150,7 @@ public function removeAttribute(string $key): void { unset($this->attribute[$key]); } + public function refresh(): void { $id = $this->getId(); @@ -157,7 +162,14 @@ public function refresh(): void $this->response->withCookie(new Cookie($this->config->getSessionName(), $this->id, time() + $this->config->getTimeout(), $this->config->getPath(), $this->config->getDomain(), $this->config->getSecure(), $this->config->getHttpOnly())); - }else{ + } elseif ($this->config->getSessionUsage() == SessionConfig::USEAGE_HEADER) { + /** @var array $_sesionIdentify */ + $_sesionIdentify = $this->request->getHeader(SessionConfig::HEADER_IDENTIFY); + if (!empty($_sesionIdentify[0])) { + $sesionIdentify = $_sesionIdentify[0]; + $this->response->withHeader(SessionConfig::HEADER_IDENTIFY, $sesionIdentify); + } + } else{ $this->response->withHeader('Authorization', 'Bearer ' .$this->id); } $this->setAttribute("createTime", time()); diff --git a/src/Session/SessionConfig.php b/src/Session/SessionConfig.php index 354388d..d12161e 100644 --- a/src/Session/SessionConfig.php +++ b/src/Session/SessionConfig.php @@ -13,8 +13,29 @@ class SessionConfig extends BaseConfig { + /** + * Session key + */ const key = "session"; + + /** + * Usage cookie + */ const USAGE_COOKIE = 'cookie'; + + /** + * Usage head + */ + const USEAGE_HEADER = 'header'; + + /** + * Header identify to identify session + */ + const HEADER_IDENTIFY = 'sessionId'; + + /** + * Usage token + */ const USAGE_TOKEN = 'token'; /** @@ -36,6 +57,13 @@ class SessionConfig extends BaseConfig protected $sessionUsage = SessionConfig::USAGE_COOKIE; + /** + * Header identity. When $sessionUsage is set to header, should set $headerIdentity + * + * @var string + */ + protected $headerIdentity = SessionConfig::HEADER_IDENTIFY; + /** * @var string */