Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Undefined index with the session token using app token #64

Closed
C-Duv opened this issue Apr 3, 2018 · 3 comments · Fixed by #67
Closed

Undefined index with the session token using app token #64

C-Duv opened this issue Apr 3, 2018 · 3 comments · Fixed by #67
Assignees
Labels
Milestone

Comments

@C-Duv
Copy link
Contributor

C-Duv commented Apr 3, 2018

I get PHP notice about an undefined Session-Token index when authenticating on the API when setting an application token (using setAppToken()).

require_once __DIR__ . '/vendor/autoload.php';
$client = new Glpi\Api\Rest\Client(
    'http://localhost/glpi/apirest.php/',
    new GuzzleHttp\Client()
);
$client->setAppToken('abcdef....');
$client->initSessionByCredentials(
    'glpi',
    'glpi'
);
var_dump($client->getFullSession()['statusCode']);
$client->killSession();

Observed Results:

PHP Notice:  Undefined index: Session-Token in .../myexample/vendor/glpi-project/php-library-glpi/src/Glpi/Api/Rest/Client.php on line 137
PHP Stack trace:
PHP   1. {main}() .../myexample/test.php:0
PHP   2. Glpi\Api\Rest\Client->initSessionByCredentials() .../myexample/test.php:10
PHP   3. Glpi\Api\Rest\Client->request() .../myexample/vendor/glpi-project/php-library-glpi/src/Glpi/Api/Rest/Client.php:81
int(200)

Expected behavior:

There should be not "PHP Notice", only:

int(200)

If I disable the app_token filtering on GLPI server side and comment the `setAppToken()`` call, the PHP notice disappears.

I tried various settings and got the following:

Application token filtering on server side Calling setAppToken() Result
No No Works OK without notice/errors
No Yes PHP Notice: Undefined index: Session-Token (in Client.php:137) and PHP Notice: Undefined index: ERROR_WRONG_APP_TOKEN_PARAMETER (in ErrorHandler.php:69) and PHP Fatal error: Uncaught exception 'Exception' (in Client.php:8)
Yes No Works OK, it rejects the client because no token was provided (PHP Fatal error: Uncaught exception 'Exception' with message 'The current API requires an App-Token header for using its methods.')
Yes Yes PHP Notice: Undefined index: Session-Token
@C-Duv
Copy link
Contributor Author

C-Duv commented Apr 3, 2018

It looks like \Glpi\Api\Rest\Client::request() assumes \Glpi\Api\Rest\Client::addTokens() always returns a Session-Token, which is false.

C-Duv added a commit to C-Duv/php-library-glpi that referenced this issue Apr 3, 2018
`request()` assumed `addTokens()` always returned a Session-Token, which is false and caused *PHP Notice: Undefined index: Session-Token*.

This commit makes `request()` check the `Session-Token` does exists in what `addTokens()` returned before using it.

Issue: glpi-project#64
@DIOHz0r
Copy link
Contributor

DIOHz0r commented Apr 3, 2018

The addTokens method is to add automaticly Session-Token wich it is always mandatory for call all GLPI Endpoints (excluding only for lostPassword and initSession). If Session-Token is not present then the problem was with a wrong init session.

@C-Duv
Copy link
Contributor Author

C-Duv commented Apr 4, 2018

So you are telling me to close this issue (#64) and the #67 PR?
I understand the PHP notices are generated because of the #65 bug on initSessionByUserToken() but shouldn't request() check what addTokens() returned anyway?

DIOHz0r pushed a commit to C-Duv/php-library-glpi that referenced this issue Apr 24, 2018
`request()` assumed `addTokens()` always returned a Session-Token, which is false and caused *PHP Notice: Undefined index: Session-Token*.

This commit makes `request()` check the `Session-Token` does exists in what `addTokens()` returned before using it.

Issue: glpi-project#64
@ajsb85 ajsb85 changed the title Getting "PHP Notice: Undefined index: Session-Token" when using application token Undefined index with the session token using app token Apr 25, 2018
@ajsb85 ajsb85 added the bug label Apr 25, 2018
@ajsb85 ajsb85 added this to the 1.0 milestone Apr 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants