|
4 | 4 |
|
5 | 5 | namespace Vonage\Client\Credentials; |
6 | 6 |
|
7 | | -class Gnp extends Keypair |
| 7 | +use Lcobucci\JWT\Encoding\JoseEncoder; |
| 8 | +use Lcobucci\JWT\Token; |
| 9 | +use Vonage\JWT\TokenGenerator; |
| 10 | + |
| 11 | +class Gnp extends AbstractCredentials |
8 | 12 | { |
9 | 13 | protected ?string $code = null; |
10 | 14 | protected ?string $state = null; |
11 | 15 | protected ?string $redirectUri = null; |
12 | 16 |
|
13 | | - public function __construct(protected string $msisdn, protected string $key, $application = null) |
| 17 | + public function __construct( |
| 18 | + protected string $msisdn, |
| 19 | + protected string $key, |
| 20 | + protected $application = null |
| 21 | + ) { |
| 22 | + } |
| 23 | + |
| 24 | + public function getKeyRaw(): string |
14 | 25 | { |
15 | | - parent::__construct($key, $application); |
| 26 | + return $this->key; |
16 | 27 | } |
17 | 28 |
|
18 | 29 | public function getMsisdn(): string |
@@ -59,4 +70,62 @@ public function setRedirectUri(?string $redirectUri): Gnp |
59 | 70 | $this->redirectUri = $redirectUri; |
60 | 71 | return $this; |
61 | 72 | } |
| 73 | + |
| 74 | + public function generateJwt(array $claims = []): Token |
| 75 | + { |
| 76 | + $generator = new TokenGenerator($this->application, $this->getKeyRaw()); |
| 77 | + |
| 78 | + if (isset($claims['exp'])) { |
| 79 | + // This will change to an Exception in 5.0 |
| 80 | + trigger_error('Expiry date is automatically generated from now and TTL, so cannot be passed in |
| 81 | + as an argument in claims', E_USER_WARNING); |
| 82 | + unset($claims['nbf']); |
| 83 | + } |
| 84 | + |
| 85 | + if (isset($claims['ttl'])) { |
| 86 | + $generator->setTTL($claims['ttl']); |
| 87 | + unset($claims['ttl']); |
| 88 | + } |
| 89 | + |
| 90 | + if (isset($claims['jti'])) { |
| 91 | + $generator->setJTI($claims['jti']); |
| 92 | + unset($claims['jti']); |
| 93 | + } |
| 94 | + |
| 95 | + if (isset($claims['nbf'])) { |
| 96 | + // Due to older versions of lcobucci/jwt, this claim has |
| 97 | + // historic fraction conversation issues. For now, nbf is not supported. |
| 98 | + // This will change to an Exception in 5.0 |
| 99 | + trigger_error('NotBefore Claim is not supported in Vonage JWT', E_USER_WARNING); |
| 100 | + unset($claims['nbf']); |
| 101 | + } |
| 102 | + |
| 103 | + if (isset($claims['sub'])) { |
| 104 | + $generator->setSubject($claims['sub']); |
| 105 | + unset($claims['sub']); |
| 106 | + } |
| 107 | + |
| 108 | + if (!empty($claims)) { |
| 109 | + foreach ($claims as $claim => $value) { |
| 110 | + $generator->addClaim($claim, $value); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + $jwt = $generator->generate(); |
| 115 | + $parser = new Token\Parser(new JoseEncoder()); |
| 116 | + |
| 117 | + // Backwards compatible for signature. In 5.0 this will return a string value |
| 118 | + return $parser->parse($jwt); |
| 119 | + } |
| 120 | + |
| 121 | + public function getApplication(): ?string |
| 122 | + { |
| 123 | + return $this->application; |
| 124 | + } |
| 125 | + |
| 126 | + public function setApplication(mixed $application): Keypair |
| 127 | + { |
| 128 | + $this->application = $application; |
| 129 | + return $this; |
| 130 | + } |
62 | 131 | } |
0 commit comments