Skip to content

Commit 99af4fb

Browse files
committedNov 11, 2016
fixed issues #38, #39, #40
updated tools/oauth2demo for 1.6.0. refactoring
1 parent 2e5163f commit 99af4fb

File tree

11 files changed

+65
-67
lines changed

11 files changed

+65
-67
lines changed
 

‎RESTController/RESTController.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,10 @@ public static function registerAutoloader() {
9090
* $iliasRoot <String> - Absolute path to ILIAS directory
9191
* $userSettings <Array[Mixed]> - Associative array of application settings
9292
*/
93-
public function __construct($iliasRoot, array $userSettings = array()) {
93+
public function __construct(array $userSettings = array()) {
9494
// Call parent (SLIM) constructor
9595
parent::__construct($userSettings);
9696

97-
// Fetch environment and remeber base-directory (just in case)
98-
$env = $this->environment();
99-
$env['ilias_root'] = $iliasRoot;
100-
$env['ctl_root'] = __DIR__;
101-
// Alternatively set as hard-coded path: "$root/Customizing/global/plugins/Services/UIComponent/UserInterfaceHook/REST/RESTController"
102-
10397
// Add Content-Type middleware (support for JSON/XML requests)
10498
$contentType = new libs\Middleware\ContentTypes();
10599
$this->add($contentType);
@@ -123,7 +117,7 @@ public function __construct($iliasRoot, array $userSettings = array()) {
123117

124118
// Set default template base-directory
125119
// DoIt: Extract using ILIAS (or keep constant)
126-
$this->view()->setTemplatesDirectory($appDirectory);
120+
$this->view()->setTemplatesDirectory(__DIR__);
127121

128122
// Set default 404 template
129123
$this->notFound(function () {

‎RESTController/core/oauth2_v2/models/Authorize.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,15 @@ public static function ShowWebsite($app, $param) {
331331
// fetch absolute dirictory of view folder
332332
$plugin = Libs\RESTilias::getPlugin();
333333
$pluginDir = str_replace('./', '', $plugin->getDirectory());
334-
$pluginDir = $pluginDir . '/RESTController/core/oauth2_v2/views/';
335-
336-
334+
$viewDir = $pluginDir . '/RESTController/core/oauth2_v2/views/';
337335

338336
// Content and further logic is managed by the template
339337
$app->response()->setFormat('HTML');
340338
$app->render(
341339
'core/oauth2_v2/views/index.php',
342340
array(
343341
'baseURL' => ILIAS_HTTP_PATH,
344-
'viewURL' => ILIAS_HTTP_PATH . '/' . $pluginDir,
342+
'viewURL' => ILIAS_HTTP_PATH . '/' . $viewDir,
345343
'endpoint' => ILIAS_HTTP_PATH . '/' . $pluginDir . '/api.php' . $routeURL,
346344
'client' => CLIENT_ID,
347345
'parameters' => $param,

‎RESTController/core/oauth2_v2/views/index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<div class="card">
4141
<img class="logo" src="<?php echo $viewURL; ?>img/logo.png">
4242

43-
<h1>Anmeldung</h1>
43+
<h1>OAuth2</h1>
4444
<h2>Anwendungs-Zugriff</h2><br>
4545

4646
<?php

‎RESTController/database/RESTclient.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public static function fromApiKey($apiKey) {
6767
* Function: getKey($key)
6868
* @See RESTDatabase->getKey(...)
6969
*/
70-
public function getKey($key) {
70+
public function getKey($key, $read = false) {
7171
// Fetch internal value from parent
72-
$value = parent::getKey($key);
72+
$value = parent::getKey($key, $read);
7373

7474
// Convert internal value when publshing
7575
// Note: Make sure to 'revert' those changes in setKey(...)!
@@ -329,7 +329,7 @@ public function isScopeAllowed($scope) {
329329
return Libs\RESTLib::CheckComplexRestriction($allowed, $scopes, ' ');
330330
}
331331

332-
332+
333333
/**
334334
* Function: isBridgeAllowed($direction)
335335
* Checks if the ILIAS <-> oAuth2 bridge is allowed for this client in the requested direction.

‎RESTController/libs/RESTDatabase.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ public static function fromWhere($where = null, $limit = false, $offset = false,
185185

186186
// Build a simple where-based query
187187
$table = static::getTableName();
188-
$class = end(explode('\\', get_called_class()));
188+
$array = explode('\\', get_called_class());
189+
$class = end($array);
189190
$sql = sprintf('SELECT %s.* FROM %s AS %s %s %s %s %s', $class, $table, $class, $joinSQL, $whereSQL, $limitSQL, $offsetSQL);
190191

191192
// Generate ilDB query-object
@@ -1175,7 +1176,8 @@ public static function getTableKeys() {
11751176
* <String> - Short name of current class name (late static binding)
11761177
*/
11771178
public static function getName() {
1178-
return end(explode('\\', get_called_class()));
1179+
$array = explode('\\', get_called_class());
1180+
return end($array);
11791181
}
11801182

11811183

‎RESTController/libs/RESTRequest.php

+4
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ public function getToken($name = 'access', $stringOnly = false) {
222222
if (isset($this->tokens[$name]))
223223
return $this->tokens[$name];
224224

225+
// Prevent undefined variables
226+
$tokenString = null;
227+
228+
// Extract token
225229
switch ($name) {
226230
// Fetch access-token
227231
default:

‎RESTController/libs/RESTResponse.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,15 @@ public function __construct($body = '', $status = 200, $headers = array()) {
5757
* @See \Slim\Http\Response->write(...) for more details
5858
*/
5959
public function write($body, $replace = false) {
60+
// Keep normal mode of operation for HTML/RAW
61+
switch ($this->format) {
62+
case 'HTML':
63+
case 'RAW':
64+
return parent::write($body, $replace);
65+
}
66+
6067
// Merged new body with old content
61-
if ($replace === false) {
68+
if ($replace !== true) {
6269
// Decode old content
6370
$oldBody = $this->decode($this->getBody());
6471

‎api.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
chdir($directory);
2020

2121
// Instantate and run the RESTController application
22-
$restCTL = new \RESTController\RESTController($directory);
22+
$restCTL = new \RESTController\RESTController();
2323
$restCTL->run();
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
2-
$api_key = "apollon"; // API-Key to be used
3-
$api_secret = "S3Tjw0N4t8"; // only needed for grant type "Clients Credentials"
4-
$subFolder = "/dev/ilias"; // need to be specified if your ILIAS installation is not located at the document root
2+
$api_key = "appollon"; // API-Key to be used
3+
$api_secret = ""; // only needed for grant type "Clients Credentials"
4+
$ilias_url = "http://ilias.localhost"; // need to be specified if your ILIAS installation is not located at the document root

‎tools/oauth2demo/endpoints/authcode_endpoint.php

+20-24
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,20 @@
1818
// Exchange OAuth 2 authorization code for bearer token
1919
if (isset($_GET['code'])){
2020
if (isset($_GET['make_curl_call'])) {
21-
// Protocol used for curl call
22-
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') {
23-
$protocol = 'http://';
24-
} else {
25-
$protocol = 'https://';
26-
}
27-
28-
// Redirection URL (but into body)
29-
$redirect_uri = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
30-
if ($_SERVER["SERVER_PORT"] != "80") {
31-
$redirect_uri = $protocol . $_SERVER['SERVER_NAME'] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER['PHP_SELF'];
32-
}
21+
$apiDir = $ilias_url . "/Customizing/global/plugins/Services/UIComponent/UserInterfaceHook/REST";
22+
$tokenUrl = $apiDir . "/api.php/v2/oauth2/token";
3323

3424
// Build the body for curl call
3525
$post = array(
3626
'grant_type' => 'authorization_code',
3727
'code' => $_GET['code'],
3828
'api_key' => $api_key,
3929
'api_secret' => $api_secret,
40-
'redirect_uri' => $redirect_uri
30+
'redirect_uri' => $_SERVER['PHP_SELF']
4131
);
4232

43-
// Endpoint (url) used for curl call
44-
$url = $subFolder. "/v2/oauth2/token";
45-
4633
//
47-
$ch = curl_init($url);
34+
$ch = curl_init($tokenUrl);
4835
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
4936
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
5037
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -61,18 +48,27 @@
6148
// Convert to array
6249
$decoded = json_decode($body, true);
6350

64-
?>
65-
<h3>OAuth2 Token via Authorization Code Workflow Retrieved!</h3>
66-
<pre>Access-Token: <?php echo (isset($decoded["access_token"])) ? $decoded["access_token"] : "[ No Data ]"; ?></pre>
67-
<pre>Refresh-Token: <?php echo (isset($decoded["refresh_token"])) ? $decoded["refresh_token"] : "[ No Data ]"; ?></pre>
68-
<h4> The client can continue now making further API requests with the obtained bearer token.</h4>
69-
<?php
51+
if (isset($decoded["access_token"])) {
52+
?>
53+
<h3>OAuth2 Token via Authorization Code Workflow Retrieved!</h3>
54+
<pre>Access-Token: <?php echo (isset($decoded["access_token"])) ? $decoded["access_token"] : "[ No Data ]"; ?></pre>
55+
<pre>Refresh-Token: <?php echo (isset($decoded["refresh_token"])) ? $decoded["refresh_token"] : "[ No Data ]"; ?></pre>
56+
<h4> The client can continue now making further API requests with the obtained bearer token.</h4>
57+
<?php
58+
}
59+
else {
60+
?>
61+
<h3>Error when requesting OAuth2 Token:</h3>
62+
<pre><?php var_dump($body); ?></pre>
63+
<?php
64+
}
7065
}
7166
else {
67+
$call = $_SERVER['REQUEST_URI'] . '&make_curl_call=1';
7268
?>
7369
<h3>The Server has authenticated your request and generated an authentication code that can be traded for a bearer token.</h3>
7470
<pre>Authorization Code: <?php echo $_GET['code']; ?></pre>
75-
<a href='<?php echo $_SERVER['REQUEST_URI']; ?>&make_curl_call=1'>Trade authentication code for bearer token</a><br><br>
71+
<a href='<?php echo $call; ?>'>Trade authentication code for bearer token</a><br><br>
7672
<?php
7773
}
7874
}

‎tools/oauth2demo/index.php

+17-20
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,18 @@
99
// Include settings
1010
require_once('config.ini.php');
1111

12-
// Generate GET/POST URLs
13-
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') {
14-
$protocol = 'http://';
15-
} else {
16-
$protocol = 'https://';
17-
}
18-
$base_url = $protocol . $_SERVER['SERVER_NAME'] . dirname($_SERVER['PHP_SELF']);
19-
if ($_SERVER["SERVER_PORT"] != "80") {
20-
$base_url = $protocol . $_SERVER['SERVER_NAME'] . ":" . $_SERVER["SERVER_PORT"] . dirname($_SERVER['PHP_SELF']);
21-
}
22-
$loginUrl = $subFolder. "/v2/oauth2/authorize?api_key=".urlencode($api_key);
12+
$self = dirname($_SERVER['PHP_SELF']);
2313

24-
// This will be the redirect targets for generating bearer tokens via GET (POST contains this info in the header)
25-
$authGrantUrl = $loginUrl."&redirect_uri=".urlencode($base_url."/endpoints/authcode_endpoint.php")."&response_type=code";
26-
$implicitGrantUrl = $loginUrl."&redirect_uri=".urlencode($base_url."/endpoints/implicitgrant_endpoint.php")."&response_type=token";
14+
$apiDir = $ilias_url . "/Customizing/global/plugins/Services/UIComponent/UserInterfaceHook/REST";
15+
$authUrl = $apiDir . "/api.php/v2/oauth2/authorize";
16+
$tokenUrl = $apiDir . "/api.php/v2/oauth2/token";
17+
18+
$authGrantRedirect = $self . "/endpoints/authcode_endpoint.php";
19+
$implicitGrantRedirect = $self . "/endpoints/implicitgrant_endpoint.php";
20+
21+
$loginUrl = $authUrl . "?api_key=" . urlencode($api_key);
22+
$authGrantUrl = $loginUrl . "&response_type=code&redirect_uri=" . urlencode($authGrantRedirect);
23+
$implicitGrantUrl = $loginUrl . "&response_type=token&redirect_uri=" . urlencode($implicitGrantRedirect);
2724
?>
2825
<h3>Initiating one of the following OAuth2 Grant Mechanism via a GET Request:</h3>
2926
<ul>
@@ -33,23 +30,23 @@
3330
<h3>Initiating one of the following OAuth2 Grant Mechanism via a POST Request:</h3>
3431
<ul>
3532
<li>
36-
<form method="POST" action="<?php echo $subFolder;?>/v2/oauth2/authorize">
33+
<form method="POST" action="<?php echo $authUrl; ?>">
3734
<input type="hidden" name="api_key" value="<?php echo $api_key; ?>" />
3835
<input type="hidden" name="response_type" value="code" />
39-
<input type="hidden" name="redirect_uri" value="<?php echo $base_url."/endpoints/authcode_endpoint.php";?>" />
36+
<input type="hidden" name="redirect_uri" value="<?php echo $authGrantRedirect; ?>" />
4037
<input type="submit" value="Authorization Code Grant" />
4138
</form>
4239
</li>
4340
<li>
44-
<form method="POST" action="<?php echo $subFolder;?>/v2/oauth2/authorize">
41+
<form method="POST" action="<?php echo $authUrl; ?>">
4542
<input type="hidden" name="api_key" value="<?php echo $api_key; ?>" />
4643
<input type="hidden" name="response_type" value="token" />
47-
<input type="hidden" name="redirect_uri" value="<?php echo $base_url."/endpoints/implicitgrant_endpoint.php"; ?>" />
44+
<input type="hidden" name="redirect_uri" value="<?php echo $implicitGrantRedirect; ?>" />
4845
<input type="submit" value="Implicit Grant" />
4946
</form>
5047
</li>
5148
<li>
52-
<form method="POST" action="<?php echo $subFolder;?>/v2/oauth2/token">
49+
<form method="POST" action="<?php echo $tokenUrl; ?>">
5350
<input type="hidden" name="grant_type" value="client_credentials" />
5451
<input type="hidden" name="scope" value="" />
5552
<input type="hidden" name="api_key" value="<?php echo $api_key; ?>" />
@@ -58,7 +55,7 @@
5855
</form>
5956
</li>
6057
<li>
61-
<form method="POST" action="<?php echo $subFolder;?>/v2/oauth2/token">
58+
<form method="POST" action="<?php echo $tokenUrl;?>">
6259
<div>
6360
<input type="hidden" name="grant_type" value="password" />
6461
<input type="hidden" name="scope" value="" />

0 commit comments

Comments
 (0)
Please sign in to comment.