繁体   English   中英

如何使用php-opencloud sdk测试我是否拥有所有正确的凭据?

[英]How can I test if I have all the right credentials with php-opencloud sdk?

现在是我的代码:

$cloud = new Rackspace('https://identity.api.rackspacecloud.com/v2.0/', $php_cloudconfig['credentials']);
$array_creds = getCredentials();
$cloud->ImportCredentials($array_creds);

$array_creds = $cloud->ExportCredentials();
setCredentials($array_creds['authorization_token'], $array_creds['expiration'], $array_creds['tenant_id'], $array_creds['service_catalog']);

function getCredentials() {
    $sql_get_credential = "SELECT * FROM cloud_apiconnection";
    $q = $conn->prepare($sql_get_credential);
    return $q->execute();
}

function setCredentials($authorization_token, $expiration, $tenant_id, $service_catalog)     {
    $sql_insert = "INSERT INTO cloud_apiconnection (authorization_token, expiration, tenant_id, service_catalog) VALUES (:authorization_token, :expiration, :tenant_id, :service_catalog)";
    $q = $conn->prepare($sql_insert);
    $q->execute(array(':authorization_token' => $authorization_token, ':expiration' => $expiration, ':tenant_id' => $tenant_id, ':service_catalog' => $service_catalog));
}

有没有一种方法可以检测凭据是否在以下位置更新:$ cloud-> ImportCredentials($ array_creds);

我在流浪,因为如果不需要,我不想写数据库。

这也是管理我与RackSpace API的连接的最佳策略吗?

维护持久会话似乎是一个不错的策略,因为您正在重新使用现有的令牌ID。 唯一的其他建议是将您的凭据缓存在本地文件中,而不是进行MySQL事务。 您实际上并不需要存储您的租户ID和服务目录,因为可以轻松地通过软件层检索它们。

要检查现有令牌的有效性,我可以这样做:

$connection = new Rackspace(...);

// Import existing credentials
$credentials = getCredentials();
$connection->importCredentials($credentials);

// Authenticate against the API to make sure your token is still valid
$connection->authenticate();

// Compare result
$newCredentials = $connection->exportCredentials();

if ($newCredentials['authorization_token'] != $credentials['authorization_token']) {
   // You know it's been updated, so save new ones
   setCredentials();
}

authenticate()方法所做的只是对Rackspace API执行请求; 并根据结果有效地让您知道现有内容是否仍然有效。 当其他方法调用authenticate() ,它们通常会事先进行另一项检查:它们会检查到期值(即,不是过去的值)。 您可以执行与它们相同的操作(以查找是否需要刷新和保存凭据):

if (time() > ($credentials['expiration'] - RAXSDK_FUDGE)) {
   // They're old, you need to call authenticate()
} else {
   // They seem to be still valid. Sweet!
}

顺便说一下,我们最近更改了此功能,以便exportCredentials()调用authenticate() -这样就意味着您不需要自己调用它。 但是对于您当前的版本,值得保留。

这能回答一切吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM