1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Google2FA\Support; |
4
|
|
|
|
5
|
|
|
use BaconQrCode\Renderer\Image\Png; |
6
|
|
|
use BaconQrCode\Writer as BaconQrCodeWriter; |
7
|
|
|
use Exception; |
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
|
1 |
|
* @param string $holder |
21
|
|
|
* @param string $secret |
22
|
1 |
|
* @param int $size |
23
|
|
|
* @return string |
24
|
1 |
|
* @throws Exception |
25
|
|
|
*/ |
26
|
|
|
public function getQRCodeGoogleUrl($company, $holder, $secret, $size = 200) |
27
|
|
|
{ |
28
|
|
|
if (! $this->allowInsecureCallToGoogleApis) { |
29
|
|
|
throw new Exception('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
|
|
|
$url = $this->getQRCodeUrl($company, $holder, $secret); |
33
|
|
|
|
34
|
|
|
return Url::generateGoogleQRCodeUrl('https://chart.googleapis.com/', 'chart', 'chs='.$size.'x'.$size.'&chld=M|0&cht=qr&chl=', $url); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
1 |
|
* Generates a QR code data url to display inline. |
39
|
|
|
* |
40
|
1 |
|
* @param string $company |
41
|
|
|
* @param string $holder |
42
|
1 |
|
* @param string $secret |
43
|
1 |
|
* @param int $size |
44
|
1 |
|
* @param string $encoding Default to UTF-8 |
45
|
|
|
* |
46
|
1 |
|
* @return string |
47
|
1 |
|
*/ |
48
|
|
|
public function getQRCodeInline($company, $holder, $secret, $size = 200, $encoding = 'utf-8') |
49
|
1 |
|
{ |
50
|
|
|
$url = $this->getQRCodeUrl($company, $holder, $secret); |
51
|
|
|
|
52
|
|
|
$renderer = new Png(); |
53
|
|
|
$renderer->setWidth($size); |
54
|
|
|
$renderer->setHeight($size); |
55
|
|
|
|
56
|
|
|
$bacon = new BaconQrCodeWriter($renderer); |
57
|
|
|
$data = $bacon->writeString($url, $encoding); |
58
|
|
|
|
59
|
|
|
return 'data:image/png;base64,'.base64_encode($data); |
60
|
|
|
} |
61
|
2 |
|
|
62
|
|
|
/** |
63
|
2 |
|
* Creates a QR code url. |
64
|
|
|
* |
65
|
|
|
* @param $company |
66
|
|
|
* @param $holder |
67
|
|
|
* @param $secret |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getQRCodeUrl($company, $holder, $secret) |
72
|
|
|
{ |
73
|
|
|
return 'otpauth://totp/'.rawurlencode($company).':'.rawurlencode($holder).'?secret='.$secret.'&issuer='.rawurlencode($company).''; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* AllowInsecureCallToGoogleApis setter. |
78
|
|
|
* |
79
|
|
|
* @param mixed $allowInsecureCallToGoogleApis |
80
|
|
|
* @return QRCode |
81
|
|
|
*/ |
82
|
|
|
public function setAllowInsecureCallToGoogleApis($allowInsecureCallToGoogleApis) |
83
|
|
|
{ |
84
|
|
|
$this->allowInsecureCallToGoogleApis = $allowInsecureCallToGoogleApis; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|