While netdex.id handles authentication, this example is a template for securely keeping track of logged in users using an RSA token system.
This example is live at example.netdex.id.
Now, let's get it live on your server, as well!
You'll need a server with:
- webserver software that can run PHP (e.g. Apache2)
- a MySQL-compatible database
- Git
- Composer
- OpenSSL
First, clone this repository to your webserver, and configure your server software to block access to the "src" folder in this repository.
Example Apache2 Configuration
<VirtualHost *:80>
ServerName example.netdex.id
DocumentRoot "/var/www/example.netdex.id"
</VirtualHost>
<Directory /var/www/example.netdex.id/src>
Deny from all
</Directory>
Example Nginx Configuration
server {
listen 80;
listen [::]:80;
server_name example.netdex.id;
root /var/www/example.netdex.id;
location /src {
deny all;
return 404;
}
}
After reloading your server software, check that the website is live and the "src" folder is not accessible.
Now, open a terminal and cd
into the "src" folder.
Here, we would first like to run composer install
to install our trusty mysqli wrapper so we don't have to worry about SQL injection attacks.
And after our efforts to make the "src" folder inaccessible, we're going to use it to keep an RSA keypair:
openssl genrsa -out private.pem 4096
openssl rsa -in private.pem -out public.pem -pubout -outform PEM
chown www-data:www-data private.pem
This will be used to create and verify user tokens using cryptography instead of a database, which is perfect for simple apps like this one.
Finally, we will create a table to store our users' data in database called netdex_example
:
CREATE TABLE `netdex_example`.`users` ( `netdex_id` CHAR(16) NOT NULL, `display_name` VARCHAR(64) NOT NULL, `bio` tinytext NOT NULL, `text` tinytext NOT NULL DEFAULT '' ) ENGINE = InnoDB;
You can also give the database any name you want, but the table name users
is hard-coded. In any case, you may want to double-check that the include.php
in the "src" folder is correctly configured to use your database.
And with that your server is configured, except for the "netdex app id", which we will obtain by creating a new app at https://netdex.id/myapps with the callback set to POST netdex_callback.php
on our webserver.
But once you have the app id filled in, you should have your very own copy of this project live, and you can freely change it into your own website! :D