diff --git a/gatewayBackend.php b/gatewayBackend.php index 8fa4b86..a74b319 100644 --- a/gatewayBackend.php +++ b/gatewayBackend.php @@ -9,11 +9,52 @@ interface gatewayBackend * @param String $sessionId * @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); } diff --git a/postgresGatewayBackend.php b/postgresGatewayBackend.php index 04749e8..3e4ae05 100644 --- a/postgresGatewayBackend.php +++ b/postgresGatewayBackend.php @@ -1,7 +1,61 @@ 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; + } } diff --git a/smsSender.php b/smsSender.php index d18d4a8..95cfeae 100644 --- a/smsSender.php +++ b/smsSender.php @@ -1,70 +1,62 @@ sessionId = $sessionId; - } + public function __construct($backend, $sessionId) + { + $this->sessionId = $sessionId; + $this->backend = $backend; + } - public function login($username, $password) - { - /* - if (valid_user($username, $password)) - { - $token = generate_token($ip, $session_id, $token, $start_time); - if ($token) - { - audit_log('Successful login by $username from $ip'); - return $token; - } - else - { - 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 login($username, $password) + { + try + { + $token = $this->backend->getToken($username, $password, $_SERVER['REMOTE_ADDR'], $this->sessionId); + } + catch (Exception $e) + { + throw new Exception('Authentication failed. Reason: ' . $e->getMessage()); + } + $this->backend->auditLog($_SERVER['REMOTE_ADDR'], 'login', 'Successful login by ' . $username); + return $token; + } - public function send($token, $recipient, $message, $passwordLocations) - { - /* - if (valid_token($token) - { - if (send_sms($recipient, $message)) - { - audit_log('Successful message sending by $token->username at $ip'); - message_log('$message successfully sent to $recipient'); - } - else - { - audit_log('Message sending failed for $token->username at $ip'); - } - } - else - { - audit_log('Message sending attempt from $ip with 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); - } + public function send($token, $recipient, $message, $passwordLocations) + { + /* + if (valid_token($token) + { + if (send_sms($recipient, $message)) + { + audit_log('Successful message sending by $token->username at $ip'); + message_log('$message successfully sent to $recipient'); + } + else + { + audit_log('Message sending failed for $token->username at $ip'); + } + } + else + { + audit_log('Message sending attempt from $ip with invalid token'); + throw new Exception('Authentication failed. Reason: Invalid Token'); + } + */ + /* TODO: implement */ + throw new Exception('This feature is not yet implemented'); + } - public function logout($token) - { - /* - delete_token($token); - audit_log('$token->username logged out at $ip'); - */ - return 'success'; - } + public function logout($token) + { + /* + delete_token($token); + audit_log('$token->username logged out at $ip'); + return 'success'; + */ + /* TODO: implement */ + throw new Exception('This feature is not yet implemented'); + } } -