user related apis
This commit is contained in:
parent
afd5476ea8
commit
55f48433ed
|
@ -31,14 +31,15 @@ def main():
|
||||||
|
|
||||||
args = parser.parse_args()
|
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)
|
db = PysonicDatabase(path=args.database_path)
|
||||||
library = PysonicLibrary(db)
|
library = PysonicLibrary(db)
|
||||||
for dirname in args.dirs:
|
for dirname in args.dirs:
|
||||||
assert os.path.exists(dirname) and dirname.startswith("/"), "--dirs must be absolute paths and exist!"
|
assert os.path.exists(dirname) and dirname.startswith("/"), "--dirs must be absolute paths and exist!"
|
||||||
try:
|
try:
|
||||||
library.add_dir(dirname)
|
library.add_root_dir(dirname)
|
||||||
except DuplicateRootException:
|
except DuplicateRootException:
|
||||||
pass
|
pass
|
||||||
library.update()
|
library.update()
|
||||||
|
@ -49,18 +50,22 @@ def main():
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
db.update_user(username, password)
|
db.update_user(username, password)
|
||||||
|
|
||||||
logging.warning("Libraries: {}".format([i["name"] for i in library.get_libraries()]))
|
# 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("Artists: {}".format([i["name"] for i in library.get_artists()]))
|
||||||
logging.warning("Albums: {}".format(len(library.get_albums())))
|
# logging.warning("Albums: {}".format(len(library.get_albums())))
|
||||||
|
|
||||||
api = PysonicApi(db, library, args)
|
api = PysonicApi(db, library, args)
|
||||||
api_config = {}
|
api_config = {}
|
||||||
if args.disable_auth:
|
if args.disable_auth:
|
||||||
logging.warning("starting up with auth disabled")
|
logging.warning("starting up with auth disabled")
|
||||||
else:
|
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,
|
api_config.update({'tools.auth_basic.on': True,
|
||||||
'tools.auth_basic.realm': 'pysonic',
|
'tools.auth_basic.realm': 'pysonic',
|
||||||
'tools.auth_basic.checkpassword': db.validate_password})
|
'tools.auth_basic.checkpassword': validate_password})
|
||||||
if args.enable_cors:
|
if args.enable_cors:
|
||||||
def cors():
|
def cors():
|
||||||
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
|
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
|
||||||
|
@ -99,5 +104,6 @@ def main():
|
||||||
logging.info("API has shut down")
|
logging.info("API has shut down")
|
||||||
cherrypy.engine.exit()
|
cherrypy.engine.exit()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -139,3 +139,34 @@ class PysonicDatabase(object):
|
||||||
for row in cursor:
|
for row in cursor:
|
||||||
libs.append(row)
|
libs.append(row)
|
||||||
return libs
|
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…
Reference in New Issue