2012-11-13 13:56:27 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_auth_riak.erl
|
|
|
|
%%% Author : Evgeniy Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Purpose : Authentification via Riak
|
|
|
|
%%% Created : 12 Nov 2012 by Evgeniy Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2012-11-13 13:56:27 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
2015-02-25 15:19:33 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2012-11-13 13:56:27 +01:00
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_auth_riak).
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
2012-11-13 13:56:27 +01:00
|
|
|
-author('alexey@process-one.net').
|
|
|
|
|
|
|
|
-behaviour(ejabberd_auth).
|
|
|
|
|
|
|
|
%% External exports
|
2017-05-11 13:37:21 +02:00
|
|
|
-export([start/1, stop/1, set_password/3, try_register/3,
|
|
|
|
get_users/2, count_users/2,
|
|
|
|
get_password/2, remove_user/2, store_type/1, export/1, import/2,
|
|
|
|
plain_password_required/1]).
|
2014-07-14 05:49:02 +02:00
|
|
|
-export([passwd_schema/0]).
|
2012-11-13 13:56:27 +01:00
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2018-06-14 13:00:47 +02:00
|
|
|
-include("scram.hrl").
|
2017-10-25 20:21:52 +02:00
|
|
|
-include("ejabberd_auth.hrl").
|
2012-11-13 13:56:27 +01:00
|
|
|
|
|
|
|
start(_Host) ->
|
|
|
|
ok.
|
|
|
|
|
2017-02-23 14:19:22 +01:00
|
|
|
stop(_Host) ->
|
|
|
|
ok.
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
plain_password_required(Server) ->
|
|
|
|
store_type(Server) == scram.
|
2012-11-13 13:56:27 +01:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
store_type(Server) ->
|
|
|
|
ejabberd_auth:password_format(Server).
|
2012-11-13 13:56:27 +01:00
|
|
|
|
2014-07-14 05:49:02 +02:00
|
|
|
passwd_schema() ->
|
|
|
|
{record_info(fields, passwd), #passwd{}}.
|
|
|
|
|
2012-11-13 13:56:27 +01:00
|
|
|
set_password(User, Server, Password) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
ejabberd_riak:put(#passwd{us = {User, Server}, password = Password},
|
|
|
|
passwd_schema(),
|
|
|
|
[{'2i', [{<<"host">>, Server}]}]).
|
|
|
|
|
|
|
|
try_register(User, Server, Password) ->
|
|
|
|
US = {User, Server},
|
|
|
|
case ejabberd_riak:get(passwd, passwd_schema(), US) of
|
|
|
|
{error, notfound} ->
|
|
|
|
ejabberd_riak:put(#passwd{us = US, password = Password},
|
|
|
|
passwd_schema(),
|
|
|
|
[{'2i', [{<<"host">>, Server}]}]);
|
|
|
|
{ok, _} ->
|
|
|
|
{error, exists};
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
2012-11-13 13:56:27 +01:00
|
|
|
end.
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
get_users(Server, _) ->
|
|
|
|
case ejabberd_riak:get_keys_by_index(passwd, <<"host">>, Server) of
|
2012-11-13 13:56:27 +01:00
|
|
|
{ok, Users} ->
|
|
|
|
Users;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
count_users(Server, _) ->
|
|
|
|
case ejabberd_riak:count_by_index(passwd, <<"host">>, Server) of
|
2012-11-13 13:56:27 +01:00
|
|
|
{ok, N} ->
|
|
|
|
N;
|
|
|
|
_ ->
|
|
|
|
0
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_password(User, Server) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case ejabberd_riak:get(passwd, passwd_schema(), {User, Server}) of
|
|
|
|
{ok, Password} ->
|
|
|
|
{ok, Password};
|
|
|
|
{error, _} ->
|
|
|
|
error
|
2012-11-13 13:56:27 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
remove_user(User, Server) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
ejabberd_riak:delete(passwd, {User, Server}).
|
2012-11-13 13:56:27 +01:00
|
|
|
|
|
|
|
export(_Server) ->
|
|
|
|
[{passwd,
|
|
|
|
fun(Host, #passwd{us = {LUser, LServer}, password = Password})
|
2016-05-04 20:01:05 +02:00
|
|
|
when LServer == Host ->
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL("delete from users where username=%(LUser)s and %(LServer)H;"),
|
|
|
|
?SQL_INSERT(
|
|
|
|
"users",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"password=%(Password)s"])];
|
2012-11-13 13:56:27 +01:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
2013-07-21 14:53:44 +02:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(LServer, [LUser, Password, _TimeStamp]) ->
|
|
|
|
Passwd = #passwd{us = {LUser, LServer}, password = Password},
|
|
|
|
ejabberd_riak:put(Passwd, passwd_schema(), [{'2i', [{<<"host">>, LServer}]}]).
|