Authorize HTTP Publish API with clientId

This commit is contained in:
Feng Lee 2017-07-26 13:16:46 +08:00
parent f25661d3cd
commit 8325056061
1 changed files with 22 additions and 21 deletions

View File

@ -47,8 +47,9 @@ handle_request(Method, "/status", Req) when Method =:= 'HEAD'; Method =:= 'GET'
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
handle_request('POST', "/mqtt/publish", Req) -> handle_request('POST', "/mqtt/publish", Req) ->
case authorized(Req) of Params = parse_params(Req),
true -> http_publish(Req); case authorized(Req, Params) of
true -> http_publish(Req, Params);
false -> Req:respond({401, [], <<"Unauthorized">>}) false -> Req:respond({401, [], <<"Unauthorized">>})
end; end;
@ -68,8 +69,7 @@ handle_request(Method, Path, Req) ->
%% HTTP Publish %% HTTP Publish
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
http_publish(Req) -> http_publish(Req, Params) ->
Params = [{iolist_to_binary(Key), Val} || {Key, Val} <- mochiweb_request:parse_post(Req)],
lager:debug("HTTP Publish: ~p", [Params]), lager:debug("HTTP Publish: ~p", [Params]),
Topics = topics(Params), Topics = topics(Params),
ClientId = get_value(<<"client">>, Params, http), ClientId = get_value(<<"client">>, Params, http),
@ -89,6 +89,9 @@ http_publish(Req) ->
Req:respond({400, [], <<"Bad Topics">>}) Req:respond({400, [], <<"Bad Topics">>})
end. end.
parse_params(Req) ->
[{iolist_to_binary(K), V} || {K, V} <- mochiweb_request:parse_post(Req)].
topics(Params) -> topics(Params) ->
Tokens = [get_value(<<"topic">>, Params) | string:tokens(get_value(<<"topics">>, Params, ""), ",")], Tokens = [get_value(<<"topic">>, Params) | string:tokens(get_value(<<"topics">>, Params, ""), ",")],
[iolist_to_binary(Token) || Token <- Tokens, Token =/= undefined]. [iolist_to_binary(Token) || Token <- Tokens, Token =/= undefined].
@ -111,9 +114,8 @@ validate(topic, Topic) ->
%% basic authorization %% basic authorization
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
authorized(Req) -> authorized(Req, Params) ->
Params = mochiweb_request:parse_post(Req), ClientId = get_value(<<"client">>, Params, http),
ClientId = get_value("client", Params, http),
case Req:get_header_value("Authorization") of case Req:get_header_value("Authorization") of
undefined -> undefined ->
false; false;
@ -121,8 +123,7 @@ authorized(Req) ->
{Username, Password} = user_passwd(BasicAuth), {Username, Password} = user_passwd(BasicAuth),
{ok, Peer} = Req:get(peername), {ok, Peer} = Req:get(peername),
case emqttd_access_control:auth(#mqtt_client{client_id = ClientId, username = Username, peername = Peer}, Password) of case emqttd_access_control:auth(#mqtt_client{client_id = ClientId, username = Username, peername = Peer}, Password) of
ok -> ok -> true;
true;
{ok, _IsSuper} -> {ok, _IsSuper} ->
true; true;
{error, Reason} -> {error, Reason} ->