forked from tinode/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
executable file
·125 lines (104 loc) · 3.97 KB
/
auth.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
118
119
120
121
122
123
124
125
#!/usr/bin/python
# Sample Tinode REST/JSON-RPC authentication service.
# See https://github.com/tinode/chat/rest-auth for details.
from flask import Flask, jsonify, make_response, request
import base64
import json
dummy_data = {}
app = Flask(__name__)
def parse_secret(ecoded_secret):
secret = base64.b64decode(ecoded_secret)
return secret.split(':')
@app.route('/')
def index():
return 'Sample Tinode REST/JSON-RPC authentication service. '+\
'See <a href="https://github.com/tinode/chat/rest-auth/">https://github.com/tinode/chat/rest-auth/</a> for details.'
@app.route('/add', methods=['POST'])
def add():
return jsonify({'err': 'unsupported'})
@app.route('/auth', methods=['POST'])
def auth():
if not request.json:
return jsonify({'err': 'malformed'})
uname, password = parse_secret(request.json.get('secret'))
if uname in dummy_data:
if dummy_data[uname]['password'] != password:
# Wrong password
return jsonify({'err': 'failed'})
if 'uid' in dummy_data[uname]:
# We have uname -> uid mapping
return jsonify({
'rec': {
'uid': dummy_data[uname]['uid'],
'authlvl': dummy_data[uname]['authlvl'],
'features': dummy_data[uname]['features']
}
})
else:
# This is the first login. Tell Tinode to create a new account.
return jsonify({
'rec': {
'authlvl': dummy_data[uname]['authlvl'],
'tags': dummy_data[uname]['tags'],
'features': dummy_data[uname]['features']
},
'newacc': {
'auth': dummy_data[uname]['auth'],
'anon': dummy_data[uname]['anon'],
'public': dummy_data[uname]['public'],
'private': dummy_data[uname]['private']
}
})
return jsonify({'err': 'unsupported'})
else:
return jsonify({'err': 'not found'})
@app.route('/checkunique', methods=['POST'])
def checkunique():
return jsonify({'err': 'unsupported'})
@app.route('/del', methods=['POST'])
def xdel():
return jsonify({'err': 'unsupported'})
@app.route('/gen', methods=['POST'])
def gen():
return jsonify({'err': 'unsupported'})
@app.route('/link', methods=['POST'])
def link():
if not request.json:
return jsonify({'err': 'malformed'})
rec = request.json.get('rec', None)
secret = request.json.get('secret', '')
if not rec or not rec['uid'] or not secret:
return jsonify({'err': 'malformed'})
# Save the link account <-> secret to database.
uname, password = parse_secret(secret)
if uname not in dummy_data:
# Unknown user name
return jsonify({'err': 'not found'})
if 'uid' in dummy_data[uname]:
# Already linked
return jsonify({'err': 'duplicate value'})
# Save updated data to file
dummy_data[uname]['uid'] = rec['uid']
with open('dummy_data.json', 'w') as outfile:
json.dump(dummy_data, outfile, indent=2, sort_keys=True)
# Success
return jsonify({})
@app.route('/upd', methods=['POST'])
def upd():
return jsonify({'err': 'unsupported'})
@app.route('/rtagns', methods=['POST'])
def rtags():
# Return dummy namespace "rest" and "email", let client check logins by regular expression.
return jsonify({'strarr': ['rest', 'email'], 'byteval': base64.b64encode('^[a-z0-9_]{3,8}$')})
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'err': 'not found'}), 404)
@app.errorhandler(405)
def not_found(error):
return make_response(jsonify({'err': 'method not allowed'}), 405)
if __name__ == '__main__':
# Load previously saved dummy data. Dummy data contains
# tinode user id <-> user name mapping and data for account creation.
with open('dummy_data.json') as infile:
dummy_data = json.load(infile)
app.run(debug=True)