Passed
Push — master ( 694922...b0526b )
by Antonio Carlos
02:27
created

QRCode::getQRCodeInline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 1
1
<?php
2
3
namespace PragmaRX\Google2FA\Support;
4
5
use PragmaRX\Google2FA\Exceptions\InsecureCallException;
6
7
trait QRCode
8
{
9
    /**
10
     * Sending your secret key to Google API is a security issue. Developer must explicitly allow it.
11
     */
12
    protected $allowInsecureCallToGoogleApis = false;
13
14
    /**
15
     * Creates a Google QR code url.
16
     *
17
     * @param string $company
18
     * @param string $holder
19
     * @param string $secret
20
     * @param int $size
21
     *
22
     * @throws \PragmaRX\Google2FA\Exceptions\InsecureCallException
23
     *
24
     * @return string
25
     */
26 2
    public function getQRCodeGoogleUrl($company, $holder, $secret, $size = 200)
27
    {
28 2
        if (!$this->allowInsecureCallToGoogleApis) {
29 1
            throw new InsecureCallException("It's not secure to send secret keys to Google Apis, you have to explicitly allow it by calling \$google2fa->setAllowInsecureCallToGoogleApis(true).");
30
        }
31
32 1
        return Url::generateGoogleQRCodeUrl(
33 1
            'https://chart.googleapis.com/',
34 1
            'chart',
35 1
            'chs='.$size.'x'.$size.'&chld=M|0&cht=qr&chl=',
36 1
            $this->getQRCodeUrl($company, $holder, $secret)
37
        );
38
    }
39
40
    /**
41
     * Creates a QR code url.
42
     *
43
     * @param $company
44
     * @param $holder
45
     * @param $secret
46
     *
47
     * @return string
48
     */
49 1
    public function getQRCodeUrl($company, $holder, $secret)
50
    {
51 1
        return 'otpauth://totp/'.rawurlencode($company).':'.rawurlencode($holder).'?secret='.$secret.'&issuer='.rawurlencode($company).'';
52
    }
53
54
    /**
55
     * AllowInsecureCallToGoogleApis setter.
56
     *
57
     * @param mixed $allowInsecureCallToGoogleApis
58
     *
59
     * @return QRCode
60
     */
61 1
    public function setAllowInsecureCallToGoogleApis($allowInsecureCallToGoogleApis)
62
    {
63 1
        $this->allowInsecureCallToGoogleApis = $allowInsecureCallToGoogleApis;
64
65 1
        return $this;
66
    }
67
}
68