rabbitmq-server/deps/rabbitmq_mqtt/test/rabbit_auth_backend_mqtt_mo...

55 lines
1.7 KiB
Erlang

%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%
%% A mock authn/authz that records information during calls. For testing purposes only.
-module(rabbit_auth_backend_mqtt_mock).
-include_lib("rabbit_common/include/rabbit.hrl").
-behaviour(rabbit_authn_backend).
-behaviour(rabbit_authz_backend).
-export([setup/1,
user_login_authentication/2, user_login_authorization/2,
check_vhost_access/3, check_resource_access/4, check_topic_access/4,
expiry_timestamp/1,
get/1]).
setup(CallerPid) ->
ets:new(?MODULE, [set, public, named_table]),
CallerPid ! {ok, self()},
receive
stop -> ok
end.
user_login_authentication(_Username, AuthProps) ->
ets:insert(?MODULE, {authentication, AuthProps}),
{ok, #auth_user{username = <<"dummy">>,
tags = [],
impl = none}}.
user_login_authorization(_, _) ->
io:format("login authorization"),
{ok, does_not_matter}.
check_vhost_access(#auth_user{}, _VHostPath, AuthzData) ->
ets:insert(?MODULE, {vhost_access, AuthzData}),
true.
check_resource_access(#auth_user{}, #resource{}, _Permission, AuthzContext) ->
ets:insert(?MODULE, {resource_access, AuthzContext}),
true.
check_topic_access(#auth_user{}, #resource{}, _Permission, TopicContext) ->
ets:insert(?MODULE, {topic_access, TopicContext}),
true.
expiry_timestamp(_) ->
never.
get(K) ->
ets:lookup(?MODULE, K).