Browse Source

user related apis

v2
dave 4 years ago
parent
commit
55f48433ed
  1. 18
      pysonic/daemon.py
  2. 31
      pysonic/database.py

18
pysonic/daemon.py

@ -31,14 +31,15 @@ def main():
args = parser.parse_args()
logging.basicConfig(level=logging.INFO if args.debug else logging.WARNING)
logging.basicConfig(level=logging.INFO if args.debug else logging.WARNING,
format="%(asctime)-15s %(levelname)-8s %(filename)s:%(lineno)d %(message)s")
db = PysonicDatabase(path=args.database_path)
library = PysonicLibrary(db)
for dirname in args.dirs:
assert os.path.exists(dirname) and dirname.startswith("/"), "--dirs must be absolute paths and exist!"
try:
library.add_dir(dirname)
library.add_root_dir(dirname)
except DuplicateRootException:
pass
library.update()
@ -49,18 +50,22 @@ def main():
except IntegrityError:
db.update_user(username, password)
logging.warning("Libraries: {}".format([i["name"] for i in library.get_libraries()]))
logging.warning("Artists: {}".format([i["name"] for i in library.get_artists()]))
logging.warning("Albums: {}".format(len(library.get_albums())))
# logging.warning("Libraries: {}".format([i["name"] for i in library.get_libraries()]))
# logging.warning("Artists: {}".format([i["name"] for i in library.get_artists()]))
# logging.warning("Albums: {}".format(len(library.get_albums())))
api = PysonicApi(db, library, args)
api_config = {}
if args.disable_auth:
logging.warning("starting up with auth disabled")
else:
def validate_password(realm, username, password):
print("I JUST VALIDATED {}:{} ({})".format(username, password, realm))
return True
api_config.update({'tools.auth_basic.on': True,
'tools.auth_basic.realm': 'pysonic',
'tools.auth_basic.checkpassword': db.validate_password})
'tools.auth_basic.checkpassword': validate_password})
if args.enable_cors:
def cors():
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
@ -99,5 +104,6 @@ def main():
logging.info("API has shut down")
cherrypy.engine.exit()
if __name__ == '__main__':
main()

31
pysonic/database.py

@ -139,3 +139,34 @@ class PysonicDatabase(object):
for row in cursor:
libs.append(row)
return libs
@readcursor
def add_user(self, cursor, username, password, is_admin=False):
cursor.execute("INSERT INTO users (username, password, admin) VALUES (?, ?, ?)",
(username, self.hashit(password), is_admin))
cursor.execute("COMMIT")
@readcursor
def update_user(self, cursor, username, password, is_admin=False):
cursor.execute("UPDATE users SET password=?, admin=? WHERE username=?;",
(self.hashit(password), is_admin, username))
cursor.execute("COMMIT")
@readcursor
def get_user(self, cursor, user):
try:
column = "id" if type(user) is int else "username"
return cursor.execute("SELECT * FROM users WHERE {}=?;".format(column), (user, )).fetchall()[0]
except IndexError:
raise NotFoundError("User doesn't exist")
def hashit(self, unicode_string):
return sha512(unicode_string.encode('UTF-8')).hexdigest()

Loading…
Cancel
Save