Completed
Push — master ( cc0a72...294b2e )
by Morgan
03:23
created

Github   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 7
c 4
b 0
f 3
lcom 1
cbo 1
dl 0
loc 73
ccs 14
cts 14
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isOrigin() 0 9 2
A isAuthentic() 0 11 2
A event() 0 4 1
A getRepoUrl() 0 4 1
A getCommitId() 0 4 1
1
<?php
2
3
namespace Morphatic\AutoDeploy\Origins;
4
5
class Github extends AbstractOrigin implements OriginInterface
6
{
7
    /**
8
     * The name of the origin.
9
     *
10
     * @var string
11
     */
12
    public $name = 'Github';
13
14
    /**
15
     * Determines whether or not the Request originated from Github.
16
     *
17
     * @return bool Returns true if the request originated from Github. False otherwise.
18
     */
19 18
    public function isOrigin()
20
    {
21
        // Correct IP range for Github maintained here:
22
        // https://help.github.com/articles/what-ip-addresses-does-github-use-that-i-should-whitelist/
23 18
        $hasGithubHeader = false !== strpos($this->request->header('User-Agent'), 'GitHub-Hookshot');
24 18
        $hasGithubIp = $this->isIpInRange($this->request->server('REMOTE_ADDR'), '192.30.252.0', 22);
25
26 18
        return $hasGithubHeader && $hasGithubIp;
27
    }
28
29
    /**
30
     * Verifies the authenticity of a webhook request from Github.
31
     *
32
     * Follows the procedure described here: https://developer.github.com/webhooks/securing/
33
     *
34
     * @return bool Returns true if the request is authentic. False otherwise.
1 ignored issue
show
Documentation introduced by
Should the return type not be null|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
35
     */
36 4
    public function isAuthentic()
37
    {
38
        // get the Github signature
39 4
        $xhub = $this->request->header('X-Hub-Signature') ?: 'nothing';
40
41
        // reconstruct the hash on this side
42 4
        $hash = 'sha1='.hash_hmac('sha1', $this->request->getContent(), config('auto-deploy.secret'));
43
44
        // securely compare them
45 4
        return hash_equals($xhub, $hash);
46
    }
47
48
    /**
49
     * Gets the event the triggered the webhook request.
50
     *
51
     * @return string The name of the event, e.g. push, release, create, etc.
52
     */
53 2
    public function event()
54
    {
55 2
        return $this->request->header('X-GitHub-Event');
56
    }
57
58
    /**
59
     * Gets the URL to be cloned from.
60
     *
61
     * @return string The URL of the repo.
62
     */
63 36
    public function getRepoUrl()
64
    {
65 36
        return $this->request->json('repository.clone_url');
66
    }
67
68
    /**
69
     * Gets the ID of the commit that is to be cloned.
70
     *
71
     * @return string The commit ID.
72
     */
73 36
    public function getCommitId()
74
    {
75 36
        return $this->request->json('after');
76
    }
77
}
78