Skip to content

Commit

Permalink
Moved request validation logic out of cb_users (2600hz#6500)
Browse files Browse the repository at this point in the history
Moved into kazoo documents users so validation logic can be used by
other parts of the code base.
  • Loading branch information
bradfordben authored and jamesaimonetti committed May 1, 2020
1 parent 5d0b15b commit d1c3ebe
Show file tree
Hide file tree
Showing 11 changed files with 652 additions and 504 deletions.
25 changes: 25 additions & 0 deletions applications/crossbar/src/cb_context.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
,add_system_error/2, add_system_error/3, add_system_error/4
,add_validation_error/4
,validate_request_data/2, validate_request_data/3, validate_request_data/4
,add_doc_validation_errors/2, update_successfully_validated_request/2
,add_content_types_provided/2
,add_content_types_accepted/2
,add_attachment_content_type/3
Expand Down Expand Up @@ -1207,3 +1208,27 @@ system_error(Context, Error) ->
]),
_ = kz_amqp_worker:cast(Notify, fun kapi_notifications:publish_system_alert/1),
add_system_error(Error, Context).

%%------------------------------------------------------------------------------
%% @doc Add kazoo_documents validation errors to a context.
%% @end
%%------------------------------------------------------------------------------
-spec add_doc_validation_errors(context(), kazoo_documents:doc_validation_errors()) -> context().
add_doc_validation_errors(Context, ValidationErrors) ->
lists:foldl(fun({Path, Reason, Msg}, C) -> add_validation_error(Path, Reason, Msg, C) end
,Context
,ValidationErrors
).

%%------------------------------------------------------------------------------
%% @doc After successful kazoo_documents validation, update the context with
%% the updated doc and set the response status to `success'
%% @end
%%------------------------------------------------------------------------------
-spec update_successfully_validated_request(context(), kz_doc:doc()) -> context().
update_successfully_validated_request(Context, Doc) ->
Updates = [{fun set_req_data/2, Doc}
,{fun set_doc/2, Doc}
,{fun set_resp_status/2, 'success'}
],
setters(Context, Updates).
6 changes: 2 additions & 4 deletions applications/crossbar/src/cb_modules_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ bind(Module, Bindings) ->

-spec pass_hashes(kz_term:ne_binary(), kz_term:ne_binary()) -> {kz_term:ne_binary(), kz_term:ne_binary()}.
pass_hashes(Username, Password) ->
Creds = list_to_binary([Username, ":", Password]),
SHA1 = kz_term:to_hex_binary(crypto:hash('sha', Creds)),
MD5 = kz_term:to_hex_binary(crypto:hash('md5', Creds)),
{MD5, SHA1}.
kzd_module_utils:pass_hashes(Username, Password).


-spec get_devices_owned_by(kz_term:ne_binary(), kz_term:ne_binary()) -> kz_json:objects().
get_devices_owned_by(OwnerID, DB) ->
Expand Down
9 changes: 0 additions & 9 deletions applications/crossbar/src/crossbar_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
,validate_auth_token/1, validate_auth_token/2
,authorize_auth_token/1
,reset_identity_secret/1
,has_identity_secret/1
,log_success_auth/4, log_success_auth/5, log_success_auth/6
,log_failed_auth/4, log_failed_auth/5, log_failed_auth/6
,get_inherited_config/1
Expand Down Expand Up @@ -193,14 +192,6 @@ reset_identity_secret(Context) ->
Doc = kz_auth_identity:reset_doc_secret(cb_context:doc(Context)),
cb_context:set_doc(Context, Doc).

%%------------------------------------------------------------------------------
%% @doc Check if user has a non-empty `pvt_signature_secret'
%% @end
%%------------------------------------------------------------------------------
-spec has_identity_secret(cb_context:context()) -> boolean().
has_identity_secret(Context) ->
kz_auth_identity:has_doc_secret(cb_context:doc(Context)).

%%------------------------------------------------------------------------------
%% @doc Get merge result of account and its parents, reseller and system
%% authentication configuration.
Expand Down
23 changes: 2 additions & 21 deletions applications/crossbar/src/crossbar_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1016,27 +1016,8 @@ handle_no_descendants(ViewOptions) ->
-spec format_emergency_caller_id_number(cb_context:context()) ->
cb_context:context().
format_emergency_caller_id_number(Context) ->
case cb_context:req_value(Context, [<<"caller_id">>, ?KEY_EMERGENCY]) of
'undefined' -> Context;
Emergency ->
format_emergency_caller_id_number(Context, Emergency)
end.

