fix(saml): sp sign request

This commit is contained in:
JimMoen 2023-09-22 21:13:04 +08:00
parent 2a8f3f9eaa
commit 6349cd3910
No known key found for this signature in database
GPG Key ID: 87A520B4F76BA86D
2 changed files with 43 additions and 17 deletions

View File

@ -12,7 +12,6 @@
]). ]).
start(_StartType, _StartArgs) -> start(_StartType, _StartArgs) ->
{ok, _} = application:ensure_all_started(esaml),
emqx_dashboard_sso_sup:start_link(). emqx_dashboard_sso_sup:start_link().
stop(_State) -> stop(_State) ->

View File

@ -91,31 +91,45 @@ desc(_) ->
%% APIs %% APIs
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
create( create(#{sp_sign_request := true} = Config) ->
try
do_create(ensure_cert_and_key(Config))
catch
Kind:Error ->
Msg = failed_to_ensure_cert_and_key,
?SLOG(error, #{msg => Msg, kind => Kind, error => Error}),
{error, Msg}
end;
create(#{sp_sign_request := false} = Config) ->
do_create(Config#{key => undefined, certificate => undefined}).
do_create(
#{ #{
dashboard_addr := DashboardAddr, dashboard_addr := DashboardAddr,
idp_metadata_url := IDPMetadataURL, idp_metadata_url := IDPMetadataURL,
sp_sign_request := SignRequest key := KeyPath,
certificate := CertPath
} = Config } = Config
) -> ) ->
{ok, _} = application:ensure_all_started(esaml),
BaseURL = binary_to_list(DashboardAddr) ++ "/api/v5", BaseURL = binary_to_list(DashboardAddr) ++ "/api/v5",
%% {Config, State} = parse_config(Config), Key = esaml_util:load_private_key(KeyPath),
Cert = esaml_util:load_certificate(CertPath),
SP = esaml_sp:setup(#esaml_sp{ SP = esaml_sp:setup(#esaml_sp{
%% TODO: save cert and key then return path key = Key,
%% TODO: #esaml_sp.key #esaml_sp.certificate support certificate = Cert,
%% key = PrivKey, sp_sign_requests = true,
%% certificate = Cert,
sp_sign_requests = SignRequest,
trusted_fingerprints = [], trusted_fingerprints = [],
consume_uri = BaseURL ++ "/sso/saml/acs", consume_uri = BaseURL ++ "/sso/saml/acs",
metadata_uri = BaseURL ++ "/sso/saml/metadata", metadata_uri = BaseURL ++ "/sso/saml/metadata",
%% TODO: support conf org and contact
org = #esaml_org{ org = #esaml_org{
name = "EMQX Team", name = "EMQX",
displayname = "EMQX Dashboard", displayname = "EMQX Dashboard",
url = DashboardAddr url = DashboardAddr
}, },
tech = #esaml_contact{ tech = #esaml_contact{
name = "EMQX Team", name = "EMQX",
email = "contact@emqx.io" email = "contact@emqx.io"
} }
}), }),
@ -124,14 +138,17 @@ create(
{ok, Config#{idp_meta => IdpMeta, sp => SP}} {ok, Config#{idp_meta => IdpMeta, sp => SP}}
catch catch
Kind:Error -> Kind:Error ->
?SLOG(error, #{msg => failed_to_load_metadata, kind => Kind, error => Error}), Reason = failed_to_load_metadata,
{error, failed_to_load_metadata} ?SLOG(error, #{msg => Reason, kind => Kind, error => Error}),
{error, Reason}
end. end.
update(_Config0, State) -> update(Config0, State) ->
{ok, State}. destroy(State),
create(Config0).
destroy(_State) -> destroy(_State) ->
_ = application:stop(esaml),
ok. ok.
login( login(
@ -184,8 +201,18 @@ do_validate_assertion(SP, DuplicateFun, Body) ->
%% Internal functions %% Internal functions
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% -define(DIR, <<"SAML_SSO_sp_certs">>). -define(DIR, <<"SAML_SSO_sp_certs">>).
%% -define(RSA_KEYS_A, [sp_public_key, sp_private_key]). -define(RSA_KEYS_A, [sp_public_key, sp_private_key]).
ensure_cert_and_key(Config) ->
case
emqx_tls_lib:ensure_ssl_files(?DIR, Config#{enable => ture}, #{required_keys => ?RSA_KEYS_A})
of
{ok, NConfig} ->
NConfig;
{error, #{which_options := [KeyPath | _]}} ->
error({missing_key, KeyPath})
end.
is_msie(Headers) -> is_msie(Headers) ->
UA = maps:get(<<"user-agent">>, Headers, <<"">>), UA = maps:get(<<"user-agent">>, Headers, <<"">>),