1
0
Fork 0

First, minimalistic version

This commit is contained in:
Gergely Polonkai (W00d5t0ck) 2011-09-29 18:50:21 +02:00
parent 244aaa00fe
commit cc5aa56d38
3 changed files with 153 additions and 66 deletions

View File

@ -9,11 +9,52 @@ interface gatewayBackend
* @param String $sessionId * @param String $sessionId
* @return String $token * @return String $token
*/ */
public function get_token($username, $password, $ip, $sessionId); public function getToken($username, $password, $ip, $sessionId);
/**
*
* @param String $token
* @param String $sessionId
* @param String $ip
* @return Boolean
*/
public function checkToken($token, $sessionId, $ip);
/**
* removeToken() Remove a logged out user's token
*
* @param String $token
*/
public function removeToken($token);
/** /**
* @param * sendSMS()
*
* Send SMS message to recipient's phone number
* @param String $token
* @param String $recipient
* @param String $message
* @param Array $passwordLocations
* @return Boolean
*/ */
public function public function sendSMS($token, $recipient, $message, $passwordLocations);
/**
* auditLog() Log audit messages
*
* @param String $ip
* @param String $event
* @param String $message
*/
public function auditLog($ip, $event, $message);
/**
* messageLog() Log sent messages
*
* @param String $recipient
* @param String $message
* @param String $ip
*/
public function messageLog($recipient, $message, $ip);
} }

View File

@ -1,7 +1,61 @@
<?php <?php
require_once 'gatewayBackend.php'; require_once 'gatewayBackend.php';
class postgresGatewayBackend implements gatewayBackend final class postgresGatewayBackend implements gatewayBackend
{ {
const GWBE_SUCCESS = 0;
const GWBE_DBERROR = 1;
const GWBE_AUTHFAIL = 2;
private $dbh = null;
public function __construct($dbHost, $dbUser, $dbPassword, $dbName)
{
$dsn = 'pgsql:host=' . $dbHost . ';dbname=' . $dbName;
$this->dbh = new PDO($dsn, $dbUser, $dbPassword);
}
public function getToken($username, $password, $ip, $sessionId)
{
$query = 'SELECT id, password FROM users WHERE username = :username:';
$sth = $this->dbh->prepare($query);
if ($sth->execute(array(':username:' => $username)))
{
/*
audit_log('Unsuccessful login by $username from $ip');
audit_log('Could not create token for $username at $ip');
return 'Authentication failed. Reason: Internal Server Error';
*/
}
else
{
throw new Exception('AuthFail', self::GWBE_DBERROR);
}
}
public function checkToken($token, $sessionId, $ip)
{
return null;
}
public function removeToken($token)
{
return null;
}
public function sendSMS($token, $recipient, $message, $passwordLocations)
{
return null;
}
public function auditLog($ip, $event, $message)
{
return null;
}
public function messageLog($recipient, $message, $ip)
{
return null;
}
} }

View File

@ -1,70 +1,62 @@
<?php <?php
class smsSender class smsSender
{ {
protected $sessionId = null; protected $sessionId = null;
protected $backend;
public function __construct($backend, $sessionId) public function __construct($backend, $sessionId)
{ {
$this->sessionId = $sessionId; $this->sessionId = $sessionId;
} $this->backend = $backend;
}
public function login($username, $password) public function login($username, $password)
{ {
/* try
if (valid_user($username, $password)) {
{ $token = $this->backend->getToken($username, $password, $_SERVER['REMOTE_ADDR'], $this->sessionId);
$token = generate_token($ip, $session_id, $token, $start_time); }
if ($token) catch (Exception $e)
{ {
audit_log('Successful login by $username from $ip'); throw new Exception('Authentication failed. Reason: ' . $e->getMessage());
return $token; }
} $this->backend->auditLog($_SERVER['REMOTE_ADDR'], 'login', 'Successful login by ' . $username);
else return $token;
{ }
audit_log('Could not create token for $username at $ip');
return 'Authentication failed. Reason: Internal Server Error';
}
}
else
{
audit_log('Unsuccessful login by $username from $ip');
return 'Authentication failed. Reason: Bad username or password';
}
*/
return array('username' => $username, 'password' => $password, 'session-id' => $this->sessionId);
}
public function send($token, $recipient, $message, $passwordLocations) public function send($token, $recipient, $message, $passwordLocations)
{ {
/* /*
if (valid_token($token) if (valid_token($token)
{ {
if (send_sms($recipient, $message)) if (send_sms($recipient, $message))
{ {
audit_log('Successful message sending by $token->username at $ip'); audit_log('Successful message sending by $token->username at $ip');
message_log('$message successfully sent to $recipient'); message_log('$message successfully sent to $recipient');
} }
else else
{ {
audit_log('Message sending failed for $token->username at $ip'); audit_log('Message sending failed for $token->username at $ip');
} }
} }
else else
{ {
audit_log('Message sending attempt from $ip with invalid token'); audit_log('Message sending attempt from $ip with invalid token');
throw new Exception('Authentication failed. Reason: Invalid Token'); throw new Exception('Authentication failed. Reason: Invalid Token');
} }
*/ */
return array('token' => $token, 'recipient' => $recipient, 'message' => $message, 'password-locations' => $passwordLocations, 'session-id' => $this->sessionId); /* TODO: implement */
} throw new Exception('This feature is not yet implemented');
}
public function logout($token) public function logout($token)
{ {
/* /*
delete_token($token); delete_token($token);
audit_log('$token->username logged out at $ip'); audit_log('$token->username logged out at $ip');
*/ return 'success';
return 'success'; */
} /* TODO: implement */
throw new Exception('This feature is not yet implemented');
}
} }