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_auth_mechanism_plain). 9-include_lib("rabbit_common/include/rabbit.hrl"). 10 11-behaviour(rabbit_auth_mechanism). 12 13-export([description/0, should_offer/1, init/1, handle_response/2]). 14 15-rabbit_boot_step({?MODULE, 16 [{description, "auth mechanism plain"}, 17 {mfa, {rabbit_registry, register, 18 [auth_mechanism, <<"PLAIN">>, ?MODULE]}}, 19 {requires, rabbit_registry}, 20 {enables, kernel_ready}]}). 21 22%% SASL PLAIN, as used by the Qpid Java client and our clients. Also, 23%% apparently, by OpenAMQ. 24 25description() -> 26 [{description, <<"SASL PLAIN authentication mechanism">>}]. 27 28should_offer(_Sock) -> 29 true. 30 31init(_Sock) -> 32 []. 33 34handle_response(Response, _State) -> 35 case extract_user_pass(Response) of 36 {ok, User, Pass} -> 37 rabbit_access_control:check_user_pass_login(User, Pass); 38 error -> 39 {protocol_error, "response ~p invalid", [Response]} 40 end. 41 42extract_user_pass(Response) -> 43 case extract_elem(Response) of 44 {ok, User, Response1} -> case extract_elem(Response1) of 45 {ok, Pass, <<>>} -> {ok, User, Pass}; 46 _ -> error 47 end; 48 error -> error 49 end. 50 51extract_elem(<<0:8, Rest/binary>>) -> 52 Count = next_null_pos(Rest, 0), 53 <<Elem:Count/binary, Rest1/binary>> = Rest, 54 {ok, Elem, Rest1}; 55extract_elem(_) -> 56 error. 57 58next_null_pos(<<>>, Count) -> Count; 59next_null_pos(<<0:8, _Rest/binary>>, Count) -> Count; 60next_null_pos(<<_:8, Rest/binary>>, Count) -> next_null_pos(Rest, Count + 1). 61