xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / tests / totp.inc.php (3fac13ef791613ae61a9cef4b6403383a0b6f68b) (2,255B) (mode 100644) [raw]
<?php
// Here are helpers for 2fa enrollment

/*
 * Do the http 2fa enrollment
 * Will return the key.
 */
function totp_enroll($db)
{
	global $test_url;

	rg_log_enter('test_enroll');

	$ret = array('ok' => 0);
	while (1) {
		rg_log('Loading enroll form...');
		$data = array(); $headers = array();
		$r = do_req($test_url . '/op/settings/totp/enroll', $data, $headers);
		if ($r === FALSE) {
			rg_log('Cannot load enroll page!');
			break;
		}
		$good_token = $r['tokens']['user_totp_enroll'];
		$key = isset($r['totp_secret']) ? $r['totp_secret'] : FALSE;
		if ($key === FALSE) {
			rg_log_ml('r: ' . print_r($r, TRUE));
			rg_log('Cannot find totp::secret!');
			break;
		}
		$ret['key'] = $key; // we may need it

		rg_log('Posting the enroll form...');
		$data = array(
			'enroll' => 1,
			'token' => $good_token,
			'totp::name' => 'test',
			'totp::secret' => $key,
			'totp::ver' => rg_totp_compute($key, time() / 30, 6)
			);
		$headers = array();
		$r = do_req($test_url . '/op/settings/totp/enroll', $data, $headers);
		if (!strstr($r['body'], 'You enrolled your new device with success')) {
			rg_log_ml('r: ' . print_r($r, TRUE));
			rg_log('Cannot enroll!');
			break;
		}
		$sql = "SELECT 1 FROM login_tokens WHERE secret = '" . $key . "'";
		$res = rg_sql_query($db, $sql);
		$rows = rg_sql_num_rows($res);
		rg_sql_free_result($res);
		if ($rows != 1) {
			rg_log('Secret not found in the database!');
			break;
		}

		$ret['ok'] = 1;
		break;
	}

	rg_log_exit();
	return $ret;
}

/*
 * Deletes scratch codes
 */
function totp_scratch_delete($list)
{
	global $test_url;

	rg_log_enter('totp_scratch_delete');

	$ret = array('ok' => 0);
	while (1) {
		$headers = array();
		$r = do_req($test_url . '/op/settings/totp/sc', $data, $headers);
		if ($r === FALSE) {
			rg_log('Cannot load sc page!');
			break;
		}
		$good_token = $r['tokens']['sc'];

		$data = array(
			'delete' => 1,
			'token' => $good_token
			);
		foreach ($list as $id)
			$data['delete_list[' . $id . ']'] = 'on';
		$headers = array();
		$r = do_req($test_url . '/op/settings/totp/sc', $data, $headers);
		if (!strstr($r['body'], 'success')) {
			rg_log('Cannot delete scratch codes!');
			break;
		}

		$ret['ok'] = 1;
		break;
	}

	rg_log_exit();
	return $ret;
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master