From 99534e7212cecc2b829631cd2cffa165ee14391e Mon Sep 17 00:00:00 2001 From: zhouzb Date: Tue, 3 Aug 2021 10:25:04 +0800 Subject: [PATCH] feat(authn api): support query authentication status --- apps/emqx_authn/src/emqx_authn.erl | 9 +++++++++ apps/emqx_authn/src/emqx_authn_api.erl | 26 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/apps/emqx_authn/src/emqx_authn.erl b/apps/emqx_authn/src/emqx_authn.erl index 27f3eab10..0f734fb30 100644 --- a/apps/emqx_authn/src/emqx_authn.erl +++ b/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}] -> diff --git a/apps/emqx_authn/src/emqx_authn_api.erl b/apps/emqx_authn/src/emqx_authn_api.erl index fe53629de..63c536d5e 100644 --- a/apps/emqx_authn/src/emqx_authn_api.erl +++ b/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),