Compare commits

..

No commits in common. "96781451b03b346eb35230ab5fe81f81ed52f955" and "33cc07914dd6ad09953a3d8b5285679d7eb4e076" have entirely different histories.

3 changed files with 8 additions and 20 deletions

View File

@ -5,7 +5,7 @@ ERLC?= erlc -server
OBJS= dudeswave.beam dudeswave_app.beam
OBJS+= dudeswave_supervisor.beam dudeswave_handler.beam
OBJS+= dudeswave_newuser_handler.beam
OBJS+= dudeswave_user_handler.beam
all: ${OBJS}

View File

@ -37,7 +37,7 @@ start(_Type, StartArgs) ->
Dispatch = cowboy_router:compile([
{'_', [
{"/user/new", dudeswave_newuser_handler, #{bucket => ?USERSBUCKET}},
{"/user", dudeswave_user_handler, #{bucket => ?USERSBUCKET}},
{"/", dudeswave_handler, #{bucket => ?APPBUCKET}}
]}
]),

View File

@ -13,7 +13,7 @@
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
%
-module(dudeswave_newuser_handler).
-module(dudeswave_user_handler).
-moduledoc """
Register a new user.
""".
@ -27,7 +27,7 @@ Register a new user.
%
-export([allowed_methods/2, content_types_accepted/2,
known_methods/2, resource_exists/2, is_conflict/2,
previously_existed/2, allow_missing_post/2, create_user/2]).
create_user/2]).
-include_lib("storage/include/storage.hrl").
@ -42,13 +42,13 @@ init(Req, State) ->
{cowboy_rest, Req, State}.
allowed_methods(Req, State) ->
{[<<"POST">>], Req, State}.
{[<<"PUT">>], Req, State}.
content_types_accepted(Req, State) ->
{[{<<"application/x-www-form-urlencoded">>, create_user}], Req, State}.
{[{{ <<"application">>, <<"json">>, '*'}, create_user}], Req, State}.
known_methods(Req, State) ->
{[<<"POST">>], Req, State}.
{[<<"PUT">>], Req, State}.
resource_exists(Req, State) ->
{ok, Bucket} = maps:find(bucket, State),
@ -68,26 +68,14 @@ is_conflict(Req, user_exists) -> {true, Req, []};
is_conflict(Req, State) -> {false, Req, State}.
previously_existed(Req, State) ->
{false, Req, State}.
allow_missing_post(Req, State) ->
{true, Req, State}.
create_user(Req, {Bucket, [{name, Name}, {username, User}, {password, Pass}]}) ->
crypto:rand_seed(),
Salt = rand:bytes(32),
Hash = crypto:hash(sha256, <<Pass/binary, Salt/binary>>),
URI = uri_string:recompose(#{
scheme => cowboy_req:scheme(Req),
host => cowboy_req:host(Req),
path => lists:flatten(["/user/", User])
}),
case storage:write(Bucket, User, Hash, [{salt, Salt}, {name, Name}]) of
ok ->
{{true, list_to_binary(URI)}, Req, []};
{true, Req, []};
{error, Reason} ->
{false, Req, Reason}
end.