forked from raptorz/pyfan
/
weblogin.py
117 lines (92 loc) · 3.42 KB
/
weblogin.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
"""
web login
~~~~~~~~~~~~~~~~
web server for user login
:copyright: 20160811 by raptor.zh@gmail.com.
"""
import json
from requests import HTTPError
from flask import Flask, request, redirect, make_response
from restclient.fanfou import Fanfou
from config import reload_config, get_fullname
import logging
logger = logging.getLogger(__name__)
app = Flask(__name__)
def index_template(content):
return u"""<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>web login</title>
</head>
<body>
<div>
{0}
</div>
</body>
</html>""".format(content)
@app.route("/")
def get_():
config = reload_config()
try:
if config.get("ACCESS_TOKEN") and config.get("ACCESS_SECRET"):
api = Fanfou(config['CLIENT_KEY'], config['CLIENT_SECRET'],
config.get('ACCESS_TOKEN'), config.get('ACCESS_SECRET'),
https=config.get('FANFOU_HTTPS', True))
data = api.account.GET_verify_credentials(mode='lite')
return index_template(u"Login ok.<br/>User: {}.<br/><a href='/logout'>Logout</a>.".format(data['screen_name']))
else:
raise HTTPError
except HTTPError:
return index_template(u"<a href='/login'>Login</a>")
@app.route("/login")
def get_login():
config = reload_config()
api = Fanfou(config['CLIENT_KEY'], config['CLIENT_SECRET'],
redirect_uri="http://{host}:{port}/callback".format(host=config['web_addr'],
port=config['web_port']),
https=config.get('FANFOU_HTTPS', True))
url = api.auth.get_request_url()
response = make_response(redirect(url))
response.set_cookie("request_token", api.auth.get_token_str(), path="/")
return response
@app.route("/callback")
def get_callback():
config = reload_config()
request_token = json.loads(request.cookies.get("request_token"))
api = Fanfou(config['CLIENT_KEY'], config['CLIENT_SECRET'],
request_token['access_token'], request_token['access_secret'],
https=config.get('FANFOU_HTTPS', True))
api.auth.get_access_token()
with open(get_fullname("config.json"), "r+") as f:
access_config = json.loads(f.read())
access_config['ACCESS_TOKEN'] = api.auth.token['oauth_token']
access_config['ACCESS_SECRET'] = api.auth.token['oauth_token_secret']
f.seek(0)
f.truncate()
f.write(json.dumps(access_config))
response = make_response(redirect("/"))
response.set_cookie("request_token", "", path="/")
return response
@app.route("/logout")
def get_logout():
with open(get_fullname("config.json"), "r+") as f:
access_config = json.loads(f.read())
access_config['ACCESS_TOKEN'] = ""
access_config['ACCESS_SECRET'] = ""
f.seek(0)
f.truncate()
f.write(json.dumps(access_config))
return redirect("/")
def error_page(error):
return index_template(u"Error: {0}".format(error))
handlers = {}
errors = [400, 401, 403, 404, 500]
[handlers.__setitem__(i, error_page) for i in errors]
app.error_handler = handlers
application = app
if __name__ == "__main__":
config = reload_config()
logging.basicConfig(level=logging.DEBUG if config['debug'] else logging.WARNING)
application.run(host=config['web_addr'], port=config['web_port'], debug=config['debug'], threaded=True)