Skip to content

Commit

Permalink
Create JWTGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Bukashk0zzz committed Dec 30, 2017
1 parent 75f5d24 commit 2ea80c6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
29 changes: 1 addition & 28 deletions Service/GuzzleJWTMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace AtlassianConnectBundle\Service;

use Firebase\JWT\JWT;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
Expand All @@ -28,36 +27,10 @@ function (RequestInterface $request) use ($issuer, $secret, $user) {
return new Request(
$request->getMethod(),
$request->getUri(),
\array_merge($request->getHeaders(), ['Authorization' => 'JWT '.static::createToken($request, $issuer, $secret, $user)]),
\array_merge($request->getHeaders(), ['Authorization' => 'JWT '.JWTGenerator::generate($request, $issuer, $secret, $user)]),
$request->getBody()
);
}
);
}

/**
* Create JWT token used by Atlassian REST API request
*
* @param RequestInterface $request
* @param string $issuer Key of the add-on
* @param string $secret Shared secret of the Tenant
* @param null|string $user
*
* @return string
*/
private static function createToken(RequestInterface $request, string $issuer, string $secret, ?string $user): string
{
$data = [
'iss' => $issuer,
'iat' => \time(),
'exp' => \strtotime('+1 day'),
'qsh' => QSHGenerator::generate((string) $request->getUri(), $request->getMethod()),
];

if ($user !== null) {
$data['sub'] = $user;
}

return JWT::encode($data, $secret);
}
}
38 changes: 38 additions & 0 deletions Service/JWTGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace AtlassianConnectBundle\Service;

use Firebase\JWT\JWT;
use Psr\Http\Message\RequestInterface;

/**
* Class JWTGenerator
*/
class JWTGenerator
{
/**
* Create JWT token used by Atlassian REST API request
*
* @param RequestInterface $request
* @param string $issuer Key of the add-on
* @param string $secret Shared secret of the Tenant
* @param null|string $user
*
* @return string
*/
public static function generate(RequestInterface $request, string $issuer, string $secret, ?string $user): string
{
$data = [
'iss' => $issuer,
'iat' => \time(),
'exp' => \strtotime('+1 day'),
'qsh' => QSHGenerator::generate((string) $request->getUri(), $request->getMethod()),
];

if ($user !== null) {
$data['sub'] = $user;
}

return JWT::encode($data, $secret);
}
}

0 comments on commit 2ea80c6

Please sign in to comment.