Compare commits

..

No commits in common. "e7e34d78d6a5771cbacf0e6062ecc655fe4f7685" and "f60a8b1725809abc94bee830b2297807290a0d83" have entirely different histories.

4 changed files with 117 additions and 18 deletions

View File

@ -9,6 +9,6 @@
{mod,{dudeswave_app,[]}},
{env, [
{ip,"127.0.0.1"},
{port,8080}
{port,8080},
]},
{start_phases,[]}]}.

View File

@ -46,7 +46,7 @@ start(_Type, StartArgs) ->
]}
]),
{ok, ListenerPid} = cowboy:start_clear(dudeswave_listener, [
{ok, ListenerPid} = cowboy:start_tls(dudeswave_listener, [
{port, Port},
Inet,
{ip, Addr}

View File

@ -37,6 +37,14 @@ This module accepts four methods:
Logout the user from the current session and invalidate all the
authentication cookies, if present.
If an operation fails, the response JSON is in the form:
```
{
"error": "error string"
}
```
JSON APIs
POST /api/v1/auth
@ -54,11 +62,29 @@ Response codes:
- 400 Bad Request
- 404 Not Found
Response body:
If authentication successful:
```
{
"result": "ok"
}
```
DELETE /api/v1/auth
- 202 Accepted
- 404 Not Found
If operation successful;
```
{
"result": "deleted"
}
```
""".
-behaviour(cowboy_handler).
@ -101,7 +127,10 @@ forbidden(Req, State) ->
case dudeswave_auth:authenticate(User, Auth, Bucket) of
{error, service_unavailable} -> exit(service_unavailable);
true -> {false, Req, State};
false -> {true, Req, State}
false ->
Resp = json:encode(#{<<"error">> => <<"authentication required">>}),
Req0 = cowboy_req:reply(403, #{}, Resp, Req),
{true, Req0, State}
end
end.
@ -119,7 +148,9 @@ resource_exists(Req, State) ->
case dudeswave_auth:details(User, Bucket) of
[] ->
{false, Req, State};
Resp = json:encode(#{<<"error">> => <<"user does not exists">>}),
Req0 = cowboy_req:reply(404, #{}, Resp, Req),
{false, Req0, State};
{error, Reason} -> exit(Reason);
_ ->
NewState = State#{
@ -149,7 +180,9 @@ delete_resource(Req, State) ->
#{max_age => 0}),
Req1 = cowboy_req:set_resp_cookie(<<"dudename">>, User, Req0,
#{max_age => 0}),
{true, Req1, State};
Resp = json:encode(#{<<"result">> => <<"deleted">>}),
Req2 = cowboy_req:reply(200, #{}, Resp, Req1),
{true, Req2, State};
{error, _} -> {false, Req, State}
end.
@ -168,15 +201,21 @@ login(Req, State) ->
case dudeswave_auth:authenticate(User, Pass, Cookies, Bucket) of
{true, Cookie, Validity} ->
Resp = json:encode(#{<<"result">> => <<"ok">>}),
Req1 = cowboy_req:set_resp_cookie(<<"dudeauth">>, Cookie, Req0,
#{max_age => Validity}),
Req2 = cowboy_req:set_resp_cookie(<<"dudename">>, User, Req1,
#{max_age => Validity}),
{true, Req2, State};
Req3 = cowboy_req:reply(200, #{}, Resp, Req2),
{true, Req3, State};
false ->
{false, Req0, State};
Resp = json:encode(#{<<"error">> => <<"authentication failed">>}),
Req1 = cowboy_req:reply(401, #{}, Resp, Req0),
{false, Req1, State};
{error, _} ->
{false, Req0, State}
Resp = json:encode(#{<<"error">> => <<"internal error">>}),
Req1 = cowboy_req:reply(500, #{}, Resp, Req0),
{false, Req1, State}
end.
% Provided but not used

View File

@ -29,6 +29,14 @@ If the session is not valid, all the requests will return `403 Forbidden` to
the client. In case a technical problem occurs, `500 Internal Server Error`
is returned.
In case of errors, all the methods returns a JSON response in the form:
```
{
"error": "error string"
}
```
This module accepts four methods:
- GET /api/v1/user
@ -81,6 +89,14 @@ PUT /api/v1/user
}
```
Response body:
```
{
"result": "created"
}
```
Response codes:
- 201 Created
@ -97,6 +113,14 @@ POST /api/v1/user
}
```
Response body:
```
{
"result": "updated"
}
```
Response codes:
- 200 OK
@ -105,6 +129,14 @@ Response codes:
DELETE /api/v1/user
Response body:
```
{
"result": "deleted"
}
```
Response codes:
- 202 Accepted
@ -151,9 +183,13 @@ forbidden(Req, State) ->
case dudeswave_auth:authenticate(User, Auth, Bucket) of
{error, service_unavailable} ->
{true, Req, State};
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{true, Req0, State};
true ->
{false, Req, State};
Resp = json:encode(#{<<"error">> => <<"authentication required">>}),
Req0 = cowboy_req:reply(403, #{}, Resp, Req),
{false, Req0, State};
false -> {true, Req, State}
end
end.
@ -183,8 +219,14 @@ resource_exists(Req, State) ->
{ok, Bucket} = maps:find(bucket, State),
case dudeswave_auth:details(User, Bucket) of
[] -> {false, Req, State};
{error, _} -> {false, Req, State};
[] ->
Resp = json:encode(#{<<"error">> => <<"user does not exists">>}),
Req0 = cowboy_req:reply(404, #{}, Resp, Req),
{false, Req0, State};
{error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{false, Req0, State};
Details ->
NewState = State#{
bucket => Bucket,
@ -209,8 +251,14 @@ delete_resource(Req, State) ->
#{dudename := User} = cowboy_req:match_cookies([dudename], Req),
case dudeswave_auth:delete(User, Bucket) of
ok -> {true, Req, State};
{error, _} -> {false, Req, State}
ok ->
Resp = json:encode(#{<<"result">> => <<"deleted">>}),
Req0 = cowboy_req:reply(200, #{}, Resp, Req),
{true, Req0, State};
{error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{false, Req0, State}
end.
delete_completed(Req, State) -> {true, Req, State}.
@ -227,8 +275,14 @@ create_user(Req, State) ->
#{<<"password">> := Pass, <<"email">> := Email} = json:decode(Data),
case dudeswave_auth:new(User, Pass, Email, Bucket) of
ok -> {true, Req0, []};
{error, _} -> {false, Req0, State}
ok ->
Resp = json:encode(#{<<"result">> => <<"created">>}),
Req1 = cowboy_req:reply(201, #{}, Resp, Req0),
{true, Req1, []};
{error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req1 = cowboy_req:reply(500, #{}, Resp, Req0),
{false, Req1, State}
end.
modify_user(Req, State) ->
@ -240,8 +294,14 @@ modify_user(Req, State) ->
<<"name">> := Name} = json:decode(Data),
case dudeswave_auth:update(User, Name, Email, Desc, Bucket) of
ok -> {true, Req0, []};
{error, _} -> {false, Req0, State}
ok ->
Resp = json:encode(#{<<"result">> => <<"details updated">>}),
Req0 = cowboy_req:reply(200, #{}, Resp, Req),
{true, Req0, []};
{error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{false, Req0, State}
end.
user_details(Req, State) ->