Passed
Push — master ( 850a47...2dcc32 )
by Antonio Carlos
03:25
created

QRCode::getQRCodeUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace PragmaRX\Google2FA\Support;
4
5
use BaconQrCode\Renderer\Image\Png;
6
use BaconQrCode\Writer as BaconQrCodeWriter;
7
use PragmaRX\Google2FA\Exceptions\InsecureCallException;
8
9
trait QRCode
10
{
11
    /**
12
     * Sending your secret key to Google API is a security issue. Developer must explicitly allow it.
13
     */
14
    protected $allowInsecureCallToGoogleApis = false;
15
16
    /**
17
     * Creates a Google QR code url.
18
     *
19
     * @param string $company
20
     * @param string $holder
21
     * @param string $secret
22
     * @param int    $size
23
     *
24
     * @throws InsecureCallException
25
     *
26
     * @return string
27
     */
28 2
    public function getQRCodeGoogleUrl($company, $holder, $secret, $size = 200)
29
    {
30 2
        if (!$this->allowInsecureCallToGoogleApis) {
31 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).');
32
        }
33
34 1
        $url = $this->getQRCodeUrl($company, $holder, $secret);
35
36 1
        return Url::generateGoogleQRCodeUrl('https://chart.googleapis.com/', 'chart', 'chs='.$size.'x'.$size.'&chld=M|0&cht=qr&chl=', $url);
37
    }
38
39
    /**
40
     * Generates a QR code data url to display inline.
41
     *
42
     * @param string $company
43
     * @param string $holder
44
     * @param string $secret
45
     * @param int    $size
46
     * @param string $encoding Default to UTF-8
47
     *
48
     * @return string
49
     */
50 1
    public function getQRCodeInline($company, $holder, $secret, $size = 200, $encoding = 'utf-8')
51
    {
52 1
        $url = $this->getQRCodeUrl($company, $holder, $secret);
53
54 1
        $renderer = new Png();
55 1
        $renderer->setWidth($size);
56 1
        $renderer->setHeight($size);
57
58 1
        $bacon = new BaconQrCodeWriter($renderer);
59 1
        $data = $bacon->writeString($url, $encoding);
60
61 1
        return 'data:image/png;base64,'.base64_encode($data);
62
    }
63
64
    /**
65
     * Creates a QR code url.
66
     *
67
     * @param $company
68
     * @param $holder
69
     * @param $secret
70
     *
71
     * @return string
72
     */
73 2
    public function getQRCodeUrl($company, $holder, $secret)
74
    {
75 2
        return 'otpauth://totp/'.rawurlencode($company).':'.rawurlencode($holder).'?secret='.$secret.'&issuer='.rawurlencode($company).'';
76 1
    }
77
78
    /**
79
     * AllowInsecureCallToGoogleApis setter.
80
     *
81
     * @param mixed $allowInsecureCallToGoogleApis
82
     *
83
     * @return QRCode
84
     */
85 1
    public function setAllowInsecureCallToGoogleApis($allowInsecureCallToGoogleApis)
86
    {
87 1
        $this->allowInsecureCallToGoogleApis = $allowInsecureCallToGoogleApis;
88
89 1
        return $this;
90
    }
91
}
92