Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
deb13:otp_authenticator_for_php [2026/08/30 21:24] Bernard Condraudeb13:otp_authenticator_for_php [2026/08/30 23:47] (current) Bernard Condrau
Line 1: Line 1:
 ====== OTP authenticator for php ====== ====== OTP authenticator for php ======
-  - Create a python virtual environment in a location your web server can access:<code>sudo mkdir -p /home/user/www/pyotp-env +  - Create a python virtual environment in a location your web server can access:<code>cd /home/user/html 
-sudo chown user:www-data /home/user/www/pyotp-env +mkdir -p /home/user/html/pyotp-env 
-sudo chmod 770 /home/user/www/pyotp-env+sudo chown user:www-data /home/user/html/pyotp-env 
 +sudo chmod 770 /home/user/html/pyotp-env
 </code> </code>
- +  - Create a virtual environment as apache2 user and activate it:<code>sudo -s -u www-data 
-  - Create the python script:<code># Install via terminal: pip install pyotp+python3 -m venv /home/user/html/pyotp-env 
 +source /home/user/html/pyotp-env/bin/activate</code> 
 +  - Install pyotp:<code>pip3 install pyotp</code> 
 +  - Create the python script ''/home/user/html/pyotp-env/otp.py'':<code># Python script to generate authenticator OTP
 import pyotp import pyotp
  
 # Your Base32 secret key (provided when setting up 2FA) # Your Base32 secret key (provided when setting up 2FA)
-secret = "JBSWY3DPEHPK3PXP+secret = "ABCDE1FGHIJK2LMN
  
 # Initialize TOTP # Initialize TOTP
Line 16: Line 20:
 # Generate the current 6-digit code # Generate the current 6-digit code
 current_code = totp.now() current_code = totp.now()
-print(f"Current 6-digit code: {current_code}")</code>+print(current_code)</code> 
 +  Check execution of pyotp:<code>python3 /home/user/html/pyotp-env/otp.py</code> 
 +  - Create a bash script:<code>vim /home/user/html/otp.sh 
 +#!/bin/bash 
 +source /home/user/html/pyotp-env/bin/activate 
 +python3 /home/user/html/pyotp-env/otp.py 
 +deactivate 
 +exit 0</code> 
 +  - Create a php web page which calls the script:<code>vim /home/user/html/otptest.php 
 +<?php 
 +$output = shell_exec('/home/bco/html/otp.sh')
 +echo $output;</code> 
 +  - Give the scripts correct ownership and permissions:<code>sudo chown www-data:www-data /home/bco/html/pyotp-env/otp.py 
 +sudo chown www-data:www-data /home/bco/html/otp.sh 
 +sudo chmod +x /home/bco/html/otp.sh</code> 
 +  - In Synology, you can check the OTP like this:<code>synootp username=user code=otp-code</code>