|
| 1 | +%% This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +%% License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +%% file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +%% |
| 5 | +%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(rabbit_auth_backend_ldap_mgmt). |
| 9 | + |
| 10 | +-behaviour(rabbit_mgmt_extension). |
| 11 | + |
| 12 | +-export([dispatcher/0, web_ui/0]). |
| 13 | + |
| 14 | +-export([init/2, |
| 15 | + content_types_accepted/2, |
| 16 | + allowed_methods/2, |
| 17 | + resource_exists/2, |
| 18 | + is_authorized/2, |
| 19 | + accept_content/2]). |
| 20 | + |
| 21 | + |
| 22 | +-include_lib("kernel/include/logger.hrl"). |
| 23 | +-include_lib("rabbitmq_web_dispatch/include/rabbitmq_web_dispatch_records.hrl"). |
| 24 | + |
| 25 | +dispatcher() -> [{"/ldap/validate/bind/:name", ?MODULE, []}]. |
| 26 | + |
| 27 | +web_ui() -> []. |
| 28 | + |
| 29 | +%%-------------------------------------------------------------------- |
| 30 | + |
| 31 | +init(Req, _Opts) -> |
| 32 | + {cowboy_rest, rabbit_mgmt_cors:set_headers(Req, ?MODULE), #context{}}. |
| 33 | + |
| 34 | +content_types_accepted(ReqData, Context) -> |
| 35 | + {[{'*', accept_content}], ReqData, Context}. |
| 36 | + |
| 37 | +allowed_methods(ReqData, Context) -> |
| 38 | + {[<<"HEAD">>, <<"PUT">>, <<"OPTIONS">>], ReqData, Context}. |
| 39 | + |
| 40 | +resource_exists(ReqData, Context) -> |
| 41 | + {true, ReqData, Context}. |
| 42 | + |
| 43 | +is_authorized(ReqData, Context) -> |
| 44 | + rabbit_mgmt_util:is_authorized(ReqData, Context). |
| 45 | + |
| 46 | +accept_content(ReqData0, Context) -> |
| 47 | + F = fun (_Values, BodyMap, ReqData1) -> |
| 48 | + _Name = name(ReqData1), |
| 49 | + Port = rabbit_mgmt_util:parse_int(maps:get(port, BodyMap, 389)), |
| 50 | + _UseSsl = rabbit_mgmt_util:parse_bool(maps:get(use_ssl, BodyMap, false)), |
| 51 | + _UseStartTls = rabbit_mgmt_util:parse_bool(maps:get(use_starttls, BodyMap, false)), |
| 52 | + Servers = maps:get(servers, BodyMap, []), |
| 53 | + _Password = maps:get(password, BodyMap, <<"">>), |
| 54 | + Options = [ |
| 55 | + {port, Port}, |
| 56 | + {timeout, 5000}, |
| 57 | + {ssl, false} |
| 58 | + ], |
| 59 | + ?LOG_DEBUG("eldap:open Servers: ~tp Options: ~tp", [Servers, Options]), |
| 60 | + case eldap:open(Servers, Options) of |
| 61 | + {ok, H} -> |
| 62 | + eldap:close(H), |
| 63 | + {true, ReqData1, Context}; |
| 64 | + {error, E} -> |
| 65 | + Reason = unicode_format(E), |
| 66 | + rabbit_mgmt_util:bad_request(Reason, ReqData1, Context) |
| 67 | + end |
| 68 | + end, |
| 69 | + rabbit_mgmt_util:with_decode([], ReqData0, Context, F). |
| 70 | + |
| 71 | +%%-------------------------------------------------------------------- |
| 72 | + |
| 73 | +name(ReqData) -> |
| 74 | + case rabbit_mgmt_util:id(name, ReqData) of |
| 75 | + [Value] -> Value; |
| 76 | + Value -> Value |
| 77 | + end. |
| 78 | + |
| 79 | +unicode_format(Arg) -> |
| 80 | + rabbit_data_coercion:to_utf8_binary(io_lib:format("~tp", [Arg])). |
0 commit comments