forked from bargate-project/bargate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotp.py
131 lines (107 loc) · 4.07 KB
/
totp.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
126
127
128
129
130
131
#!/usr/bin/python
#
# This file is part of Bargate.
#
# Bargate is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bargate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bargate. If not, see <http://www.gnu.org/licenses/>.
# pip install onetimepass
# pip install pyqrcode
from bargate import app
import bargate.core
import os
import base64
from redis import Redis
from flask import g, Flask, session, render_template, redirect, url_for, request, flash, abort
import onetimepass
import pyqrcode
import StringIO
def totp_generate_secret_key():
return base64.b32encode(os.urandom(10)).decode('utf-8')
def totp_get_secret_key(userid):
return g.redis.get('totp.%s.key' % userid)
def totp_get_uri(userid):
## check the user has a key, if not generate it.
otp_secret = totp_get_secret_key(userid)
if otp_secret == None:
otp_secret = totp_generate_secret_key()
g.redis.set('totp.%s.key' % userid,otp_secret)
return 'otpauth://totp/{0}?secret={1}&issuer={2}'.format(session['username'], otp_secret, app.config['TOTP_IDENT'])
def totp_verify_token(userid, token):
otp_secret = totp_get_secret_key(userid)
if otp_secret == None:
return False
else:
return onetimepass.valid_totp(token, otp_secret)
def totp_return_qrcode(userid):
url = pyqrcode.create(totp_get_uri(userid))
stream = StringIO.StringIO()
url.svg(stream, scale=5)
return stream.getvalue().encode('utf-8'), 200, {
'Content-Type': 'image/svg+xml',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'}
def totp_user_enabled(userid):
totp_enable = g.redis.get('totp.%s.enabled' % userid)
if totp_enable == None:
return False
else:
return True
################################################################################
@app.route('/totp_qrcode_img')
@bargate.core.login_required
def totp_qrcode_view():
if not totp_user_enabled(session['username']):
return totp_return_qrcode(session['username'])
else:
abort(403)
@app.route('/2step', methods=['GET','POST'])
@bargate.core.login_required
def totp_user_view():
if not totp_user_enabled(session['username']):
if request.method == 'GET':
return render_template('totp_enable.html',active="user")
elif request.method == 'POST':
## verify the token entered
token = request.form['totp_token']
if totp_verify_token(session['username'],token):
flash("Two step logon has been enabled for your account","alert-success")
g.redis.set('totp.%s.enabled' % session['username'],"True")
else:
flash("Invalid code! Two step logons could not be enabled","alert-danger")
return redirect(url_for('totp_user_view'))
else:
if request.method == 'GET':
return render_template('totp_disable.html',active="user")
elif request.method == 'POST':
## verify the token entered
token = request.form['totp_token']
if totp_verify_token(session['username'],token):
g.redis.delete('totp.%s.enabled' % session['username'])
g.redis.delete('totp.%s.key' % session['username'])
flash("Two step logons have been disabled for your account","alert-warning")
else:
flash("Invalid code! Two step logons were not disabled","alert-danger")
return redirect(url_for('totp_user_view'))
@app.route('/verify2step', methods=['GET','POST'])
def totp_logon_view():
if request.method == 'GET':
return render_template('totp_verify.html',active="user")
elif request.method == 'POST':
## verify the token entered
token = request.form['totp_token']
if totp_verify_token(session['username'],token):
return bargate.core.logon_ok()
else:
flash("Invalid two step code!","alert-danger")
return redirect(url_for('totp_logon_view'))