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,[]}}, {mod,{dudeswave_app,[]}},
{env, [ {env, [
{ip,"127.0.0.1"}, {ip,"127.0.0.1"},
{port,8080}, {port,8080}
]}, ]},
{start_phases,[]}]}. {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}, {port, Port},
Inet, Inet,
{ip, Addr} {ip, Addr}

View File

@ -37,14 +37,6 @@ This module accepts four methods:
Logout the user from the current session and invalidate all the Logout the user from the current session and invalidate all the
authentication cookies, if present. authentication cookies, if present.
If an operation fails, the response JSON is in the form:
```
{
"error": "error string"
}
```
JSON APIs JSON APIs
POST /api/v1/auth POST /api/v1/auth
@ -62,29 +54,11 @@ Response codes:
- 400 Bad Request - 400 Bad Request
- 404 Not Found - 404 Not Found
Response body:
If authentication successful:
```
{
"result": "ok"
}
```
DELETE /api/v1/auth DELETE /api/v1/auth
- 202 Accepted - 202 Accepted
- 404 Not Found - 404 Not Found
If operation successful;
```
{
"result": "deleted"
}
```
""". """.
-behaviour(cowboy_handler). -behaviour(cowboy_handler).
@ -127,10 +101,7 @@ forbidden(Req, State) ->
case dudeswave_auth:authenticate(User, Auth, Bucket) of case dudeswave_auth:authenticate(User, Auth, Bucket) of
{error, service_unavailable} -> exit(service_unavailable); {error, service_unavailable} -> exit(service_unavailable);
true -> {false, Req, State}; true -> {false, Req, State};
false -> false -> {true, Req, State}
Resp = json:encode(#{<<"error">> => <<"authentication required">>}),
Req0 = cowboy_req:reply(403, #{}, Resp, Req),
{true, Req0, State}
end end
end. end.
@ -148,9 +119,7 @@ resource_exists(Req, State) ->
case dudeswave_auth:details(User, Bucket) of case dudeswave_auth:details(User, Bucket) of
[] -> [] ->
Resp = json:encode(#{<<"error">> => <<"user does not exists">>}), {false, Req, State};
Req0 = cowboy_req:reply(404, #{}, Resp, Req),
{false, Req0, State};
{error, Reason} -> exit(Reason); {error, Reason} -> exit(Reason);
_ -> _ ->
NewState = State#{ NewState = State#{
@ -180,9 +149,7 @@ delete_resource(Req, State) ->
#{max_age => 0}), #{max_age => 0}),
Req1 = cowboy_req:set_resp_cookie(<<"dudename">>, User, Req0, Req1 = cowboy_req:set_resp_cookie(<<"dudename">>, User, Req0,
#{max_age => 0}), #{max_age => 0}),
Resp = json:encode(#{<<"result">> => <<"deleted">>}), {true, Req1, State};
Req2 = cowboy_req:reply(200, #{}, Resp, Req1),
{true, Req2, State};
{error, _} -> {false, Req, State} {error, _} -> {false, Req, State}
end. end.
@ -201,21 +168,15 @@ login(Req, State) ->
case dudeswave_auth:authenticate(User, Pass, Cookies, Bucket) of case dudeswave_auth:authenticate(User, Pass, Cookies, Bucket) of
{true, Cookie, Validity} -> {true, Cookie, Validity} ->
Resp = json:encode(#{<<"result">> => <<"ok">>}),
Req1 = cowboy_req:set_resp_cookie(<<"dudeauth">>, Cookie, Req0, Req1 = cowboy_req:set_resp_cookie(<<"dudeauth">>, Cookie, Req0,
#{max_age => Validity}), #{max_age => Validity}),
Req2 = cowboy_req:set_resp_cookie(<<"dudename">>, User, Req1, Req2 = cowboy_req:set_resp_cookie(<<"dudename">>, User, Req1,
#{max_age => Validity}), #{max_age => Validity}),
Req3 = cowboy_req:reply(200, #{}, Resp, Req2), {true, Req2, State};
{true, Req3, State};
false -> false ->
Resp = json:encode(#{<<"error">> => <<"authentication failed">>}), {false, Req0, State};
Req1 = cowboy_req:reply(401, #{}, Resp, Req0),
{false, Req1, State};
{error, _} -> {error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal error">>}), {false, Req0, State}
Req1 = cowboy_req:reply(500, #{}, Resp, Req0),
{false, Req1, State}
end. end.
% Provided but not used % 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` the client. In case a technical problem occurs, `500 Internal Server Error`
is returned. is returned.
In case of errors, all the methods returns a JSON response in the form:
```
{
"error": "error string"
}
```
This module accepts four methods: This module accepts four methods:
- GET /api/v1/user - GET /api/v1/user
@ -89,14 +81,6 @@ PUT /api/v1/user
} }
``` ```
Response body:
```
{
"result": "created"
}
```
Response codes: Response codes:
- 201 Created - 201 Created
@ -113,14 +97,6 @@ POST /api/v1/user
} }
``` ```
Response body:
```
{
"result": "updated"
}
```
Response codes: Response codes:
- 200 OK - 200 OK
@ -129,14 +105,6 @@ Response codes:
DELETE /api/v1/user DELETE /api/v1/user
Response body:
```
{
"result": "deleted"
}
```
Response codes: Response codes:
- 202 Accepted - 202 Accepted
@ -183,13 +151,9 @@ forbidden(Req, State) ->
case dudeswave_auth:authenticate(User, Auth, Bucket) of case dudeswave_auth:authenticate(User, Auth, Bucket) of
{error, service_unavailable} -> {error, service_unavailable} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}), {true, Req, State};
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{true, Req0, State};
true -> true ->
Resp = json:encode(#{<<"error">> => <<"authentication required">>}), {false, Req, State};
Req0 = cowboy_req:reply(403, #{}, Resp, Req),
{false, Req0, State};
false -> {true, Req, State} false -> {true, Req, State}
end end
end. end.
@ -219,14 +183,8 @@ resource_exists(Req, State) ->
{ok, Bucket} = maps:find(bucket, State), {ok, Bucket} = maps:find(bucket, State),
case dudeswave_auth:details(User, Bucket) of case dudeswave_auth:details(User, Bucket) of
[] -> [] -> {false, Req, State};
Resp = json:encode(#{<<"error">> => <<"user does not exists">>}), {error, _} -> {false, Req, State};
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 -> Details ->
NewState = State#{ NewState = State#{
bucket => Bucket, bucket => Bucket,
@ -251,14 +209,8 @@ delete_resource(Req, State) ->
#{dudename := User} = cowboy_req:match_cookies([dudename], Req), #{dudename := User} = cowboy_req:match_cookies([dudename], Req),
case dudeswave_auth:delete(User, Bucket) of case dudeswave_auth:delete(User, Bucket) of
ok -> ok -> {true, Req, State};
Resp = json:encode(#{<<"result">> => <<"deleted">>}), {error, _} -> {false, Req, State}
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. end.
delete_completed(Req, State) -> {true, Req, State}. delete_completed(Req, State) -> {true, Req, State}.
@ -275,14 +227,8 @@ create_user(Req, State) ->
#{<<"password">> := Pass, <<"email">> := Email} = json:decode(Data), #{<<"password">> := Pass, <<"email">> := Email} = json:decode(Data),
case dudeswave_auth:new(User, Pass, Email, Bucket) of case dudeswave_auth:new(User, Pass, Email, Bucket) of
ok -> ok -> {true, Req0, []};
Resp = json:encode(#{<<"result">> => <<"created">>}), {error, _} -> {false, Req0, State}
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. end.
modify_user(Req, State) -> modify_user(Req, State) ->
@ -294,14 +240,8 @@ modify_user(Req, State) ->
<<"name">> := Name} = json:decode(Data), <<"name">> := Name} = json:decode(Data),
case dudeswave_auth:update(User, Name, Email, Desc, Bucket) of case dudeswave_auth:update(User, Name, Email, Desc, Bucket) of
ok -> ok -> {true, Req0, []};
Resp = json:encode(#{<<"result">> => <<"details updated">>}), {error, _} -> {false, Req0, State}
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. end.
user_details(Req, State) -> user_details(Req, State) ->