使用 PubNub JS SDK 隐藏您的 API 密钥
使用 PubNub 访问管理器检查最新文档 - https://www.pubnub.com/docs/security/access-control
使用PubNub 访问管理器,您不再需要担心在 JavaScript 或任何其他语言中隐藏您的publish_key
和subscribe_key
源代码!通常,您会认为隐藏您的密钥成为阻止访问 PubNub 频道上的数据流的一种手段。但是,这不是必需的,可以使用最佳实践方法:以下是管理访问的新方法和管理密钥的新方法的解决方案。
PubNub 访问管理器示例 JS/PHP 授予撤销 SDK
您可以在 PubNub 全球实时网络上实时发布每个用户的连接grant()
和访问。revoke()
PubNub 网络中的各种安全级别使用授予/撤销(白名单)权限方案,其中在层次结构中找到的第一个授予授予读/写访问权限。基于此层次结构评估发布和订阅的权限。我们的pam.php
PubNub 访问管理器 PHP 类终于准备好了!您可以通过查看下面的示例使用代码开始,其中包含 SDK 的完整代码覆盖率。您可以通过 GitHub Gist 链接找到所有源代码:
用于授予和撤销访问权限的 PubNub 访问管理器 (PAM) PHP 完整库

包括 PAM 和初始化类访问
require('pam.php');
$manager = new access(
"pub-c-e132b7b4-0c2c-4d36-a828-1de1ea50d167",
"sub-c-f95db694-6ff9-11e3-9291-02ee2ddab7fe",
"sec-c-OWFkNWQ1NDctN2JiNy00NzJmLTk3Y2ItN2ExODZlYzkyNzY0"
);
授予用户访问权限
authkey
使用of gZW5jb2RlZCBmaWx
withread
和write
access for 5
minute授予用户访问权限ttl
。 你可以做authkey
任何你想做的事!
print_r($manager->grant(
"my_channel", // CHANNEL
"gZW5jb2RlZCBmaWx", // STRING (AUTH KEY)
true, // READ
true, // WRITE
5 // TTL in MINUTES
));
授予用户在线访问权限
还授予对在线通道的访问权限(PubNub 开发控制台需要)。
print_r($manager->grant(
"my_channel-pnpres", // CHANNEL
"gZW5jb2RlZCBmaWx", // STRING (AUTH KEY)
true, // READ
true, // WRITE
5 // TTL in MINUTES
));
授予全球访问权限(对所有用户)
排除authkey
并且您可以全局授予对所有人的访问权限。
print_r($manager->grant_global(
"my_channel", // CHANNEL
true, // READ
true, // WRITE
5 // TTL in MINUTES
));
永远授予访问权限
ttl
您可以通过将参数设置为 来永久授予访问权限0
。
print_r($manager->grant_global(
"my_channel", // CHANNEL
true, // READ
true, // WRITE
0 // FOREVER GRANT!!!
));
撤销用户访问
立即撤销对用户的访问权限。
print_r($manager->revoke(
"some-other-channel", // CHANNEL
"gZW5jb2RlZCBmaWx" // STRING (AUTH KEY)
));
撤销全局访问
您还可以通过排除authkey
参数来撤销全局访问。
print_r($manager->revoke(
"some-other-channel" // CHANNEL
));
PAM(PubNub 访问管理器)PHP 类 SDKpam.php
完整文件可以在这里找到:PubNub Access Manager (PAM) PHP Full Library for Granting and Revoking Access
<?php
class access {
function __construct( $pubkey, $subkey, $seckey ) {
$this->publish_key = $pubkey;
$this->subscribe_key = $subkey;
$this->secret_key = $seckey;
}
function grant_global( $channel, $read=True, $write=True, $ttl=5 ) {
/** Grant GLOBAL Access on a Channel. **/
return $this->_auth(array(
"channel" => $channel,
"r" => $read ? 1 : 0,
"w" => $write ? 1 : 0,
"ttl" => $ttl
));
}
function grant( $channel, $authkey=False, $read=True, $write=True, $ttl=5 ) {
/** Grant Access on a Channel. **/
return $this->_auth(array(
"channel" => $channel,
"auth" => $authkey,
"r" => $read ? 1 : 0,
"w" => $write ? 1 : 0,
"ttl" => $ttl
));
}
function revoke( $channel, $authkey=False, $read=False, $write=False, $ttl=1 ) {
/** Revoke Access on a Channel.**/
return $this->_auth(array(
"channel" => $channel,
"auth" => $authkey,
"r" => $read ? 1 : 0,
"w" => $write ? 1 : 0,
"ttl" => $ttl
));
}
function _sign($message) {
/** Calculate a signature by secret key and message. **/
return strtr( base64_encode(hash_hmac(
'sha256',
utf8_encode($message),
utf8_encode($this->secret_key),
true
)), '+/', '-_' );
}
function _auth($query) {
/** Issue an authenticated request.**/
if (!array_key_exists( 'timestamp', $query )) {
$query['timestamp'] = time();
}
## Global Grant?
if ((array_key_exists('auth',$query)) && !$query['auth']) {
unset($query['auth']);
}
## Construct String to Sign
$params = array();
$sorted_keys = array_keys($query);
sort($sorted_keys);
foreach ($sorted_keys as $key) array_push(
$params,
$key . "=" . $query[$key]
);
$string_to_sign =
$this->subscribe_key . "\n" .
$this->publish_key . "\n" .
"grant" . "\n" .
implode( "&", $params );
$signature = $this->_sign($string_to_sign);
$url = (
"https://pubsub.pubnub.com/v1/auth/grant/sub-key/" .
$this->subscribe_key . "?" .
implode( "&", $params ) .
"&signature=" . $signature
);
$workspace_curl = curl_init();
curl_setopt( $workspace_curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $workspace_curl, CURLOPT_URL, $url );
$result = curl_exec($workspace_curl);
return $workspace_details =json_decode( $result, true );
}
}
?>
pam.php
: PubNub Access Manager (PAM) PHP 完整库,用于授予和撤销访问权限
PubNub 开发控制台测试链接:
警告:PubNub 开发控制台也需要 Presence 频道的授权! 您可以通过授予-pnpres
频道名称的后缀来设置在线访问。
http://www.pubnub.com/console/?channel=my_channel&sub=sub-c-f95db694-6ff9-11e3-9291-02ee2ddab7fe&pub=pub-c-e132b7b4-0c2c-4d36-a828-1de1ea50d167&sec=sec-c-OWFkNWQ1NDctNctNzky3JiNy00NzlJmItk2JiNy0ExODZlJmItk2