Hardware Reference
In-Depth Information
Storing Secrets in a File
One of the simplest ways to store data is in a
lat ile
. his is nothing more than a list of
entries in a basic text ile. To read a username and password pair of values from a ile, change
the
door_controller.py
code by adding the following
FileAuthenticator
class
before the
main
function:
class FileAuthenticator:
filename = “secrets.txt”
def readFile(self):
secrets = open(self.filename, 'r')
print “reading from file”
for line in secrets:
line = line.rstrip('\n')
self.id, self.secretPassword = line.split(',')
def check(self,token):
self.readFile()
print “checking input of '” + token.id + “', ;
password: “ + token.secret + “, against secret password ;
'” + self.secretPassword +”'”
result = (token.secret == self.secretPassword) & ;
(token.id == self.id)
print “authentication is: “ + str(result)
return result
Now tell the main part of the program to use the new
FileAuthenticator
class instead of
BasicAuthenticator
. Change the line
Authenticator = BasicAuthenticator()
to
Authenticator = FileAuthenticator()
he ile
secrets.txt
will hold the your secret authentication information. Use a text edi-
tor to create the ile
secrets.txt
in the same folder as your program ile and add the fol-
lowing sample line:
Andrew,9876