Compare commits
No commits in common. "ab3ca1ec64d7343d31cff6c876dfd94761d591fe" and "895e0a53306390f03153a7ac37711db48a0ea9eb" have entirely different histories.
ab3ca1ec64
...
895e0a5330
|
@ -13,7 +13,7 @@
|
||||||
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
%
|
%
|
||||||
-module(dudeswave_user_handler).
|
-module(dudeswave_register_handler).
|
||||||
-moduledoc """
|
-moduledoc """
|
||||||
Register a new user.
|
Register a new user.
|
||||||
""".
|
""".
|
||||||
|
@ -53,14 +53,17 @@ known_methods(Req, State) ->
|
||||||
resource_exists(Req, State) ->
|
resource_exists(Req, State) ->
|
||||||
{ok, Bucket} = maps:find(bucket, State),
|
{ok, Bucket} = maps:find(bucket, State),
|
||||||
|
|
||||||
case cowboy:read_urlencoded_body(Req) of
|
case cowboy:read_body(Req, #{period => 5000, length => 8192}) of
|
||||||
{ok, [{name, Name}, {username, User}, {password, Password}], NewReq} ->
|
{ok, Body, NewReq} ->
|
||||||
|
#{<<"name">> := Name, <<"password">> := Pass,
|
||||||
|
<<"user">> := User} = json:decode(Body),
|
||||||
|
|
||||||
case storage:read(Bucket, User) of
|
case storage:read(Bucket, User) of
|
||||||
{ok, [_R]} ->
|
{ok, [_R]} ->
|
||||||
{true, NewReq, user_exists};
|
{true, NewReq, user_exists};
|
||||||
{ok, []} ->
|
{ok, []} ->
|
||||||
{false, NewReq, {Bucket, [{name, Name},
|
{false, NewReq, {Bucket, [{name, Name},
|
||||||
{username, User},{password, Password}]}}
|
{username, User},{password, Pass}]}}
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -70,7 +73,8 @@ is_conflict(Req, State) -> {false, Req, State}.
|
||||||
|
|
||||||
create_user(Req, {Bucket, [{name, Name}, {username, User}, {password, Pass}]}) ->
|
create_user(Req, {Bucket, [{name, Name}, {username, User}, {password, Pass}]}) ->
|
||||||
Salt = rand:bytes(32),
|
Salt = rand:bytes(32),
|
||||||
Hash = crypto:hash(sha256, <<Pass/binary, Salt/binary>>),
|
SaltedPW = <<Pass/binary, Salt/binary>>,
|
||||||
|
Hash = crypto:hash(sha256, SaltedPW),
|
||||||
|
|
||||||
case storage:write(Bucket, User, Hash, [{salt, Salt}, {name, Name}]) of
|
case storage:write(Bucket, User, Hash, [{salt, Salt}, {name, Name}]) of
|
||||||
ok ->
|
ok ->
|
||||||
|
|
Loading…
Reference in New Issue