Skip to content

Commit b10dc23

Browse files
committed
28 Jan 2024
Added QR code login
1 parent db4f965 commit b10dc23

File tree

12 files changed

+210
-20
lines changed

12 files changed

+210
-20
lines changed

assets/PAGE-check.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ var check = {
6464

6565
// (C) "SWITCH ON" QR SCANNER
6666
qron : () => {
67-
if (qrscan.scanner==null) {
68-
qrscan.init(check.hSKU, check.hBatch, check.pre);
69-
}
67+
qrscan.init(txt => {
68+
try {
69+
let item = JSON.parse(txt);
70+
check.hSKU.value = item.S;
71+
check.hBatch.value = item.B;
72+
check.pre();
73+
} catch (e) {
74+
console.error(e);
75+
cb.modal("Invalid QR Code", "Failed to parse scanned QR code.");
76+
}
77+
});
7078
qrscan.show();
7179
},
7280

assets/PAGE-login.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ var nin = {
103103
}
104104
};
105105

106-
// (IV) INIT
106+
// (IV) QR LOGIN
107+
var qr = {
108+
// (A) INITIALIZE QR SCANNER
109+
go : () => {
110+
if (qrscan.scanner==null) {
111+
qrscan.init(token => cb.api({
112+
mod : "session", act : "qrlogin",
113+
data : { token : token },
114+
passmsg : false,
115+
onpass : () => location.href = cbhost.base
116+
}));
117+
}
118+
qrscan.show();
119+
}
120+
};
121+
122+
// (V) INIT
107123
window.addEventListener("load", wa.init);
108124
window.addEventListener("load", nin.init);

