MSPF-320: Fix review comments

This commit is contained in:
Dmitry Manik 2017-12-08 20:53:00 +03:00 committed by Andrew Mayorov
parent 942ffed841
commit 69ec14439a
5 changed files with 98 additions and 43 deletions

View File

@ -1,10 +1,9 @@
-module(shortener_auth).
-export([authorize_api_key/2]).
-export([authorize_operation/1]).
-type context() :: capi_authorizer_jwt:t().
-type claims() :: capi_authorizer_jwt:claims().
-type context() :: shortener_authorizer_jwt:t().
-type claims() :: shortener_authorizer_jwt:claims().
-export_type([context/0]).
-export_type([claims/0]).
@ -54,20 +53,4 @@ authorize_api_key(_OperationID, bearer, Token) ->
% NOTE
% We are knowingly delegating actual request authorization to the logic handler
% so we could gather more data to perform fine-grained access control.
capi_authorizer_jwt:verify(Token).
%%
-spec authorize_operation(Auth :: capi_authorizer_jwt:t()) ->
ok | {error, unauthorized}.
authorize_operation({{_SubjectID, ACL}, _}) ->
{Scope, Permission} = {shortened_urls, write},
case lists:member(Permission, capi_acl:match(Scope, ACL)) of
true ->
ok;
false ->
{error, unauthorized}
end.
%%
shortener_authorizer_jwt:verify(Token).

View File

@ -283,15 +283,6 @@ check_expiration(C, V) ->
%%
% encode_roles(Roles) ->
% #{
% <<"resource_access">> => #{
% <<"common-api">> => #{
% <<"roles">> => Roles
% }
% }
% }.
decode_roles(Claims = #{
<<"resource_access">> := #{
<<"shortener">> := #{

View File

@ -17,11 +17,10 @@
-define(REALM, <<"external">>).
-spec authorize_api_key(swag_server:operation_id(), swag_server:api_key()) ->
Result :: false | {true, capi_auth:context()}.
Result :: false | {true, shortener_auth:context()}.
authorize_api_key(OperationID, ApiKey) ->
_ = capi_utils:logtag_process(operation_id, OperationID),
capi_auth:authorize_api_key(OperationID, ApiKey).
shortener_auth:authorize_api_key(OperationID, ApiKey).
-type request_data() :: #{atom() | binary() => term()}.
@ -34,7 +33,6 @@ authorize_api_key(OperationID, ApiKey) ->
handle_request(OperationID, Req, Context) ->
_ = lager:info("Processing request ~p", [OperationID]),
% Auth should be here
ReqContext = create_context(Req, get_auth_context(Context)),
process_request(OperationID, Req, Context, ReqContext).
@ -77,7 +75,7 @@ process_request('DeleteShortenedUrl', Req, Context, ReqCtx) ->
shorten_url(Params, _Context, ReqCtx) ->
ShortenedUrl = create_shortened_url(Params),
{ok, _Result} = shortener_automaton_client:call(?NS, ShortenedUrl, marshal(ShortenedUrl), ReqCtx),
{ok, _Result} = shortener_automaton_client:start(?NS, ShortenedUrl, marshal(ShortenedUrl), ReqCtx),
{ok, ShortenedUrl}.
get_shortened_url(ID, _Context, ReqCtx) ->

View File

@ -0,0 +1,92 @@
-module(shortener_processor).
-behaviour(woody_server_thrift_handler).
-export([handle_function/4]).
%%
-include_lib("mg_proto/include/mg_proto_state_processing_thrift.hrl").
-type id() :: mg_proto_base_thrift:'ID'().
-type tag() :: {tag, mg_proto_base_thrift:'Tag'()}.
-type ref() :: id() | tag().
-type ns() :: mg_proto_base_thrift:'Namespace'().
-type args() :: _.
-type context() :: woody_context:ctx().
-type handler_opts() :: #{
handler => module(),
user_identity => undefined | woody_user_identity:user_identity()
}.
-type client_opts() :: #{
url := woody:url(),
transport_opts => [{_, _}]
}.
%%
-spec start(ns(), id(), term()) ->
{ok, term()} | {error, exists | term()} | no_return().
start(Ns, ID, Args) ->
call_automaton('Start', [Ns, ID, wrap_args(Args)]).
-spec call(ns(), ref(), term()) ->
{ok, term()} | {error, notfound | failed} | no_return().
call(Ns, Ref, Args) ->
Descriptor = prepare_descriptor(Ns, Ref, #'HistoryRange'{}),
case call_automaton('Call', [Descriptor, wrap_args(Args)]) of
{ok, Response} ->
% should be specific to a processing interface already
{ok, unmarshal_term(Response)};
{error, _} = Error ->
Error
end.
-spec get_history(ns(), ref()) ->
{ok, history()} | {error, notfound} | no_return().
get_history(Ns, Ref) ->
get_history(Ns, Ref, #'HistoryRange'{}).
-spec get_history(ns(), ref(), undefined | event_id(), undefined | non_neg_integer()) ->
{ok, history()} | {error, notfound} | no_return().
get_history(Ns, Ref, AfterID, Limit) ->
get_history(Ns, Ref, #'HistoryRange'{'after' = AfterID, limit = Limit}).
get_history(Ns, Ref, Range) ->
Descriptor = prepare_descriptor(Ns, Ref, Range),
case call_automaton('GetMachine', [Descriptor]) of
{ok, #'Machine'{history = History}} when is_list(History) ->
{ok, unmarshal_events(History)};
Error ->
Error
end.
%%
-type func() :: 'ProcessSignal' | 'ProcessCall'.
-spec handle_function(func(), woody:args(), handler_opts()) ->
term() | no_return().
handle_function(Func, Args, Opts) ->
scoper:scope(machine,
fun() -> handle_function_(Func, Args, Opts) end
).
-spec handle_function_(func(), woody:args(), #{ns := ns()}) -> term() | no_return().
handle_function_('ProcessSignal', [Args], #{ns := Ns} = _Opts) ->
#'SignalArgs'{signal = {_Type, Signal}, machine = Machine} = Args,
% dispatch_signal(Ns, Signal, Machine);
handle_function_('ProcessCall', [Args], #{ns := Ns} = _Opts) ->
#'CallArgs'{arg = Payload, machine = Machine} = Args,
% dispatch_call(Ns, Payload, Machine).

View File

@ -1,9 +0,0 @@
-module(shortener_utils).
-export([unique_id/0]).
-spec unique_id() -> dmsl_base_thrift:'ID'().
unique_id() ->
<<ID:64>> = snowflake:new(),
genlib_format:format_int_base(ID, 62).