php回源鉴权,鉴权示例

Python版本

Demo示例如下所示。

说明 如果URL中包含中文,请进行UrlEncode编码。

import re

import time

import hashlib

import datetime

def md5sum(src):

m = hashlib.md5()

m.update(src)

return m.hexdigest()

#鉴权方式A

def a_auth(uri, key, exp):

p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")

if not p:

return None

m = p.match(uri)

scheme, host, path, args = m.groups()

if not scheme: scheme = "http://"

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)

#鉴权方式B

def b_auth(uri, key, exp):

p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")

if not p:

return None

m = p.match(uri)

scheme, host, path, args = m.groups()

if not scheme: scheme = "http://"

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)

#鉴权方式C

def c_auth(uri, key, exp):

p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")

if not p:

return None

m = p.match(uri)

scheme, host, path, args = m.groups()

if not scheme: scheme = "http://"

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 = "http://xc.cdnpe.com/ping?foo=bar" # original uri

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 : %s\nAUTH: %s" %(uri, authuri))

if __name__ == "__main__":

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值