Skip to content
This repository has been archived by the owner on May 24, 2018. It is now read-only.

Added missing commands #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Plugin for PocketMine-MP that prevents people to impersonate an account, requeri

* `/login <password>`
* `/register <password>`
* `/unregister <password>` (TODO)
* For OPs: `/simpleauth <command: help|unregister> [parameters...]` (TODO)
* `/unregister <password>`
* For OPs: `/simpleauth <command: help|unregister> [parameters ...]`


## Configuration
Expand Down Expand Up @@ -50,6 +50,7 @@ You can modify the _SimpleAuth/config.yml_ file on the _plugins_ directory once
| simpleauth.lastip | true | Allows authenticating using the lastIP when enabled in the config |
| simpleauth.command.register | true | Allows registering an account |
| simpleauth.command.login | true | Allows logging into an account |
| simpleauth.command.simpleauth | op | Manage accounts |

## For developers

Expand Down
11 changes: 11 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ commands:
description: "Registers an account"
usage: "/register <password>"
permission: simpleauth.command.register
unregister:
description: "Unregister an account"
usage: "/unregister <password>"
permission: simpleauth.command.register
simpleauth:
description: "Manage accounts"
usage: "/simpleauth help|unregister [parameters ...]"
permission: simpleauth.command.simpleauth

permissions:
simpleauth:
Expand All @@ -41,3 +49,6 @@ permissions:
simpleauth.command.login:
description: "Allows logging into an account"
default: true
simpleauth.command.simpleauth:
description: "manage simpleauth accounts"
default: op
12 changes: 11 additions & 1 deletion resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ login:
error:
password: "Incorrect password!"
registered: "This account is not registered."
block: "Too many tries!"
block: "Too many tries!"
ingame: "This command only works in-game."

simpleauth:
unregistered: "%s unregistered"
notregistered: "You are no longer registered!"
help: "Use unregister to unregister players"
error:
unregister: "Unable to unregister %s"
usage1: "/simpleauth unregister player"
notfound: "Player %s not found"
88 changes: 84 additions & 4 deletions src/SimpleAuth/SimpleAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,21 @@ public function getDataProvider(){
return $this->provider;
}

/**
* @api
*
* @param IPlayer $player
* @param string $password
*
* @return bool
*/
public function checkPassword(IPlayer $player, $password){
if(($data = $this->provider->getPlayer($player)) === null){
return false;
}
return hash_equals($data["hash"], $this->hash(strtolower($player->getName()), $password));
}

/* -------------------------- Non-API part -------------------------- */

public function closePlayer(Player $player){
Expand Down Expand Up @@ -252,7 +267,7 @@ public function onCommand(CommandSender $sender, Command $command, $label, array

$password = implode(" ", $args);

if(hash_equals($data["hash"], $this->hash(strtolower($sender->getName()), $password)) and $this->authenticatePlayer($sender)){
if($this->checkPassword($sender, $password) and $this->authenticatePlayer($sender)){
return true;
}else{
$this->tryAuthenticatePlayer($sender);
Expand All @@ -261,7 +276,7 @@ public function onCommand(CommandSender $sender, Command $command, $label, array
return true;
}
}else{
$sender->sendMessage(TextFormat::RED . "This command only works in-game.");
$sender->sendMessage(TextFormat::RED . $this->getMessage("login.error.ingame"));

return true;
}
Expand All @@ -287,13 +302,78 @@ public function onCommand(CommandSender $sender, Command $command, $label, array
return true;
}
}else{
$sender->sendMessage(TextFormat::RED . "This command only works in-game.");
$sender->sendMessage(TextFormat::RED . $this->getMessage("login.error.ingame"));

return true;
}
break;
}
case "unregister":
if($sender instanceof Player){
if(!$this->isPlayerRegistered($sender) or ($data = $this->provider->getPlayer($sender)) === null){
$sender->sendMessage(TextFormat::RED . $this->getMessage("login.error.registered"));

return true;
}
if(count($args) !== 1){
$sender->sendMessage(TextFormat::RED . "Usage: " . $command->getUsage());

return true;
}
$password = implode(" ", $args);

if($this->checkPassword($sender, $password)){
if ($this->deauthenticatePlayer($sender) and $this->unregisterPlayer($sender)){
$sender->sendMessage(TextFormat::RED . $this->getMessage("join.register"));
}else{
$sender->sendMessage(TextFormat::RED . $this->getMessage("register.error.general"));
}
return true;
}else{
$sender->sendMessage(TextFormat::RED . $this->getMessage("login.error.password"));

return true;
}
}else{
$sender->sendMessage(TextFormat::RED . $this->getMessage("login.error.ingame"));

return true;
}
break;
case "simpleauth":
if(count($args) < 1){
return false;
}
$subcmd = strtolower(array_shift($args));
switch($subcmd){
case "unregister":
if(count($args) < 1){
$sender->sendMessage(TextFormat::RED . $this->getMessage("simpleauth.error.usage1"));
return true;
}
foreach($args as $name){
$player = $this->getServer()->getOfflinePlayer($name);
if(!$this->isPlayerRegistered($player)){
$sender->sendMessage(TextFormat::RED. sprintf($this->getMessage("simpleauth.error.notfound"), $name));
continue;
}
if($this->unregisterPlayer($player)){
$sender->sendMessage(TextFormat::GREEN . sprintf($this->getMessage("simpleauth.unregistered"), $name));
if($player instanceof Player){
$player->sendMessage(TextFormat::YELLOW.$this->getMessage("simpleauth.notregistered"));
$this->deauthenticatePlayer($player);
}
}else{
$sender->sendMessage(TextFormat::RED . sprintf($this->getMessage("simpleauth.error.unregister"), $name));
}
}
return true;
break;
case "help":
$sender->sendMessage(TextFormat::GREEN . $this->getMessage("simpleauth.help"));
return true;
}
break;
}
return false;
}

Expand Down