Use rabbit_json library to produce json representations
This commit is contained in:
parent
fe3d65002d
commit
67638fa535
|
|
@ -22,68 +22,9 @@ bootstrap_oauth(Req0, State) ->
|
|||
JSContent = oauth_initialize_if_required() ++ set_token_auth(Req0),
|
||||
{ok, cowboy_req:reply(200, #{<<"content-type">> => <<"text/javascript; charset=utf-8">>}, JSContent, Req0), State}.
|
||||
|
||||
authSettings() ->
|
||||
EnableOAUTH = application:get_env(rabbitmq_management, oauth_enabled, false),
|
||||
Data = case EnableOAUTH of
|
||||
true ->
|
||||
OAuthInitiatedLogonType = application:get_env(rabbitmq_management, oauth_initiated_logon_type, sp_initiated),
|
||||
OAuthProviderUrl = application:get_env(rabbitmq_management, oauth_provider_url, ""),
|
||||
case OAuthInitiatedLogonType of
|
||||
sp_initiated ->
|
||||
OAuthClientId = application:get_env(rabbitmq_management, oauth_client_id, ""),
|
||||
OAuthClientSecret = application:get_env(rabbitmq_management, oauth_client_secret, undefined),
|
||||
OAuthMetadataUrl = application:get_env(rabbitmq_management, oauth_metadata_url, undefined),
|
||||
OAuthScopes = application:get_env(rabbitmq_management, oauth_scopes, undefined),
|
||||
OAuthResourceId = application:get_env(rabbitmq_auth_backend_oauth2, resource_server_id, ""),
|
||||
case is_invalid([OAuthResourceId]) of
|
||||
true ->
|
||||
json_field(oauth_enabled, false, true);
|
||||
false ->
|
||||
case is_invalid([OAuthClientId, OAuthProviderUrl]) of
|
||||
true ->
|
||||
json_field(oauth_enabled, false, true);
|
||||
false ->
|
||||
json_field(oauth_enabled, true) ++
|
||||
json_field(oauth_client_id, OAuthClientId) ++
|
||||
json_field(oauth_client_secret, OAuthClientSecret) ++
|
||||
json_field(oauth_provider_url, OAuthProviderUrl) ++
|
||||
json_field(oauth_scopes, OAuthScopes) ++
|
||||
json_field(oauth_metadata_url, OAuthMetadataUrl) ++
|
||||
json_field(oauth_resource_id, OAuthResourceId, true)
|
||||
end
|
||||
end;
|
||||
idp_initiated ->
|
||||
[ json_field(oauth_enabled, true) ++
|
||||
json_field(oauth_initiated_logon_type, idp_initiated) ++
|
||||
json_field(oauth_provider_url, OAuthProviderUrl, true)
|
||||
]
|
||||
end;
|
||||
false ->
|
||||
[ json_field(oauth_enabled, false, true) ]
|
||||
end,
|
||||
"{" ++ Data ++ "}".
|
||||
|
||||
is_invalid(List) ->
|
||||
lists:any(fun(V) -> V == "" end, List).
|
||||
|
||||
json_field(Field, Value) -> json_field(Field, Value, false).
|
||||
|
||||
json_field(_Field, Value, _LastField) when Value == undefined -> [ ];
|
||||
json_field(Field, Value, LastField) when is_number(Value) ->
|
||||
["\"", atom_to_list(Field), "\": ", Value, append_comma_if(not LastField)];
|
||||
json_field(Field, Value, LastField) when is_boolean(Value) ->
|
||||
["\"", atom_to_list(Field), "\": ", atom_to_list(Value), append_comma_if(not LastField)];
|
||||
json_field(Field, Value, LastField) when is_atom(Value) ->
|
||||
["\"", atom_to_list(Field), "\": \"", atom_to_list(Value), "\"", append_comma_if(not LastField)];
|
||||
json_field(Field, Value, LastField) ->
|
||||
["\"", atom_to_list(Field), "\": \"", Value, "\"", append_comma_if(not LastField)].
|
||||
|
||||
|
||||
append_comma_if(Append) when Append == true -> ",";
|
||||
append_comma_if(Append) when Append == false -> "".
|
||||
|
||||
oauth_initialize_if_required() ->
|
||||
"function oauth_initialize_if_required() { return oauth_initialize(" ++ authSettings() ++ ") }".
|
||||
["function oauth_initialize_if_required() { return oauth_initialize(" ,
|
||||
rabbit_json:encode(rabbit_mgmt_format:format_nulls(rabbit_mgmt_wm_auth:authSettings())) , ") }" ].
|
||||
|
||||
set_token_auth(Req0) ->
|
||||
case application:get_env(rabbitmq_management, oauth_enabled, false) of
|
||||
|
|
|
|||
Loading…
Reference in New Issue