Added: external auth

This commit is contained in:
vl 2012-05-04 18:33:59 +02:00
parent 2cc8300fcc
commit f8556043e2
10 changed files with 90 additions and 65 deletions

View file

@ -20,30 +20,28 @@ require_once(RYAPI_PATH.'client/config.php');
// Og (non-ryzom.com) method // Og (non-ryzom.com) method
function ryzom_authenticate_with_serverkey($cid, $name, $authserver, $authkey) { function ryzom_authenticate_with_serverkey($cid, $name, $authserver, $authkey) {
global $_RYZOM_API_CONFIG;
$fn = $_RYZOM_API_CONFIG['auth_script'].'?name='.$name.'&cid='.$cid.'&authkey='.$authkey.'&authserver='.$authserver;
$res = file_get_contents($fn);
return $res == '1';
} }
// Ig method // Ig method
function ryzom_authenticate_ingame($cid, $name, $authkey) { function ryzom_authenticate_ingame($cid, $name, $authkey) {
global $_RYZOM_API_CONFIG; if (isset($_SESSION['user']))
$fn = $_RYZOM_API_CONFIG['auth_script'].'?name='.$name.'&cid='.$cid.'&authkey='.$authkey.'&ig=1'; return true;
$res = file_get_contents($fn); if (ryzom_get_param('user'))
echo $res; return true;
return $res == '1';
return false;
} }
// Session method // Session method
function ryzom_authenticate_with_session($name, $redirect) { function ryzom_authenticate_with_session($name, $redirect) {
global $_RYZOM_API_CONFIG; if (isset($_SESSION['user']))
$fn = $_RYZOM_API_CONFIG['auth_script'].'?name='.$name; return true;
$res = file_get_contents($fn); if (ryzom_get_param('user'))
return $res == '1'; return true;
return false;
} }
?> ?>

View file

@ -17,7 +17,23 @@
*/ */
function ryzom_user_get_info($cid) { function ryzom_user_get_info($cid) {
return Array(); if (isset($_SESSION['user']))
return $_SESSION['user'];
$user = unserialize(base64_decode(ryzom_get_param('user')));
$_SESSION['user'] = $user;
return $user;
} }
function ryzom_get_user_id($cid, $name, $creation_date) {
if (isset($_SESSION['user']))
return $_SESSION['user']['id'];
$user = unserialize(base64_decode(ryzom_get_param('user')));
$_SESSION['user'] = $user;
return $user['id'];
}
?> ?>

View file

