Skip to content

Commit

Permalink
Move to emqx/emqx-auth-ldap
Browse files Browse the repository at this point in the history
  • Loading branch information
emqplus committed Jul 16, 2018
1 parent 33fe8fc commit 1e01b9f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 46 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

emqx_auth_ldap
=============
==============

LDAP Authentication Plugin for EMQ X Broker
EMQ X LDAP Authentication Plugin

Build
-----
Expand Down
1 change: 1 addition & 0 deletions priv/emqx_auth_ldap.schema
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%%-*- mode: erlang -*-
%% emqx_auth_ldap config mapping

{mapping, "auth.ldap.servers", "emqx_auth_ldap.ldap", [
{default, "127.0.0.1"},
{datatype, string}
Expand Down
17 changes: 13 additions & 4 deletions src/emqx_acl_ldap.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
%% Copyright (c) 2018 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Copyright (c) 2013-2017 EMQ Enterprise, Inc. All Rights Reserved.
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% @doc ACL with OpenLDAP
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

-module(emqx_acl_ldap).

Expand All @@ -25,7 +33,7 @@ init(AclDn) ->
{ok, #state{acl_dn = AclDn}}.

check_acl({#mqtt_client{username = <<$$, _/binary>>}, _PubSub, _Topic}, _State) ->
ignore;
ignore;

check_acl({Client, PubSub, Topic}, #state{acl_dn = AclDn}) ->
Filter = gen_filter(Client, AclDn),
Expand Down Expand Up @@ -58,7 +66,7 @@ compile(Attributes) ->

filter(PubSub, Rules) ->
[Term || Term = {_, _, Access, _} <- Rules, Access =:= PubSub orelse Access =:= pubsub].


allow(<<"1">>) -> allow;
allow(<<"0">>) -> deny.
Expand All @@ -77,3 +85,4 @@ reload_acl(_State) ->

description() ->
"ACL with LDAP".

9 changes: 2 additions & 7 deletions src/emqx_auth_ldap.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2013-2018 EMQ Enterprise, Inc. (http://emqtt.io)
%% Copyright (c) 2018 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand All @@ -12,15 +11,11 @@
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------

-module(emqx_auth_ldap).

-author("Feng Lee <feng@emqtt.io>").

-include_lib("eldap/include/eldap.hrl").

-include_lib("emqx/include/emqx.hrl").
-include_lib("eldap/include/eldap.hrl").

-import(proplists, [get_value/2, get_value/3]).

Expand Down
19 changes: 3 additions & 16 deletions src/emqx_auth_ldap_app.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2013-2018 EMQ Enterprise, Inc. (http://emqtt.io)
%% Copyright (c) 2018 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand All @@ -12,20 +11,15 @@
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------

-module(emqx_auth_ldap_app).

-behaviour(application).

%% Application callbacks
-export([start/2, prep_stop/1, stop/1]).

-include("emqx_auth_ldap.hrl").

%%--------------------------------------------------------------------
%% Application callbacks
%%--------------------------------------------------------------------
-export([start/2, prep_stop/1, stop/1]).

start(_StartType, _StartArgs) ->
{ok, Sup} = emqx_auth_ldap_sup:start_link(),
Expand All @@ -51,13 +45,6 @@ reg_authmod(AuthDn) ->
reg_aclmod(AclDn) ->
emqx_access_control:register_mod(acl, emqx_acl_ldap, AclDn).

%%--------------------------------------------------------------------
%% Internal function
%%--------------------------------------------------------------------

if_enabled(Cfg, Fun) ->
case application:get_env(?APP, Cfg) of
{ok, Dn} -> Fun(Dn);
undefined -> ok
end.
case application:get_env(?APP, Cfg) of {ok, Dn} -> Fun(Dn); undefined -> ok end.

5 changes: 2 additions & 3 deletions src/emqx_auth_ldap_cfg.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2013-2018 EMQ Enterprise, Inc. (http://emqtt.io)
%% Copyright (c) 2018 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand All @@ -12,7 +11,6 @@
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------

-module(emqx_auth_ldap_cfg).

Expand Down Expand Up @@ -71,3 +69,4 @@ keys() ->
"auth.ldap.timeout",
"auth.ldap.auth_dn",
"auth.ldap.password_hash"].

14 changes: 6 additions & 8 deletions src/emqx_auth_ldap_cli.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2013-2018 EMQ Enterprise, Inc. (http://emqtt.io)
%% Copyright (c) 2018 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand All @@ -12,7 +11,6 @@
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------

-module(emqx_auth_ldap_cli).

Expand Down Expand Up @@ -44,16 +42,17 @@ gen_filter(Client, Dn) ->
end.

%%--------------------------------------------------------------------
%% ldap Connect/Search
%% LDAP Connect/Search
%%--------------------------------------------------------------------

connect(Opts) ->
Servers = get_value(servers, Opts, ["localhost"]),
Port = get_value(port, Opts, 389),
Timeout = get_value(timeout, Opts, 30),
BindDn = get_value(bind_dn, Opts),
BindPassword = get_value(bind_password, Opts),
LdapOpts = case get_value(ssl, Opts, false) of
true ->
true ->
SslOpts = get_value(sslopts, Opts),
[{port, Port}, {timeout, Timeout}, {sslopts, SslOpts}];
false ->
Expand All @@ -63,8 +62,7 @@ connect(Opts) ->
case eldap:open(Servers, LdapOpts) of
{ok, LDAP} ->
case catch eldap:simple_bind(LDAP, BindDn, BindPassword) of
ok ->
{ok, LDAP};
ok -> {ok, LDAP};
{error, Error} ->
{error, Error};
{'EXIT', Reason} ->
Expand All @@ -76,4 +74,4 @@ connect(Opts) ->

search(Base, Filter) ->
ecpool:with_client(?APP, fun(C) -> eldap:search(C, [{base, Base}, {filter, Filter}]) end).

8 changes: 2 additions & 6 deletions src/emqx_auth_ldap_sup.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2013-2018 EMQ Enterprise, Inc. (http://emqtt.io)
%% Copyright (c) 2018 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand All @@ -12,7 +11,6 @@
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------

-module(emqx_auth_ldap_sup).

Expand All @@ -21,15 +19,13 @@
-include("emqx_auth_ldap.hrl").

-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->
%% ldap Connection Pool.
%% LDAP Connection Pool.
{ok, Server} = application:get_env(?APP, ldap),
PoolSpec = ecpool:pool_spec(?APP, ?APP, emqx_auth_ldap_cli, Server),
{ok, {{one_for_one, 10, 100}, [PoolSpec]}}.
Expand Down

0 comments on commit 1e01b9f

Please sign in to comment.