summaryrefslogtreecommitdiffstats
path: root/python/webiopi-passwd.py
blob: 7c0fbff2a5e3ee47b92b326260accb6443ebb796 (plain)
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
#!/usr/bin/python
#   Copyright 2012-2013 Eric Ptak - trouch.com
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import sys
file = None

print("WebIOPi passwd file generator")
if len(sys.argv)  == 2:
    file = sys.argv[1]
    if file == "--help" or file == "-h":
        print("Usage: webiopi-passwd [--help|file]")
        print("Compute and display hash used by WebIOPi for Authentication")
        print("Login and Password are prompted")
        print("\t--help\tDisplay this help")
        print("\t-h")
        print("\tfile\tSave hash to file")
        sys.exit()
else:
    file = "/etc/webiopi/passwd"       

f = open(file, "w")
_LOGIN      = "Enter Login: "
_PASSWORD   = "Enter Password: "
_CONFIRM    = "Confirm password: "
_DONTMATCH  = "Passwords don't match !"

import getpass
try:
    login = raw_input(_LOGIN)
except NameError:
    login = input(_LOGIN)
password = getpass.getpass(_PASSWORD)
password2 = getpass.getpass(_CONFIRM)
while password != password2:
    print(_DONTMATCH)
    password = getpass.getpass(_PASSWORD)
    password2 = getpass.getpass(_CONFIRM)

from webiopi.utils.crypto import encryptCredentials
auth = encryptCredentials(login, password)
print("\nHash: %s" % auth)
if file:
    f.write(auth)
    f.close()
    print("Saved to %s" % file)