예제 #1
0
 /**
  */
 public function testSignAndEncrypt()
 {
     $jose = Jose::getInstance();
     $jwe = $jose->signAndEncrypt(['iss' => 'My app', 'exp' => time() + 3600, 'iat' => time(), 'nbf' => time(), 'sub' => 'foo@bar', 'jti' => '0123456789', 'aud' => 'My service'], 'My EC Key', ['alg' => 'ES256'], '7', ['alg' => 'A128KW', 'enc' => 'A128CBC-HS256', 'zip' => 'DEF'], [], [], JSONSerializationModes::JSON_FLATTENED_SERIALIZATION, 'foo,bar,baz');
     //First, we load the JWE
     $jws = $jose->load($jwe);
     $this->assertInstanceOf('Jose\\JWEInterface', $jws);
     $this->assertEquals('A128KW', $jws->getAlgorithm());
     $this->assertEquals('A128CBC-HS256', $jws->getEncryptionAlgorithm());
     $this->assertEquals('DEF', $jws->getZip());
     $this->assertTrue(is_array($jws->getPayload()));
     $jose->verify($jws);
     //Then, we load the JWS
     $result = $jose->load($jws->getPayload());
     $this->assertInstanceOf('Jose\\JWSInterface', $result);
     $this->assertEquals('ES256', $result->getAlgorithm());
     $this->assertEquals('My app', $result->getIssuer());
     $jose->verify($result);
 }
예제 #2
0
<?php

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Spomky-Labs
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */
include_once __DIR__ . '/../vendor/autoload.php';
use SpomkyLabs\Service\Jose;
$jose = Jose::getInstance();
$jose->getConfiguration()->set('algorithms', ['HS512']);
$jose->getConfiguration()->set('audience', 'My service');
$jose->getKeysetManager()->loadKeyFromValues('SHARED_KEY', ['alg' => 'HS512', 'use' => 'sig', 'kty' => 'oct', 'k' => 'GawgguFyGrWKav7AX4VKUg']);
$payload = ['iss' => 'My app', 'exp' => time() + 3600, 'iat' => time(), 'nbf' => time(), 'sub' => 'foo@bar', 'jti' => '0123456789', 'aud' => 'My service'];
$header = ['alg' => 'HS512'];
$jws = $jose->sign('SHARED_KEY', $payload, $header);
print_r(sprintf("\n\nJWS\n---------------------------------------------\n%s\n---------------------------------------------\n", $jws));
$loaded = $jose->load($jws);
print_r(sprintf("\n\nLoaded JWS\n    \n---------------------------------------------\n\n    JWT ID: %s\n    Key ID: %s\n    Subject: %s\n    Algorithm: %s\n    Audience: %s\n    Issuer: %s\n    Content type: %s\n    Expires at: %s\n    Issued at: %s\n    Not before: %s\n    Payload: %s\n    Type: %s\n    \n---------------------------------------------\n", $loaded->getJWTID(), $loaded->getKeyID(), $loaded->getSubject(), $loaded->getAlgorithm(), $loaded->getAudience(), $loaded->getIssuer(), $loaded->getContentType(), $loaded->getExpirationTime(), $loaded->getIssuedAt(), $loaded->getNotBefore(), json_encode($loaded->getPayload()), $loaded->getType()));
$jose->verify($loaded);
 /**
  * @return \Jose\LoaderInterface
  */
 public function getJWTLoader()
 {
     $jose = Jose::getInstance();
     return $jose->getLoader();
 }