assets/PAGE-move.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,17 @@ var move = {
7373
// (C) "SWITCH ON" QR SCANNER
7474
qron : () => {
7575
if (qrscan.scanner==null) {
76-
qrscan.init(move.hSKU, move.hBatch, () => {
77-
if (move.hForm.checkValidity()) { move.save(); }
78-
else { move.hForm.reportValidity(); }
76+
qrscan.init(txt => {
77+
try {
78+
let item = JSON.parse(txt);
79+
move.hSKU.value = item.S;
80+
move.hBatch.value = item.B;
81+
if (move.hForm.checkValidity()) { move.save(); }
82+
else { move.hForm.reportValidity(); }
83+
} catch (e) {
84+
console.error(e);
85+
cb.modal("Invalid QR Code", "Failed to parse scanned QR code.");
86+
}
7987
});
8088
}
8189
qrscan.show();

assets/PAGE-qrscan.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var qrscan = {
33
scanner : null, // scanner object
44

55
// (B) INITIALIZE
6-
init : (hSKU, hBatch, after) => {
6+
init : after => {
77
// (B1) ATTACH HTML
88
let hScan = document.createElement("div");
99
hScan.id = "qr-wrapA";
@@ -21,15 +21,7 @@ var qrscan = {
2121
qrscan.scanner = new Html5QrcodeScanner("qr-cam", { fps: 10, qrbox: 250 });
2222
qrscan.scanner.render((txt, res) => {
2323
qrscan.hide();
24-
try {
25-
let item = JSON.parse(txt);
26-
hSKU.value = item.S;
27-
hBatch.value = item.B;
28-
after();
29-
} catch (e) {
30-
console.error(e);
31-
cb.modal("Invalid QR Code", "Failed to parse scanned QR code.");
32-
}
24+
after(txt);
3325
});
3426
},
3527

assets/PAGE-users.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,26 @@ var usr = {
146146
cb.page(1);
147147
},
148148

149-
// (K) IMPORT USERS
149+
// (K) SHOW WRITE QR PAGE
150+
hqNull : null, // html null token button
151+
qrShow : id => cb.load({
152+
page : "users/qr", target : "cb-page-2",
153+
data : { id : id },
154+
onload : () => {
155+
usr.hqNull = document.getElementById("qr-null");
156+
cb.page(2);
157+
}
158+
}),
159+
160+
// (L) NULLIFY QR TOKEN
161+
qrNull : id => cb.api({
162+
mod : "session", act : "qrdel",
163+
data : { id : id },
164+
passmsg : "Login token nullified.",
165+
onpass : res => usr.hqNull.disabled = true
166+
}),
167+
168+
// (M) IMPORT USERS
150169
import : () => im.init({
151170
name : "USERS",
152171
at : 2, back : 1,
@@ -160,6 +179,7 @@ var usr = {
160179
]
161180
})
162181
};
182+
163183
window.addEventListener("load", () => {
164184
usr.list();
165185
autocomplete.attach({

lib/API-session.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
// (A4) NFC LOGIN
1717
"nfcadd" => ["NFCIN", "add", "A"],
1818
"nfcdel" => ["NFCIN", "del", "A"],
19-
"nfclogin" => ["NFCIN", "login"]
19+
"nfclogin" => ["NFCIN", "login"],
20+
// (A5) QR LOGIN
21+
"qradd" => ["QRIN", "add", "A"],
22+
"qrdel" => ["QRIN", "del", "A"],
23+
"qrlogin" => ["QRIN", "login"]
2024
]);
2125

2226
// (B) INVALID REQUEST

lib/LIB-QRIN.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
class QRIN extends Core {
3+
// (A) INIT
4+
private $nlen = 12; // 12 characters qr login random hash
5+
function __construct ($core) {
6+
parent::__construct($core);
7+
$core->load("Users");
8+
}
9+
10+
// (B) CREATE NEW NFC LOGIN TOKEN
11+
// $id : user id
12+
function add ($id) {
13+
// (B1) UPDATE TOKEN
14+
$token = $this->Core->random($this->nlen);
15+
$this->Users->hashAdd($id, "QR", password_hash($token, PASSWORD_DEFAULT));
16+
17+
// (B2) RETURN ENCODED TOKEN
18+
require PATH_LIB . "JWT/autoload.php";
19+
return Firebase\JWT\JWT::encode([$id, $token], JWT_SECRET, JWT_ALGO);
20+
}
21+
22+
// (C) NULLIFY QR TOKEN
23+
// $id : user id
24+
function del ($id) {
25+
$this->Users->hashDel($id, "QR");
26+
return true;
27+
}
28+
29+
// (D) NFC TOKEN LOGIN
30+
function login ($token) {
31+
// (D1) DECODE TOKEN
32+
$valid = true;
33+
try {
34+
require PATH_LIB . "JWT/autoload.php";
35+
$token = Firebase\JWT\JWT::decode(
36+
$token, new Firebase\JWT\Key(JWT_SECRET, JWT_ALGO)
37+
);
38+
$valid = is_object($token);
39+
if ($valid) {
40+
$token = (array) $token;
41+
$valid = count($token)==2;
42+
}
43+
} catch (Exception $e) { $valid = false; }
44+
45+
// (D2) VERIFY TOKEN
46+
if ($valid) {
47+
$user = $this->Users->get($token[0], "QR");
48+
$valid = (is_array($user) && password_verify($token[1], $user["hash_code"]));
49+
}
50+
51+
// (D3) SESSION START
52+
if ($valid) {
53+
$_SESSION["user"] = $user;
54+
unset($_SESSION["user"]["user_password"]);
55+
unset($_SESSION["user"]["hash_code"]);
56+
unset($_SESSION["user"]["hash_time"]);
57+
unset($_SESSION["user"]["hash_tries"]);
58+
$this->Session->save();
59+
return true;
60+
}
61+
62+
// (D4) NADA
63+
$this->error = "Invalid token";
64+
return false;
65+
}
66+
}

lib/LIB-Users.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Users extends Core {
1010
// (A) SETTINGS
1111
private $hvalid = 900; // validation link good for 15 mins
12-
private $hlen = 12; // 24 characters validation hash
12+
private $hlen = 12; // 12 characters validation hash
1313

1414
// (B) PASSWORD CHECKER (HELPER)
1515
// $password : password to check

pages/PAGE-login.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
$_PMETA = ["load" => [
77
["s", HOST_ASSETS."PAGE-nfc.js", "defer"],
88
["s", HOST_ASSETS."PAGE-wa-helper.js", "defer"],
9+
["s", HOST_ASSETS."html5-qrcode.min.js", "defer"],
10+
["s", HOST_ASSETS."PAGE-qrscan.js", "defer"],
911
["s", HOST_ASSETS."PAGE-login.js", "defer"],
12+
["c", HOST_ASSETS."PAGE-qrscan.css"],
1013
["c", HOST_ASSETS."PAGE-login.css"]
1114
]];
1215

@@ -53,6 +56,10 @@
5356
<button type="button" id="nfc-a" onclick="nin.go()" disabled class="my-1 btn btn-primary d-flex-inline">
5457
<i class="ico-sm icon-feed"></i> <span id="nfc-b">NFC</span>
5558
</button>
59+
<!-- (LOGIN WITH QR) -->
60+
<button type="button" id="qr-in" onclick="qr.go()" class="my-1 btn btn-primary d-flex-inline">
61+
<i class="ico-sm icon-qrcode"></i> QR
62+
</button>
5663
</form>
5764

5865
<!-- (C2-3) SOCIAL LOGIN -->

pages/PAGE-users-list.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<li class="dropdown-item" onclick="usr.addEdit(<?=$id?>)">
1717
<i class="text-secondary ico-sm icon-pencil"></i> Edit
1818
</li>
19+
<li class="dropdown-item" onclick="usr.qrShow(<?=$id?>)">
20+
<i class="text-secondary ico-sm icon-qrcode"></i> QR Login
21+
</li>
1922
<li class="dropdown-item" onclick="usr.nfcShow(<?=$id?>)">
2023
<i class="text-secondary ico-sm icon-feed"></i> NFC Login
2124
</li>

0 commit comments

Comments
 (0)