Compare commits

..

3 Commits

Author SHA1 Message Date
absc e7e34d78d6 We plan to use this with a reverse proxy. The plan may change in the future. 2024-08-12 21:01:16 +00:00
absc e8ccc3a29f Keep it simple for now. We want to write a proper test suite for this and
letting it crash is more than good enough.

It was a mistake to try to add more complications to the APIs at this point
in time.
2024-08-12 21:00:26 +00:00
absc 2ad3b52c06 Corrected syntax error. 2024-08-12 20:41:24 +00:00
4 changed files with 18 additions and 117 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_tls(dudeswave_listener, [
{ok, ListenerPid} = cowboy:start_clear(dudeswave_listener, [
{port, Port},
Inet,
{ip, Addr}

View File

@ -37,14 +37,6 @@ 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
@ -62,29 +54,11 @@ 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).
@ -127,10 +101,7 @@ forbidden(Req, State) ->
case dudeswave_auth:authenticate(User, Auth, Bucket) of
{error, service_unavailable} -> exit(service_unavailable);
true -> {false, Req, State};
false ->
Resp = json:encode(#{<<"error">> => <<"authentication required">>}),
Req0 = cowboy_req:reply(403, #{}, Resp, Req),
{true, Req0, State}
false -> {true, Req, State}
end
end.
@ -148,9 +119,7 @@ resource_exists(Req, State) ->
case dudeswave_auth:details(User, Bucket) of
[] ->
Resp = json:encode(#{<<"error">> => <<"user does not exists">>}),
Req0 = cowboy_req:reply(404, #{}, Resp, Req),
{false, Req0, State};
{false, Req, State};
{error, Reason} -> exit(Reason);
_ ->
NewState = State#{
@ -180,9 +149,7 @@ delete_resource(Req, State) ->
#{max_age => 0}),
Req1 = cowboy_req:set_resp_cookie(<<"dudename">>, User, Req0,
#{max_age => 0}),
Resp = json:encode(#{<<"result">> => <<"deleted">>}),
Req2 = cowboy_req:reply(200, #{}, Resp, Req1),
{true, Req2, State};
{true, Req1, State};
{error, _} -> {false, Req, State}
end.
@ -201,21 +168,15 @@ 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}),
Req3 = cowboy_req:reply(200, #{}, Resp, Req2),
{true, Req3, State};
{true, Req2, State};
false ->
Resp = json:encode(#{<<"error">> => <<"authentication failed">>}),
Req1 = cowboy_req:reply(401, #{}, Resp, Req0),
{false, Req1, State};
{false, Req0, State};
{error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal error">>}),
Req1 = cowboy_req:reply(500, #{}, Resp, Req0),
{false, Req1, State}
{false, Req0, State}
end.
% Provided but not used

View File

@ -29,14 +29,6 @@ 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
@ -89,14 +81,6 @@ PUT /api/v1/user
}
```
Response body:
```
{
"result": "created"
}
```
Response codes:
- 201 Created
@ -113,14 +97,6 @@ POST /api/v1/user
}
```
Response body:
```
{
"result": "updated"
}
```
Response codes:
- 200 OK
@ -129,14 +105,6 @@ Response codes:
DELETE /api/v1/user
Response body:
```
{
"result": "deleted"
}
```
Response codes:
- 202 Accepted
@ -183,13 +151,9 @@ forbidden(Req, State) ->
case dudeswave_auth:authenticate(User, Auth, Bucket) of
{error, service_unavailable} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{true, Req0, State};
{true, Req, State};
true ->
Resp = json:encode(#{<<"error">> => <<"authentication required">>}),
Req0 = cowboy_req:reply(403, #{}, Resp, Req),
{false, Req0, State};
{false, Req, State};
false -> {true, Req, State}
end
end.
@ -219,14 +183,8 @@ resource_exists(Req, State) ->
{ok, Bucket} = maps:find(bucket, State),
case dudeswave_auth:details(User, Bucket) of
[] ->
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};
[] -> {false, Req, State};
{error, _} -> {false, Req, State};
Details ->
NewState = State#{
bucket => Bucket,
@ -251,14 +209,8 @@ delete_resource(Req, State) ->
#{dudename := User} = cowboy_req:match_cookies([dudename], Req),
case dudeswave_auth:delete(User, Bucket) of
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}
ok -> {true, Req, State};
{error, _} -> {false, Req, State}
end.
delete_completed(Req, State) -> {true, Req, State}.
@ -275,14 +227,8 @@ create_user(Req, State) ->
#{<<"password">> := Pass, <<"email">> := Email} = json:decode(Data),
case dudeswave_auth:new(User, Pass, Email, Bucket) of
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}
ok -> {true, Req0, []};
{error, _} -> {false, Req0, State}
end.
modify_user(Req, State) ->
@ -294,14 +240,8 @@ modify_user(Req, State) ->
<<"name">> := Name} = json:decode(Data),
case dudeswave_auth:update(User, Name, Email, Desc, Bucket) of
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}
ok -> {true, Req0, []};
{error, _} -> {false, Req0, State}
end.
user_details(Req, State) ->