1%% This Source Code Form is subject to the terms of the Mozilla Public
2%% License, v. 2.0. If a copy of the MPL was not distributed with this
3%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4%%
5%% Copyright (c) 2007-2021 VMware, Inc. or its affiliates.  All rights reserved.
6%%
7
8-module(rabbit_credential_validation).
9
10-include_lib("rabbit_common/include/rabbit.hrl").
11
12%% used for backwards compatibility
13-define(DEFAULT_BACKEND, rabbit_credential_validator_accept_everything).
14
15%%
16%% API
17%%
18
19-export([validate/2, backend/0]).
20
21%% Validates a username/password pair by delegating to the effective
22%% `rabbit_credential_validator`. Used by `rabbit_auth_backend_internal`.
23%% Note that some validators may choose to only validate passwords.
24%%
25%% Possible return values:
26%%
27%% * ok: provided credentials passed validation.
28%% * {error, Error, Args}: provided password password failed validation.
29
30-spec validate(rabbit_types:username(), rabbit_types:password()) -> 'ok' | {'error', string()}.
31
32validate(Username, Password) ->
33    Backend = backend(),
34    Backend:validate(Username, Password).
35
36-spec backend() -> atom().
37
38backend() ->
39  case application:get_env(rabbit, credential_validator) of
40    undefined      ->
41      ?DEFAULT_BACKEND;
42    {ok, Proplist} ->
43      proplists:get_value(validation_backend, Proplist, ?DEFAULT_BACKEND)
44  end.
45