public function generateAction() { $console = $this->getServiceLocator()->get('console'); $jwtService = new Jwt(); // Make sure that we are running in a console and the user has not tricked our // application into running this action from a public web server. $request = $this->getRequest(); if (!$request instanceof ConsoleRequest) { throw new RuntimeException('You can only use this action from a console.'); } $privateKeyPath = ''; while (!file_exists($privateKeyPath)) { $privateKeyPath = Prompt\Line::prompt("Private Key path: ", false, 255); } $privateKey = file_get_contents($privateKeyPath); $iss = Prompt\Line::prompt("(iss) The issuer, usually the client_id: ", false, 255); $sub = Prompt\Line::prompt("(sub) The subject, usually a user_id: ", true, 255); $aud = Prompt\Line::prompt("(aud) The audience, usually the URI for the oauth server. Not required.: ", true, 255); $exp = Prompt\Line::prompt("(exp) The expiration date in seconds since epoch. If the current time is" . " greater than the exp, the JWT is invalid. Not required: ", true, 255); $nbf = Prompt\Line::prompt('(nbt) The "not before" time in seconds since epoch. If the current time is' . 'less than the nbf, the JWT is invalid. Not required: ', true, 255); $jti = Prompt\Line::prompt('(jti) The "jwt token identifier", or nonce for this JWT. Not Required: ', true, 255); $console->write($jwtService->generate($privateKey, $iss, $sub, $aud, $exp, $nbf, $jti) . "\n", Color::YELLOW); }
/** * Creates a test JWT token to be tested in an api client such as PostMan * @return ViewModel */ public function testJwtAction() { $jwt_id = (int) $this->params()->fromRoute('jwt_id', false); $client_id = (int) $this->params()->fromRoute('client_id', false); $jwtObject = $this->jwtService->find($jwt_id); $clientObject = $this->clientService->find($client_id); if (!$clientObject instanceof Client) { $this->flashMessenger()->addErrorMessage('Missing client object'); return $this->redirect()->toRoute('zf-oauth-doctrine-gui/clients'); } if (!$jwtObject instanceof Jwt) { $this->flashMessenger()->addErrorMessage('Missing jwt object'); return $this->redirect()->toRoute('zf-oauth-doctrine-gui/clients'); } $jwt_array = ['issuer' => $clientObject->getClientId(), 'subject' => $jwtObject->getSubject()]; $prg = $this->prg(); if ($prg instanceof Response) { return $prg; } elseif ($prg === false) { return new ViewModel(['form' => $this->testJwtForm, 'client_id' => $client_id, 'jwt_id' => $jwt_id, 'jwt_array' => $jwt_array, 'jwt' => '']); } $privateKey = $prg['jwt']['privkey']; $iss = $prg['jwt']['iss']; $sub = $prg['jwt']['sub']; $aud = $prg['jwt']['aud']; $exp = $prg['jwt']['exp']; $nbf = $prg['jwt']['nbt']; $jti = $prg['jwt']['jti']; $jwtService = new JwtClient(); $jwt = $jwtService->generate($privateKey, $iss, $sub, $aud, $exp, $nbf, $jti); return new ViewModel(['form' => $this->testJwtForm, 'client_id' => $client_id, 'jwt_id' => $jwt_id, 'jwt_array' => $jwt_array, 'jwt' => $jwt]); }