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 / http_totp.php (9ca337f8d5e07ce0f6624c4375cd015d62abe4d9) (1,777B) (mode 100644) [raw]
<?php
//
// Will test the login by TOTP
//

error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/util.inc.php");
require_once("helpers.inc.php");
require_once("http.inc.php");

rg_log_set_file("http_totp.log");

$rg_sql = "host=localhost user=rocketgit dbname=rocketgit connect_timeout=10";
$rg_no_db = TRUE;
require_once("common.php");

$_testns = 'http_totp';
$rg_cache_enable = TRUE;

$rg_user_max_len = 60;

rg_test_create_user($db, $rg_ui);

// Add an totp token to this account
$key = 'ACHCBCCVQ7AK4RGM';

$r = rg_totp_enroll($db, $rg_ui['uid'], 'test', $key, '127.0.0.1', TRUE);
if ($r !== TRUE) {
	rg_log('cannot enroll!');
	exit(1);
}

// Now test the login without and with login_token
$lt = rg_totp_compute($key, time() / 30, 6);

// First we need to load the form so we can get the token
// We provide an old cookie to test if we generate a new pre-login one
$r = do_req($test_url . "/op/login", $data, $headers);
if ($r === FALSE) {
	rg_log("Cannot load login form.");
	exit(1);
}
$good_sid = $r['sid'];
$good_token = $r['tokens']['login'];


rg_log("Do the login (sid=$good_sid token=$good_token"
	. " login_token=$lt)...");
$data = array(
	"doit" => 1,
	"token" => $good_token,
	"user" => $rg_ui['username'],
	"pass" => $rg_ui['pass'],
	"login_token" => $lt,
	"lock_ip" => 0);
$headers = array("Cookie: sid=" . $good_sid);
$r = do_req($test_url . "/op/login", $data, $headers);
if ($r === FALSE) {
	rg_log_ml("Cannot login: " . print_r($r, TRUE));
	exit(1);
}
if (strstr($r['body'], "invalid user")) {
	rg_log_ml("Login invalid. r=" . print_r($r, TRUE));
	exit(1);
}

rg_prof_log();
rg_log("OK!");
?>
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