@ -5,7 +5,8 @@ function ryzom_app_authenticate(&$user, $ask_login=true, $welcome_message='') {
$authserver = ryzom_get_param('authserver'); $authserver = ryzom_get_param('authserver');
$authkey = ryzom_get_param('authkey'); $authkey = ryzom_get_param('authkey');
$lang = ryzom_get_param('lang'); $lang = ryzom_get_param('lang');
$cid = ryzom_get_param('cid', '');
$is_ingame = false;
// we have to set the $user['lang'] even for anonymous user or we cannot display the test in the right langage // we have to set the $user['lang'] even for anonymous user or we cannot display the test in the right langage
if($lang == '') { if($lang == '') {
$l = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); $l = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
@ -14,15 +15,20 @@ function ryzom_app_authenticate(&$user, $ask_login=true, $welcome_message='') {
else else
$lang = 'en'; $lang = 'en';
} }
$user['message'] = '';
$user['lang'] = $lang; $user['lang'] = $lang;
$user['ig'] = false;
if (RYZOM_IG || ryzom_get_param('ig')) { if ((isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Ryzom')) || ryzom_get_param('ig')) {
$user['ig'] = true;
// Ingame // Ingame
$cid = ryzom_get_param('cid'); $shardid = ryzom_get_param('shardid');
if (!ryzom_authenticate_ingame($cid, $name, $authkey)) if (!ryzom_authenticate_ingame($shardid, $cid, $name, $authkey))
return false; return false;
$is_ingame = true;
} else { } else {
// Outgame : Use session // Outgame : Use session
$error_message = '';
if (!ryzom_authenticate_with_session($name, $cid, $error_message)) { if (!ryzom_authenticate_with_session($name, $cid, $error_message)) {
if ($ask_login) { if ($ask_login) {
$c = ''; $c = '';
@ -47,8 +53,12 @@ function ryzom_app_authenticate(&$user, $ask_login=true, $welcome_message='') {
// get user informations // get user informations
$user = ryzom_user_get_info($cid); $user = ryzom_user_get_info($cid);
$user['lang'] = $_SESSION['lang']; $user['lang'] = $_SESSION['lang'];
if (isset($user['creation_date']))
$user['id'] = ryzom_get_user_id($cid, $user['char_name'], $user['creation_date']); $user['id'] = ryzom_get_user_id($cid, $user['char_name'], $user['creation_date']);
if ($is_ingame && $user['last_played_date'] != '0')
$user['ig'] = true;
else
$user['ig'] = false;
unset($user['last_played_date']); unset($user['last_played_date']);
unset($user['creation_date']); unset($user['creation_date']);
return true; return true;

View file

@ -33,7 +33,7 @@ class ryLogger {
function getLogs() { function getLogs() {
$ret = ''; $ret = '';
if ($this->logs && $this->enable) if ($this->logs && $this->enable)
$ret = "<b>Debug</b>\n".implode("\n", $this->logs); $ret = "<b>Debug</b>\n\n".implode("\n", $this->logs);
$this->logs = array(); $this->logs = array();
return $ret; return $ret;
} }

View file

@ -16,7 +16,7 @@
* along with ryzom_api. If not, see <http://www.gnu.org/licenses/>. * along with ryzom_api. If not, see <http://www.gnu.org/licenses/>.
*/ */
function ryzom_app_render($title, $content, $bgcolor='', $javascript=array(), $homeLink=false) { function ryzom_app_render($title, $content, $ig=false, $bgcolor='', $javascript=array(), $homeLink=false) {
$c = ''; $c = '';
// Render header // Render header
$title_prefix = ''; $title_prefix = '';
@ -25,38 +25,35 @@ function ryzom_app_render($title, $content, $bgcolor='', $javascript=array(), $h
} }
if (!$bgcolor) if (!$bgcolor)
$bgcolor = '#000000'.(RYZOM_IG?'00':''); $bgcolor = '#000000'.($ig?'00':'');
$c .= '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'."\n"; if (!$ig) {
$c .= '<html> $c .= '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'."\n";
<head>'."\n"; $c .= '<html>
$c .= ' <title>'.$title_prefix.(translation_exists($title)?_t($title):$title).'</title>'."\n"; <head>'."\n";
$c .= ' <meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8" />'."\n"; $c .= ' <title>'.$title_prefix.(translation_exists($title)?_t($title):$title).'</title>'."\n";
$c .= ' <meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8" />'."\n";
if (!RYZOM_IG) {
$c .= ryzom_render_header(); $c .= ryzom_render_header();
$c .= ryzom_render_header_www(); $c .= ryzom_render_header_www();
} $events = ON_IPHONE ? 'onorientationchange="updateOrientation();" ' : '';
$c .= ' </head>'."\n";
$c .= ' <body '.$events.'bgcolor="'.$bgcolor.'">'."\n";
// Javascript
$js_code = '';
foreach ($javascript as $js)
$js_code .= '<script type="text/javascript" src="'.$js.'"></script>';
$c .= $js_code;
$events = ON_IPHONE ? 'onorientationchange="updateOrientation();" ' : '';
$c .= ' </head>'."\n";
$c .= ' <body '.$events.'bgcolor="'.$bgcolor.'">'."\n";
// Javascript
$js_code = '';
foreach ($javascript as $js)
$js_code .= '<script type="text/javascript" src="'.$js.'"></script>';
$c .= $js_code;
if (RYZOM_IG)
$c .= $content;
else{
$c .= ryzom_render_www(ryzom_render_window($title, $content, $homeLink)); $c .= ryzom_render_www(ryzom_render_window($title, $content, $homeLink));
$c .= '</body></html>';
} else {
$c .= '<html><body>';
$c .= $content;
$debug = ryLogger::getInstance()->getLogs();
if ($debug)
$c .= '<table width="100%"><tr bgcolor="#002200"><td>'.$debug.'</td></tr></table>';
$c .= '</body></html>';
} }
$c .= '</body></html>';
return $c; return $c;
} }
@ -140,7 +137,7 @@ function ryzom_render_window_begin($title, $homeLink=false) {
return ' return '
<div class="ryzom-ui ryzom-ui-header"> <div class="ryzom-ui ryzom-ui-header">
<div class="ryzom-ui-tl"><div class="ryzom-ui-tr"> <div class="ryzom-ui-tl"><div class="ryzom-ui-tr">
<div class="ryzom-ui-t">'.$title_prefix.(translation_exists($title)?_t($title):$title).$homeLink.'</div> <div class="ryzom-ui-t">'.(translation_exists($title)?_t($title):$title).$homeLink.'</div>
</div> </div>
</div> </div>
<div class="ryzom-ui-l"><div class="ryzom-ui-r"><div class="ryzom-ui-m"> <div class="ryzom-ui-l"><div class="ryzom-ui-r"><div class="ryzom-ui-m">

View file

@ -25,12 +25,6 @@ if (!defined('ON_IPHONE')) {
else else
define('ON_IPHONE', false); define('ON_IPHONE', false);
} }
if (!defined('RYZOM_IG')) {
if (isset($_SERVER['HTTP_USER_AGENT']))
define('RYZOM_IG', strpos($_SERVER['HTTP_USER_AGENT'], 'Ryzom'));
else
define('RYZOM_IG', false);
}
$includes = array('auth', 'config', 'utils', 'user'); $includes = array('auth', 'config', 'utils', 'user');

View file

@ -23,8 +23,15 @@ function ryzom_authenticate_with_serverkey($cid, $name, $authserver, $authkey) {
return true; return true;
} }
function ryzom_authenticate_ingame($cid, $name, $authkey) { function ryzom_authenticate_ingame($shardid, $cid, $name, $authkey) {
return file_get_contents(RYAPI_AUTH_SCRIPT) == '1'; $db = new ServerDatabase(RYAPI_NELDB_HOST, RYAPI_NELDB_LOGIN, RYAPI_NELDB_PASS, RYAPI_NELDB_RING);
$uid = intval($cid / 16);
$sql = "SELECT cookie FROM ring_users WHERE user_id = $uid";
$row = $db->query_single_row($sql);
$rawkey = $shardid.$name.$cid.'\''.trim($row['cookie']).'\'';
$md5rawkey = md5($rawkey);
return $authkey == $md5rawkey;
} }
// take the character name and the account password and check if it's valid // take the character name and the account password and check if it's valid

View file

@ -15,6 +15,8 @@
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with ryzom_api. If not, see <http://www.gnu.org/licenses/>. * along with ryzom_api. If not, see <http://www.gnu.org/licenses/>.
*/ */
define('RYAPI_SHARDID', '101');
define('RYAPI_WEBDB_HOST', 'localhost'); define('RYAPI_WEBDB_HOST', 'localhost');
define('RYAPI_WEBDB_LOGIN', 'localhost'); define('RYAPI_WEBDB_LOGIN', 'localhost');
define('RYAPI_WEBDB_PASS', 'localhost'); define('RYAPI_WEBDB_PASS', 'localhost');

View file

@ -29,6 +29,6 @@ else
// Content // Content
$c = _t('access', $num_access['num_access']).'<br/>'; $c = _t('access', $num_access['num_access']).'<br/>';
echo ryzom_app_render(APP_NAME, $c); echo ryzom_app_render(APP_NAME, $c, $user['ig']);
?> ?>

View file

@ -1,7 +1,6 @@
<?php <?php
include_once('config.php'); include_once('config.php');
include_once('lang.php'); include_once('lang.php');
// List of apps // List of apps
@ -9,7 +8,6 @@ $apps = array(
'app_test', 'app_test',
); );
$c = ''; $c = '';
// Ask to authenticate user (using ingame or session method) and fill $user with all information // Ask to authenticate user (using ingame or session method) and fill $user with all information
@ -17,7 +15,7 @@ $logged = ryzom_app_authenticate($user, false);
if ($logged) { if ($logged) {
$c .= '<h1>'._t('welcome', $user['char_name']).'</h1>'; $c .= '<h1>'._t('welcome', $user['char_name']).'</h1>';
} else { } else {
if (!RYZOM_IG) { if (!$user['ig']) {
if ($user['message']) if ($user['message'])
$c .= '<div style="text-align: center"><strong style="color: #FF5555">'._t($user['message']).'</strong></div><br />'; $c .= '<div style="text-align: center"><strong style="color: #FF5555">'._t($user['message']).'</strong></div><br />';
$c .= ryzom_render_login_form(ryzom_get_param('name')); $c .= ryzom_render_login_form(ryzom_get_param('name'));
@ -28,8 +26,11 @@ foreach ($apps as $app) {
$c .= '<a href="'.RYAPP_URL.'/'.$app.'/index.php"><img src="'.RYAPP_URL.'/'.$app.'/favicon.png" />'._t($app).'</a><br />'; $c .= '<a href="'.RYAPP_URL.'/'.$app.'/index.php"><img src="'.RYAPP_URL.'/'.$app.'/favicon.png" />'._t($app).'</a><br />';
} }
if($logged) $c .= '<br /><a href="'.RYAPP_URL.'/index.php?action=logout">'._t('logout').'</a>'; if ($logged && !$user['ig'])
$c .= '<br /><a href="'.RYAPP_URL.'/index.php?action=logout">'._t('logout').'</a>';
echo ryzom_app_render('Ryzom', $c); // Print GET values on debug view
p($_GET);
echo ryzom_app_render('Ryzom', $c, $user['ig']);
?> ?>