From acb4f26f1a29c86c5f430f817bd24c469cebdd47 Mon Sep 17 00:00:00 2001 From: shubham_meena Date: Tue, 12 Aug 2014 00:33:58 +0530 Subject: [PATCH] API key management plugin for ams --HG-- branch : Gsoc14-ryzomAppImprovements --- .../ams_lib/plugins/API_key_management/.info | 8 + .../API_key_management/API_key_management.php | 206 ++++++++++++++++++ .../API_key_management/generate_key.php | 53 +++++ .../API_key_management/templates/gen_key.tpl | 46 ++++ .../API_key_management/templates/index.tpl | 133 +++++++++++ 5 files changed, 446 insertions(+) create mode 100644 code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/.info create mode 100644 code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/API_key_management.php create mode 100644 code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/generate_key.php create mode 100644 code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/templates/gen_key.tpl create mode 100644 code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/templates/index.tpl diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/.info b/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/.info new file mode 100644 index 000000000..b185a31db --- /dev/null +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/.info @@ -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 + + + diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/API_key_management.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/API_key_management.php new file mode 100644 index 000000000..1676f243a --- /dev/null +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/API_key_management.php @@ -0,0 +1,206 @@ + 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; + + } diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/generate_key.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/generate_key.php new file mode 100644 index 000000000..1ddfab7ed --- /dev/null +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/generate_key.php @@ -0,0 +1,53 @@ + 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; + } + } diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/templates/gen_key.tpl b/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/templates/gen_key.tpl new file mode 100644 index 000000000..1ab283449 --- /dev/null +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/templates/gen_key.tpl @@ -0,0 +1,46 @@ +
+
+
+

API KEY management

+
+ + + +
+
+
+
+

Generate Access Key

+
+ + +
+
+
+
+
+ Generate Key + +
+ +
+
+ + +
+
+
+
+ +
+ +
+
+
+
+
+
+
+
+ + diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/templates/index.tpl b/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/templates/index.tpl new file mode 100644 index 000000000..eac902aae --- /dev/null +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/plugins/API_key_management/templates/index.tpl @@ -0,0 +1,133 @@ +{block name=content} + +{if isset($smarty.get.plugin_action) and $smarty.get.plugin_action eq 'generate_key'} +
+
+
+

API KEY management

+
+ + + +
+
+
+
+

Generate Access Key

+
+ + +
+
+
+
+
+ Generate Key + +
+ +
+
+ + +
+
+
+ +
+ +
+ +
+
+
+ +
+ +
+
+ +
+ +
+
+ + +
+
+
+ +
+ +
+ +
+
+
+
+
+
+
+
+{else} +
+
+
+

API KEY management

+
+ + + +
+
+ + + {if isset($hook_info.API_key_management['gen_key_validate']) and $hook_info.API_key_management['gen_key_validate'] eq 'false' }

Please enter all the fields

{/if} + {if isset($smarty.get.success) and $smarty.get.success eq '1'}

Key added successfully

{/if} + {if isset($smarty.get.success) and $smarty.get.success eq '2'}

Key deleted successfully

{/if} +
+ +
+
+
+

All the keys you have generated will be shown and you can customize from here.

+ + + + + + + + + + + + + + {foreach from=$hook_info.API_key_management.api_keys item=element} + + + + + + + + {/foreach} + + + +
NameTypeCharacterAccess KeyExpiresActions
{$element.FrName}{$element.UserType}{$element.UserCharacter}{$element.AccessToken}{$element.ExpiryDate} +
+
+
+
+
+ {/if} +{/block}