44
55namespace Vonage \Client \Credentials ;
66
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
812{
9- public function __construct (protected string $ msisdn , protected string $ key , $ application = null )
13+ protected ?string $ code = null ;
14+ protected ?string $ state = null ;
15+ protected ?string $ redirectUri = null ;
16+
17+ public function __construct (
18+ protected string $ msisdn ,
19+ protected string $ key ,
20+ protected $ application = null
21+ ) {
22+ }
23+
24+ public function getKeyRaw (): string
1025 {
11- parent :: __construct ( $ key , $ application ) ;
26+ return $ this -> key ;
1227 }
1328
1429 public function getMsisdn (): string
@@ -22,4 +37,95 @@ public function setMsisdn(string $msisdn): Gnp
2237
2338 return $ this ;
2439 }
40+
41+ public function getCode (): ?string
42+ {
43+ return $ this ->code ;
44+ }
45+
46+ public function setCode (?string $ code ): Gnp
47+ {
48+ $ this ->code = $ code ;
49+ return $ this ;
50+ }
51+
52+ public function getState (): ?string
53+ {
54+ return $ this ->state ;
55+ }
56+
57+ public function setState (?string $ state ): Gnp
58+ {
59+ $ this ->state = $ state ;
60+ return $ this ;
61+ }
62+
63+ public function getRedirectUri (): ?string
64+ {
65+ return $ this ->redirectUri ;
66+ }
67+
68+ public function setRedirectUri (?string $ redirectUri ): Gnp
69+ {
70+ $ this ->redirectUri = $ redirectUri ;
71+ return $ this ;
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+ }
25131}
0 commit comments