/** * Fetch the access token * * @return string|null Base64 encoded JSON Web Token or null if not found. */ public function fetchToken(RequestInterface $request) { /* If using PHP in CGI mode and non standard environment */ $server_params = $request->getServerParams(); if (isset($server_params[$this->options["environment"]])) { $message = "Using token from environent"; $header = $server_params[$this->options["environment"]]; } else { $message = "Using token from request header"; $header = $request->getHeader("Authorization"); $header = isset($header[0]) ? $header[0] : ""; } if (preg_match("/Bearer\\s+(.*)\$/i", $header, $matches)) { $this->log(LogLevel::DEBUG, $message); return $matches[1]; } /* Bearer not found, try a cookie. */ $cookie_params = $request->getCookieParams(); if (isset($cookie_params[$this->options["cookie"]])) { $this->log(LogLevel::DEBUG, "Using token from cookie"); $this->log(LogLevel::DEBUG, $cookie_params[$this->options["cookie"]]); return $cookie_params[$this->options["cookie"]]; } /* If everything fails log and return false. */ $this->message = "Token not found"; $this->log(LogLevel::WARNING, $this->message); return false; }
/** * Fetch the access token * * @return string|null Base64 encoded JSON Web Token or null if not found. */ public function fetchToken(RequestInterface $request) { /* If using PHP in CGI mode and non standard environment */ $server_params = $request->getServerParams(); $header = ""; /* Check for each given environment */ foreach ((array) $this->options["environment"] as $environment) { if (isset($server_params[$environment])) { $message = "Using token from environment"; $header = $server_params[$environment]; } } /* Nothing in environment, try header instead */ if (empty($header)) { $message = "Using token from request header"; $headers = $request->getHeader("Authorization"); $header = isset($headers[0]) ? $headers[0] : ""; } /* Try apache_request_headers() as last resort */ if (empty($header) && function_exists("apache_request_headers")) { $headers = apache_request_headers(); $header = isset($headers["Authorization"]) ? $headers["Authorization"] : ""; } if (preg_match("/Bearer\\s+(.*)\$/i", $header, $matches)) { $this->log(LogLevel::DEBUG, $message); return $matches[1]; } /* Bearer not found, try a cookie. */ $cookie_params = $request->getCookieParams(); if (isset($cookie_params[$this->options["cookie"]])) { $this->log(LogLevel::DEBUG, "Using token from cookie"); $this->log(LogLevel::DEBUG, $cookie_params[$this->options["cookie"]]); return $cookie_params[$this->options["cookie"]]; } /* If everything fails log and return false. */ $this->message = "Token not found"; $this->log(LogLevel::WARNING, $this->message); return false; }