Skip to content

Instantly share code, notes, and snippets.

@bhaktaraz
Last active January 21, 2020 05:44
Show Gist options
  • Save bhaktaraz/edbaa25c4100c9699bf1e5e2a80a8a90 to your computer and use it in GitHub Desktop.
Save bhaktaraz/edbaa25c4100c9699bf1e5e2a80a8a90 to your computer and use it in GitHub Desktop.
Build a JWT endpoint to work with the Support SDK for mobile applications to authenticate users in Zendesk Support (PHP/Symfony) Implentation
<?php
/**
* Created by PhpStorm.
* User: bhaktaraz
* Date: 11/26/19
* Time: 2:10 PM
*/
namespace App\Controller\API\V1;
use App\Entity\User;
use Firebase\JWT\JWT;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use FOS\RestBundle\Controller\AbstractFOSRestController;
class UtilityController extends AbstractFOSRestController
{
/**
* Zendesk Support service POST request JWT endpoint
*
* @Route("/api/jwt-zendesk", methods={"POST"})
* @SWG\Response(
* response=200,
* description="Returns a jwt token response",
* )
*
* @SWG\Parameter(
* name="user_token",
* in="query",
* type="string",
* description="User identifier provided by the app (User Id)"
* )
*
* @SWG\Tag(name="Utility")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function jwt(Request $request)
{
$token = $request->get('user_token');
if(empty($token)){
$view = $this->view(null, 401);
return $this->handleView($view);
}
$em = $this->getDoctrine()->getManager();
/** @var User $user */
$user = $em->getRepository(User::class)->find($token);
if(!$user instanceof User){
$view = $this->view(null, 401);
return $this->handleView($view);
}
$secret = $this->getParameter('zendesk_secret_key');
$payload = [
'name' => $user->getName(),
'email' => $user->getEmail(),
'phone' => $user->getPhone(),
'jti' => $token, "iat" => time()
];
$jwt = JWT::encode($payload, $secret,'HS256');
$response["jwt"] = $jwt;
$view = $this->view($response, 200);
return $this->handleView($view);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment