No description
Find a file
2024-12-17 19:33:36 +01:00
.gitignore Rename config.json to config.template 2024-12-17 19:30:26 +01:00
.htaccess added htaccess 2024-03-13 14:31:01 +01:00
base32.php Add TTN credential encoding function 2024-12-17 19:33:36 +01:00
config.template Add e-mail settings to config.template 2024-12-17 19:33:36 +01:00
index.php Remove trailing whitespace from all files 2024-12-17 19:33:36 +01:00
README.md Add initial README file 2024-12-17 19:33:36 +01:00
template.sql Fix typo in template.sql 2024-12-17 19:33:36 +01:00
ttn.php Add TTN credential encoding function 2024-12-17 19:33:36 +01:00
users.php Remove trailing whitespace from all files 2024-12-17 19:33:36 +01:00

MJSAPI

Dependencies

  • Apache2 with PHP, htaccess and mod_rewrite. Another webserver with PHP support can work, but then the redirects in .htaccess might need to be ported to it.
  • PHP module: sqlite3

On Debian, this can be achieved with;

apt install apache2  libapache2-mod-php php-sqlite3
sudo a2enmod rewrite
sudo tee /etc/apache2/conf-enabled/local-allow-htaccess-rewrite.conf <<EOF
<Directory /var/www/>
  AllowOverride FileInfo AuthConfig Limit Indexes
</Directory>
EOF
sudo systemctl restart apache2

Setup

  1. Clone git repository inside the Apache documentroot and make the directory writable by PHP. E.g.:

    sudo mkdir /var/www/html/mjsapi
    sudo chown "$USER" /var/www/html/mjsapi
    git clone https://src.giplt.nl/harmen/mjsapi.git /var/www/html/mjsapi
    sudo chown "www-data" /var/www/html/mjsapi
    

    This first creates the directory writable to your current user (so you do not need to run git as sudo), and then changes the directory ownership to allow PHP to create the database.

  2. Copy config.template to config.json and fill in config values:

    • systemEmailName and systemEmailAddress: Used as the sender of outgoing e-mails.
    • sqliteUserDatabase: Path to the database file. It will be created and filled when it does not exist yet.
    • JWT_KEY: This must be a random value with at least 256 bits (matching the HS256 algorithm used) of randomness (32 bytes). It can be a raw binary value, ASCII string or hex string. For example, generate with: openssl rand -hex 32.

Register user manually

  1. Register account:

    API=http://localhost/mjsapi
    curl -i -d '{"email": "email@example.org", "password1": "foo", "password2": "foo", "display_name": "My Name"}' -X POST "$API/users/register"
    
  2. Click link in confirmation e-mail.

  3. Login:

    curl -i -d '{"email": "email@example.org", "password": "foo"}' -X POST "$API/users/authenticate"
    
  4. Extract the JWT from the response and use that to authenticate further requests (e.g.):

    JWT=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZGlzcGxheV9uYW1lIjoiTXkgTmFtZSIsImVtYWlsIjoibWF0dGhpanNAc3RkaW4ubmwiLCJhZG1pbiI6MSwiaWF0IjoxNzM0NDU0MTA2fQ==.MTU5ODQ1NmQ2MGFjMTFkNTEwY2EwODU5YmRlNzhhZmVkZTM4ZTg1YzMwMjRlNDFjMjlkNGJkOWNiNDU3MTMxMw==
    
    curl -i -H "Authorization: JWT $JWT" -X GET "$API/users"