예제 #1
0
 public function testMakeOrderJwtWithExistingApplication()
 {
     $order = Order::create($expectedPackageId = 123);
     $order->setAppId($expectedApplicationId = 456)->setPrice($expectedPrice = 100)->setTrialDuration($expectedTrialDuration = 3)->setTrialDurationUnit($expectedTrialDurationUnit = Order::DURATION_UNIT_MONTH)->setFirstBillingDate($expectedFirstBillingDate = new \DateTime('now +1 day'))->setBillingDayOfMonth($expectedBillingDayOfMonth = 15)->setDescription($expectedDescription = 'description');
     $client = new MarketClient($encoder = new Encoder(), new PrivateKeyFileProvider('file://' . __DIR__ . '/key.pem'), $expectedIssuer = 'zYua50VjHMoK443jTsKaentBe15U6z4B', $expectedTarget = 'https://market.appsco.com/checkout');
     $response = $client->makeOrder($order);
     $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $response);
     $url = $response->getTargetUrl();
     $this->assertStringStartsWith($expectedTarget, $url);
     $jwt = null;
     $arr = parse_url($url);
     $query = $arr['query'];
     parse_str($query);
     $this->assertNotEmpty($jwt);
     /** @var Order $jwt */
     $jwt = $encoder->decode($jwt, 'Appsco\\Market\\ApiBundle\\Model\\Order');
     $this->assertNotEmpty($jwt->getJwtId());
     $this->assertEquals($expectedIssuer, $jwt->getIssuer());
     $this->assertGreaterThanOrEqual(0, time() - $jwt->getIssuedAt());
     $this->assertInstanceOf('Appsco\\Market\\ApiBundle\\Model\\Order', $jwt);
     $this->assertEquals($expectedPackageId, $jwt->getPackageId());
     $this->assertEquals($expectedApplicationId, $order->getAppId());
     $this->assertEquals($expectedPrice, $order->getPrice());
     $this->assertEquals($expectedTrialDuration, $order->getTrialDuration());
     $this->assertEquals($expectedTrialDurationUnit, $order->getTrialDurationUnit());
     $this->assertEquals($expectedFirstBillingDate->setTime(0, 0, 0), $order->getFirstBillingDate());
     $this->assertEquals($expectedBillingDayOfMonth, $order->getBillingDayOfMonth());
     $this->assertEquals($expectedDescription, $order->getDescription());
     $encoder->verify($jwt, 'file://' . __DIR__ . '/key.crt');
 }
예제 #2
0
 public function testEncodeDecode01()
 {
     $jwt = new Jwt();
     $jwt->setIssuedAt($expectedIssuedAt = time())->setIssuer($expectedIssuer = 'BWC')->setSubject($expectedSubject = 'subject')->setAudience($expectedAudience = 'audience')->setExpirationTime($expectedExpirationTime = $expectedIssuedAt + 120)->setNotBefore($expectedNotBefore = $expectedIssuedAt - 120)->setJwtId($expectedId = mt_rand(10000, 999999))->set('email', $expectedEmail = '*****@*****.**');
     $encoder = new Encoder();
     $key = 's466j5424G1eLsSBT45I2p94t';
     $token = $encoder->encode($jwt, $key);
     $jwt = $encoder->decode($token, null, $key);
     $this->assertEquals($expectedIssuedAt, $jwt->getIssuedAt());
     $this->assertEquals($expectedIssuer, $jwt->getIssuer());
     $this->assertEquals($expectedSubject, $jwt->getSubject());
     $this->assertEquals($expectedAudience, $jwt->getAudience());
     $this->assertEquals($expectedExpirationTime, $jwt->getExpirationTime());
     $this->assertEquals($expectedNotBefore, $jwt->getNotBefore());
     $this->assertEquals($expectedId, $jwt->getJwtId());
     $this->assertEquals($expectedEmail, $jwt->get('email'));
 }
 /**
  * @param Notification $notification
  * @throws InvalidNotificationException
  * @return void
  */
 public function validate(Notification $notification)
 {
     $certificateList = $this->client->certificateGet($notification->getIssuer());
     if (0 == count($certificateList->getCertificates())) {
         throw new InvalidNotificationException(sprintf("Issuer '%s' has no Appsco certificates", $notification->getIssuer()));
     }
     $error = null;
     foreach ($certificateList->getCertificates() as $certificate) {
         try {
             $this->jwtEncoder->verify($notification, $certificate->getCertificate());
             $error = null;
             break;
         } catch (\Exception $ex) {
             $error = $ex;
         }
     }
     if ($error) {
         throw new InvalidNotificationException(sprintf("Unable to verify certificate of issuer '%s'", $notification->getIssuer()), 0, $error);
     }
 }
예제 #4
0
 /**
  * @test
  * @expectedException \BWC\Component\Jwe\JweException
  * @expectedExceptionMessage Invalid signature
  */
 public function shouldThrowIfInvalidSignatureOnDecodeAndVerify()
 {
     $key = 'secret_key-alt';
     $token = 'eyJoMSI6MSwiaDIiOjIsInR5cCI6IkpXVCIsImFsZyI6IkhTMjU2In0.eyJwbDEiOjEsInBsMiI6Mn0.RASIJrI-LX98YBIQDw3g9sXnNPrCdVBr02YyO1Z4i0o';
     $expectedHeader = array('h1' => 1, 'h2' => 2);
     $expectedPayload = array('pl1' => 1, 'pl2' => 2);
     $encoder = new Encoder();
     $jose = $encoder->decode($token, null, $key);
     $this->assertArrayHasKey('h1', $jose->getHeader());
     $this->assertArrayHasKey('h2', $jose->getHeader());
     $this->assertEquals($expectedHeader['h1'], $jose->getHeader()['h1']);
     $this->assertEquals($expectedHeader['h2'], $jose->getHeader()['h2']);
     $this->assertEquals($expectedPayload, $jose->getPayload());
 }
예제 #5
0
 /**
  * @param string $jwtToken
  * @return \BWC\Component\Jwe\Jose
  */
 public function receiveNotification($jwtToken)
 {
     $notification = $this->encoder->decode($jwtToken, 'Appsco\\Market\\ApiBundle\\Model\\Notification');
     return $notification;
 }