Skip to content

Commit 175c75f

Browse files
committed
auth认证模块重构
1 parent 5f395ba commit 175c75f

3 files changed

Lines changed: 33 additions & 46 deletions

File tree

dueros/auth.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import tornado.httpserver
32
import tornado.ioloop
43
import tornado.web
@@ -13,19 +12,17 @@
1312

1413

1514
class MainHandler(tornado.web.RequestHandler):
16-
def initialize(self, config, output):
17-
self.config = dueros.config.load(configfile=config)
15+
def initialize(self, output):
16+
self.config = dueros.config.load()
1817
self.output = output
1918

20-
if ('host_url' in self.config) and self.config['host_url'] == 'dueros-h2.baidu.com':
21-
self.token_url = 'https://openapi.baidu.com/oauth/2.0/token'
22-
self.oauth_url = 'https://openapi.baidu.com/oauth/2.0/authorize'
23-
self.scope = 'basic'
19+
self.token_url = 'https://openapi.baidu.com/oauth/2.0/token'
20+
self.oauth_url = 'https://openapi.baidu.com/oauth/2.0/authorize'
2421

2522
@tornado.web.asynchronous
2623
def get(self):
2724
redirect_uri = self.request.protocol + "://" + self.request.host + "/authresponse"
28-
print '================request.path=',self.request.path
25+
print '================request.path=', self.request.path
2926
if self.request.path == '/authresponse':
3027
code = self.get_argument("code")
3128
payload = {
@@ -56,40 +53,34 @@ def get(self):
5653
else:
5754
payload = {
5855
"client_id": self.config['client_id'],
59-
"scope": self.scope,
60-
# "scope_data": scope_data,
56+
"scope": 'basic',
6157
"response_type": "code",
6258
"redirect_uri": redirect_uri
6359
}
6460

65-
6661
req = requests.Request('GET', self.oauth_url, params=payload)
6762
p = req.prepare()
68-
print '============redirect url=',p.url
63+
print '============redirect url=', p.url
6964
self.redirect(p.url)
7065

7166

72-
def login(config, output):
73-
application = tornado.web.Application([(r".*", MainHandler, dict(config=config, output=output))])
67+
def login():
68+
application = tornado.web.Application([(r".*", MainHandler, dict(output=dueros.config.DEFAULT_CONFIG_FILE))])
7469
http_server = tornado.httpserver.HTTPServer(application)
7570
http_server.listen(3000)
7671
tornado.ioloop.IOLoop.instance().start()
7772
tornado.ioloop.IOLoop.instance().close()
7873

79-
80-
@click.command()
81-
@click.option('--config', '-c', help='configuration json file with product_id, client_id and client_secret')
82-
@click.option('--output', '-o', default=dueros.config.DEFAULT_CONFIG_FILE, help='output json file with refresh token')
83-
def main(config, output):
74+
def main():
8475
try:
8576
import webbrowser
8677
except ImportError:
8778
print('Go to http://{your device IP}:3000 to start')
88-
login(config, output)
79+
login()
8980
return
9081

9182
import threading
92-
webserver = threading.Thread(target=login, args=(config, output))
83+
webserver = threading.Thread(target=login)
9384
webserver.daemon = True
9485
webserver.start()
9586
print("A web page should is opened. If not, go to http://127.0.0.1:3000 to start")
@@ -104,4 +95,3 @@ def main(config, output):
10495

10596
if __name__ == '__main__':
10697
main()
107-

dueros/config.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,27 @@
22

33
import json
44
import os
5-
import sys
65
import uuid
76

7+
# 配置文件保存位置
88
DEFAULT_CONFIG_FILE = os.path.join(os.path.expanduser('~'), '.dueros.json')
99

10+
# 开发者注册信息
11+
client_id = "5GFgMRfHOhIvI0B8AZB78nt676FeWA9n"
12+
client_secret = "eq2eCNfbtOrGwdlA4vB1N1EaiwjBMu7i"
1013

11-
def load(configfile=None):
12-
if configfile is None:
13-
if os.path.isfile(DEFAULT_CONFIG_FILE):
14-
configfile = DEFAULT_CONFIG_FILE
15-
else:
16-
product_id = "EddyLiu-" + uuid.uuid4().hex
17-
# return {
18-
# "dueros-device-id": product_id,
19-
# "host_url": "dueros-h2.baidu.com",
20-
# "client_id": "5GFgMRfHOhIvI0B8AZB78nt676FeWA9n",
21-
# "client_secret": "eq2eCNfbtOrGwdlA4vB1N1EaiwjBMu7i"
22-
# }
23-
24-
return {
25-
"dueros-device-id": product_id,
26-
"host_url": "dueros-h2.baidu.com",
27-
"client_id": "5GFgMRfHOhIvI0B8AZB78nt676FeWA9n",
28-
"client_secret": "eq2eCNfbtOrGwdlA4vB1N1EaiwjBMu7i"
29-
}
14+
15+
def load():
16+
if os.path.isfile(DEFAULT_CONFIG_FILE):
17+
configfile = DEFAULT_CONFIG_FILE
18+
else:
19+
product_id = "EddyLiu-" + uuid.uuid4().hex
20+
21+
return {
22+
"dueros-device-id": product_id,
23+
"client_id": client_id,
24+
"client_secret": client_secret
25+
}
3026

3127
with open(configfile, 'r') as f:
3228
config = json.load(f)

dueros/dueros_core.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ def __init__(self, config=None):
7272
self.requests = requests.Session()
7373

7474
self._configfile = config
75-
self._config = dueros.config.load(configfile=config)
75+
self._config = dueros.config.load()
7676

77-
if self._config['host_url'] == 'dueros-h2.baidu.com':
78-
self._config['api'] = 'dcs/v1'
79-
self._config['refresh_url'] = 'https://openapi.baidu.com/oauth/2.0/token'
77+
self._config['host_url'] = 'dueros-h2.baidu.com'
78+
79+
self._config['api'] = 'dcs/v1'
80+
self._config['refresh_url'] = 'https://openapi.baidu.com/oauth/2.0/token'
8081

8182
self.last_activity = datetime.datetime.utcnow()
8283
self._ping_time = None

0 commit comments

Comments
 (0)