| class CHAP: | class CHAP: | ||||
| test_password = 'Test123' | test_password = 'Test123' | ||||
| # initializes class-instance and instance variables | # initializes class-instance and instance variables | ||||
| def __init__(self): | |||||
| self.key = ''; | |||||
| self.authenticated = False; | |||||
| def __init__( self ): | |||||
| self.keys = {} | |||||
| self.authenticated = {} | |||||
| # tells the server to start the autentification process | # tells the server to start the autentification process | ||||
| # and send the generated random salt | # and send the generated random salt | ||||
| def init(self): | |||||
| self.key = str( uuid.uuid4() ) | |||||
| return self.key | |||||
| def init( self ): | |||||
| session = str( uuid.uuid4() ) | |||||
| key = str( uuid.uuid4() ) | |||||
| while session in self.keys: | |||||
| session = str( uuid.uuid4() ) | |||||
| self.keys[ session ] = key | |||||
| return [ session, key ] | |||||
| # checks if send hash is same as internally generated to validate if the correct | # checks if send hash is same as internally generated to validate if the correct | ||||
| # password was used | # password was used | ||||
| def auth(self, password_hash): | |||||
| def auth( self, password_hash ): | |||||
| combined = CHAP.test_password + self.key | combined = CHAP.test_password + self.key | ||||
| passhash = hashlib.sha256( combined.encode( 'utf-8' ) ).hexdigest() | passhash = hashlib.sha256( combined.encode( 'utf-8' ) ).hexdigest() | ||||
| self.authenticated = passhash == password_hash | self.authenticated = passhash == password_hash | ||||
| return self.authenticated | return self.authenticated | ||||
| # adds functionality for users to log them selfes off, but also need the | |||||
| # the password_hash to ensure, that nobody else logs you off | |||||
| def logout( self, session, password_hash ): | |||||
| if session in self.authenticated: | |||||
| if self.auth( password_hash ): | |||||
| self.authenticated[ session ] = False | |||||
| return self.authenticated.get( session ) | |||||
| # a little method that refuses to say hi, if you | # a little method that refuses to say hi, if you | ||||
| # are not authenticated | # are not authenticated | ||||
| def hello(self): | |||||
| def hello( self ): | |||||
| if ( self.authenticated ): | if ( self.authenticated ): | ||||
| return 'Hi, you are authenticated' | return 'Hi, you are authenticated' | ||||
| else: | else: |