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
10 changes: 2 additions & 8 deletions apps/capi/src/capi_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
-type consumer() :: client | merchant | provider.
-type token_spec() :: #{
party := binary(),
scope := {invoice | invoice_template | customer, binary()},
scope := {invoice | invoice_template, binary()},
shop => binary(),
lifetime => pos_integer() | unlimited,
metadata => token_keeper_client:metadata()
Expand Down Expand Up @@ -132,7 +132,6 @@ issue_access_token(TokenSpec, WoodyContext) ->
%%

-define(DEFAULT_INVOICE_ACCESS_TOKEN_LIFETIME, 259200).
-define(DEFAULT_CUSTOMER_ACCESS_TOKEN_LIFETIME, 259200).

-include_lib("bouncer_proto/include/bouncer_ctx_v1_thrift.hrl").

Expand Down Expand Up @@ -169,7 +168,6 @@ resolve_auth_scope(TokenSpec) ->
).

resolve_auth_method(#{scope := {invoice, _}}) -> ?CTX_V1_AUTHMETHOD_INVOICEACCESSTOKEN;
resolve_auth_method(#{scope := {customer, _}}) -> ?CTX_V1_AUTHMETHOD_CUSTOMERACCESSTOKEN;
resolve_auth_method(#{scope := {invoice_template, _}}) -> ?CTX_V1_AUTHMETHOD_INVOICETEMPLATEACCESSTOKEN.

resolve_auth_expiration(TokenSpec) ->
Expand All @@ -187,13 +185,9 @@ get_token_lifetime(#{lifetime := LifeTime} = TokenSpec) when LifeTime =/= undefi
get_token_lifetime(#{scope := {invoice, _}}) ->
?DEFAULT_INVOICE_ACCESS_TOKEN_LIFETIME;
get_token_lifetime(#{scope := {invoice_template, _}}) ->
unlimited;
get_token_lifetime(#{scope := {customer, _}}) ->
?DEFAULT_CUSTOMER_ACCESS_TOKEN_LIFETIME.
unlimited.

%% Forbid creation of unlimited lifetime invoice and customer tokens
verify_token_lifetime(#{scope := {invoice, _}}, LifeTime) when LifeTime =/= unlimited -> ok;
verify_token_lifetime(#{scope := {customer, _}}, LifeTime) when LifeTime =/= unlimited -> ok;
verify_token_lifetime(#{scope := {invoice_template, _}}, _LifeTime) -> ok.

%%
Expand Down
43 changes: 1 addition & 42 deletions apps/capi/src/capi_bouncer_context.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,16 @@
id => swag_server:operation_id(),
party => entity_id(),
shop => entity_id(),
contract => entity_id(),
invoice => entity_id(),
payment => entity_id(),
refund => entity_id(),
invoice_template => entity_id(),
customer => entity_id(),
binding => entity_id(),
file => entity_id(),
webhook => entity_id()
}.

-type prototype_payproc() :: #{
invoice => invoice_id() | invoice() | undefined,
invoice_template => invoice_template_id() | invoice_template() | undefined,
customer => customer_id() | customer() | undefined
invoice_template => invoice_template_id() | invoice_template() | undefined
}.

-type prototype_webhooks() :: #{
Expand All @@ -53,9 +48,6 @@
-type invoice_template_id() :: dmsl_domain_thrift:'InvoiceTemplateID'().
-type invoice_template() :: dmsl_domain_thrift:'InvoiceTemplate'().

-type customer_id() :: dmsl_payproc_thrift:'CustomerID'().
-type customer() :: dmsl_payproc_thrift:'Customer'().

-type webhook_id() :: dmsl_webhooker_thrift:'WebhookID'().
-type webhook() :: dmsl_webhooker_thrift:'Webhook'().

Expand Down Expand Up @@ -93,14 +85,10 @@ build(operation, #{id := OperationID} = Params, Acc, _WoodyCtx) ->
id = operation_id_to_binary(OperationID),
party = maybe_entity(party, Params),
shop = maybe_entity(shop, Params),
contract = maybe_entity(contract, Params),
invoice = maybe_entity(invoice, Params),
payment = maybe_entity(payment, Params),
refund = maybe_entity(refund, Params),
invoice_template = maybe_entity(invoice_template, Params),
customer = maybe_entity(customer, Params),
binding = maybe_entity(binding, Params),
file = maybe_entity(file, Params),
webhook = maybe_entity(webhook, Params)
}
}
Expand All @@ -117,11 +105,6 @@ build(payproc, #{} = Params, Acc, WoodyCtx) ->
invoice_template,
Params,
fun(V) -> build_invoice_template_ctx(V, WoodyCtx) end
),
customer = maybe_with(
customer,
Params,
fun(V) -> build_customer_ctx(V, WoodyCtx) end
)
}
};
Expand Down Expand Up @@ -184,28 +167,6 @@ build_invoice_template_ctx(#domain_InvoiceTemplate{id = ID, owner_id = OwnerID,
shop = build_entity(ShopID)
}.

build_customer_ctx(ID, WoodyCtx) when is_binary(ID) ->
maybe_with_woody_result(
customer_management,
'Get',
{ID, #payproc_EventRange{}},
WoodyCtx,
fun build_customer_ctx/1
);
build_customer_ctx(Customer, _WoodyCtx) ->
build_customer_ctx(Customer).

build_customer_ctx(#payproc_Customer{id = ID, owner_id = OwnerID, shop_id = ShopID, bindings = Bindings}) ->
#ctx_v1_Customer{
id = ID,
party = build_entity(OwnerID),
shop = build_entity(ShopID),
bindings = build_set(lists:map(fun build_binding_ctx/1, Bindings))
}.

build_binding_ctx(#payproc_CustomerBinding{id = ID}) ->
build_entity(ID).

%%

build_webhook_ctx(ID, WoodyCtx) when is_integer(ID) ->
Expand All @@ -230,8 +191,6 @@ build_webhook_filter_details(#webhooker_PartyEventFilter{}, Ctx) ->
Ctx;
build_webhook_filter_details(#webhooker_InvoiceEventFilter{shop_id = ShopID}, Ctx) ->
Ctx#ctx_v1_WebhookFilter{shop = 'maybe'(ShopID, fun build_entity/1)};
build_webhook_filter_details(#webhooker_CustomerEventFilter{shop_id = ShopID}, Ctx) ->
Ctx#ctx_v1_WebhookFilter{shop = 'maybe'(ShopID, fun build_entity/1)};
build_webhook_filter_details(#webhooker_WalletEventFilter{}, Ctx) ->
Ctx.

Expand Down
28 changes: 27 additions & 1 deletion apps/capi/src/capi_domain.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
-include_lib("damsel/include/dmsl_domain_thrift.hrl").
-include_lib("damsel/include/dmsl_domain_conf_thrift.hrl").

-export([head/0]).
-export([get_payment_institution/2]).
-export([get_payment_institutions/1]).
-export([get/2]).
-export([get/3]).
-export([get_ext/3]).
-export([get_objects_by_type/2]).
-export([encode_enum/2]).
-export([encode_enum/3]).
Expand All @@ -14,13 +17,19 @@
-type processing_context() :: capi_handler:processing_context().
-type ref() :: dmsl_domain_thrift:'Reference'().
-type data() :: _.
-type revision() :: dmt_client:version().

-type payment_institution() :: dmsl_domain_thrift:'PaymentInstitution'().
-type payment_institution_ref() :: dmsl_domain_thrift:'PaymentInstitutionRef'().
-type payment_institution_object() :: dmsl_domain_thrift:'PaymentInstitutionObject'().
-type realm() :: dmsl_domain_thrift:'PaymentInstitutionRealm'().

-export_type([realm/0]).
-export_type([revision/0]).

-spec head() -> revision().
head() ->
dmt_client:get_last_version().

-spec get_payment_institution(payment_institution_ref(), processing_context()) ->
{ok, payment_institution()} | {error, not_found}.
Expand Down Expand Up @@ -67,15 +76,32 @@ get_payment_institutions(Context) ->

-spec get(ref(), processing_context() | undefined) -> {ok, data()} | {error, not_found}.
get(Ref, Context) ->
get(Ref, latest, Context).

-spec get(ref(), revision(), processing_context() | undefined) -> {ok, data()} | {error, not_found}.
get(Ref, Revision, Context) ->
try
Opts = make_opts(Context),
{_Type, Object} = dmt_client:checkout_object(latest, Ref, Opts),
{_Type, Object} = dmt_client:checkout_object(Revision, Ref, Opts),
{ok, Object}
catch
throw:#'domain_conf_ObjectNotFound'{} ->
{error, not_found}
end.

-spec get_ext(ref(), revision(), processing_context() | undefined) -> {ok, data()} | {error, not_found}.
get_ext(Ref, Revision, Context) ->
try
Opts = make_opts(Context),
{ok, extract_data(dmt_client:checkout_object(Revision, Ref, Opts))}
catch
throw:#'domain_conf_ObjectNotFound'{} ->
{error, not_found}
end.

extract_data({_Tag, {_Name, _Ref, Data}}) ->
Data.

-spec encode_enum(Type :: atom(), binary()) -> {ok, atom()} | {error, unknown_atom | unknown_variant}.
encode_enum(Type, Binary) ->
encode_enum(dmsl_domain_thrift, Type, Binary).
Expand Down
Loading
Loading