1 |
|
%%%---------------------------------------------------------------------- |
2 |
|
%%% File : cyrsasl_anonymous.erl |
3 |
|
%%% Author : Magnus Henoch <henoch@dtek.chalmers.se> |
4 |
|
%%% Purpose : ANONYMOUS SASL mechanism |
5 |
|
%%% See http://www.ietf.org/internet-drafts/draft-ietf-sasl-anon-05.txt |
6 |
|
%%% Created : 23 Aug 2005 by Magnus Henoch <henoch@dtek.chalmers.se> |
7 |
|
%%% |
8 |
|
%%% |
9 |
|
%%% ejabberd, Copyright (C) 2002-2011 ProcessOne |
10 |
|
%%% |
11 |
|
%%% This program is free software; you can redistribute it and/or |
12 |
|
%%% modify it under the terms of the GNU General Public License as |
13 |
|
%%% published by the Free Software Foundation; either version 2 of the |
14 |
|
%%% License, or (at your option) any later version. |
15 |
|
%%% |
16 |
|
%%% This program is distributed in the hope that it will be useful, |
17 |
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 |
|
%%% General Public License for more details. |
20 |
|
%%% |
21 |
|
%%% You should have received a copy of the GNU General Public License |
22 |
|
%%% along with this program; if not, write to the Free Software |
23 |
|
%%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
24 |
|
%%% |
25 |
|
%%%---------------------------------------------------------------------- |
26 |
|
|
27 |
|
-module(cyrsasl_anonymous). |
28 |
|
-xep([{xep, 175}, {version, "1.2"}]). |
29 |
|
-export([mechanism/0, mech_new/3, mech_step/2]). |
30 |
|
|
31 |
|
-ignore_xref([mech_new/3]). |
32 |
|
|
33 |
|
-behaviour(cyrsasl). |
34 |
|
|
35 |
|
-record(state, {creds}). |
36 |
|
|
37 |
|
-spec mechanism() -> cyrsasl:mechanism(). |
38 |
|
mechanism() -> |
39 |
5911 |
<<"ANONYMOUS">>. |
40 |
|
|
41 |
|
-spec mech_new(Host :: jid:server(), |
42 |
|
Creds :: mongoose_credentials:t(), |
43 |
|
Socket :: term()) -> {ok, tuple()}. |
44 |
|
mech_new(_Host, Creds, _Socket) -> |
45 |
2 |
{ok, #state{creds = Creds}}. |
46 |
|
|
47 |
|
-spec mech_step(State :: tuple(), ClientIn :: binary()) -> R when |
48 |
|
R :: {ok, mongoose_credentials:t()} | {error, binary()}. |
49 |
|
mech_step(#state{creds = Creds}, _ClientIn) -> |
50 |
|
%% We generate a random username: |
51 |
2 |
User = <<(mongoose_bin:gen_from_crypto())/binary, |
52 |
|
(integer_to_binary(erlang:unique_integer([positive])))/binary>>, |
53 |
|
%% Checks that the username is available |
54 |
2 |
JID = jid:make_bare(User, mongoose_credentials:lserver(Creds)), |
55 |
2 |
case ejabberd_auth:does_user_exist(JID) of |
56 |
:-( |
true -> {error, <<"not-authorized">>}; |
57 |
2 |
false -> {ok, mongoose_credentials:extend(Creds, [{username, User}, |
58 |
|
{auth_module, ?MODULE}])} |
59 |
|
end. |