鉴权代码示例__周边工具_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-阿里云