Skip to content

Commit 7f7bd59

Browse files
20181129 deployment
1 parent d9d945f commit 7f7bd59

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3432
-7
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/**
4+
* JWT encoding
5+
*
6+
* PHP Version 5.2+
7+
*
8+
* @category Authentication
9+
* @package HPS
10+
* @author Heartland Payment Systems <entapp_devportal@e-hps.com>
11+
* @license Custom https://github.com/hps/heartland-php/blob/master/LICENSE.txt
12+
* @link https://developer.heartlandpaymentsystems.com
13+
*/
14+
15+
class JWT
16+
{
17+
/**
18+
* Encodes a JWT with a `$key` and a `$payload`
19+
*
20+
* @param string $key key used to sign the JWT
21+
* @param mixed $payload payload to be included
22+
*
23+
* @return string
24+
*/
25+
public static function encode($key = '', $payload = array())
26+
{
27+
$header = array('typ' => 'JWT', 'alg' => 'HS256');
28+
29+
$parts = array(
30+
self::urlsafeBase64Encode(json_encode($header)),
31+
self::urlsafeBase64Encode(json_encode($payload)),
32+
);
33+
$signingData = implode('.', $parts);
34+
$signature = self::sign($key, $signingData);
35+
$parts[] = self::urlsafeBase64Encode($signature);
36+
37+
return implode('.', $parts);
38+
}
39+
40+
/**
41+
* Creates a url-safe base64 encoded AnyValuesToken
42+
*
43+
* @param string $data data to be encoded
44+
*
45+
* @return string
46+
*/
47+
public static function urlsafeBase64Encode($data)
48+
{
49+
return str_replace('=', '', strtr(base64_encode($data), '+/', '-_'));
50+
}
51+
52+
/**
53+
* Signs a set of `$signingData` with a given `$key`
54+
*
55+
* @param string $key key used to sign the JWT
56+
* @param string $signingData data to be signed
57+
*
58+
* @return string
59+
*/
60+
public static function sign($key, $signingData)
61+
{
62+
return hash_hmac('sha256', $signingData, $key, true);
63+
}
64+
};

0 commit comments

Comments
 (0)