-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathb2AuthorizeCfWorker.py
96 lines (73 loc) · 3.05 KB
/
b2AuthorizeCfWorker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import requests
import base64
import json
flagDebug = True
bucketSourceId = 'cdb0bd378798e11f6427041b'
bucketFilenamePrefix = ''
# for b64 encoding.
b2AppKey = b'K000uBzMpPUsL0zM32R9MEgpU9yT4IoQ'
b2AppKeyId = b'000d0da781f4e4b0000000033'
# Cloudflare settings
cfAccountId = '379de8739ad820309deed3244553423534532' # Your Cloudflare Account ID
cfWorkerApi = 'c641673a3ae68de751172aab8805a3579eca6' # The API key to modify the below worker
cfWorkerName = 'b2cdn' # worker script name
# An authorization token is valid for not more than 1 week
# This sets it to the maximum time value
maxSecondsAuthValid = 7*24*60*60 # one week in seconds
# DO NOT CHANGE ANYTHING BELOW THIS LINE ###
baseAuthorizationUrl = 'https://api.backblazeb2.com/b2api/v2/b2_authorize_account'
b2GetDownloadAuthApi = '/b2api/v2/b2_get_download_authorization'
# Get fundamental authorization code
idAndKey = b2AppKeyId + b':' + b2AppKey
b2AuthKeyAndId = base64.b64encode(idAndKey)
basicAuthString = 'Basic ' + b2AuthKeyAndId.decode('UTF-8')
authorizationHeaders = {'Authorization' : basicAuthString}
resp = requests.get(baseAuthorizationUrl, headers=authorizationHeaders)
if flagDebug:
print (resp.status_code)
print (resp.headers)
print (resp.content)
respData = json.loads(resp.content.decode("UTF-8"))
bAuToken = respData["authorizationToken"]
bFileDownloadUrl = respData["downloadUrl"]
bPartSize = respData["recommendedPartSize"]
bApiUrl = respData["apiUrl"]
# Get specific download authorization
getDownloadAuthorizationUrl = bApiUrl + b2GetDownloadAuthApi
downloadAuthorizationHeaders = { 'Authorization' : bAuToken}
resp2 = requests.post(getDownloadAuthorizationUrl,
json = {'bucketId' : bucketSourceId,
'fileNamePrefix' : "",
'validDurationInSeconds' : maxSecondsAuthValid },
headers=downloadAuthorizationHeaders )
resp2Content = resp2.content.decode("UTF-8")
resp2Data = json.loads(resp2Content)
bDownAuToken = resp2Data["authorizationToken"]
if flagDebug:
print("authorizationToken: " + bDownAuToken)
print("downloadUrl: " + bFileDownloadUrl)
print("recommendedPartSize: " + str(bPartSize))
print("apiUrl: " + bApiUrl)
workerTemplate = """addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
let authToken='<B2_DOWNLOAD_TOKEN>'
let b2Headers = new Headers(request.headers)
b2Headers.append("Authorization", authToken)
modRequest = new Request(request.url, {
method: request.method,
headers: b2Headers
})
const response = await fetch(modRequest)
return response
}"""
workerCode = workerTemplate.replace('<B2_DOWNLOAD_TOKEN>', bDownAuToken)
cfHeaders = { 'Authorization' : "Bearer " + cfWorkerApi,
'Content-Type' : 'application/javascript' }
cfUrl = 'https://api.cloudflare.com/client/v4/accounts/' + cfAccountId + "/workers/scripts/" + cfWorkerName
resp = requests.put(cfUrl, headers=cfHeaders, data=workerCode)
if flagDebug:
print(resp)
print(resp.headers)
print(resp.content)