Completed
Push — master ( 56c97c...2e9e0c )
by Marek
16s queued 14s
created

ige.account.passwordGen()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
#
2
#  Copyright 2001 - 2018 Ludek Smid [http://www.ospace.net/]
3
#
4
#  This file is part of Outer Space.
5
#
6
#  Outer Space is free software; you can redistribute it and/or modify
7
#  it under the terms of the GNU General Public License as published by
8
#  the Free Software Foundation; either version 2 of the License, or
9
#  (at your option) any later version.
10
#
11
#  Outer Space is distributed in the hope that it will be useful,
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#  GNU General Public License for more details.
15
#
16
#  You should have received a copy of the GNU General Public License
17
#  along with Outer Space; if not, write to the Free Software
18
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
21
import hashlib
22
import os
23
import time
24
25
from passlib.context import CryptContext
26
27
import ige.Const as Const
28
29
CONTEXT = CryptContext(schemes=["pbkdf2_sha512"],
30
                       pbkdf2_sha512__default_rounds=50000,
31
                       pbkdf2_sha512__salt_size=64)
32
33
def passwordGen():
34
    return hashlib.sha1(os.urandom(160)).hexdigest()
35
36
class Account(object):
37
38
    def __init__(self, login, nick, email, passwd, passwdHashed = True):
39
        # credentials
40
        self.login = login
41
        self.nick = nick
42
        self.email = email
43
        self.passwdHashed = passwdHashed
44
        self.passwd = None
45
        self.setPassword(passwd)
46
        # account options
47
        self.lastLogin = 0
48
        self.blockedUntil = -1 # -1 for not blocked, > 0 for blocked
49
        self.blocked = 0 # 1 for blocked account
50
        self.confToken = hashlib.md5('%s%s%d' % (login, email, time.time())).hexdigest() # when None, it's confirmed TODO make it work
51
        self.hostIDs = {} # hostids and times
52
        self.isAI = False
53
54
    def addHostID(self, hostID):
55
        if hostID:
56
            self.hostIDs[hostID] = time.time()
57
58
    def setPassword(self, plainPassword):
59
        if self.passwdHashed:
60
            self.passwd = CONTEXT.hash(plainPassword)
61
        else:
62
            self.passwd = plainPassword
63
64
    def verifyPassword(self, password):
65
        if self.passwdHashed:
66
            return CONTEXT.verify(password, self.passwd)
67
        else:
68
            return password == self.passwd
69
70
class AIAccount(Account):
71
72
    def __init__(self, login, nick, aiType):
73
        password = passwordGen()
74
        Account.__init__(self, login, nick, None, password, passwdHashed = False)
75
76
        self.isAI = True
77
        self.aiType = aiType
78
        self.galaxyNames = []
79
80
class AdminAccount(Account):
81
    def __init__(self):
82
        password = passwordGen()
83
        Account.__init__(self, Const.ADMIN_LOGIN, Const.ADMIN_NICK, None, password, passwdHashed = False)
84