-
Notifications
You must be signed in to change notification settings - Fork 0
/
hassio_oauth.py
executable file
·79 lines (64 loc) · 2.58 KB
/
hassio_oauth.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
"""Run small webservice for oath."""
import json
import sys
from pathlib import Path
import cherrypy
from requests_oauthlib import OAuth2Session
from google.oauth2.credentials import Credentials
class oauth2Site(object):
"""Website for handling oauth2."""
def __init__(self, user_data, cred_file):
"""Init webpage."""
self.cred_file = cred_file
self.user_data = user_data
self.oauth2 = OAuth2Session(
self.user_data['client_id'],
redirect_uri='urn:ietf:wg:oauth:2.0:oob',
scope="https://www.googleapis.com/auth/assistant-sdk-prototype"
)
self.auth_url, _ = self.oauth2.authorization_url(self.user_data['auth_uri'], access_type='offline', prompt='consent')
@cherrypy.expose
def index(self):
"""Landingpage."""
return str("""<html>
<head></head>
<body>
<p>
Get token from google: <a href="{url}" target="_blank">Authentication</a>
</p>
<form method="get" action="token">
<input type="text" value="" name="token" />
<button type="submit">Connect</button>
</form>
</body>
</html>""").format(url=self.auth_url)
@cherrypy.expose
def token(self, token):
"""Read access token and process it."""
self.oauth2.fetch_token(self.user_data['token_uri'], client_secret=self.user_data['client_secret'], code=token)
# create credentials
credentials = Credentials(
self.oauth2.token['access_token'],
refresh_token=self.oauth2.token.get('refresh_token'),
token_uri=self.user_data['token_uri'],
client_id=self.user_data['client_id'],
client_secret=self.user_data['client_secret'],
scopes=self.oauth2.scope
)
# write credentials json file
with self.cred_file.open('w') as json_file:
json_file.write(json.dumps({
'refresh_token': credentials.refresh_token,
'token_uri': credentials.token_uri,
'client_id': credentials.client_id,
'client_secret': credentials.client_secret,
'scopes': credentials.scopes,
}))
sys.exit(0)
if __name__ == '__main__':
oauth_json = Path(sys.argv[1])
cred_json = Path(sys.argv[2])
with oauth_json.open('r') as data:
user_data = json.load(data)['installed']
cherrypy.config.update({'server.socket_port': 9324, 'server.socket_host': '0.0.0.0'})
cherrypy.quickstart(oauth2Site(user_data, cred_json))