Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ wc-shell: dev-image
$(DOCKER_RUN) --interactive --tty $(DEV_IMAGE_TAG)

wc-%: dev-image
$(DOCKER_RUN) $(DEV_IMAGE_TAG) make $*
$(DOCKER_RUN) $(DEV_IMAGE_TAG) make $(if $(MAKE_ARGS),$(MAKE_ARGS) $*,$*)

# Pass CT_CASE through to container env
wc-common-test.%: MAKE_ARGS=$(if $(CT_CASE),CT_CASE=$(CT_CASE))


# Rebar tasks

Expand Down Expand Up @@ -74,6 +78,9 @@ eunit:
common-test:
$(REBAR) ct --cover

common-test.%: test/%.erl
$(REBAR) ct --cover --suite=$^ $(if $(CT_CASE),--case=$(strip $(CT_CASE)))

cover:
$(REBAR) covertool generate

Expand Down
4 changes: 2 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
{test, [
{deps, [
%% Libraries generated with swagger-codegen-erlang from valitydev/swag-wallets
{swag_server_wallet, {git, "https://github.com/valitydev/swag-wallets", {ref, "805f079"}}},
{swag_client_wallet, {git, "https://github.com/valitydev/swag-wallets", {ref, "0ca43f1"}}},
{swag_server_wallet, {git, "https://github.com/valitydev/swag-wallets", {ref, "96add0f"}}},
{swag_client_wallet, {git, "https://github.com/valitydev/swag-wallets", {ref, "5fd60dd"}}},
{meck, "0.9.2"}
]},
{cover_enabled, true},
Expand Down
8 changes: 4 additions & 4 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
0},
{<<"bouncer_proto">>,
{git,"https://github.com/valitydev/bouncer-proto.git",
{ref,"07dcc7b9b4599923b20bcf5f84642c3a1d6570e2"}},
{ref,"31866c36c049dc568d4bc795a641690db3cb20ab"}},
0},
{<<"cache">>,{pkg,<<"cache">>,<<"2.3.3">>},1},
{<<"certifi">>,{pkg,<<"certifi">>,<<"2.8.0">>},2},
{<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.9.0">>},1},
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.11.0">>},2},
{<<"damsel">>,
{git,"https://github.com/valitydev/damsel.git",
{ref,"24932cdc557a75bfb3a4aeb1738638366003aba4"}},
{ref,"ab44b9db25a76a2c50545fd884e4cdf3d3e3b628"}},
0},
{<<"dmt_client">>,
{git,"https://github.com/valitydev/dmt-client.git",
Expand All @@ -37,11 +37,11 @@
0},
{<<"fistful_proto">>,
{git,"https://github.com/valitydev/fistful-proto.git",
{ref,"54f1d580854d38429f56ce8cc14bac307eeb4ec7"}},
{ref,"8b04c7faca7393b9a6a5509205be383d68039907"}},
0},
{<<"fistful_reporter_proto">>,
{git,"https://github.com/valitydev/fistful-reporter-proto.git",
{ref,"9e3e82424f905841b4f2cd66b38a7b0727667ab2"}},
{ref,"6d5695d2e8aa13247f93451937adefa70c6edeca"}},
0},
{<<"genlib">>,
{git,"https://github.com/valitydev/genlib.git",
Expand Down
63 changes: 10 additions & 53 deletions src/wapi_backend_utils.erl
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
-module(wapi_backend_utils).

-include_lib("fistful_proto/include/fistful_fistful_base_thrift.hrl").
-include_lib("fistful_proto/include/fistful_identity_thrift.hrl").
-include_lib("fistful_proto/include/fistful_wallet_thrift.hrl").
-include_lib("fistful_proto/include/fistful_destination_thrift.hrl").
-include_lib("fistful_proto/include/fistful_w2w_transfer_thrift.hrl").
-include_lib("fistful_proto/include/fistful_wthd_thrift.hrl").

-define(EXTERNAL_ID, <<"externalID">>).
Expand Down Expand Up @@ -33,23 +30,13 @@
-type hash() :: integer().
-type params() :: map().
-type gen_type() ::
identity
| identity_challenge
| wallet
| destination
| withdrawal
| w2w_transfer.
destination
| withdrawal.
-type entity_type() ::
identity
| wallet
| destination
| withdrawal
| w2w_transfer.
destination
| withdrawal.
-type entity_state() ::
fistful_identity_thrift:'IdentityState'()
| fistful_wallet_thrift:'WalletState'()
| fistful_destination_thrift:'DestinationState'()
| fistful_w2w_transfer_thrift:'W2WTransferState'()
fistful_destination_thrift:'DestinationState'()
| fistful_wthd_thrift:'WithdrawalState'().

-export([gen_id/3]).
Expand All @@ -59,7 +46,6 @@
-export([add_to_ctx/3]).
-export([get_from_ctx/2]).
-export([get_idempotent_key/3]).
-export([issue_grant_token/3]).
-export([create_params_hash/1]).
-export([decode_resource/1]).
-export([tokenize_resource/1]).
Expand Down Expand Up @@ -112,7 +98,6 @@ gen_sequence_id(Type, IdempotentKey, Hash, #{woody_context := WoodyCtx}) ->
make_ctx(Params) ->
#{
?CTX_NS => genlib_map:compact(#{
<<"owner">> => maps:get(<<"partyID">>, Params),
<<"metadata">> => maps:get(<<"metadata">>, Params, undefined)
})
}.
Expand Down Expand Up @@ -153,24 +138,6 @@ get_from_ctx(Key, Ctx) ->
LegacyNSs
).

