Skip to content

Commit

Permalink
Add generate functiones and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svenstm committed Feb 22, 2016
1 parent 90cb3f4 commit 09c2ce3
Show file tree
Hide file tree
Showing 13 changed files with 918 additions and 19 deletions.
15 changes: 15 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
coverage:
enabled: true
remote: false
include:
- lib/*
31 changes: 14 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
{
"name": "legalthings/authorizer",
"description": "Authorizer for HTTP requests",
"license": "MIT",
"require": {
"guzzle/guzzle": "v3.9.3"
},
"require-dev": {
"codeception/specify": "0.4.1",
"codeception/phpbuiltinserver": "v1.2.1",
"codeception/codeception": "2.0.13",
"aws/aws-sdk-php": "^3.12"
},
"autoload": {
"psr-4": {
"LegalThings\\": "src/"
}
}
"name" : "legalthings/authorizer",
"description" : "Authorizer for HTTP requests",
"license" : "MIT",
"require" : {
"guzzlehttp/guzzle": "^6.1"
},
"require-dev" : {
"codeception/codeception": "^2.1"
},
"autoload" : {
"psr-4" : {
"LegalThings\\" : "src/"
}
}
}
63 changes: 61 additions & 2 deletions src/Authorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ class Authorizer
* Time restrictions are unix timestamps, but may be omitted
* @return string $encryptedSecret An utf8_encoded encrypted secret
*/
public static function sign($allowedResource, $authzgen)
public static function sign($allowedResource, $authzgen, $handler = null)
{
if (!isset(self::$globalSecret)) trigger_error('$globalSecret is not set', E_USER_WARNING);

list($cerfiticateUrl, $timeStart, $timeEnd) = explode(';', $authzgen) + [null, null, null];

$publicKey = self::downloadSigningKey($cerfiticateUrl);
$publicKey = self::downloadSigningKey($cerfiticateUrl, ['handler' => $handler]);

$resourceSecret = join(';', [
$timeStart,
Expand Down Expand Up @@ -114,6 +114,10 @@ public static function getPublicKey()
if (!isset(self::$publicKeyPath)) {
throw new \RuntimeException('Path to the authorizer public key is not set');
}

if(!file_exists (self::$publicKeyPath)) {
throw new \RuntimeException('Path to public key does not exist');
}

$publicKey = file_get_contents(self::$publicKeyPath);
self::assertIsValidKey('public', $publicKey, self::$publicKeyPath);
Expand All @@ -131,12 +135,67 @@ protected static function getPrivateKey()
if (!isset(self::$privateKeyPath)) {
throw new \RuntimeException('Path to the authorizer private key is not set');
}

if(!file_exists (self::$privateKeyPath)) {
throw new \RuntimeException('Path to private key does not exist');
}

$privateKey = file_get_contents(self::$privateKeyPath);
self::assertIsValidKey('private', $privateKey, self::$privateKeyPath);

return $privateKey;
}

/**
* Generate a private key
*
* @param array $options
* @throws RuntimeException
* @return string
*/
public static function createPrivateKey($options = []) {

if (!isset(self::$privateKeyPath)) {
throw new \RuntimeException('Path to the authorizer private key is not set');
}

if(file_exists (self::$privateKeyPath)) {
throw new \RuntimeException('Private key already exists');
}

$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);

$config = $options + $config;

$res = openssl_pkey_new($config);
openssl_pkey_export($res, $key);
file_put_contents(self::$privateKeyPath, $key);
return $key;
}

/**
* Generate a public based on existing private keyu
* @throws \RuntimeException
* @return string
*/
public static function createPublicKey() {
if (!isset(self::$publicKeyPath)) {
throw new \RuntimeException('Path to the authorizer public key is not set');
}

if(file_exists (self::$publicKeyPath)) {
throw new \RuntimeException('Public key already exists');
}
$privateKey = openssl_get_privatekey(self::getPrivateKey());
$pubKey = openssl_pkey_get_details($privateKey);
$key = $pubKey["key"];
file_put_contents(self::$publicKeyPath, $key);
return $key;
}

/**
* Get a public key through a remote url
Expand Down
3 changes: 3 additions & 0 deletions tests/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';
28 changes: 28 additions & 0 deletions tests/_data/private_key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDE+bm7nLBNwkwg
Ftk7kiNpT9Y+dbAwzi27nwirYsf+Uqs6dKV5GY84/kYZ1rCYhAcPKIThBlOIATks
13Evy59Kjx8B2AYBmPECgG3+2oQTWZ6db6DzKYdZjEX72ZbtuF7DwmruPLdWF0Dg
d2FKPbUQgTABhBrYqPouqG3h/buFBNw3CtV7LUZ+XwAlSKn+fY4d9SYW9C/cLejC
UabqNK6/+DZOSbnSeeQHhuJn3YP9lS7TOCxV4SR9HMqhyhhKbtdbfxCeChWcpSAq
m6XvG4PtSsNgCfk7R/USrR6VjklLtRNdkcEhKzJ0DRDoFAYSJgLdkEHDLTSAUX28
aHXOdh/BAgMBAAECggEAPWy2q7fIDueZP6SAZ6BB5LRd1ipzIDUbc9e7J17tiPSU
quz3MRV0YUyB5craVSVO4j47ecS0RcRD5STmCgaQjPqmq7eG3/amqaYNJx1TdIlM
+kF2Sn3X1Gf25vvKoiUNgDRfjh1m+rTTqWvw7eriTNxMcRyWFpxTMDub0I6EbI0z
aBNMdbpoAtKVeVmO4Gvu+2pYOPU8jJfe72nzXEvUH7TRAgo1rTIV9w0+Fe/Ypegz
xolDaYpz8xyYZIVZoLdEIHotEXaFUGSJFiSI9FNKVKnRdmSVd0srGrjC4iXJobeM
92mR/dTW7tbu9twBkBVSlHT54zW2m2M+TlvscVmU8QKBgQD84fesaedp+IFc6s5S
dCPC7EAuTRxco3oAg9jcDR1JlI0f2JhB96R7PZDgKCXIT9aom3Et0yoACYA5dieY
81YaKfU5p8G24cO8urDEhPKESS3mU78wQDkRwrOM9lKFQ2BzKRaw02F9JZfUksC8
7YOU+vHaPVTT/8uW29idRuomLQKBgQDHZ1RQxoe3J3KVibs85bHmDwevAcE1cT3A
pROB8ehg9D2e74I/YZT/QMueOWFYLD/MuBvZFaAjUTHsotNivenxV/2Jzfc3BdCM
DS1rRLQVGWUGoffjRmIlMIkZojs62cfSVfG7mJJx9OJRMTMpudybCGQgsmGXpDzG
89/kMCNQZQKBgBqAQ8hZwNqmJEhsgvq5GRZQU9mGUzaSGzL6njtRVIRlgPSZajtf
jGkt5oTXMD5xv3kj8RRwULJ4TGu5KiUpOxi61OwZ3H8h4bVhiyzNEe0zDhBZLmwM
Du8BjHd1SOiWSwFGiQ422LyYt/Sa/H/QFD4EnlvUmZMt91Pz7httVxRxAoGAVqgY
I1Zz6muxRz1o6okj68gCXdLUZWZGIO9cb+9yPphGI0kRAHxm5RsCoktre4cXXt9v
8sKMKm0B1kXYuaOxBFpp67ZfxkC3pfWgFGryG+1CU0SWVuXihyj1m/c96FB2n3kM
KaufkPiMVh6cjnigX5ug7/i1otIUO54GYcNtxWUCgYEAvxdZ99L1uDStWqChXyKX
xvz5Qc2mEoAjySXU0av+wqrGIjo4NbZ0xyGMiJ51hKvf43kjXsUUCiSWX9jxBZu1
MHH4yAfMqnpDqWeewMiwWI9EROCezIoeaG2EC8A/CQeI1Hpiwts2iWU1hhOQGMqf
Js7JktyyXX39VQaiB+ed+4w=
-----END PRIVATE KEY-----
9 changes: 9 additions & 0 deletions tests/_data/public_key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxPm5u5ywTcJMIBbZO5Ij
aU/WPnWwMM4tu58Iq2LH/lKrOnSleRmPOP5GGdawmIQHDyiE4QZTiAE5LNdxL8uf
So8fAdgGAZjxAoBt/tqEE1menW+g8ymHWYxF+9mW7bhew8Jq7jy3VhdA4HdhSj21
EIEwAYQa2Kj6Lqht4f27hQTcNwrVey1Gfl8AJUip/n2OHfUmFvQv3C3owlGm6jSu
v/g2Tkm50nnkB4biZ92D/ZUu0zgsVeEkfRzKocoYSm7XW38QngoVnKUgKpul7xuD
7UrDYAn5O0f1Eq0elY5JS7UTXZHBISsydA0Q6BQGEiYC3ZBBwy00gFF9vGh1znYf
wQIDAQAB
-----END PUBLIC KEY-----
10 changes: 10 additions & 0 deletions tests/_support/UnitHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Codeception\Module;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class UnitHelper extends \Codeception\Module
{

}
26 changes: 26 additions & 0 deletions tests/_support/UnitTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null)
*
* @SuppressWarnings(PHPMD)
*/
class UnitTester extends \Codeception\Actor
{
use _generated\UnitTesterActions;

/**
* Define custom actions here
*/
}
Loading

0 comments on commit 09c2ce3

Please sign in to comment.