mirror of
https://port.numenaute.org/aleajactaest/khanat-opennel-code.git
synced 2024-12-24 18:08:44 +00:00
API key management plugin for ams
This commit is contained in:
parent
d721261c0f
commit
6abb3ac265
5 changed files with 446 additions and 0 deletions
|
@ -0,0 +1,8 @@
|
|||
PluginName = API Key Management
|
||||
Description = Provides public access to the API's by generating access tokens.
|
||||
Version = 1.0.0
|
||||
Type = automatic
|
||||
TemplatePath = ../../../ams_lib/plugins/API_key_management/templates/index.tpl
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,206 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Global and Local Hooks for the API key Management plugin
|
||||
* Global Hooks are defined with the prefix(name of the plugin)
|
||||
* Local Hooks are defined with normal function name
|
||||
*
|
||||
* All the Global Hooks are called during the page load
|
||||
* and Local Hooks are called according to conditions
|
||||
*
|
||||
* @author shubham meena mentored by Matthew Lagoe
|
||||
*/
|
||||
|
||||
|
||||
// Global variables to store the data
|
||||
$return_set = array();
|
||||
$var_set = array();
|
||||
|
||||
|
||||
/**
|
||||
* Display hook for api key management
|
||||
*/
|
||||
function api_key_management_hook_display()
|
||||
{
|
||||
global $return_set;
|
||||
// to display plugin name in menu bar
|
||||
$return_set['menu_display'] = 'API Key Management';
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to validate the posted data
|
||||
*/
|
||||
function hook_validate( $var )
|
||||
{
|
||||
if ( isset( $var ) && !empty( $var ) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Local Hook to set the POST variables and validate them
|
||||
*/
|
||||
function hook_variables()
|
||||
{
|
||||
global $var_set;
|
||||
global $return_set;
|
||||
|
||||
if ( hook_validate( $_POST['expDate'] ) && hook_validate( $_POST['sp_name'] ) && hook_validate( $_POST['api_type'] )
|
||||
&& hook_validate( $_POST['character_name'] ) )
|
||||
{
|
||||
$var_set['ExpiryDate'] = $_POST['expDate'];
|
||||
$var_set['FrName'] = $_POST['sp_name'];
|
||||
$var_set['UserType'] = $_POST['api_type'];
|
||||
$var_set['UserCharacter'] = $_POST['character_name'];
|
||||
$var_set['User'] = $_SESSION['user'];
|
||||
$var_set['AddedOn'] = date( "Y-m-d H:i:s" );
|
||||
$var_set['Items'] = '';
|
||||
$return_set['gen_key_validate'] = 'true';
|
||||
}
|
||||
else
|
||||
{
|
||||
$return_set['gen_key_validate'] = 'false';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Global Hook to create table of the API_key_management
|
||||
* if not created.Contains the sql code
|
||||
*/
|
||||
|
||||
function api_key_management_hook_create_tb()
|
||||
{
|
||||
$dbl = new DBLayer( "lib" );
|
||||
$sql = "
|
||||
--
|
||||
-- Database: `ryzom_ams_lib`
|
||||
--
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Table structure for table `ams_api_keys`
|
||||
--
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `ams_api_keys` (
|
||||
`SNo` int(10) NOT NULL AUTO_INCREMENT,
|
||||
`User` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
|
||||
`FrName` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
|
||||
`UserType` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
|
||||
`UserCharacter` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
|
||||
`ExpiryDate` date DEFAULT NULL,
|
||||
`AccessToken` text COLLATE utf8_unicode_ci DEFAULT NULL,
|
||||
`AddedOn` datetime DEFAULT NULL,
|
||||
`Items` text COLLATE utf8_unicode_ci,
|
||||
PRIMARY KEY (`SNo`),
|
||||
KEY `User` (`User`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
|
||||
|
||||
--
|
||||
-- Constraints for table `ams_api_keys`
|
||||
--
|
||||
ALTER TABLE `ams_api_keys`
|
||||
ADD CONSTRAINT `ams_api_keys_ibfk_1` FOREIGN KEY (`User`) REFERENCES `ryzom_ams`.`ams_user` (`Login`);";
|
||||
|
||||
$dbl -> executeWithoutParams( $sql );
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to store data to database which is sent as post
|
||||
* method from the forms in this plugin
|
||||
* It also calls the local hook
|
||||
*/
|
||||
function api_key_management_hook_store_db()
|
||||
{
|
||||
global $var_set;
|
||||
global $return_set;
|
||||
|
||||
// if the form been submited move forward
|
||||
if ( @hook_validate( $_POST['gen_key'] ) ) {
|
||||
|
||||
// local hook to validate the POST variables
|
||||
hook_variables();
|
||||
|
||||
// if validation successfull move forward
|
||||
if ( $return_set['gen_key_validate'] == 'true' && $_GET['plugin_action'] == 'generate_key' )
|
||||
{
|
||||
// this part generated the access token
|
||||
include 'generate_key.php';
|
||||
$var_set['AccessToken'] = generate_key :: randomToken( 56, false, true, false );
|
||||
|
||||
// database connection
|
||||
$db = new DBLayer( 'lib' );
|
||||
// insert the form data to the database
|
||||
$db -> insert( 'ams_api_keys', $var_set );
|
||||
|
||||
// redirect to the the main page with success code
|
||||
// 1 refers to the successfull addition of key to the database
|
||||
header( "Location: index.php?page=layout_plugin&&name=API_key_management&&success=1" );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Global Hook to load the data from db and set it
|
||||
* into the global array to return it to the template
|
||||
*/
|
||||
function api_key_management_hook_load_db()
|
||||
{
|
||||
global $var_set;
|
||||
global $return_set;
|
||||
|
||||
$db = new DBLayer( 'lib' );
|
||||
|
||||
// returns the regestered keys
|
||||
$sth = $db -> select( 'ams_api_keys', array( 'user' => $_SESSION['user'] ), 'User = :user' );
|
||||
$row = $sth -> fetchAll();
|
||||
$return_set['api_keys'] = $row;
|
||||
|
||||
// returns the characters with respect to the user id in the ring_tool->characters
|
||||
$db = new DBLayer( 'ring' );
|
||||
$sth = $db -> selectWithParameter( 'char_name', 'characters' , array(), '1' );
|
||||
$row = $sth -> fetchAll();
|
||||
$return_set['characters'] = $row;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Global Hook to update or delete the data from db
|
||||
*/
|
||||
function api_key_management_hook_update_db()
|
||||
{
|
||||
global $var_set;
|
||||
global $return_set;
|
||||
|
||||
$db = new DBLayer( 'lib' );
|
||||
if ( isset( $_GET['delete_id'] ) )
|
||||
{
|
||||
// removes the registered key using get variable which contains the id of the registered key
|
||||
$db -> delete( 'ams_api_keys', array( 'SNo' => $_GET['delete_id'] ), 'SNo = :SNo' );
|
||||
|
||||
// redirecting to the API_key_management plugins template with success code
|
||||
// 2 refers to the succssfull delete condition
|
||||
header( "Location: index.php?page=layout_plugin&&name=API_key_management&&success=2" );
|
||||
exit;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Global Hook to return global variables which contains
|
||||
* the content to use in the smarty templates
|
||||
*
|
||||
* @return $return_set global array returns the template data
|
||||
*/
|
||||
function api_key_management_hook_return_global()
|
||||
{
|
||||
global $return_set;
|
||||
return $return_set;
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class for API_Key_management plugin
|
||||
* Contains the function to generate random Tokken
|
||||
*
|
||||
* @author shubham meena mentored by Matthew Lagoe
|
||||
*/
|
||||
|
||||
class generate_key {
|
||||
|
||||
/**
|
||||
* Static function to generate random token which is registerd with the user
|
||||
* to allow public access using this random token
|
||||
* It return different types of tokkens according to the parameters pass through it
|
||||
* like length , if standard chracter requires, if special character requires etc
|
||||
*/
|
||||
public static function randomToken( $len = 64, $output = 5, $standardChars = true, $specialChars = true, $chars = array() ) {
|
||||
$out = '';
|
||||
$len = intval( $len );
|
||||
$outputMap = array( 1 => 2, 2 => 8, 3 => 10, 4 => 16, 5 => 10 );
|
||||
if ( !is_array( $chars ) ) {
|
||||
$chars = array_unique( str_split( $chars ) );
|
||||
}
|
||||
if ( $standardChars ) {
|
||||
$chars = array_merge( $chars, range( 48, 57 ), range( 65, 90 ), range( 97, 122 ) );
|
||||
}
|
||||
if ( $specialChars ) {
|
||||
$chars = array_merge( $chars, range( 33, 47 ), range( 58, 64 ), range( 91, 96 ), range( 123, 126 ) );
|
||||
}
|
||||
array_walk( $chars, function( &$val ) {
|
||||
if ( !is_int( $val ) ) {
|
||||
$val = ord( $val ); }
|
||||
}
|
||||
);
|
||||
if ( is_int( $len ) ) {
|
||||
while ( $len ) {
|
||||
$tmp = ord( openssl_random_pseudo_bytes( 1 ) );
|
||||
if ( in_array( $tmp, $chars ) ) {
|
||||
if ( !$output || !in_array( $output, range( 1, 5 ) ) || $output == 3 || $output == 5 ) {
|
||||
$out .= ( $output == 3 ) ? $tmp : chr( $tmp );
|
||||
}
|
||||
else {
|
||||
$based = base_convert( $tmp, 10, $outputMap[$output] );
|
||||
$out .= ( ( ( $output == 1 ) ? '00' : ( ( $output == 4 ) ? '0x' : '' ) ) . ( ( $output == 2 ) ? sprintf( '%03d', $based ) : $based ) );
|
||||
}
|
||||
$len--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ( empty( $out ) ) ? false : $out;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
<div class="row-fluid">
|
||||
<div class="box span12">
|
||||
<div class="box-header well" data-original-title>
|
||||
<h2><i class="icon-user"></i> API KEY management</h2>
|
||||
<div class="box-icon">
|
||||
<a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
|
||||
<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
|
||||
<a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="box span4">
|
||||
<div class="box-header well" data-original-title="">
|
||||
<h2><i class="icon-th"></i> Generate Access Key</h2>
|
||||
<div class="box-icon">
|
||||
<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
|
||||
<a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="box-content">
|
||||
<div class="row-fluid">
|
||||
<form id="generateKey" class="form-vertical" method="post" action="index.php?page=layout_plugin&&name={$arrkey}">
|
||||
<legend>Generate Key</legend>
|
||||
|
||||
<div class="control-group ">
|
||||
<label class="control-label">Expirey:</label>
|
||||
<div class="controls">
|
||||
<div class="input-prepend">
|
||||
<span style="margin-left:5px;" class="add-on"><i class="icon-time"></i></span>
|
||||
<input type="text" value="Expiry Date" placeholder="Expiry Date" name="expDate" id="expDate" class="input-xlarge">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label class="control-label"></label>
|
||||
<div class="controls">
|
||||
<button type="submit" name="gen_key" value="true" class="btn btn-primary" style="margin-left:5px; margin-top:10px;">Generate Key</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div><!--/span-->
|
||||
</div><!--/span-->
|
||||
</div><!--/row-->
|
||||
|
||||
|
|
@ -0,0 +1,133 @@
|
|||
{block name=content}
|
||||
|
||||
{if isset($smarty.get.plugin_action) and $smarty.get.plugin_action eq 'generate_key'}
|
||||
<div class="row-fluid">
|
||||
<div class="box span12">
|
||||
<div class="box-header well" data-original-title>
|
||||
<h2><i class="icon-user"></i> API KEY management</h2>
|
||||
<div class="box-icon">
|
||||
<a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
|
||||
<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
|
||||
<a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="box span4">
|
||||
<div class="box-header well" data-original-title="">
|
||||
<h2><i class="icon-th"></i> Generate Access Key</h2>
|
||||
<div class="box-icon">
|
||||
<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
|
||||
<a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="box-content">
|
||||
<div class="row-fluid">
|
||||
<form id="generateKey" class="form-vertical" method="post" action="index.php?page=layout_plugin&&name={$arrkey}&&plugin_action=generate_key">
|
||||
<legend>Generate Key</legend>
|
||||
|
||||
<div class="control-group">
|
||||
<label class="control-label">Name:</label>
|
||||
<div class="controls">
|
||||
<div class="input-prepend">
|
||||
<span class="add-on" style="margin-left:5px;"><i class="icon-user"></i></span>
|
||||
<input type="text" class="input-xlarge" id="sp_name" name="sp_name" placeholder="Your friendly name">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="control-group">
|
||||
<label class="control-label">Type:</label>
|
||||
<div class="controls">
|
||||
<select name="api_type">
|
||||
<option value="Character">Character</option>
|
||||
<option value="Corporation">Corporation</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label class="control-label">Character:</label>
|
||||
<div class="controls">
|
||||
<select name="character_name">
|
||||
{foreach from=$hook_info.API_key_management.characters item=element}
|
||||
<option value="{$element.char_name}">{$element.char_name}</option>
|
||||
{/foreach}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="control-group ">
|
||||
<label class="control-label">Expirey:</label>
|
||||
<div class="controls">
|
||||
<div class="input-prepend">
|
||||
<span style="margin-left:5px;" class="add-on"><i class="icon-time"></i></span>
|
||||
<input type="text" placeholder="Expiry Date" name="expDate" id="expDate" class="input-xlarge">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="control-group">
|
||||
<label class="control-label"></label>
|
||||
<div class="controls">
|
||||
<button type="submit" name="gen_key" value="true" class="btn btn-primary" style="margin-left:5px; margin-top:10px;">Generate Key</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div><!--/span-->
|
||||
</div><!--/span-->
|
||||
</div><!--/row-->
|
||||
{else}
|
||||
<div class="row-fluid">
|
||||
<div class="box span12">
|
||||
<div class="box-header well" data-original-title>
|
||||
<h2><i class="icon-user"></i> API KEY management</h2>
|
||||
<div class="box-icon">
|
||||
<a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
|
||||
<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
|
||||
<a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
{if isset($hook_info.API_key_management['gen_key_validate']) and $hook_info.API_key_management['gen_key_validate'] eq 'false' }<div class="alert alert-error"><p>Please enter all the fields</p></div>{/if}
|
||||
{if isset($smarty.get.success) and $smarty.get.success eq '1'}<div class="alert alert-error"><p>Key added successfully</p></div>{/if}
|
||||
{if isset($smarty.get.success) and $smarty.get.success eq '2'}<div class="alert alert-error"><p>Key deleted successfully</p></div>{/if}
|
||||
<center>
|
||||
<a href="index.php?page=layout_plugin&&name=API_key_management&&plugin_action=generate_key"><button class="btn btn-primary btn-large dropdown-toggle">Generate key</button></a>
|
||||
</center>
|
||||
<div class="box-content">
|
||||
<div class="row-fluid">
|
||||
<center><p>All the keys you have generated will be shown and you can customize from here.</p></center>
|
||||
|
||||
<table class="table table-striped table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Type</th>
|
||||
<th>Character</th>
|
||||
<th>Access Key</th>
|
||||
<th>Expires</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{foreach from=$hook_info.API_key_management.api_keys item=element}
|
||||
<tr>
|
||||
<td class="center">{$element.FrName}</td>
|
||||
<td class="center">{$element.UserType}</td>
|
||||
<td class="center">{$element.UserCharacter}</td>
|
||||
<td class="center">{$element.AccessToken}</td>
|
||||
<td class="center">{$element.ExpiryDate}</td>
|
||||
<td><a href="index.php?page=layout_plugin&&name={$arrkey}&&delete_id={$element.SNo}"><button class="btn btn-primary btn-large">Delete</button></a>
|
||||
</tr>
|
||||
{/foreach}
|
||||
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
</div>
|
||||
</div><!--/span-->
|
||||
</div><!--/span-->
|
||||
</div><!--/row-->
|
||||
{/if}
|
||||
{/block}
|
Loading…
Reference in a new issue