OpenVPN
totpauth.py
Go to the documentation of this file.
1#! /usr/bin/python3
2# Copyright (c) 2021 OpenVPN Inc <sales@openvpn.net>
3# Copyright (c) 2021 Arne Schwabe <arne@rfc2549.org>
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22
23import sys
24import os
25from base64 import standard_b64decode
26
27import pyotp
28
29# Example script demonstrating how to use the auth-pending API in
30# OpenVPN. This script is provided under MIT license to allow easy
31# modification for other purposes.
32#
33# This needs support of crtext support on the client (e.g. OpenVPN for Android)
34# See also the management-notes.txt file for more information about the auth pending
35# protocol
36#
37# To use this script add the following lines in the openvpn config
38
39# client-crresponse /path/to/totpauth.py
40# auth-user-pass-verify /path/to/totpauth.py via-file
41# auth-user-pass-optional
42# auth-gen-token
43
44# Note that this script does NOT verify username/password
45# It is only meant for querying additional 2FA when certificates are
46# used to authenticate
47
48
49# For this demo script we hardcode the TOTP secrets in a simple dictionary.
50secrets = {"Test-Client": "OS6JDNRK2BNUPQVX",
51 "Client-2": "IXWEMP7SK2QWSHTG"}
52
53
54def main():
55 # Get common name and script type from environment
56 script_type = os.environ['script_type']
57 cn = os.environ['common_name']
58
59 if script_type == 'user-pass-verify':
60 # signal text based challenge response
61 if cn in secrets:
62 extra = "CR_TEXT:E,R:Please enter your TOTP code!"
63 write_auth_pending(300, 'crtext', extra)
64
65 # Signal authentication being deferred
66 sys.exit(2)
67 else:
68 # For unknown CN we report failure. Change to 0
69 # to allow CNs without secret to auth without 2FA
70 sys.exit(1)
71
72 elif script_type == 'client-crresponse':
73 response = None
74
75 # Read the crresponse from the argument file
76 # and convert it into text. A failure because of bad user
77 # input (e.g. invalid base64) will make the script throw
78 # an error and make OpenVPN return AUTH_FAILED
79 with open(sys.argv[1], 'r') as crinput:
80 response = crinput.read()
81 response = standard_b64decode(response)
82 response = response.decode().strip()
83
84 if cn not in secrets:
86 return
87
88 totp = pyotp.TOTP(secrets[cn])
89
90 # Check if the code is valid (and also allow code +/-1)
91 if totp.verify(response, valid_window=1):
93 else:
95 else:
96 print(f"Unknown script type {script_type}")
97 sys.exit(1)
98
99
101 with open(os.environ['auth_control_file'], 'w') as auth_control:
102 auth_control.write("%d" % status)
103
104
105def write_auth_pending(timeout, method, extra):
106 with open(os.environ['auth_pending_file'], 'w') as auth_pending:
107 auth_pending.write("%d\n%s\n%s" % (timeout, method, extra))
108
109
110if __name__ == '__main__':
111 main()
write_auth_pending(timeout, method, extra)
Definition totpauth.py:105
write_auth_control(status)
Definition totpauth.py:100