-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d0aa1e3
Showing
707 changed files
with
96,750 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php namespace pineapple; | ||
|
||
require_once('DatabaseConnection.php'); | ||
|
||
class APITokens extends Module | ||
{ | ||
private $dbConnection; | ||
|
||
const DATABASE = "/etc/pineapple/pineapple.db"; | ||
|
||
public function __construct($request) | ||
{ | ||
parent::__construct($request, __CLASS__); | ||
$this->dbConnection = new DatabaseConnection(self::DATABASE); | ||
$this->dbConnection->exec("CREATE TABLE IF NOT EXISTS api_tokens (token VARCHAR NOT NULL, name VARCHAR NOT NULL);"); | ||
} | ||
|
||
public function getApiTokens() | ||
{ | ||
$this->response = array("tokens" => $this->dbConnection->query("SELECT ROWID, token, name FROM api_tokens;")); | ||
} | ||
|
||
public function checkApiToken() | ||
{ | ||
if (isset($this->request->token)) { | ||
$token = $this->request->token; | ||
$result = $this->dbConnection->query("SELECT token FROM api_tokens WHERE token='%s';", $token); | ||
if (!empty($result) && isset($result[0]["token"]) && $result[0]["token"] === $token) { | ||
$this->response = array("valid" => true); | ||
} | ||
} | ||
$this->response = array("valid" => false); | ||
} | ||
|
||
public function addApiToken() | ||
{ | ||
if (isset($this->request->name)) { | ||
$token = hash('sha512', openssl_random_pseudo_bytes(32)); | ||
$name = $this->request->name; | ||
$this->dbConnection->exec("INSERT INTO api_tokens(token, name) VALUES('%s','%s');", $token, $name); | ||
$this->response = array("success" => true, "token" => $token, "name" => $name); | ||
} else { | ||
$this->error = "Missing token name"; | ||
} | ||
} | ||
|
||
public function revokeApiToken() | ||
{ | ||
if (isset($this->request->id)) { | ||
$this->dbConnection->exec("DELETE FROM api_tokens WHERE ROWID='%s'", $this->request->id); | ||
} elseif (isset($this->request->token)) { | ||
$this->dbConnection->exec("DELETE FROM api_tokens WHERE token='%s'", $this->request->token); | ||
} elseif (isset($this->request->name)) { | ||
$this->dbConnection->exec("DELETE FROM api_tokens WHERE name='%s'", $this->request->name); | ||
} else { | ||
$this->error = "The revokeApiToken API call requires either a 'id', 'token', or 'name' parameter"; | ||
} | ||
} | ||
|
||
public function route() | ||
{ | ||
switch ($this->request->action) { | ||
case 'checkApiToken': | ||
$this->checkApiToken(); | ||
break; | ||
|
||
case 'addApiToken': | ||
$this->addApiToken(); | ||
break; | ||
|
||
case 'getApiTokens': | ||
$this->getApiTokens(); | ||
break; | ||
|
||
case 'revokeApiToken': | ||
$this->revokeApiToken(); | ||
break; | ||
|
||
default: | ||
$this->error = "Unknown action"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
registerController("APITokenController", ['$api', '$scope', function($api, $scope) { | ||
$scope.apiTokens = []; | ||
$scope.newToken = { | ||
name: "", | ||
token: "" | ||
}; | ||
|
||
$scope.getApiTokens = function(){ | ||
$api.request({ | ||
'module': 'APITokens', | ||
'action': 'getApiTokens' | ||
}, function(response){ | ||
$scope.apiTokens = response.tokens; | ||
}); | ||
}; | ||
|
||
$scope.genApiToken = function(){ | ||
$api.request({ | ||
'module': 'APITokens', | ||
'action': 'addApiToken', | ||
'name': $scope.newToken.name | ||
}, function(response){ | ||
$scope.newToken.name = ""; | ||
$scope.newToken.token = response.token; | ||
$scope.getApiTokens(); | ||
}); | ||
}; | ||
|
||
$scope.revokeApiToken = function($event){ | ||
var id = $event.target.getAttribute('tokenid'); | ||
$api.request({ | ||
'module': 'APITokens', | ||
'action': 'revokeApiToken', | ||
'id': id | ||
}, function(){ | ||
$scope.getApiTokens(); | ||
}); | ||
}; | ||
|
||
$scope.selectElem = function(elem){ | ||
var selectRange = document.createRange(); | ||
selectRange.selectNodeContents(elem); | ||
var selection = window.getSelection(); | ||
selection.removeAllRanges(); | ||
selection.addRange(selectRange); | ||
} | ||
|
||
$scope.selectOnClick = function($event){ | ||
var elem = $event.target; | ||
$scope.selectElem(elem); | ||
}; | ||
|
||
$scope.getApiTokens(); | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<style type="text/css"> | ||
.panel { | ||
width: 90%; | ||
float:right; | ||
margin-right: 5%; | ||
} | ||
.table { | ||
table-layout:fixed; | ||
} | ||
.table td { | ||
white-space: nowrap; | ||
overflow: scroll; | ||
text-overflow: ellipsis | ||
} | ||
</style> | ||
<div class="row" ng-controller="APITokenController"> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading"> | ||
<h3 class="panel-title"> | ||
Manage API Tokens | ||
<span class="pull-right"><button class="btn btn-primary" style="padding: 0px 5px;" ng-click="getApiTokens();">Refresh</button></span> | ||
</h3> | ||
</div> | ||
<table class="table table-hover table-responsive table-condensed" ng-show="apiTokens.length"> | ||
<thead> | ||
<th>ID</th> | ||
<th>Name</th> | ||
<th>Token</th> | ||
</thead> | ||
<tbody> | ||
<tr ng-repeat="apiToken in apiTokens"> | ||
<td class="col-md-1">{{ apiToken.rowid }}</td> | ||
<td class="col-md-3">{{ apiToken.name }}</td> | ||
<td class="col-md-5 token" ng-click="selectOnClick($event);">{{ apiToken.token }}</td> | ||
<td class="col-md-3"><span class="pull-right"><button tokenid="{{ apiToken.rowid }}" class="btn btn-danger btn-sm" ng-click="revokeApiToken($event);">Revoke</button></span></td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
|
||
<div class="panel-body" ng-hide="apiTokens.length"> | ||
<center><i>No API Tokens</i></center> | ||
</div> | ||
|
||
</div> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading"> | ||
<h3 class="panel-title"> | ||
Generate New Token | ||
</h3> | ||
</div> | ||
<div class="panel-body"> | ||
<form class="form-inline" role="form" ng-submit="genApiToken()" novalidate> | ||
<div class="form-group"> | ||
<label for="tokenName">Token Name:</label> | ||
<input name="tokenName" type="text" class="form-control" id="tokenName" ng-model="newToken.name" autofocus> | ||
</div> | ||
<button type="submit" class="btn btn-success">Generate</button> | ||
</form> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"author": "Tesla", | ||
"description": "Create and delete API tokens on the WiFi Pineapple", | ||
"devices": [ | ||
"nano", | ||
"tetra" | ||
], | ||
"title": "APITokens", | ||
"version": "1.2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php namespace pineapple; | ||
|
||
class Cabinet extends Module | ||
{ | ||
|
||
public function route() | ||
{ | ||
switch($this->request->action) { | ||
case 'getDirectoryContents': | ||
$this->getDirectoryContents(); | ||
break; | ||
|
||
case 'getParentDirectory': | ||
$this->getParentDirectory(); | ||
break; | ||
|
||
case 'deleteFile': | ||
$this->deleteFile(); | ||
break; | ||
|
||
case 'editFile': | ||
$this->editFile(); | ||
break; | ||
|
||
case 'getFileContents': | ||
$this->getFileContents(); | ||
break; | ||
|
||
case 'createFolder': | ||
$this->createFolder(); | ||
break; | ||
} | ||
} | ||
|
||
protected function getDirectoryContents() | ||
{ | ||
$dir = $this->request->directory; | ||
|
||
$success = false; | ||
$contents = array(); | ||
if (file_exists($dir)) { | ||
foreach (preg_grep('/^([^.])/', scandir($dir)) as $file) { | ||
$obj = array("name" => $file, "directory" => is_dir($dir . '/' . $file), | ||
"path" => realpath($dir . '/' . $file), | ||
"permissions" => substr(sprintf('%o', fileperms($dir . '/' . $file)), -4), | ||
"size" => filesize($dir . '/' . $file)); | ||
array_push($contents, $obj); | ||
} | ||
$success = true; | ||
} | ||
|
||
$this->response = array("success" => $success, "contents" => $contents, "directory" => $dir); | ||
|
||
} | ||
|
||
protected function getParentDirectory() | ||
{ | ||
$dir = $this->request->directory; | ||
$success = false; | ||
$parent = ""; | ||
|
||
if (file_exists($dir)) { | ||
$parent = dirname($dir); | ||
$success = true; | ||
} | ||
|
||
$this->response = array("success" => $success, "parent" => $parent); | ||
|
||
} | ||
|
||
protected function deleteFile() | ||
{ | ||
$f = $this->request->file; | ||
$success = false; | ||
|
||
if (file_exists($f)) { | ||
if (!is_dir($f)) { | ||
unlink($f); | ||
} else { | ||
foreach (preg_grep('/^([^.])/', scandir($f)) as $file) { | ||
unlink($f . '/' . $file); | ||
} | ||
rmdir($f); | ||
} | ||
} | ||
|
||
if (!file_exists($f)) { | ||
$success = true; | ||
} | ||
|
||
$this->response = array("success" => $success); | ||
|
||
} | ||
|
||
protected function editFile() | ||
{ | ||
$f = $this->request->file; | ||
$data = $this->request->contents; | ||
$success = false; | ||
|
||
file_put_contents($f, $data); | ||
if (file_exists($f)) { | ||
$success = true; | ||
} | ||
|
||
$this->response = array("success" => $success); | ||
} | ||
|
||
protected function getFileContents() | ||
{ | ||
$f = $this->request->file; | ||
$success = false; | ||
$content = ""; | ||
|
||
if (file_exists($f)) { | ||
$success = true; | ||
$content = file_get_contents($f); | ||
} | ||
|
||
$this->response = array("success" => $success, "content" => $content); | ||
|
||
} | ||
|
||
protected function createFolder() | ||
{ | ||
$dir = $this->request->directory; | ||
$name = $this->request->name; | ||
$success = false; | ||
|
||
if (!is_dir($dir . '/' . $name)) { | ||
$success = true; | ||
mkdir($dir . "/" . $name); | ||
} | ||
|
||
$this->response = array("success" => $success); | ||
} | ||
|
||
} |
Oops, something went wrong.