-
Notifications
You must be signed in to change notification settings - Fork 32
/
auth
executable file
·88 lines (78 loc) · 3.31 KB
/
auth
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
#!/usr/bin/env python3
# BSD 2-Clause License
#
# Copyright (c) 2017, Tim Sammut with modifications by Ilker Temir
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import base64
import cgi
from http import cookies as Cookie
import json
import onetimepass
import os
import re
import settings
import sys
import time
import urllib
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
STATE_DIR = os.path.join(BASE_DIR, 'state')
TEMPLATE_FILE = os.path.join(BASE_DIR, 'template.html')
TOKEN_FILE = os.path.join(BASE_DIR, 'tokens.json')
fields = cgi.FieldStorage()
token_user = fields.getvalue('p', None)
url = os.environ.get('QUERY_STRING', '/')
with open(TOKEN_FILE, 'r') as f:
secret = json.load(f)
user = os.environ.get('REMOTE_USER', None)
user_secret = secret.get(user, None)
if user is None or user_secret is None:
print("Content-type: text/html\r\n\r\n")
print("Contact your administrator to obtain your 2FA secret.\n")
sys.exit()
if onetimepass.valid_totp(token=token_user, secret=user_secret, window=1):
cookie = Cookie.SimpleCookie()
# Create a random key and store it in session cookie
random = os.urandom(80)
key = re.sub(r'[=+/]+', '', base64.b64encode(random).decode("ascii"))
cookie['2FA_Auth'] = key
cookie['2FA_Auth']['path'] = '/'
cookie['2FA_Auth']['expires'] = settings.EXPIRATION_TIME
# You need to comment out following two lines if you are using http
cookie['2FA_Auth']['secure'] = True
cookie['2FA_Auth']['httponly'] = True
# Create a local state file with the key value, it will be checked
# by Apache mod_rewrite
state_file = os.path.join(STATE_DIR, key)
with open(state_file, 'w+') as state:
# Following write is optional, used for logging
state.write('%s\n%s\n%s\n' % (os.environ.get('HTTP_USER_AGENT', ''),
os.environ.get('REMOTE_ADDR', ''),
os.environ.get('REMOTE_USER', '')))
print("Status: 302 Moved")
print("Location: " + urllib.parse.unquote(url))
print(cookie)
print()
else:
print("Content-type: text/html\r\n\r\n")
print(open(TEMPLATE_FILE).read())