Skip to content

Commit 561673d

Browse files
committed
first commit
0 parents  commit 561673d

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor
2+
/.idea
3+
.DS_Store
4+
coverage.xml
5+
composer.lock

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "laravel-shield/github",
3+
"description": "A github service for shield",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Ashley Clarke",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"laravel-shield/shield": "^1.0"
14+
},
15+
"require-dev": {
16+
"laravel-shield/testing": "^1.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Shield\\GitHub\\": "src/"
21+
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"Shield\\GitHub\\Test\\": "tests/"
26+
}
27+
}
28+
}

phpunit.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
13+
<testsuite name="Unit Tests">
14+
<directory suffix="Test.php">./tests/Unit</directory>
15+
</testsuite>
16+
17+
</testsuites>
18+
<filter>
19+
<whitelist processUncoveredFilesFromWhitelist="true">
20+
<directory suffix=".php">./src</directory>
21+
</whitelist>
22+
</filter>
23+
<php>
24+
<env name="APP_ENV" value="testing"/>
25+
<env name="CACHE_DRIVER" value="array"/>
26+
<env name="SESSION_DRIVER" value="array"/>
27+
<env name="QUEUE_DRIVER" value="sync"/>
28+
</php>
29+
<logging>
30+
<log type="coverage-clover" target="coverage.xml" showUncoveredFiles="true"/>
31+
</logging>
32+
</phpunit>

src/GitHub.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Shield\GitHub;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Collection;
7+
use Shield\Shield\Contracts\Service;
8+
9+
/**
10+
* Class GitHub
11+
*
12+
* @package \Shield\GitHub
13+
*/
14+
class GitHub implements Service
15+
{
16+
public function verify(Request $request, Collection $config): bool
17+
{
18+
$generated = 'sha1=' . hash_hmac('sha1', $request->getContent(), $config->get('token'));
19+
20+
return hash_equals($generated, $request->header('X-Hub-Signature'));
21+
}
22+
23+
public function headers(): array
24+
{
25+
return ['X-Hub-Signature'];
26+
}
27+
}

tests/Unit/ServiceTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Shield\GitHub\Test\Unit;
4+
5+
use PHPUnit\Framework\Assert;
6+
use Shield\GitHub\GitHub;
7+
use Shield\Shield\Contracts\Service;
8+
use Shield\Testing\TestCase;
9+
10+
/**
11+
* Class ServiceTest
12+
*
13+
* @package \Shield\GitHub\Test
14+
*/
15+
class ServiceTest extends TestCase
16+
{
17+
/**
18+
* @var \Shield\GitHub\GitHub
19+
*/
20+
private $service;
21+
22+
public function setUp()
23+
{
24+
parent::setUp();
25+
26+
$this->service = new GitHub;
27+
}
28+
29+
/** @test */
30+
public function it_is_a_service()
31+
{
32+
Assert::assertInstanceOf(Service::class, new GitHub);
33+
}
34+
35+
/** @test */
36+
public function it_can_verify_a_valid_request()
37+
{
38+
$token = 'raNd0mk3y';
39+
40+
$this->app['config']['shield.services.github.options.token'] = $token;
41+
42+
$content = 'sample content';
43+
44+
$request = $this->request($content);
45+
46+
$headers = [
47+
'X-Hub-Signature' => 'sha1=' . hash_hmac('sha1', $content, $token)
48+
];
49+
50+
$request->headers->add($headers);
51+
52+
Assert::assertTrue($this->service->verify($request, collect($this->app['config']['shield.services.github.options'])));
53+
}
54+
55+
/** @test */
56+
public function it_will_not_verify_a_bad_request()
57+
{
58+
$this->app['config']['shield.services.github.token'] = 'good';
59+
60+
$content = 'sample content';
61+
62+
$request = $this->request($content);
63+
64+
$headers = [
65+
'X-Hub-Signature' => 'sha1=' . hash_hmac('sha1', $content, 'bad')
66+
];
67+
68+
$request->headers->add($headers);
69+
70+
Assert::assertFalse($this->service->verify($request, collect($this->app['config']['shield.services.github.options'])));
71+
}
72+
73+
/** @test */
74+
public function it_has_correct_headers_required()
75+
{
76+
Assert::assertArraySubset(['X-Hub-Signature'], $this->service->headers());
77+
}
78+
}

0 commit comments

Comments
 (0)