-
Notifications
You must be signed in to change notification settings - Fork 1
/
Firebase.php
94 lines (86 loc) · 2.49 KB
/
Firebase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace Pierrocknroll\JwtAuthFirebase;
use Firebase\JWT\JWT as FireJWT;
use Tymon\JWTAuth\Contracts\Providers\JWT;
use Tymon\JWTAuth\Exceptions\{
JWTException, TokenInvalidException
};
use Tymon\JWTAuth\Providers\JWT\Provider;
class Firebase extends Provider implements JWT
{
/**
* Constructor.
*
* @param string $secret
* @param string $algo
* @param array $keys
* @param string|null $driver
*/
public function __construct($secret = null, $algo = null, array $keys = [], $driver = null)
{
$secret = empty($secret) ? $this->config('secret') : $secret;
$algo = empty($algo) ? $this->config('algo') : $algo;
$keys = empty($keys) ? $this->config('keys') : $keys;
parent::__construct($secret, $algo, $keys);
}
/**
* Helper to get the config values.
*
* @param string $key
* @param string $default
*
* @return mixed
*/
protected function config($key, $default = null)
{
return config("jwt.$key", $default);
}
/**
* Create a JSON Web Token.
*
* @param array $payload
*
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*
* @return string
*/
public function encode(array $payload)
{
$key = $this->isAsymmetric() ? $this->getPrivateKey() : $this->getSecret();
try {
return FireJWT::encode($payload, $key, $this->getAlgo());
} catch (\Exception $e) {
throw new JWTException('Could not create token: ' . $e->getMessage(), $e->getCode(), $e);
}
}
/**
* Decode a JSON Web Token.
*
* @param string $token
*
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*
* @return array
*/
public function decode($token)
{
$key = $this->isAsymmetric() ? $this->getPublicKey() : $this->getSecret();
try {
return (array) FireJWT::decode($token, $key, [$this->getAlgo()]);
} catch (\Exception $e) {
throw new TokenInvalidException('Could not decode token: ' . $e->getMessage(), $e->getCode(), $e);
}
}
/**
* Determine if the algorithm is asymmetric, and thus
* requires a public/private key combo.
*
* @return bool
*/
protected function isAsymmetric()
{
if (isset(FireJWT::$supported_algs[$this->getAlgo()]))
return in_array('openssl', FireJWT::$supported_algs[$this->getAlgo()]);
return false;
}
}