1
- <?php
2
-
3
- use CodeLts \U2F \U2FServer \Registration ;
4
-
5
- /**
6
- * @param string $location
7
- */
8
- function redirect ($ location )
9
- {
10
- header ("Location: $ location " );die ();
11
- }
12
-
13
- /**
14
- * @return string
15
- */
16
- function appID ()
17
- {
18
- $ scheme = isset ($ _SERVER ['HTTPS ' ]) ? "https:// " : "http:// " ;
19
- return $ scheme . $ _SERVER ['HTTP_HOST ' ];
20
- }
21
-
22
- /**
23
- * @return PDO $pdo
24
- */
25
- function getDBConnection ()
26
- {
27
- $ SQLiteFile = __DIR__ . '/database/database.sqlite ' ;
28
- $ pdo = new PDO ("sqlite: $ SQLiteFile " );
29
-
30
- $ pdo ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
31
- $ pdo ->setAttribute (PDO ::ATTR_DEFAULT_FETCH_MODE , PDO ::FETCH_OBJ );
32
-
33
- return $ pdo ;
34
- }
35
-
36
- function getUser ($ name )
37
- {
38
- $ pdo = getDBConnection ();
39
- $ statement = $ pdo ->prepare ("SELECT * FROM users WHERE NAME = ? " );
40
- $ statement ->execute ([$ name ]);
41
-
42
- return $ statement ->fetch ();
43
- }
44
-
45
- function getU2FRegistrations (stdClass $ user )
46
- {
47
- $ pdo = getDBConnection ();
48
- $ statement = $ pdo ->prepare ("SELECT * FROM registrations WHERE user_id = ? " );
49
- $ statement ->execute ([$ user ->id ]);
50
-
51
- return $ statement ->fetchAll ();
52
- }
53
-
54
- function storeU2FRegistration (stdClass $ user , Registration $ registration )
55
- {
56
- $ pdo = getDBConnection ();
57
- $ statement = $ pdo ->prepare ("
58
- INSERT INTO registrations
59
- (user_id, keyHandle, publicKey, certificate, counter)
60
- VALUES (?, ?, ?, ?, ?)
61
- " );
62
- $ statement ->execute ([
63
- $ user ->id ,
64
- $ registration ->getKeyHandle (),
65
- $ registration ->getPublicKey (),
66
- $ registration ->getCertificate (),
67
- $ registration ->getCounter ()
68
- ]);
69
-
70
- }
71
-
72
- function updateU2FRegistration (stdClass $ registration )
73
- {
74
- $ pdo = getDBConnection ();
75
- $ statement = $ pdo ->prepare ("UPDATE registrations SET counter = ? WHERE id = ? " );
76
- $ statement ->execute ([$ registration ->counter , $ registration ->id ]);
77
- }
1
+ <?php
2
+
3
+ declare (strict_types = 1 );
4
+
5
+ use CodeLts \U2F \U2FServer \Registration ;
6
+
7
+ function redirect (string $ location ): void
8
+ {
9
+ header ('Location: ' . $ location );
10
+ die ();
11
+ }
12
+
13
+ function appID (): string
14
+ {
15
+ $ scheme = isset ($ _SERVER ['HTTPS ' ]) ? 'https:// ' : 'http:// ' ;
16
+ return $ scheme . $ _SERVER ['HTTP_HOST ' ];
17
+ }
18
+
19
+ function getDBConnection (): PDO
20
+ {
21
+ $ SQLiteFile = __DIR__ . '/database/database.sqlite ' ;
22
+ $ pdo = new PDO ('sqlite: ' . $ SQLiteFile );
23
+ $ pdo ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
24
+ $ pdo ->setAttribute (PDO ::ATTR_DEFAULT_FETCH_MODE , PDO ::FETCH_OBJ );
25
+ return $ pdo ;
26
+ }
27
+
28
+ function getUser (string $ name )
29
+ {
30
+ $ pdo = getDBConnection ();
31
+ $ statement = $ pdo ->prepare ('SELECT * FROM users WHERE NAME = ? ' );
32
+ $ statement ->execute ([$ name ]);
33
+ return $ statement ->fetch ();
34
+ }
35
+
36
+ function getU2FRegistrations (stdClass $ user )
37
+ {
38
+ $ pdo = getDBConnection ();
39
+ $ statement = $ pdo ->prepare ('SELECT * FROM registrations WHERE user_id = ? ' );
40
+ $ statement ->execute ([$ user ->id ]);
41
+ return $ statement ->fetchAll ();
42
+ }
43
+
44
+ function storeU2FRegistration (stdClass $ user , Registration $ registration ): void
45
+ {
46
+ $ pdo = getDBConnection ();
47
+ $ statement = $ pdo ->prepare (
48
+ '
49
+ INSERT INTO registrations
50
+ (user_id, keyHandle, publicKey, certificate, counter)
51
+ VALUES (?, ?, ?, ?, ?)
52
+ '
53
+ );
54
+ $ statement ->execute (
55
+ [
56
+ $ user ->id ,
57
+ $ registration ->getKeyHandle (),
58
+ $ registration ->getPublicKey (),
59
+ $ registration ->getCertificate (),
60
+ $ registration ->getCounter ()
61
+ ]
62
+ );
63
+ }
64
+
65
+ function updateU2FRegistration (stdClass $ registration ): void
66
+ {
67
+ $ pdo = getDBConnection ();
68
+ $ statement = $ pdo ->prepare ('UPDATE registrations SET counter = ? WHERE id = ? ' );
69
+ $ statement ->execute ([$ registration ->counter , $ registration ->id ]);
70
+ }
0 commit comments