static function decode($jwt_string) { $segments = explode('.', $jwt_string); switch (count($segments)) { case 3: $jwt = new self(); $jwt->raw = $jwt_string; $jwt->header = (array) $jwt->extract($segments[0]); $jwt->claims = (array) $jwt->extract($segments[1]); $jwt->signature = $jwt->extract($segments[2], 'as_binary'); return $jwt; case 5: $jwe = new JOSE_JWE($jwt_string); $jwe->auth_data = $segments[0]; $jwe->header = (array) $jwe->extract($segments[0]); $jwe->jwe_encrypted_key = $jwe->extract($segments[1], 'as_binary'); $jwe->iv = $jwe->extract($segments[2], 'as_binary'); $jwe->cipher_text = $jwe->extract($segments[3], 'as_binary'); $jwe->authentication_tag = $jwe->extract($segments[4], 'as_binary'); return $jwe; default: throw new JOSE_Exception_InvalidFormat('JWT should have exact 3 or 5 segments'); } }
function testDecode() { $input = 'eyJhbGciOiJSU0ExXzUiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0.A2_4qS-61x17Q9NdT5kE0Qv5vw-D7zqGxACw42qM6l1iIHu31cENA8O5GTUhWordW3f93WY4ap1ZvCHO7pbbCF4NpOIMKjZtHObHRtPnA12zn-JZIxPCUHDtIQ6ucT-B0g5AmKDEDFO78Murz5l9QZH_Tl5t5x5-Asi3BO9Mm4s5dldykMvFxdC1j5IZ1ZBgN243OdKmvkTa0dn9wgjz9XEZHXoX_TKE4kDMyzIgW_U6Y4mP-cfZjQhTZAGwsBEz1kYTbM0bCf-FK3BBktpWZzjp4Y7cL6Zc7CabkNWAmMPcenxOFQZCOTeikmj4xrgZ9uPJ-DwJJNlnW_jPhEaesw.E1-sid2lZsrNOqc9vjgajg.s9vv7y5Qt5MwpA2AEGeuBQ.gH1oQlBSCdMK_jJEtoyWAw'; $jwe = JOSE_JWE::decode($input); $this->assertNull($jwe->plain_text); $this->assertEquals(array("alg" => "RSA1_5", "enc" => "A128CBC-HS256"), $jwe->header); }