Compare commits
No commits in common. "e7e34d78d6a5771cbacf0e6062ecc655fe4f7685" and "f60a8b1725809abc94bee830b2297807290a0d83" have entirely different histories.
e7e34d78d6
...
f60a8b1725
|
@ -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,[]}]}.
|
||||||
|
|
|
@ -46,7 +46,7 @@ start(_Type, StartArgs) ->
|
||||||
]}
|
]}
|
||||||
]),
|
]),
|
||||||
|
|
||||||
{ok, ListenerPid} = cowboy:start_clear(dudeswave_listener, [
|
{ok, ListenerPid} = cowboy:start_tls(dudeswave_listener, [
|
||||||
{port, Port},
|
{port, Port},
|
||||||
Inet,
|
Inet,
|
||||||
{ip, Addr}
|
{ip, Addr}
|
||||||
|
|
|
@ -37,6 +37,14 @@ 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
|
||||||
|
@ -54,11 +62,29 @@ 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).
|
||||||
|
@ -101,7 +127,10 @@ 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 -> {true, Req, State}
|
false ->
|
||||||
|
Resp = json:encode(#{<<"error">> => <<"authentication required">>}),
|
||||||
|
Req0 = cowboy_req:reply(403, #{}, Resp, Req),
|
||||||
|
{true, Req0, State}
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -119,7 +148,9 @@ resource_exists(Req, 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">>}),
|
||||||
|
Req0 = cowboy_req:reply(404, #{}, Resp, Req),
|
||||||
|
{false, Req0, State};
|
||||||
{error, Reason} -> exit(Reason);
|
{error, Reason} -> exit(Reason);
|
||||||
_ ->
|
_ ->
|
||||||
NewState = State#{
|
NewState = State#{
|
||||||
|
@ -149,7 +180,9 @@ 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}),
|
||||||
{true, Req1, State};
|
Resp = json:encode(#{<<"result">> => <<"deleted">>}),
|
||||||
|
Req2 = cowboy_req:reply(200, #{}, Resp, Req1),
|
||||||
|
{true, Req2, State};
|
||||||
{error, _} -> {false, Req, State}
|
{error, _} -> {false, Req, State}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -168,15 +201,21 @@ 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}),
|
||||||
{true, Req2, State};
|
Req3 = cowboy_req:reply(200, #{}, Resp, Req2),
|
||||||
|
{true, Req3, State};
|
||||||
false ->
|
false ->
|
||||||
{false, Req0, State};
|
Resp = json:encode(#{<<"error">> => <<"authentication failed">>}),
|
||||||
|
Req1 = cowboy_req:reply(401, #{}, Resp, Req0),
|
||||||
|
{false, Req1, State};
|
||||||
{error, _} ->
|
{error, _} ->
|
||||||
{false, Req0, State}
|
Resp = json:encode(#{<<"error">> => <<"internal error">>}),
|
||||||
|
Req1 = cowboy_req:reply(500, #{}, Resp, Req0),
|
||||||
|
{false, Req1, State}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
% Provided but not used
|
% Provided but not used
|
||||||
|
|
|
@ -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`
|
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
|
||||||
|
@ -81,6 +89,14 @@ PUT /api/v1/user
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Response body:
|
||||||
|
|
||||||
|
```
|
||||||
|
{
|
||||||
|
"result": "created"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Response codes:
|
Response codes:
|
||||||
|
|
||||||
- 201 Created
|
- 201 Created
|
||||||
|
@ -97,6 +113,14 @@ POST /api/v1/user
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Response body:
|
||||||
|
|
||||||
|
```
|
||||||
|
{
|
||||||
|
"result": "updated"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Response codes:
|
Response codes:
|
||||||
|
|
||||||
- 200 OK
|
- 200 OK
|
||||||
|
@ -105,6 +129,14 @@ Response codes:
|
||||||
|
|
||||||
DELETE /api/v1/user
|
DELETE /api/v1/user
|
||||||
|
|
||||||
|
Response body:
|
||||||
|
|
||||||
|
```
|
||||||
|
{
|
||||||
|
"result": "deleted"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Response codes:
|
Response codes:
|
||||||
|
|
||||||
- 202 Accepted
|
- 202 Accepted
|
||||||
|
@ -151,9 +183,13 @@ 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} ->
|
||||||
{true, Req, State};
|
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
|
||||||
|
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
|
||||||
|
{true, Req0, State};
|
||||||
true ->
|
true ->
|
||||||
{false, Req, State};
|
Resp = json:encode(#{<<"error">> => <<"authentication required">>}),
|
||||||
|
Req0 = cowboy_req:reply(403, #{}, Resp, Req),
|
||||||
|
{false, Req0, State};
|
||||||
false -> {true, Req, State}
|
false -> {true, Req, State}
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
@ -183,8 +219,14 @@ 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};
|
[] ->
|
||||||
{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 ->
|
Details ->
|
||||||
NewState = State#{
|
NewState = State#{
|
||||||
bucket => Bucket,
|
bucket => Bucket,
|
||||||
|
@ -209,8 +251,14 @@ 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 -> {true, Req, State};
|
ok ->
|
||||||
{error, _} -> {false, Req, State}
|
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.
|
end.
|
||||||
|
|
||||||
delete_completed(Req, State) -> {true, Req, State}.
|
delete_completed(Req, State) -> {true, Req, State}.
|
||||||
|
@ -227,8 +275,14 @@ 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 -> {true, Req0, []};
|
ok ->
|
||||||
{error, _} -> {false, Req0, State}
|
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.
|
end.
|
||||||
|
|
||||||
modify_user(Req, State) ->
|
modify_user(Req, State) ->
|
||||||
|
@ -240,8 +294,14 @@ 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 -> {true, Req0, []};
|
ok ->
|
||||||
{error, _} -> {false, Req0, State}
|
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.
|
end.
|
||||||
|
|
||||||
user_details(Req, State) ->
|
user_details(Req, State) ->
|
||||||
|
|
Loading…
Reference in New Issue