Expose context fragment baking (#3)

Also allow to pass IPs as `inet:ip_address()`es.
This commit is contained in:
Andrew Mayorov 2020-12-04 16:21:40 +03:00 committed by GitHub
parent 41f766feba
commit 4cb77d31e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 12 deletions

View File

@ -7,16 +7,18 @@
-export([judge/3]).
-export([bake_context_fragment/1]).
%%
-type woody_context() :: woody_context:ctx().
-type context_fragment_id() :: binary().
-type ruleset_id() :: binary().
-type encoded_bouncer_fragment() :: bouncer_context_thrift:'ContextFragment'().
-type encoded_context_fragment() :: bouncer_context_thrift:'ContextFragment'().
-type context_fragment() ::
bouncer_context_helpers:context_fragment()
| {encoded_fragment, encoded_bouncer_fragment()}.
| {encoded_fragment, encoded_context_fragment()}.
-type judge_context() :: #{
fragments => #{context_fragment_id() => context_fragment()}
@ -30,6 +32,7 @@
-export_type([judgement/0]).
-export_type([judge_context/0]).
-export_type([context_fragment/0]).
-export_type([encoded_context_fragment/0]).
-spec judge(ruleset_id(), judge_context(), woody_context()) -> judgement().
judge(RulesetID, JudgeContext, WoodyContext) ->
@ -71,12 +74,7 @@ collect_fragments(_, Context) ->
collect_fragments_(FragmentID, {encoded_fragment, EncodedFragment}, Acc0) ->
Acc0#{FragmentID => EncodedFragment};
collect_fragments_(FragmentID, ContextFragment = #bctx_v1_ContextFragment{}, Acc0) ->
Acc0#{
FragmentID => #bctx_ContextFragment{
type = v1_thrift_binary,
content = encode_context_fragment(ContextFragment)
}
}.
collect_fragments_(FragmentID, bake_context_fragment(ContextFragment), Acc0).
%%
@ -87,6 +85,14 @@ parse_judgement(#bdcs_Judgement{resolution = forbidden}) ->
%%
-spec bake_context_fragment(bouncer_context_helpers:context_fragment()) ->
{encoded_fragment, encoded_context_fragment()}.
bake_context_fragment(ContextFragment) ->
{encoded_fragment, #bctx_ContextFragment{
type = v1_thrift_binary,
content = encode_context_fragment(ContextFragment)
}}.
encode_context_fragment(ContextFragment) ->
Type = {struct, struct, {bouncer_context_v1_thrift, 'ContextFragment'}},
Codec = thrift_strict_binary_codec:new(),

View File

@ -20,7 +20,7 @@
-type method() :: binary().
-type email() :: binary().
-type timestamp() :: binary().
-type ip() :: string().
-type ip() :: inet:ip_address() | string() | binary().
-type context_fragment() :: bouncer_context_v1_thrift:'ContextFragment'().
-type woody_context() :: woody_context:ctx().
@ -248,7 +248,9 @@ maybe_marshal_user_role(Role) ->
)
}.
maybe_marshal_ip(IP) when is_tuple(IP) ->
list_to_binary(inet:ntoa(IP));
maybe_marshal_ip(IP) when is_list(IP) ->
list_to_binary(IP);
maybe_marshal_ip(undefined) ->
undefined;
maybe_marshal_ip(IP) ->
list_to_binary(IP).
undefined.