JlSonic/login.jl

61 lines
1.7 KiB
Julia
Raw Normal View History

2019-05-17 20:22:01 +02:00
using MD5
using HTTP
2019-05-19 22:24:18 +02:00
using JSON2
const users = JlSonic.User[]
2019-05-17 20:22:01 +02:00
function getlogin(app, req)
query = HTTP.URIs.queryparams(req[:query])
username = string(get(query, "u", ""))
token = get(query, "t", "")
salt = get(query, "s", "")
password = get(query, "p", "")
req[:login] = Dict(:name => username,
:token => token,
:salt => salt,
:password => password,
:login => false)
return app(req)
end
function checkpassword(app, req)
2019-05-19 22:24:18 +02:00
global users
usern = findfirst(u -> u.name == req[:login][:name], users)
usern === nothing && return app(req)
user = users[usern]
req[:login][:user] = user
2019-05-17 20:22:01 +02:00
if !isempty(req[:login][:salt])
2019-05-19 22:24:18 +02:00
if bytes2hex(MD5.md5(string(user.password, req[:login][:salt]))) ==
2019-05-17 20:22:01 +02:00
req[:login][:token]
req[:login][:login] = true
end
elseif !isempty(req[:login][:password])
if startswith(req[:login][:password], "enc:")
req[:login][:login] =
2019-05-19 22:24:18 +02:00
String(hex2bytes(split(req[:login][:password], ":")[2])) ==
user.password
2019-05-17 20:22:01 +02:00
else
2019-05-19 22:24:18 +02:00
req[:login][:login] = user.password == req[:login][:password]
2019-05-17 20:22:01 +02:00
end
end
return app(req)
end
2019-05-19 22:24:18 +02:00
function saveusers(file = expanduser("~/.config/beets/users.jsonl"))
global users
open(file, "w") do f
write(f, join(JSON2.write.(users), "\n"))
end
end
function loadusers(; file = expanduser("~/.config/beets/users.jsonl"))
global users
isfile(file) || touch(file)
ps = JSON2.readlines(file)
p = JSON2.read.(ps, JlSonic.User)
empty!(users)
2019-05-19 23:27:44 +02:00
append!(users, p)
2019-05-19 22:24:18 +02:00
end
2019-05-17 20:22:01 +02:00
sonic_login = stack(getlogin, checkpassword)