本文整理汇总了PHP中Api::generateToken方法的典型用法代码示例。如果您正苦于以下问题:PHP Api::generateToken方法的具体用法?PHP Api::generateToken怎么用?PHP Api::generateToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Api
的用法示例。
在下文中一共展示了Api::generateToken方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testcheckAuthWithInvalidSubAttribute
/**
* @covers Api::checkAuth
* @depends testGenerateToken
* @runInSeparateProcess
*/
public function testcheckAuthWithInvalidSubAttribute()
{
//set method for use in CLI
$_SERVER['REQUEST_METHOD'] = 'GET';
//create valid token
require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/User.php';
$user = new User(1);
$userProfile = $user->getProfile();
unset($userProfile->sub);
$token = $this->object->generateToken($userProfile);
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer ' . $token->token;
$this->object = new Api('json', array('GET'));
ob_start();
$this->assertFalse($this->object->checkAuth());
$output = ob_get_contents();
ob_end_clean();
$this->assertEquals('{"code":401,"message":"Subject not found"}', $output, 'Output should be a json string but found: ' . $output);
}
示例2: Api
<?php
/**
* Authenticate user and create a token.
*
* Provides a token required for others API call
*
* @version 1.0.0
*
* @api
*/
require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/Api.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/server/lib/User.php';
$api = new Api('json', ['POST']);
switch ($api->method) {
case 'POST':
if (!$api->checkParameterExists('login', $login) || !$api->checkParameterExists('password', $password)) {
$api->output(400, 'Both login and password must be provided');
//login or password was not provided
return;
}
$user = new User();
if (!$user->checkCredentials($login, $password)) {
$api->output(401, 'Invalid credentials');
header('WWW-Authenticate: Bearer realm="WMP"');
//invalid credentials
return;
}
$api->output(201, $api->generateToken($user->getProfile()));
break;
}