ServiceAuth::getOption()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is a part of nekland youtube api package
5
 *
6
 * (c) Nekland <[email protected]>
7
 *
8
 * For the full license, take a look to the LICENSE file
9
 * on the root directory of this project
10
 */
11
12
namespace Nekland\YoutubeApi\Http\Auth;
13
14
15
use Guzzle\Http\Client;
16
use Namshi\JOSE\JWS;
17
use Nekland\BaseApi\Http\Auth\AuthInterface;
18
use Nekland\YoutubeApi\Exception\AuthException;
19
use Nekland\YoutubeApi\Exception\MissingOptionException;
20
21
/**
22
 * Class ServiceAuth
23
 *
24
 * Options:
25
 *      email: google api service account
26
 *      cert_file: path to the file that contain the certificate
27
 *      cert_password: password to decrypt the (p12) certificate
28
 */
29
class ServiceAuth implements AuthInterface
30
{
31
    /**
32
     * @var array
33
     */
34
    protected $options;
35
36
    /**
37
     * @var \Guzzle\Http\ClientInterface
38
     */
39
    protected $client;
40
41
    /**
42
     * @param \Guzzle\Http\ClientInterface $client
43
     */
44
    public function __construct(\Guzzle\Http\ClientInterface $client = null)
45
    {
46
        if (null === $client) {
47
            $this->client = new Client();
48
        } else {
49
            $this->client = $client;
50
        }
51
    }
52
53
    /**
54
     * @param \Guzzle\Http\Message\Request $request
55
     */
56
    public function auth(\Guzzle\Http\Message\Request $request)
57
    {
58
        if (!$request->hasHeader('Authorization')) {
59
            $token = $this->getToken();
60
            $request->addHeader('Authorization', 'Bearer '.$token);
61
        }
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    private function getToken()
68
    {
69
        $jws = new JWS('RS256');
70
        $jws->setPayload(array(
71
            'iss'   => $this->getIss(),
72
            'scope' => 'https://www.googleapis.com/auth/youtube',
73
            'aud'   =>'https://www.googleapis.com/oauth2/v4/token',
74
            'exp'   => time() + 60,
75
            'iat'   => time()
76
        ));
77
78
        $jws->sign($this->getPrivateKey());
0 ignored issues
show
Documentation introduced by
$this->getPrivateKey() is of type string, but the function expects a resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
80
        $response = $this->client->post(
81
            'https://www.googleapis.com/oauth2/v4/token',
82
            ['Content-Type' => 'application/x-www-form-urlencoded'],
83
            [
84
                'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
85
                'assertion'  => $jws->getTokenString()
86
            ],
87
            ['debug' => true]
88
        );
89
90
        $finalArray = json_decode((string) $response->send()->getBody(), true);
91
92
        return $finalArray['access_token'];
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    protected function getIss()
99
    {
100
        return $this->getOption('email');
101
    }
102
103
    /**
104
     * @return string
105
     * @throws \Nekland\YoutubeApi\Exception\AuthException
106
     */
107
    protected function getPrivateKey()
108
    {
109
        $file            = $this->getOption('cert_file');
110
        $res             = array();
111
        $brutCertificate = file_get_contents($file);
112
113
        $worked = openssl_pkcs12_read($brutCertificate, $res, $this->getOption('cert_password', 'notasecret'));
114
115
        if ($worked) {
116
            return $res['pkey'];
117
        }
118
119
        throw new AuthException(sprintf('The key to open the p12 "%" file looks wrong, or the file is malformed.', $file));
120
    }
121
122
    /**
123
     * @param string $option
124
     * @param mixed  $default
125
     * @return mixed
126
     * @throws \Nekland\YoutubeApi\Exception\MissingOptionException
127
     */
128
    protected function getOption($option, $default=null)
129
    {
130
        if (isset($this->options[$option])) {
131
            return $this->options[$option];
132
        }
133
134
        if (null !== $default) {
135
            return $default;
136
        }
137
138
        throw new MissingOptionException(sprintf('The option "%s" is missing on the configuration of "%".', $option, get_class($this)));
139
    }
140
141
    /**
142
     * @param array $options
143
     * @return AuthInterface
144
     */
145
    public function setOptions(array $options)
146
    {
147
        $this->options = $options;
148
149
        return $this;
150
    }
151
}
152