Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathOAuth2Helper.py
More file actions
Latest commit
47 lines (42 loc) · 1.57 KB
/
Copy pathOAuth2Helper.py
File metadata and controls
47 lines (42 loc) · 1.57 KB
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
importrequests
importbase64
importjson
importrandom
importconfig
defget_bearer_token(auth_code):
"""Gets bearer token from authorization code"""
token_endpoint=get_discovery_doc()['token_endpoint']
auth_header='Basic '+to_base64(config.CLIENT_ID+':'+config.CLIENT_SECRET)
headers= {
'Accept': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
'Authorization': auth_header
}
payload= {
'code': auth_code,
'redirect_uri': config.REDIRECT_URI,
'grant_type': 'authorization_code'
}
r=requests.post(token_endpoint, data=payload, headers=headers)
ifr.status_code!=200:
returnr.text
bearer=json.loads(r.text)
returnbearer
defget_discovery_doc():
"""Gets OAuth2 discover document based on configured environment"""
ifconfig.ENVIRONMENT=="Sandbox":
req=requests.get("https://developer.intuit.com/.well-known/openid_sandbox_configuration/")
else:
req=requests.get("https://developer.intuit.com/.well-known/openid_configuration/")
ifreq.status_code>=400:
return''
discovery_doc=req.json()
returndiscovery_doc
defto_base64(s):
"""String to Base64"""
returnbase64.b64encode(bytes(s, 'utf-8')).decode()
defrandom_string(length, allowed_chars='abcdefghijklmnopqrstuvwxyz''ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
return''.join(random.choice(allowed_chars) foriinrange(length))
defsecret_key():
chars='abcdefghijklmnopqrstuvwxyz0123456789'
returnrandom_string(40, chars)