Просмотр исходного кода

feat(authn api): support query authentication status

zhouzb 4 лет назад
Родитель
Сommit
99534e7212
2 измененных файлов с 34 добавлено и 1 удалено
  1. 9 0
      apps/emqx_authn/src/emqx_authn.erl
  2. 25 1
      apps/emqx_authn/src/emqx_authn_api.erl

+ 9 - 0
apps/emqx_authn/src/emqx_authn.erl

@@ -20,6 +20,7 @@
 
 -export([ enable/0
         , disable/0
+        , is_enabled/0
         ]).
 
 -export([authenticate/2]).
@@ -84,6 +85,14 @@ disable() ->
     emqx:unhook('client.authenticate', {?MODULE, authenticate, []}),
     ok.
 
+is_enabled() ->
+    Callbacks = emqx_hooks:lookup('client.authenticate'),
+    lists:any(fun({callback, {?MODULE, authenticate, []}, _, _}) ->
+                  true;
+                 (_) ->
+                  false
+              end, Callbacks).
+
 authenticate(Credential, _AuthResult) ->
     case mnesia:dirty_read(?CHAIN_TAB, ?CHAIN) of
         [#chain{authenticators = Authenticators}] ->

+ 25 - 1
apps/emqx_authn/src/emqx_authn_api.erl

@@ -131,6 +131,27 @@ authentication_api() ->
                 },
                 <<"400">> => ?ERR_RESPONSE(<<"Bad Request">>)
             }
+        },
+        get => #{
+            description => "Get status of authentication",
+            responses => #{
+                <<"200">> => #{
+                    description => <<"OK">>,
+                    content => #{
+                        'application/json' => #{
+                            schema => #{
+                                type => object,
+                                properties => #{
+                                    enabled => #{
+                                        type => boolean,
+                                        example => true
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
         }
     },
     {"/authentication", Metadata, authentication}.
@@ -1153,7 +1174,10 @@ authentication(post, Request) ->
             serialize_error({invalid_parameter, enable});
         _ ->
             serialize_error({missing_parameter, enable})
-    end.
+    end;
+authentication(get, _Request) ->
+    Enabled = emqx_authn:is_enabled(),
+    {200, #{enabled => Enabled}}.
 
 authenticators(post, Request) ->
     {ok, Body, _} = cowboy_req:read_body(Request),