/** * @return JWT $this */ public function sign() { if (empty($this->key)) { throw new \InvalidArgumentException('Secret key needs to be used.'); } $headerData = array('typ' => 'JWT', 'alg' => $this->algorithm); $jwtSegment[0] = $this->encoder->encodeData($headerData); $jwtSegment[1] = $this->encoder->encodeData($this->payloadData); $signature = $this->secureHash(implode('.', $jwtSegment)); $jwtSegment[2] = $this->encoder->encode($signature); $this->jsonWebToken = implode('.', $jwtSegment); $this->tokenValidated = true; return $this; }