Skip to content

Commit

Permalink
- Added sogo-auth.php / sogo-tokengenerate.php
Browse files Browse the repository at this point in the history
  • Loading branch information
xLixon committed Sep 10, 2024
1 parent 37beed6 commit 3030b77
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
60 changes: 60 additions & 0 deletions data/web/sogossologin/sogo-auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

session_start();
$session_var_user_allowed = 'sogo-sso-user-allowed';
$session_var_pass = 'sogo-sso-pass';


function checkTokenExists($pdo, $username, $token): bool
{
try {

$stmt = $pdo->prepare("SELECT * FROM `sogo_sso_tokens` WHERE `username` = :username AND `token` = :token");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':token', $token);

$stmt->execute();

$res = $stmt->fetchAll();
if(count($res) == 1){
return true;
}else{
return false;
}
} catch (PDOException $e) {
return false;
}
}






if(isset($_GET['email']) && $_GET['token']){
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
if(checkTokenExists($pdo, $_GET['email'], $_GET['token'])){
try {
$sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
$_SESSION[$session_var_user_allowed][] = $_GET['email'];
$_SESSION[$session_var_pass] = $sogo_sso_pass;
$stmt = $pdo->prepare("REPLACE INTO sasl_log (`service`, `app_password`, `username`, `real_rip`) VALUES ('SSO', 0, :username, :remote_addr)");
$stmt->execute(array(
':username' => $_GET['email'],
':remote_addr' => (isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : $_SERVER['REMOTE_ADDR'])
));
}catch (PDOException $e){
echo $e->getMessage();
}


header("Location: /SOGo/so/{$_GET['email']}");
}else{
http_response_code(401);
}
}

// if username is empty, SOGo will use the normal login methods / login form
header("X-User: ");
header("X-Auth: ");
header("X-Auth-Type: ");
76 changes: 76 additions & 0 deletions data/web/sogossologin/sogo-tokengenerate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
$_POST = json_decode(file_get_contents('php://input'), true);

function createIfTableDoesntExist($pdo, $debug = false)
{
try {
$stmt = $pdo->prepare("CREATE TABLE IF NOT EXISTS `sogo_sso_tokens` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`username` TEXT NOT NULL,
`token` TEXT NOT NULL
)");
$stmt->execute();
} catch (PDOException $e) {
if ($debug) echo $e->getMessage();
}
}

function showTables($pdo)
{
try {
$stmt2 = $pdo->query("SHOW TABLES");
$res = $stmt2->fetchAll(PDO::FETCH_ASSOC);
var_dump($res);
} catch (PDOException $e) {
echo $e->getMessage();
}
}

function writeTokenToDB($username, $token, $pdo): bool
{
try {
$stmt = $pdo->prepare("INSERT INTO `sogo_sso_tokens` (`username`, `token`) VALUES (:username, :token)");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':token', $token);
$success = $stmt->execute();
return $success;
} catch (PDOException $e) {
echo $e->getMessage();
return false;
}
}

function generateToken($username): string
{
return md5(base64_encode($username) . random_bytes(16) . md5(time()));
}

function getApiKey($pdo)
{
try {
$stmt = $pdo->prepare("SELECT `api_key` FROM `api` LIMIT 1");
$stmt->execute();
return $stmt->fetchColumn();

} catch (PDOException $e) {
return null;
}
}


if (isset($_POST['username']) && isset($_POST['apikey'])) {

if ($_POST['apikey'] == getApiKey($pdo)) {
$username = $_POST['username'];
$token = generateToken($username);
createIfTableDoesntExist($pdo);
writeTokenToDB($username, $token, $pdo);
echo json_encode(array(
"success" => true,
"username"=> $username,
"token" => $token
));
}
}

0 comments on commit 3030b77

Please sign in to comment.