Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit 9d9a859

Browse files
committed
Apply most of the wdes/coding-standard
1 parent 04e6cbb commit 9d9a859

19 files changed

+696
-693
lines changed

PDO-Without-AJAX/authenticate.php

+34-43
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,34 @@
1-
<?php
2-
3-
require("../vendor/autoload.php");
4-
require("functions.php");
5-
6-
use CodeLts\U2F\U2FServer\U2FServer as U2F;
7-
session_start();
8-
9-
// Authenticate user
10-
$user = getUser($_POST['username']);
11-
if(!$user){
12-
$_SESSION['error'] = "No user with that name in database.";
13-
redirect("index.php");
14-
}
15-
16-
// Get any U2F registrations associated with the user
17-
$registrations = getU2FRegistrations($user);
18-
19-
// If we have registrations this means we need to authenticate via U2F
20-
if(!empty($registrations)){
21-
22-
// Call the U2F makeAuthentication method, passing in the user's registration(s) and the app ID
23-
$authenticationRequest = U2F::makeAuthentication($registrations, appID());
24-
25-
// Store the request for later
26-
$_SESSION['authenticationRequest'] = $authenticationRequest;
27-
28-
// Store the user attempting to authenticate
29-
$_SESSION['authenticatingUser'] = $user;
30-
31-
// now pass the data to the U2F authentication view.
32-
$templates = new League\Plates\Engine(__DIR__.'/views');
33-
echo $templates->render('u2f-authentication', ['authenticationRequest' => json_encode($authenticationRequest)]);
34-
35-
}
36-
37-
// If we don't have U2F registrations this means we can proceed to dashboard
38-
else{
39-
40-
$_SESSION['authenticatedUser'] = $user;
41-
redirect("dashboard.php");
42-
43-
}
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
require('../vendor/autoload.php');
6+
require('functions.php');
7+
use CodeLts\U2F\U2FServer\U2FServer as U2F;
8+
9+
session_start();
10+
// Authenticate user
11+
$user = getUser($_POST['username']);
12+
if (!$user) {
13+
$_SESSION['error'] = 'No user with that name in database.';
14+
redirect('index.php');
15+
}
16+
17+
// Get any U2F registrations associated with the user
18+
$registrations = getU2FRegistrations($user);
19+
// If we have registrations this means we need to authenticate via U2F
20+
if (!empty($registrations)) {
21+
// Call the U2F makeAuthentication method, passing in the user's registration(s) and the app ID
22+
$authenticationRequest = U2F::makeAuthentication($registrations, appID());
23+
// Store the request for later
24+
$_SESSION['authenticationRequest'] = $authenticationRequest;
25+
// Store the user attempting to authenticate
26+
$_SESSION['authenticatingUser'] = $user;
27+
// now pass the data to the U2F authentication view.
28+
$templates = new League\Plates\Engine(__DIR__ . '/views');
29+
echo $templates->render('u2f-authentication', ['authenticationRequest' => json_encode($authenticationRequest)]);
30+
} else {
31+
// If we don't have U2F registrations this means we can proceed to dashboard
32+
$_SESSION['authenticatedUser'] = $user;
33+
redirect('dashboard.php');
34+
}

PDO-Without-AJAX/dashboard.php

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
<?php
1+
<?php
2+
3+
declare(strict_types = 1);
24

3-
require("../vendor/autoload.php");
4-
require("functions.php");
5-
6-
session_start();
7-
8-
if(isset($_SESSION['authenticatedUser'])){
9-
$user = $_SESSION['authenticatedUser'];
10-
}
11-
else{
12-
$_SESSION['error'] = "You are not logged in.";
13-
redirect('index.php');die;
14-
}
15-
16-
// Get any U2F registrations associated with the user
17-
$registrations = getU2FRegistrations($user);
18-
19-
$templates = new League\Plates\Engine(__DIR__.'/views');
20-
echo $templates->render('dashboard', ['user' => $user, 'registrations' => $registrations]);
5+
require('../vendor/autoload.php');
6+
require('functions.php');
7+
session_start();
8+
if (isset($_SESSION['authenticatedUser'])) {
9+
$user = $_SESSION['authenticatedUser'];
10+
} else {
11+
$_SESSION['error'] = 'You are not logged in.';
12+
redirect('index.php');
13+
die;
14+
}
15+
16+
// Get any U2F registrations associated with the user
17+
$registrations = getU2FRegistrations($user);
18+
$templates = new League\Plates\Engine(__DIR__ . '/views');
19+
echo $templates->render('dashboard', ['user' => $user, 'registrations' => $registrations]);
+24-26
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
<?php
1+
<?php
2+
3+
declare(strict_types = 1);
24