-spec issue_grant_token(_, binary(), handler_context()) -> {ok, binary()} | {error, expired}.
issue_grant_token(TokenSpec, Expiration, Context) ->
case get_expiration_deadline(Expiration) of
{ok, Deadline} ->
{ok, wapi_tokens_legacy:issue_access_token(wapi_handler_utils:get_owner(Context), TokenSpec, Deadline)};
Error = {error, _} ->
Error
end.

get_expiration_deadline(Expiration) ->
Deadline = genlib_rfc3339:parse(Expiration, second),
case genlib_time:unow() - Deadline < 0 of
true ->
{ok, Deadline};
false ->
{error, expired}
end.

-spec create_params_hash(term()) -> integer().
create_params_hash(Value) ->
erlang:phash2(Value).
Expand Down Expand Up @@ -213,19 +180,9 @@ tokenize_resource(Value) ->

-spec get_entity_owner(entity_type(), entity_state()) -> {ok, id()}.
get_entity_owner(Type, State) ->
{ok, get_context_owner(get_context_from_state(Type, State))}.

get_context_from_state(identity, #identity_IdentityState{context = Context}) ->
Context;
get_context_from_state(wallet, #wallet_WalletState{context = Context}) ->
Context;
get_context_from_state(destination, #destination_DestinationState{context = Context}) ->
Context;
get_context_from_state(w2w_transfer, #w2w_transfer_W2WTransferState{context = Context}) ->
Context;
get_context_from_state(withdrawal, #wthd_WithdrawalState{context = Context}) ->
Context.
{ok, get_owner(Type, State)}.

get_context_owner(ContextThrift) ->
Context = wapi_codec:unmarshal(context, ContextThrift),
wapi_backend_utils:get_from_ctx(<<"owner">>, Context).
get_owner(destination, #destination_DestinationState{party_id = PartyID}) ->
PartyID;
get_owner(withdrawal, #wthd_WithdrawalState{party_id = PartyID}) ->
PartyID.
60 changes: 13 additions & 47 deletions src/wapi_bouncer_context.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
-type prototype_operation() :: #{
id => operation_id(),
party => maybe_undefined(entity_id()),
identity => maybe_undefined(entity_id()),
wallet => maybe_undefined(entity_id()),
withdrawal => maybe_undefined(entity_id()),
deposit => maybe_undefined(entity_id()),
w2w_transfer => maybe_undefined(entity_id()),
source => maybe_undefined(entity_id()),
destination => maybe_undefined(entity_id()),
report => maybe_undefined(entity_id()),
Expand All @@ -36,33 +34,26 @@
-type prototype_wallet() :: [wallet_entity()].

-type wallet_entity() ::
{identity, identity_data()}
| {wallet, wallet_data()}
{wallet, wallet_data()}
| {withdrawal, withdrawal_data()}
| {deposit, deposit_data()}
| {w2w_transfer, w2w_transfer_data()}
| {source, source_data()}
| {destination, destination_data()}
| {webhook, webhook_data()}
| {report, report_data()}.

-type wallet_entity_type() ::
identity
party
| wallet
| withdrawal
| deposit
| w2w_transfer
| source
| destination
| webhook
| webhook_filter
| report
| report_file.

-type identity_data() :: #{
id => entity_id()
}.

-type wallet_data() :: #{
id => entity_id(),
party => entity_id(),
Expand All @@ -79,11 +70,6 @@
party => entity_id()
}.

-type w2w_transfer_data() :: #{
id => entity_id(),
party => entity_id()
}.

-type source_data() :: #{
id => entity_id(),
party => entity_id()
Expand All @@ -96,13 +82,13 @@

-type webhook_data() :: #{
id => entity_id(),
identity => entity_id(),
party => entity_id(),
wallet => entity_id()
}.

-type report_data() :: #{
id => entity_id(),
identity => entity_id(),
party => entity_id(),
files => [entity_id()]
}.

Expand Down Expand Up @@ -143,18 +129,15 @@ build(operation, #{id := OperationID} = Params, Acc) ->
op = #ctx_v1_WalletAPIOperation{
id = operation_id_to_binary(OperationID),
party = 'maybe'(party, Params),
identity = 'maybe'(identity, Params),
wallet = 'maybe'(wallet, Params),
withdrawal = 'maybe'(withdrawal, Params),
deposit = 'maybe'(deposit, Params),
w2w_transfer = 'maybe'(w2w_transfer, Params),
source = 'maybe'(source, Params),
destination = 'maybe'(destination, Params),
report = wapi_handler_utils:maybe_with(report, Params, fun genlib:to_binary/1),
file = 'maybe'(file, Params),
webhook = 'maybe'(webhook, Params)
},
grants = wapi_handler_utils:maybe_with(grants, Params, fun build_grants/1)
}
}
};
build(wallet, Params, Acc) when is_list(Params) ->
Expand Down Expand Up @@ -198,22 +181,23 @@ build_wallet_entity(Type, Params, {IDKey, ID}) ->