-spec format_emergency_caller_id_number(cb_context:context(), kz_json:object()) ->
cb_context:context().
format_emergency_caller_id_number(Context, Emergency) ->
case kz_json:get_ne_binary_value(<<"number">>, Emergency) of
'undefined' -> Context;
Number ->
NEmergencyJObj = kz_json:set_value(<<"number">>, knm_converters:normalize(Number), Emergency),
CallerIdJObj = cb_context:req_value(Context, <<"caller_id">>),
NCallerIdJObj = kz_json:set_value(?KEY_EMERGENCY, NEmergencyJObj, CallerIdJObj),

lager:debug("setting emergency caller id from ~s to ~s", [Number, knm_converters:normalize(Number)]),
cb_context:set_req_data(Context
,kz_json:set_value(<<"caller_id">>, NCallerIdJObj, cb_context:req_data(Context))
)
end.
Doc = cb_context:req_data(Context),
cb_context:set_req_data(Context, kzd_module_utils:maybe_normalize_emergency_caller_id_number(Doc)).

-type refresh_type() :: 'user' | 'device' | 'sys_info' | 'account'.

Expand Down
30 changes: 5 additions & 25 deletions applications/crossbar/src/modules/cb_accounts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -504,23 +504,21 @@ prepare_context(Account, Context) ->
cb_context:setters(Context, [{fun cb_context:set_account_id/2, AccountId}]).

%%------------------------------------------------------------------------------
%% @doc
%% @doc Validate the request JObj passes all validation checks and add / alter
%% any required fields.
%% @end
%%------------------------------------------------------------------------------
-spec validate_request(kz_term:api_ne_binary(), cb_context:context()) -> cb_context:context().
validate_request(AccountId, Context) ->
ReqJObj = cb_context:req_data(Context),

ParentId = get_parent_id_from_req(Context),
case kzd_accounts:validate(ParentId, AccountId, ReqJObj) of
{'true', AccountJObj} ->
lager:debug("validated account object"),
%% Some checks depend on private_fields like `pvt_tree'.
NewAccountJObj = maybe_add_pvt_fields(AccountId, AccountJObj),
update_validated_request(AccountId, Context, NewAccountJObj);
Context1 = cb_context:update_successfully_validated_request(Context, NewAccountJObj),
extra_validation(AccountId, Context1);
{'validation_errors', ValidationErrors} ->
lager:info("validation errors on account"),
add_validation_errors(Context, ValidationErrors);
cb_context:add_doc_validation_errors(Context, ValidationErrors);
{'system_error', Error} ->
lager:info("system error validating account: ~p", [Error]),
cb_context:add_system_error(Error, Context)
Expand All @@ -534,15 +532,6 @@ maybe_add_pvt_fields(AccountId, AccountJObj) -> %% Existing account (update)
%% Merge private_fields into req obj in order to allow checks to read and use them when needed.
kz_json:merge(kz_doc:private_fields(Existing), AccountJObj).

-spec update_validated_request(kz_term:ne_binary(), cb_context:context(), kzd_accounts:doc()) -> cb_context:context().
update_validated_request(AccountId, Context, AccountJObj) ->
Updates = [{fun cb_context:set_req_data/2, AccountJObj}
,{fun cb_context:set_doc/2, AccountJObj}
,{fun cb_context:set_resp_status/2, 'success'}
],
Context1 = cb_context:setters(Context, Updates),
extra_validation(AccountId, Context1).

-spec get_parent_id_from_req(cb_context:context()) -> kz_term:api_ne_binary().
get_parent_id_from_req(Context) ->
case props:get_value(<<"accounts">>, cb_context:req_nouns(Context)) of
Expand All @@ -554,15 +543,6 @@ get_parent_id_from_req(Context) ->
end
end.

add_validation_errors(Context, ValidationErrors) ->
lists:foldl(fun add_validation_error/2
,Context
,ValidationErrors
).

add_validation_error({Path, Reason, Msg}, Context) ->
cb_context:add_validation_error(Path, Reason, Msg, Context).

-spec extra_validation(kz_term:ne_binary(), cb_context:context()) -> cb_context:context().
extra_validation(AccountId, Context) ->
Extra = [fun(_, C) -> maybe_import_enabled(C) end
Expand Down
Loading

0 comments on commit d1c3ebe

Please sign in to comment.