3-
require_once('../functions.php');
4-
5-
$pdo = getDBConnection();
6-
7-
// Create users table
8-
$pdo->exec(
9-
"create table if not exists users (
10-
id integer primary key,
11-
name varchar(255)
12-
)"
13-
);
14-
15-
// Create registrations table
16-
$pdo->exec(
17-
"create table if not exists registrations (
18-
id integer primary key,
19-
user_id integer,
20-
keyHandle varchar(255),
21-
publicKey varchar(255),
22-
certificate text,
23-
counter integer
24-
)"
25-
);
26-
27-
echo "Database migration completed.<br/>";
5+
require_once('../functions.php');
6+
$pdo = getDBConnection();
7+
// Create users table
8+
$pdo->exec(
9+
'create table if not exists users (
10+
id integer primary key,
11+
name varchar(255)
12+
)'
13+
);
14+
// Create registrations table
15+
$pdo->exec(
16+
'create table if not exists registrations (
17+
id integer primary key,
18+
user_id integer,
19+
keyHandle varchar(255),
20+
publicKey varchar(255),
21+
certificate text,
22+
counter integer
23+
)'
24+
);
25+
echo 'Database migration completed.<br/>';

PDO-Without-AJAX/database/reset.php

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
<?php
2-
3-
require_once("../functions.php");
4-
5-
$pdo = getDBConnection();
6-
7-
$pdo->exec("DROP TABLE IF EXISTS 'users'");
8-
$pdo->exec("DROP TABLE IF EXISTS 'registrations'");
9-
echo "Tables dropped.<br/>";
10-
11-
require("migrations.php");
12-
require("seeds.php");
13-
14-
echo "Database has been reset<br/>";
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
require_once('../functions.php');
6+
$pdo = getDBConnection();
7+
$pdo->exec("DROP TABLE IF EXISTS 'users'");
8+
$pdo->exec("DROP TABLE IF EXISTS 'registrations'");
9+
echo 'Tables dropped.<br/>';
10+
require('migrations.php');
11+
require('seeds.php');
12+
echo 'Database has been reset<br/>';

PDO-Without-AJAX/database/seeds.php

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
<?php
2-
3-
require_once("../functions.php");
4-
5-
$pdo = getDBConnection();
6-
7-
// Insert User seeds
8-
$pdo->exec("
9-
INSERT INTO `users` VALUES (1,'williamdes');
10-
INSERT INTO `users` VALUES (2,'donkey');
11-
INSERT INTO `users` VALUES (3,'shrek');
12-
");
13-
14-
echo "Your database is seeded.<br/>";
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
require_once('../functions.php');
6+
$pdo = getDBConnection();
7+
8+
// Insert User seeds
9+
$pdo->exec(
10+
'
11+
INSERT INTO `users` VALUES (1,\'williamdes\');
12+
INSERT INTO `users` VALUES (2,\'donkey\');
13+
INSERT INTO `users` VALUES (3,\'shrek\');
14+
'
15+
);
16+
echo 'Your database is seeded.<br/>';

PDO-Without-AJAX/functions.php

+70-77
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,70 @@
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+
}

PDO-Without-AJAX/index.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<?php
1+
<?php
2+
3+
declare(strict_types = 1);
24

3-
require("../vendor/autoload.php");
4-
5-
$templates = new League\Plates\Engine(__DIR__.'/views');
6-
echo $templates->render('index');
5+
require('../vendor/autoload.php');
6+
$templates = new League\Plates\Engine(__DIR__ . '/views');
7+
echo $templates->render('index');

0 commit comments

Comments
 (0)