840
財經資訊
鑒權代碼示例__周邊工具_CDN-阿裏雲
概述
URL鑒權規則請查閱 URL鑒權文檔,通過這個 demo 您可以根據業務需要,方便的對URL進行鑒權處理。以下Python Demo包含三種鑒權方式:A鑒權方式、B鑒權方式、C鑒權方式,分別描述了三種不同鑒權方式的請求URL構成、哈希字符串構成等內容
Python版本
import re
import time
import hashlib
import datetime
def md5sum(src):
m = hashlib.md5()
m.update(src)
return m.hexdigest()
def a_auth(uri, key, exp):
p = re.compile("^(https://|https://)?([^/?]+)(/[^?]*)?(\?.*)?$")
if not p:
return None
m = p.match(uri)
scheme, host, path, args = m.groups()
if not scheme: scheme = "https://"
if not path: path = "/"
if not args: args = ""
rand = "0" # "0" by default, other value is ok
uid = "0" # "0" by default, other value is ok
sstring = "%s-%s-%s-%s-%s" %(path, exp, rand, uid, key)
hashvalue = md5sum(sstring)
auth_key = "%s-%s-%s-%s" %(exp, rand, uid, hashvalue)
if args:
return "%s%s%s%s&auth_key=%s" %(scheme, host, path, args, auth_key)
else:
return "%s%s%s%s?auth_key=%s" %(scheme, host, path, args, auth_key)
def b_auth(uri, key, exp):
p = re.compile("^(https://|https://)?([^/?]+)(/[^?]*)?(\?.*)?$")
if not p:
return None
m = p.match(uri)
scheme, host, path, args = m.groups()
if not scheme: scheme = "https://"
if not path: path = "/"
if not args: args = ""
# convert unix timestamp to "YYmmDDHHMM" format
nexp = datetime.datetime.fromtimestamp(exp).strftime('%Y%m%d%H%M')
sstring = key + nexp + path
hashvalue = md5sum(sstring)
return "%s%s/%s/%s%s%s" %(scheme, host, nexp, hashvalue, path, args)
def c_auth(uri, key, exp):
p = re.compile("^(https://|https://)?([^/?]+)(/[^?]*)?(\?.*)?$")
if not p:
return None
m = p.match(uri)
scheme, host, path, args = m.groups()
if not scheme: scheme = "https://"
if not path: path = "/"
if not args: args = ""
hexexp = "%x" %exp
sstring = key + path + hexexp
hashvalue = md5sum(sstring)
return "%s%s/%s/%s%s%s" %(scheme, host, hashvalue, hexexp, path, args)
def main():
uri = "https://xc.cdnpe.com/ping?foo=bar" # original uri
key = "<input private key>" # private key of authorization
exp = int(time.time()) + 1 * 3600 # expiration time: 1 hour after current itme
authuri = a_auth(uri, key, exp) # auth type: a_auth / b_auth / c_auth
print("URL : %snAUTH: %s" %(uri, authuri))
if __name__ == "__main__":
main()
最後更新:2016-12-19 10:00:09
上一篇:
日誌合並工具__周邊工具_CDN-阿裏雲
下一篇:
CDN服務如何開啟GZIP壓縮功能___產品使用問題_CDN-阿裏雲
查詢所有地域下未關聯任何實例的安全組 ID__腳本使用示例_用戶指南_命令行工具 CLI-阿裏雲
SDK接口簡介__數據訂閱_用戶指南_數據傳輸-阿裏雲
隊列使用手冊__Java SDK_SDK使用手冊_消息服務-阿裏雲
產品名詞解釋__產品簡介_批量計算-阿裏雲
雲服務器 ECS 安全組 應用案例
CDN加速域名不可訪問或者慢定位步驟__運維技術分享_技術運維問題_CDN-阿裏雲
錄製工具__腳本編寫_使用手冊_性能測試-阿裏雲
移動數據分析服務協議__產品簡介_移動數據分析-阿裏雲
ClearAccountAlias__安全設置接口_RAM API文檔_訪問控製-阿裏雲
查詢API綁定的密鑰__後端簽名密鑰相關接口_API_API 網關-阿裏雲
相關內容
常見錯誤說明__附錄_大數據計算服務-阿裏雲
發送短信接口__API使用手冊_短信服務-阿裏雲
接口文檔__Android_安全組件教程_移動安全-阿裏雲
運營商錯誤碼(聯通)__常見問題_短信服務-阿裏雲
設置短信模板__使用手冊_短信服務-阿裏雲
OSS 權限問題及排查__常見錯誤及排除_最佳實踐_對象存儲 OSS-阿裏雲
消息通知__操作指南_批量計算-阿裏雲
設備端快速接入(MQTT)__快速開始_阿裏雲物聯網套件-阿裏雲
查詢API調用流量數據__API管理相關接口_API_API 網關-阿裏雲
使用STS訪問__JavaScript-SDK_SDK 參考_對象存儲 OSS-阿裏雲