build_wallet_entity_(deposit, #{<<"wallet">> := WalletID}) ->
#{wallet => WalletID};
build_wallet_entity_(webhook, #{<<"identityID">> := Identity} = Webhook) ->
build_wallet_entity_(webhook, #{<<"partyID">> := Party} = Webhook) ->
Scope = 'maybe'(<<"scope">>, Webhook),
WalletID = 'maybe'(<<"walletID">>, Scope),
#{identity => Identity, wallet => WalletID};
#{party => Party, wallet => WalletID};
build_wallet_entity_(report, #{<<"files">> := Files}) ->
#{files => lists:map(fun(#{<<"id">> := FileID}) -> FileID end, Files)};
%% identity => IdentityID,
%% party => PartyID,
build_wallet_entity_(_, _) ->
#{}.

%%

build_entity_ctx({identity, Data}) ->
build_entity_ctx({party, Data}) ->
%% TODO: fix after edit bouncer proto
#base_Entity{
id = 'maybe'(id, Data),
type = <<"Identity">>,
type = <<"Party">>,
party = 'maybe'(party, Data)
};
build_entity_ctx({wallet, Data}) ->
Expand All @@ -239,12 +223,6 @@ build_entity_ctx({deposit, Data}) ->
wallet = 'maybe'(wallet, Data)
}
};
build_entity_ctx({w2w_transfer, Data}) ->
#base_Entity{
id = 'maybe'(id, Data),
type = <<"W2WTransfer">>,
party = 'maybe'(party, Data)
};
build_entity_ctx({source, Data}) ->
#base_Entity{
id = 'maybe'(id, Data),
Expand All @@ -261,17 +239,17 @@ build_entity_ctx({webhook, Data}) ->
#base_Entity{
id = 'maybe'(id, Data),
type = <<"WalletWebhook">>,
party = 'maybe'(party, Data),
wallet = #base_WalletAttrs{
identity = 'maybe'(identity, Data),
wallet = 'maybe'(wallet, Data)
}
};
build_entity_ctx({report, Data}) ->
#base_Entity{
id = 'maybe'(id, Data),
type = <<"WalletReport">>,
party = 'maybe'(party, Data),
wallet = #base_WalletAttrs{
identity = 'maybe'(identity, Data),
report = wapi_handler_utils:maybe_with(files, Data, fun build_report_attrs/1)
}
}.
Expand All @@ -286,18 +264,6 @@ build_entity_ctx({report, Data}) ->
operation_id_to_binary(V) ->
erlang:atom_to_binary(V, utf8).

build_grants(Grants) when is_list(Grants) ->
build_set(lists:map(fun build_grant/1, Grants)).

build_grant(Grant) ->
#ctx_v1_WalletGrant{
wallet = 'maybe'(wallet, Grant),
destination = 'maybe'(destination, Grant),
body = wapi_handler_utils:maybe_with(body, Grant, fun build_cash/1),
created_at = 'maybe'(created_at, Grant),
expires_on = 'maybe'(expires_on, Grant)
}.

build_cash(Cash) ->
#base_Cash{
amount = 'maybe'(amount, Cash),
Expand Down
Loading
Loading