| 1 |
|
%% Copyright (c) 2022 Peter Morgan <peter.james.morgan@gmail.com> |
| 2 |
|
%% |
| 3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
|
%% you may not use this file except in compliance with the License. |
| 5 |
|
%% You may obtain a copy of the License at |
| 6 |
|
%% |
| 7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
%% |
| 9 |
|
%% Unless required by applicable law or agreed to in writing, software |
| 10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
|
%% See the License for the specific language governing permissions and |
| 13 |
|
%% limitations under the License. |
| 14 |
|
|
| 15 |
|
|
| 16 |
|
-module(pgmp_mm_auth_md5). |
| 17 |
|
|
| 18 |
|
|
| 19 |
|
-export([callback_mode/0]). |
| 20 |
|
-export([handle_event/4]). |
| 21 |
|
-import(pgmp_codec, [marshal/2]). |
| 22 |
|
-import(pgmp_codec, [size_inclusive/1]). |
| 23 |
|
-import(pgmp_statem, [nei/1]). |
| 24 |
|
|
| 25 |
|
|
| 26 |
|
callback_mode() -> |
| 27 |
:-( |
[handle_event_function, state_enter]. |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
handle_event({call, _}, {request, _}, _, _) -> |
| 31 |
:-( |
{keep_state_and_data, postpone}; |
| 32 |
|
|
| 33 |
|
handle_event(internal, |
| 34 |
|
{recv = EventName, {authentication = Tag, authenticated = Type}}, |
| 35 |
|
_, |
| 36 |
|
Data) -> |
| 37 |
:-( |
{next_state, |
| 38 |
|
authenticated, |
| 39 |
|
Data, |
| 40 |
|
[pop_callback_module, |
| 41 |
|
nei({telemetry, |
| 42 |
|
EventName, |
| 43 |
|
#{count => 1}, |
| 44 |
|
#{tag => Tag, type => Type}})]}; |
| 45 |
|
|
| 46 |
|
handle_event(internal, |
| 47 |
|
{recv = EventName, {error_response = Tag, Errors}}, |
| 48 |
|
_, |
| 49 |
|
Data) -> |
| 50 |
:-( |
{next_state, |
| 51 |
|
startup_failure, |
| 52 |
|
Data#{errors => Errors}, |
| 53 |
|
[pop_callback_module, |
| 54 |
|
nei({telemetry, EventName, #{count => 1}, #{tag => Tag}})]}; |
| 55 |
|
|
| 56 |
|
handle_event(internal, |
| 57 |
|
{md5_password, <<Salt:4/bytes>>}, |
| 58 |
|
_, |
| 59 |
|
#{config := #{user := User, password := Password}}) -> |
| 60 |
|
%% src/common/md5_common.c |
| 61 |
|
%% src/interfaces/libpq/fe-auth.c |
| 62 |
:-( |
{keep_state_and_data, |
| 63 |
|
nei({send, |
| 64 |
|
["p", |
| 65 |
|
size_inclusive( |
| 66 |
|
marshal( |
| 67 |
|
string, |
| 68 |
|
["md5", md5([md5([Password(), User]), Salt])]))]})}; |
| 69 |
|
|
| 70 |
|
handle_event(EventType, EventContent, State, Data) -> |
| 71 |
:-( |
pgmp_mm_common:handle_event(EventType, |
| 72 |
|
EventContent, |
| 73 |
|
State, |
| 74 |
|
Data). |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
md5(Data) -> |
| 78 |
:-( |
string:lowercase(binary:encode_hex(crypto:hash(md5, Data))). |