22
33namespace OAuth2 \Encryption ;
44
5+ use Firebase \JWT \JWT ;
6+ use Firebase \JWT \Key ;
7+
58/**
69 * Bridge file to use the firebase/php-jwt package for JWT encoding and decoding.
710 * @author Francis Chuang <francis.chuang@gmail.com>
@@ -10,38 +13,48 @@ class FirebaseJwt implements EncryptionInterface
1013{
1114 public function __construct ()
1215 {
13- if (!class_exists (' \ JWT' )) {
16+ if (!class_exists (JWT ::class )) {
1417 throw new \ErrorException ('firebase/php-jwt must be installed to use this feature. You can do this by running "composer require firebase/php-jwt" ' );
1518 }
1619 }
1720
1821 public function encode ($ payload , $ key , $ alg = 'HS256 ' , $ keyId = null )
1922 {
20- return \ JWT ::encode ($ payload , $ key , $ alg , $ keyId );
23+ return JWT ::encode ($ payload , $ key , $ alg , $ keyId );
2124 }
2225
2326 public function decode ($ jwt , $ key = null , $ allowedAlgorithms = null )
2427 {
2528 try {
26-
2729 //Maintain BC: Do not verify if no algorithms are passed in.
2830 if (!$ allowedAlgorithms ) {
29- $ key = null ;
31+ $ tks = \explode ('. ' , $ jwt );
32+ if (\count ($ tks ) === 3 ) {
33+ [$ headb64 ] = $ tks ;
34+ $ headerRaw = JWT ::urlsafeB64Decode ($ headb64 );
35+ if (($ header = JWT ::jsonDecode ($ headerRaw ))) {
36+ $ key = new Key ($ key , $ header ->alg );
37+ }
38+ }
39+ } elseif (is_array ($ allowedAlgorithms )) {
40+ $ key = new Key ($ key , $ allowedAlgorithms [0 ]);
41+ } else {
42+ $ key = new Key ($ key , $ allowedAlgorithms );
3043 }
3144
32- return (array )\ JWT ::decode ($ jwt , $ key, $ allowedAlgorithms );
45+ return (array ) JWT ::decode ($ jwt , $ key );
3346 } catch (\Exception $ e ) {
3447 return false ;
3548 }
3649 }
3750
3851 public function urlSafeB64Encode ($ data )
3952 {
40- return \ JWT ::urlsafeB64Encode ($ data );
53+ return JWT ::urlsafeB64Encode ($ data );
4154 }
4255
4356 public function urlSafeB64Decode ($ b64 )
4457 {
45- return \ JWT ::urlsafeB64Decode ($ b64 );
58+ return JWT ::urlsafeB64Decode ($ b64 );
4659 }
4760}
0 commit comments