2015-09-25 14:53:25 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_oauth.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : OAUTH2 support
|
|
|
|
%%% Created : 20 Mar 2015 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2015-09-25 14:53:25 +02: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.
|
|
|
|
%%%
|
|
|
|
%%% 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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_oauth).
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
|
|
|
handle_info/2, terminate/2, code_change/3]).
|
|
|
|
|
2017-02-24 10:05:47 +01:00
|
|
|
-export([start_link/0,
|
2015-09-25 14:53:25 +02:00
|
|
|
get_client_identity/2,
|
|
|
|
verify_redirection_uri/3,
|
|
|
|
authenticate_user/2,
|
|
|
|
authenticate_client/2,
|
|
|
|
associate_access_code/3,
|
|
|
|
associate_access_token/3,
|
|
|
|
associate_refresh_token/3,
|
2016-10-05 13:21:11 +02:00
|
|
|
check_token/1,
|
2015-09-25 14:53:25 +02:00
|
|
|
check_token/4,
|
|
|
|
check_token/2,
|
2016-10-05 13:21:11 +02:00
|
|
|
scope_in_scope_list/2,
|
2015-09-25 14:53:25 +02:00
|
|
|
process/2,
|
2019-09-06 16:46:03 +02:00
|
|
|
config_reloaded/0,
|
|
|
|
verify_resowner_scope/3]).
|
2015-09-25 14:53:25 +02:00
|
|
|
|
2018-08-07 16:57:10 +02:00
|
|
|
-export([get_commands_spec/0,
|
2019-09-27 19:36:35 +02:00
|
|
|
oauth_issue_token/3, oauth_list_tokens/0, oauth_revoke_token/1,
|
2019-10-03 05:18:07 +02:00
|
|
|
oauth_add_client_password/3,
|
|
|
|
oauth_add_client_implicit/3,
|
|
|
|
oauth_remove_client/1]).
|
2016-06-29 05:22:28 +02:00
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2015-09-25 14:53:25 +02:00
|
|
|
-include("logger.hrl").
|
|
|
|
-include("ejabberd_http.hrl").
|
|
|
|
-include("ejabberd_web_admin.hrl").
|
2016-07-20 15:55:45 +02:00
|
|
|
-include("ejabberd_oauth.hrl").
|
2016-06-29 05:22:28 +02:00
|
|
|
-include("ejabberd_commands.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2016-06-29 05:22:28 +02:00
|
|
|
|
2017-05-21 10:31:30 +02:00
|
|
|
-callback init() -> any().
|
|
|
|
-callback store(#oauth_token{}) -> ok | {error, any()}.
|
|
|
|
-callback lookup(binary()) -> {ok, #oauth_token{}} | error.
|
|
|
|
-callback clean(non_neg_integer()) -> any().
|
2016-06-29 05:22:28 +02:00
|
|
|
|
2019-10-03 05:18:07 +02:00
|
|
|
-record(oauth_ctx, {
|
|
|
|
password :: binary() | admin_generated,
|
2019-10-11 15:51:59 +02:00
|
|
|
client :: #oauth_client{} | undefined
|
2019-10-03 05:18:07 +02:00
|
|
|
}).
|
|
|
|
|
2016-06-29 05:22:28 +02:00
|
|
|
%% There are two ways to obtain an oauth token:
|
|
|
|
%% * Using the web form/api results in the token being generated in behalf of the user providing the user/pass
|
|
|
|
%% * Using the command line and oauth_issue_token command, the token is generated in behalf of ejabberd' sysadmin
|
|
|
|
%% (as it has access to ejabberd command line).
|
2015-09-25 14:53:25 +02:00
|
|
|
|
2016-06-29 05:22:28 +02:00
|
|
|
get_commands_spec() ->
|
|
|
|
[
|
|
|
|
#ejabberd_commands{name = oauth_issue_token, tags = [oauth],
|
2016-07-19 04:27:49 +02:00
|
|
|
desc = "Issue an oauth token for the given jid",
|
2016-06-29 05:22:28 +02:00
|
|
|
module = ?MODULE, function = oauth_issue_token,
|
2016-07-20 01:18:07 +02:00
|
|
|
args = [{jid, string},{ttl, integer}, {scopes, string}],
|
2016-06-29 05:22:28 +02:00
|
|
|
policy = restricted,
|
2017-05-19 16:56:37 +02:00
|
|
|
args_example = ["user@server.com", 3600, "connected_users_number;muc_online_rooms"],
|
2016-08-09 19:22:51 +02:00
|
|
|
args_desc = ["Jid for which issue token",
|
|
|
|
"Time to live of generated token in seconds",
|
|
|
|
"List of scopes to allow, separated by ';'"],
|
2016-06-29 05:22:28 +02:00
|
|
|
result = {result, {tuple, [{token, string}, {scopes, string}, {expires_in, string}]}}
|
|
|
|
},
|
|
|
|
#ejabberd_commands{name = oauth_list_tokens, tags = [oauth],
|
2017-03-29 12:41:27 +02:00
|
|
|
desc = "List oauth tokens, user, scope, and seconds to expire (only Mnesia)",
|
|
|
|
longdesc = "List oauth tokens, their user and scope, and how many seconds remain until expirity",
|
2016-06-29 05:22:28 +02:00
|
|
|
module = ?MODULE, function = oauth_list_tokens,
|
|
|
|
args = [],
|
|
|
|
policy = restricted,
|
2016-07-19 01:25:23 +02:00
|
|
|
result = {tokens, {list, {token, {tuple, [{token, string}, {user, string}, {scope, string}, {expires_in, string}]}}}}
|
2016-06-29 05:22:28 +02:00
|
|
|
},
|
|
|
|
#ejabberd_commands{name = oauth_revoke_token, tags = [oauth],
|
2017-03-29 12:41:27 +02:00
|
|
|
desc = "Revoke authorization for a token (only Mnesia)",
|
2016-06-29 05:22:28 +02:00
|
|
|
module = ?MODULE, function = oauth_revoke_token,
|
|
|
|
args = [{token, string}],
|
|
|
|
policy = restricted,
|
2016-07-19 05:51:04 +02:00
|
|
|
result = {tokens, {list, {token, {tuple, [{token, string}, {user, string}, {scope, string}, {expires_in, string}]}}}},
|
2016-06-29 05:22:28 +02:00
|
|
|
result_desc = "List of remaining tokens"
|
2019-09-27 19:36:35 +02:00
|
|
|
},
|
2019-10-03 05:18:07 +02:00
|
|
|
#ejabberd_commands{name = oauth_add_client_password, tags = [oauth],
|
|
|
|
desc = "Add OAUTH client_id with password grant type",
|
|
|
|
module = ?MODULE, function = oauth_add_client_password,
|
|
|
|
args = [{client_id, binary},
|
|
|
|
{client_name, binary},
|
|
|
|
{secret, binary}],
|
|
|
|
policy = restricted,
|
|
|
|
result = {res, restuple}
|
|
|
|
},
|
|
|
|
#ejabberd_commands{name = oauth_add_client_implicit, tags = [oauth],
|
|
|
|
desc = "Add OAUTH client_id with implicit grant type",
|
|
|
|
module = ?MODULE, function = oauth_add_client_implicit,
|
2019-09-27 19:36:35 +02:00
|
|
|
args = [{client_id, binary},
|
2019-10-03 05:18:07 +02:00
|
|
|
{client_name, binary},
|
|
|
|
{redirect_uri, binary}],
|
2019-09-27 19:36:35 +02:00
|
|
|
policy = restricted,
|
|
|
|
result = {res, restuple}
|
|
|
|
},
|
|
|
|
#ejabberd_commands{name = oauth_remove_client, tags = [oauth],
|
|
|
|
desc = "Remove OAUTH client_id",
|
|
|
|
module = ?MODULE, function = oauth_remove_client,
|
|
|
|
args = [{client_id, binary}],
|
|
|
|
policy = restricted,
|
|
|
|
result = {res, restuple}
|
2016-06-29 05:22:28 +02:00
|
|
|
}
|
|
|
|
].
|
|
|
|
|
2016-07-20 01:18:07 +02:00
|
|
|
oauth_issue_token(Jid, TTLSeconds, ScopesString) ->
|
2016-06-29 05:22:28 +02:00
|
|
|
Scopes = [list_to_binary(Scope) || Scope <- string:tokens(ScopesString, ";")],
|
2017-02-26 08:07:12 +01:00
|
|
|
try jid:decode(list_to_binary(Jid)) of
|
2016-07-19 01:25:23 +02:00
|
|
|
#jid{luser =Username, lserver = Server} ->
|
2019-12-23 13:55:56 +01:00
|
|
|
Ctx1 = #oauth_ctx{password = admin_generated},
|
|
|
|
case oauth2:authorize_password({Username, Server}, Scopes, Ctx1) of
|
2016-07-20 01:18:07 +02:00
|
|
|
{ok, {_Ctx,Authorization}} ->
|
2016-07-20 19:47:11 +02:00
|
|
|
{ok, {_AppCtx2, Response}} = oauth2:issue_token(Authorization, [{expiry_time, TTLSeconds}]),
|
2017-02-26 08:07:12 +01:00
|
|
|
{ok, AccessToken} = oauth2_response:access_token(Response),
|
|
|
|
{ok, VerifiedScope} = oauth2_response:scope(Response),
|
2016-07-20 01:18:07 +02:00
|
|
|
{AccessToken, VerifiedScope, integer_to_list(TTLSeconds) ++ " seconds"};
|
2017-02-26 08:07:12 +01:00
|
|
|
{error, Error} ->
|
|
|
|
{error, Error}
|
|
|
|
end
|
|
|
|
catch _:{bad_jid, _} ->
|
2016-07-19 01:25:23 +02:00
|
|
|
{error, "Invalid JID: " ++ Jid}
|
2016-06-29 05:22:28 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
oauth_list_tokens() ->
|
2016-07-19 01:25:23 +02:00
|
|
|
Tokens = mnesia:dirty_match_object(#oauth_token{_ = '_'}),
|
2016-06-29 05:22:28 +02:00
|
|
|
{MegaSecs, Secs, _MiniSecs} = os:timestamp(),
|
|
|
|
TS = 1000000 * MegaSecs + Secs,
|
2017-02-26 08:07:12 +01:00
|
|
|
[{Token, jid:encode(jid:make(U,S)), Scope, integer_to_list(Expires - TS) ++ " seconds"} ||
|
2016-07-19 01:25:23 +02:00
|
|
|
#oauth_token{token=Token, scope=Scope, us= {U,S},expire=Expires} <- Tokens].
|
2016-06-29 05:22:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
oauth_revoke_token(Token) ->
|
|
|
|
ok = mnesia:dirty_delete(oauth_token, list_to_binary(Token)),
|
|
|
|
oauth_list_tokens().
|
|
|
|
|
2019-10-03 05:18:07 +02:00
|
|
|
oauth_add_client_password(ClientID, ClientName, Secret) ->
|
|
|
|
DBMod = get_db_backend(),
|
|
|
|
DBMod:store_client(#oauth_client{client_id = ClientID,
|
|
|
|
client_name = ClientName,
|
|
|
|
grant_type = password,
|
|
|
|
options = [{secret, Secret}]}),
|
|
|
|
{ok, []}.
|
|
|
|
|
|
|
|
oauth_add_client_implicit(ClientID, ClientName, RedirectURI) ->
|
|
|
|
DBMod = get_db_backend(),
|
|
|
|
DBMod:store_client(#oauth_client{client_id = ClientID,
|
|
|
|
client_name = ClientName,
|
|
|
|
grant_type = implicit,
|
|
|
|
options = [{redirect_uri, RedirectURI}]}),
|
|
|
|
{ok, []}.
|
2019-09-27 19:36:35 +02:00
|
|
|
|
|
|
|
oauth_remove_client(Client) ->
|
|
|
|
DBMod = get_db_backend(),
|
|
|
|
DBMod:remove_client(Client),
|
|
|
|
{ok, []}.
|
|
|
|
|
2017-04-21 08:02:10 +02:00
|
|
|
config_reloaded() ->
|
|
|
|
DBMod = get_db_backend(),
|
|
|
|
case init_cache(DBMod) of
|
|
|
|
true ->
|
|
|
|
ets_cache:setopts(oauth_cache, cache_opts());
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
2016-06-29 05:22:28 +02:00
|
|
|
|
2015-09-25 14:53:25 +02:00
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
|
|
|
|
init([]) ->
|
2017-02-24 10:05:47 +01:00
|
|
|
DBMod = get_db_backend(),
|
|
|
|
DBMod:init(),
|
2017-04-21 08:02:10 +02:00
|
|
|
init_cache(DBMod),
|
2017-02-24 10:05:47 +01:00
|
|
|
Expire = expire(),
|
|
|
|
application:set_env(oauth2, backend, ejabberd_oauth),
|
2019-10-15 11:18:29 +02:00
|
|
|
application:set_env(oauth2, expiry_time, Expire div 1000),
|
2017-02-24 10:05:47 +01:00
|
|
|
application:start(oauth2),
|
|
|
|
ejabberd_commands:register_commands(get_commands_spec()),
|
2017-04-21 08:02:10 +02:00
|
|
|
ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 50),
|
2019-10-15 11:18:29 +02:00
|
|
|
erlang:send_after(expire(), self(), clean),
|
2015-09-25 14:53:25 +02:00
|
|
|
{ok, ok}.
|
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2015-09-25 14:53:25 +02:00
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
|
|
|
{noreply, State}.
|
2015-09-25 14:53:25 +02:00
|
|
|
|
|
|
|
handle_info(clean, State) ->
|
|
|
|
{MegaSecs, Secs, MiniSecs} = os:timestamp(),
|
|
|
|
TS = 1000000 * MegaSecs + Secs,
|
2016-07-20 15:55:45 +02:00
|
|
|
DBMod = get_db_backend(),
|
|
|
|
DBMod:clean(TS),
|
2019-10-15 11:18:29 +02:00
|
|
|
erlang:send_after(trunc(expire() * (1 + MiniSecs / 1000000)),
|
2015-09-25 14:53:25 +02:00
|
|
|
self(), clean),
|
|
|
|
{noreply, State};
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
|
|
|
{noreply, State}.
|
2015-09-25 14:53:25 +02:00
|
|
|
|
2019-07-29 09:46:20 +02:00
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ejabberd_hooks:delete(config_reloaded, ?MODULE, config_reloaded, 50).
|
2015-09-25 14:53:25 +02:00
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
|
|
|
|
|
|
|
|
2019-10-03 05:18:07 +02:00
|
|
|
get_client_identity({client, ClientID}, Ctx) ->
|
|
|
|
{ok, {Ctx, {client, ClientID}}}.
|
2015-09-25 14:53:25 +02:00
|
|
|
|
2019-10-03 05:18:07 +02:00
|
|
|
verify_redirection_uri(_ClientID, RedirectURI, Ctx) ->
|
|
|
|
case Ctx of
|
|
|
|
#oauth_ctx{client = #oauth_client{grant_type = implicit} = Client} ->
|
|
|
|
case get_redirect_uri(Client) of
|
|
|
|
RedirectURI ->
|
|
|
|
{ok, Ctx};
|
|
|
|
_ ->
|
|
|
|
{error, invalid_uri}
|
|
|
|
end;
|
|
|
|
#oauth_ctx{client = #oauth_client{}} ->
|
|
|
|
{error, invalid_client};
|
|
|
|
_ ->
|
|
|
|
{ok, Ctx}
|
|
|
|
end.
|
2015-09-25 14:53:25 +02:00
|
|
|
|
2016-07-19 01:25:23 +02:00
|
|
|
authenticate_user({User, Server}, Ctx) ->
|
2017-02-25 08:01:01 +01:00
|
|
|
case jid:make(User, Server) of
|
2015-09-25 18:47:27 +02:00
|
|
|
#jid{} = JID ->
|
|
|
|
Access =
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:oauth_access(JID#jid.lserver),
|
2015-09-25 18:47:27 +02:00
|
|
|
case acl:match_rule(JID#jid.lserver, Access, JID) of
|
|
|
|
allow ->
|
2016-07-19 01:25:23 +02:00
|
|
|
case Ctx of
|
2019-10-03 05:18:07 +02:00
|
|
|
#oauth_ctx{password = admin_generated} ->
|
2015-09-25 18:47:27 +02:00
|
|
|
{ok, {Ctx, {user, User, Server}}};
|
2019-10-03 05:18:07 +02:00
|
|
|
#oauth_ctx{password = Password}
|
|
|
|
when is_binary(Password) ->
|
|
|
|
case ejabberd_auth:check_password(User, <<"">>, Server, Password) of
|
|
|
|
true ->
|
|
|
|
{ok, {Ctx, {user, User, Server}}};
|
|
|
|
false ->
|
|
|
|
{error, badpass}
|
|
|
|
end
|
2015-09-25 18:47:27 +02:00
|
|
|
end;
|
|
|
|
deny ->
|
|
|
|
{error, badpass}
|
|
|
|
end;
|
|
|
|
error ->
|
2015-09-25 14:53:25 +02:00
|
|
|
{error, badpass}
|
|
|
|
end.
|
|
|
|
|
2019-10-03 05:18:07 +02:00
|
|
|
authenticate_client(ClientID, Ctx) ->
|
|
|
|
case ejabberd_option:oauth_client_id_check() of
|
|
|
|
allow ->
|
|
|
|
{ok, {Ctx, {client, ClientID}}};
|
|
|
|
deny -> {error, not_allowed};
|
|
|
|
db ->
|
|
|
|
DBMod = get_db_backend(),
|
|
|
|
case DBMod:lookup_client(ClientID) of
|
|
|
|
{ok, #oauth_client{} = Client} ->
|
|
|
|
{ok, {Ctx#oauth_ctx{client = Client}, {client, ClientID}}};
|
|
|
|
_ ->
|
|
|
|
{error, not_allowed}
|
|
|
|
end
|
|
|
|
end.
|
2015-09-25 14:53:25 +02:00
|
|
|
|
2019-09-06 16:46:03 +02:00
|
|
|
-spec verify_resowner_scope({user, binary(), binary()}, [binary()], any()) ->
|
|
|
|
{ok, any(), [binary()]} | {error, any()}.
|
|
|
|
verify_resowner_scope({user, _User, _Server}, Scope, Ctx) ->
|
|
|
|
Cmds = [atom_to_binary(Name, utf8) || {Name, _, _} <- ejabberd_commands:list_commands()],
|
|
|
|
AllowedScopes = [<<"ejabberd:user">>, <<"ejabberd:admin">>, <<"sasl_auth">>] ++ Cmds,
|
|
|
|
case oauth2_priv_set:is_subset(oauth2_priv_set:new(Scope),
|
|
|
|
oauth2_priv_set:new(AllowedScopes)) of
|
|
|
|
true ->
|
|
|
|
{ok, {Ctx, Scope}};
|
|
|
|
false ->
|
|
|
|
{error, badscope}
|
|
|
|
end;
|
|
|
|
verify_resowner_scope(_, _, _) ->
|
|
|
|
{error, badscope}.
|
|
|
|
|
2016-06-29 05:22:28 +02:00
|
|
|
%% This is callback for oauth tokens generated through the command line. Only open and admin commands are
|
|
|
|
%% made available.
|
2016-07-20 01:18:07 +02:00
|
|
|
%verify_client_scope({client, ejabberd_ctl}, Scope, Ctx) ->
|
|
|
|
% RegisteredScope = dict:fetch_keys(get_cmd_scopes()),
|
|
|
|
% case oauth2_priv_set:is_subset(oauth2_priv_set:new(Scope),
|
|
|
|
% oauth2_priv_set:new(RegisteredScope)) of
|
|
|
|
% true ->
|
|
|
|
% {ok, {Ctx, Scope}};
|
|
|
|
% false ->
|
|
|
|
% {error, badscope}
|
|
|
|
% end.
|
2016-06-29 05:22:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-20 01:18:07 +02:00
|
|
|
-spec seconds_since_epoch(integer()) -> non_neg_integer().
|
|
|
|
seconds_since_epoch(Diff) ->
|
|
|
|
{Mega, Secs, _} = os:timestamp(),
|
|
|
|
Mega * 1000000 + Secs + Diff.
|
2016-06-29 05:22:28 +02:00
|
|
|
|
|
|
|
|
2016-02-08 10:50:28 +01:00
|
|
|
associate_access_code(_AccessCode, _Context, AppContext) ->
|
2015-09-25 14:53:25 +02:00
|
|
|
%put(?ACCESS_CODE_TABLE, AccessCode, Context),
|
|
|
|
{ok, AppContext}.
|
|
|
|
|
|
|
|
associate_access_token(AccessToken, Context, AppContext) ->
|
2016-07-20 01:18:07 +02:00
|
|
|
{user, User, Server} = proplists:get_value(<<"resource_owner">>, Context, <<"">>),
|
|
|
|
Expire = case proplists:get_value(expiry_time, AppContext, undefined) of
|
|
|
|
undefined ->
|
|
|
|
proplists:get_value(<<"expiry_time">>, Context, 0);
|
2016-07-20 19:47:11 +02:00
|
|
|
ExpiresIn ->
|
2016-07-20 01:18:07 +02:00
|
|
|
%% There is no clean way in oauth2 lib to actually override the TTL of the generated token.
|
|
|
|
%% It always pass the global configured value. Here we use the app context to pass the per-case
|
|
|
|
%% ttl if we want to override it.
|
2016-07-20 19:47:11 +02:00
|
|
|
seconds_since_epoch(ExpiresIn)
|
2016-06-29 05:22:28 +02:00
|
|
|
end,
|
2016-07-19 01:25:23 +02:00
|
|
|
{user, User, Server} = proplists:get_value(<<"resource_owner">>, Context, <<"">>),
|
2015-09-25 14:53:25 +02:00
|
|
|
Scope = proplists:get_value(<<"scope">>, Context, []),
|
|
|
|
R = #oauth_token{
|
|
|
|
token = AccessToken,
|
2016-07-19 01:25:23 +02:00
|
|
|
us = {jid:nodeprep(User), jid:nodeprep(Server)},
|
2015-09-25 14:53:25 +02:00
|
|
|
scope = Scope,
|
|
|
|
expire = Expire
|
|
|
|
},
|
2016-08-04 00:58:56 +02:00
|
|
|
store(R),
|
2015-09-25 14:53:25 +02:00
|
|
|
{ok, AppContext}.
|
|
|
|
|
2016-02-08 10:50:28 +01:00
|
|
|
associate_refresh_token(_RefreshToken, _Context, AppContext) ->
|
2015-09-25 14:53:25 +02:00
|
|
|
%put(?REFRESH_TOKEN_TABLE, RefreshToken, Context),
|
|
|
|
{ok, AppContext}.
|
|
|
|
|
2016-10-05 13:21:11 +02:00
|
|
|
scope_in_scope_list(Scope, ScopeList) ->
|
|
|
|
TokenScopeSet = oauth2_priv_set:new(Scope),
|
|
|
|
lists:any(fun(Scope2) ->
|
|
|
|
oauth2_priv_set:is_member(Scope2, TokenScopeSet) end,
|
|
|
|
ScopeList).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec check_token(binary()) -> {ok, {binary(), binary()}, [binary()]} |
|
|
|
|
{false, expired | not_found}.
|
2016-10-05 13:21:11 +02:00
|
|
|
check_token(Token) ->
|
|
|
|
case lookup(Token) of
|
|
|
|
{ok, #oauth_token{us = US,
|
|
|
|
scope = TokenScope,
|
|
|
|
expire = Expire}} ->
|
|
|
|
{MegaSecs, Secs, _} = os:timestamp(),
|
|
|
|
TS = 1000000 * MegaSecs + Secs,
|
|
|
|
if
|
|
|
|
Expire > TS ->
|
|
|
|
{ok, US, TokenScope};
|
|
|
|
true ->
|
|
|
|
{false, expired}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{false, not_found}
|
|
|
|
end.
|
2015-09-25 14:53:25 +02:00
|
|
|
|
2016-07-19 04:27:49 +02:00
|
|
|
check_token(User, Server, ScopeList, Token) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2016-08-04 00:58:56 +02:00
|
|
|
case lookup(Token) of
|
|
|
|
{ok, #oauth_token{us = {LUser, LServer},
|
2015-09-25 14:53:25 +02:00
|
|
|
scope = TokenScope,
|
2016-08-04 00:58:56 +02:00
|
|
|
expire = Expire}} ->
|
2015-09-25 14:53:25 +02:00
|
|
|
{MegaSecs, Secs, _} = os:timestamp(),
|
|
|
|
TS = 1000000 * MegaSecs + Secs,
|
2016-07-30 18:51:54 +02:00
|
|
|
if
|
|
|
|
Expire > TS ->
|
|
|
|
TokenScopeSet = oauth2_priv_set:new(TokenScope),
|
|
|
|
lists:any(fun(Scope) ->
|
|
|
|
oauth2_priv_set:is_member(Scope, TokenScopeSet) end,
|
|
|
|
ScopeList);
|
|
|
|
true ->
|
|
|
|
{false, expired}
|
|
|
|
end;
|
2015-09-25 14:53:25 +02:00
|
|
|
_ ->
|
2016-07-30 18:51:54 +02:00
|
|
|
{false, not_found}
|
2015-09-25 14:53:25 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-19 04:27:49 +02:00
|
|
|
check_token(ScopeList, Token) ->
|
2016-08-04 00:58:56 +02:00
|
|
|
case lookup(Token) of
|
|
|
|
{ok, #oauth_token{us = US,
|
2015-09-25 14:53:25 +02:00
|
|
|
scope = TokenScope,
|
2016-08-04 00:58:56 +02:00
|
|
|
expire = Expire}} ->
|
2015-09-25 14:53:25 +02:00
|
|
|
{MegaSecs, Secs, _} = os:timestamp(),
|
|
|
|
TS = 1000000 * MegaSecs + Secs,
|
2016-07-30 18:51:54 +02:00
|
|
|
if
|
|
|
|
Expire > TS ->
|
|
|
|
TokenScopeSet = oauth2_priv_set:new(TokenScope),
|
|
|
|
case lists:any(fun(Scope) ->
|
|
|
|
oauth2_priv_set:is_member(Scope, TokenScopeSet) end,
|
|
|
|
ScopeList) of
|
|
|
|
true -> {ok, user, US};
|
|
|
|
false -> {false, no_matching_scope}
|
2016-06-29 05:22:28 +02:00
|
|
|
end;
|
2016-07-30 18:51:54 +02:00
|
|
|
true ->
|
|
|
|
{false, expired}
|
2015-09-25 14:53:25 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2016-07-30 18:51:54 +02:00
|
|
|
{false, not_found}
|
2015-09-25 14:53:25 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2016-08-04 00:58:56 +02:00
|
|
|
store(R) ->
|
2017-04-21 08:02:10 +02:00
|
|
|
DBMod = get_db_backend(),
|
|
|
|
case DBMod:store(R) of
|
|
|
|
ok ->
|
|
|
|
ets_cache:delete(oauth_cache, R#oauth_token.token,
|
|
|
|
ejabberd_cluster:get_nodes());
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2016-08-04 00:58:56 +02:00
|
|
|
|
|
|
|
lookup(Token) ->
|
2017-04-21 08:02:10 +02:00
|
|
|
ets_cache:lookup(oauth_cache, Token,
|
|
|
|
fun() ->
|
|
|
|
DBMod = get_db_backend(),
|
|
|
|
DBMod:lookup(Token)
|
|
|
|
end).
|
|
|
|
|
|
|
|
-spec init_cache(module()) -> boolean().
|
|
|
|
init_cache(DBMod) ->
|
|
|
|
UseCache = use_cache(DBMod),
|
|
|
|
case UseCache of
|
|
|
|
true ->
|
|
|
|
ets_cache:new(oauth_cache, cache_opts());
|
|
|
|
false ->
|
|
|
|
ets_cache:delete(oauth_cache)
|
|
|
|
end,
|
|
|
|
UseCache.
|
|
|
|
|
|
|
|
use_cache(DBMod) ->
|
|
|
|
case erlang:function_exported(DBMod, use_cache, 0) of
|
|
|
|
true -> DBMod:use_cache();
|
2019-06-14 11:33:26 +02:00
|
|
|
false -> ejabberd_option:oauth_use_cache()
|
2017-04-21 08:02:10 +02:00
|
|
|
end.
|
2016-08-04 00:58:56 +02:00
|
|
|
|
2017-04-21 08:02:10 +02:00
|
|
|
cache_opts() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxSize = ejabberd_option:oauth_cache_size(),
|
|
|
|
CacheMissed = ejabberd_option:oauth_cache_missed(),
|
2019-06-15 11:53:16 +02:00
|
|
|
LifeTime = ejabberd_option:oauth_cache_life_time(),
|
2017-04-21 08:02:10 +02:00
|
|
|
[{max_size, MaxSize}, {life_time, LifeTime}, {cache_missed, CacheMissed}].
|
2016-08-04 00:58:56 +02:00
|
|
|
|
2015-09-25 14:53:25 +02:00
|
|
|
expire() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:oauth_expire().
|
2015-09-25 14:53:25 +02:00
|
|
|
|
|
|
|
-define(DIV(Class, Els),
|
|
|
|
?XAE(<<"div">>, [{<<"class">>, Class}], Els)).
|
|
|
|
-define(INPUTID(Type, Name, Value),
|
|
|
|
?XA(<<"input">>,
|
|
|
|
[{<<"type">>, Type}, {<<"name">>, Name},
|
|
|
|
{<<"value">>, Value}, {<<"id">>, Name}])).
|
|
|
|
-define(LABEL(ID, Els),
|
|
|
|
?XAE(<<"label">>, [{<<"for">>, ID}], Els)).
|
|
|
|
|
|
|
|
process(_Handlers,
|
|
|
|
#request{method = 'GET', q = Q, lang = Lang,
|
|
|
|
path = [_, <<"authorization_token">>]}) ->
|
|
|
|
ResponseType = proplists:get_value(<<"response_type">>, Q, <<"">>),
|
|
|
|
ClientId = proplists:get_value(<<"client_id">>, Q, <<"">>),
|
|
|
|
RedirectURI = proplists:get_value(<<"redirect_uri">>, Q, <<"">>),
|
|
|
|
Scope = proplists:get_value(<<"scope">>, Q, <<"">>),
|
|
|
|
State = proplists:get_value(<<"state">>, Q, <<"">>),
|
|
|
|
Form =
|
|
|
|
?XAE(<<"form">>,
|
|
|
|
[{<<"action">>, <<"authorization_token">>},
|
|
|
|
{<<"method">>, <<"post">>}],
|
2019-06-22 16:08:45 +02:00
|
|
|
[?LABEL(<<"username">>, [?CT(?T("User (jid)")), ?C(<<": ">>)]),
|
2019-09-09 12:12:54 +02:00
|
|
|
?INPUTID(<<"email">>, <<"username">>, <<"">>),
|
2015-09-25 14:53:25 +02:00
|
|
|
?BR,
|
2019-06-22 16:08:45 +02:00
|
|
|
?LABEL(<<"password">>, [?CT(?T("Password")), ?C(<<": ">>)]),
|
2015-09-25 14:53:25 +02:00
|
|
|
?INPUTID(<<"password">>, <<"password">>, <<"">>),
|
|
|
|
?INPUT(<<"hidden">>, <<"response_type">>, ResponseType),
|
|
|
|
?INPUT(<<"hidden">>, <<"client_id">>, ClientId),
|
|
|
|
?INPUT(<<"hidden">>, <<"redirect_uri">>, RedirectURI),
|
|
|
|
?INPUT(<<"hidden">>, <<"scope">>, Scope),
|
|
|
|
?INPUT(<<"hidden">>, <<"state">>, State),
|
|
|
|
?BR,
|
2019-06-22 16:08:45 +02:00
|
|
|
?LABEL(<<"ttl">>, [?CT(?T("Token TTL")), ?C(<<": ">>)]),
|
2016-07-20 01:18:07 +02:00
|
|
|
?XAE(<<"select">>, [{<<"name">>, <<"ttl">>}],
|
|
|
|
[
|
|
|
|
?XAC(<<"option">>, [{<<"value">>, <<"3600">>}],<<"1 Hour">>),
|
|
|
|
?XAC(<<"option">>, [{<<"value">>, <<"86400">>}],<<"1 Day">>),
|
|
|
|
?XAC(<<"option">>, [{<<"value">>, <<"2592000">>}],<<"1 Month">>),
|
2016-07-23 18:57:57 +02:00
|
|
|
?XAC(<<"option">>, [{<<"selected">>, <<"selected">>},{<<"value">>, <<"31536000">>}],<<"1 Year">>),
|
2016-07-20 01:18:07 +02:00
|
|
|
?XAC(<<"option">>, [{<<"value">>, <<"315360000">>}],<<"10 Years">>)]),
|
|
|
|
?BR,
|
2019-06-22 16:08:45 +02:00
|
|
|
?INPUTT(<<"submit">>, <<"">>, ?T("Accept"))
|
2015-09-25 14:53:25 +02:00
|
|
|
]),
|
|
|
|
Top =
|
|
|
|
?DIV(<<"section">>,
|
|
|
|
[?DIV(<<"block">>,
|
|
|
|
[?A(<<"https://www.ejabberd.im">>,
|
|
|
|
[?XA(<<"img">>,
|
|
|
|
[{<<"height">>, <<"32">>},
|
|
|
|
{<<"src">>, logo()}])]
|
|
|
|
)])]),
|
|
|
|
Middle =
|
|
|
|
?DIV(<<"white section">>,
|
|
|
|
[?DIV(<<"block">>,
|
|
|
|
[?XC(<<"h1">>, <<"Authorization request">>),
|
|
|
|
?XE(<<"p">>,
|
|
|
|
[?C(<<"Application ">>),
|
|
|
|
?XC(<<"em">>, ClientId),
|
|
|
|
?C(<<" wants to access scope ">>),
|
|
|
|
?XC(<<"em">>, Scope)]),
|
|
|
|
Form
|
|
|
|
])]),
|
|
|
|
Bottom =
|
|
|
|
?DIV(<<"section">>,
|
|
|
|
[?DIV(<<"block">>,
|
|
|
|
[?XAC(<<"a">>,
|
|
|
|
[{<<"href">>, <<"https://www.ejabberd.im">>},
|
|
|
|
{<<"title">>, <<"ejabberd XMPP server">>}],
|
|
|
|
<<"ejabberd">>),
|
2016-11-16 18:24:12 +01:00
|
|
|
?C(<<" is maintained by ">>),
|
2015-09-25 14:53:25 +02:00
|
|
|
?XAC(<<"a">>,
|
|
|
|
[{<<"href">>, <<"https://www.process-one.net">>},
|
|
|
|
{<<"title">>, <<"ProcessOne - Leader in Instant Messaging and Push Solutions">>}],
|
|
|
|
<<"ProcessOne">>)
|
|
|
|
])]),
|
|
|
|
Body = ?DIV(<<"container">>, [Top, Middle, Bottom]),
|
|
|
|
ejabberd_web:make_xhtml(web_head(), [Body]);
|
|
|
|
process(_Handlers,
|
|
|
|
#request{method = 'POST', q = Q, lang = _Lang,
|
|
|
|
path = [_, <<"authorization_token">>]}) ->
|
2016-02-08 10:50:28 +01:00
|
|
|
_ResponseType = proplists:get_value(<<"response_type">>, Q, <<"">>),
|
2015-09-25 14:53:25 +02:00
|
|
|
ClientId = proplists:get_value(<<"client_id">>, Q, <<"">>),
|
|
|
|
RedirectURI = proplists:get_value(<<"redirect_uri">>, Q, <<"">>),
|
|
|
|
SScope = proplists:get_value(<<"scope">>, Q, <<"">>),
|
2016-07-22 20:37:48 +02:00
|
|
|
StringJID = proplists:get_value(<<"username">>, Q, <<"">>),
|
2017-02-26 08:07:12 +01:00
|
|
|
#jid{user = Username, server = Server} = jid:decode(StringJID),
|
2015-09-25 14:53:25 +02:00
|
|
|
Password = proplists:get_value(<<"password">>, Q, <<"">>),
|
|
|
|
State = proplists:get_value(<<"state">>, Q, <<"">>),
|
|
|
|
Scope = str:tokens(SScope, <<" ">>),
|
2016-07-20 01:18:07 +02:00
|
|
|
TTL = proplists:get_value(<<"ttl">>, Q, <<"">>),
|
|
|
|
ExpiresIn = case TTL of
|
|
|
|
<<>> -> undefined;
|
2016-11-13 08:44:53 +01:00
|
|
|
_ -> binary_to_integer(TTL)
|
2016-07-20 01:18:07 +02:00
|
|
|
end,
|
2015-09-25 14:53:25 +02:00
|
|
|
case oauth2:authorize_password({Username, Server},
|
|
|
|
ClientId,
|
|
|
|
RedirectURI,
|
|
|
|
Scope,
|
2019-10-03 05:18:07 +02:00
|
|
|
#oauth_ctx{password = Password}) of
|
2015-09-25 14:53:25 +02:00
|
|
|
{ok, {_AppContext, Authorization}} ->
|
|
|
|
{ok, {_AppContext2, Response}} =
|
2016-07-20 01:18:07 +02:00
|
|
|
oauth2:issue_token(Authorization, [{expiry_time, ExpiresIn} || ExpiresIn /= undefined ]),
|
2015-09-25 14:53:25 +02:00
|
|
|
{ok, AccessToken} = oauth2_response:access_token(Response),
|
|
|
|
{ok, Type} = oauth2_response:token_type(Response),
|
2016-07-20 01:18:07 +02:00
|
|
|
%%Ugly: workardound to return the correct expirity time, given than oauth2 lib doesn't really have
|
|
|
|
%%per-case expirity time.
|
|
|
|
Expires = case ExpiresIn of
|
|
|
|
undefined ->
|
|
|
|
{ok, Ex} = oauth2_response:expires_in(Response),
|
|
|
|
Ex;
|
|
|
|
_ ->
|
|
|
|
ExpiresIn
|
|
|
|
end,
|
2015-09-25 14:53:25 +02:00
|
|
|
{ok, VerifiedScope} = oauth2_response:scope(Response),
|
|
|
|
%oauth2_wrq:redirected_access_token_response(ReqData,
|
|
|
|
% RedirectURI,
|
|
|
|
% AccessToken,
|
|
|
|
% Type,
|
|
|
|
% Expires,
|
|
|
|
% VerifiedScope,
|
|
|
|
% State,
|
|
|
|
% Context);
|
|
|
|
{302, [{<<"Location">>,
|
|
|
|
<<RedirectURI/binary,
|
|
|
|
"?access_token=", AccessToken/binary,
|
|
|
|
"&token_type=", Type/binary,
|
|
|
|
"&expires_in=", (integer_to_binary(Expires))/binary,
|
|
|
|
"&scope=", (str:join(VerifiedScope, <<" ">>))/binary,
|
|
|
|
"&state=", State/binary>>
|
|
|
|
}],
|
|
|
|
ejabberd_web:make_xhtml([?XC(<<"h1">>, <<"302 Found">>)])};
|
|
|
|
{error, Error} when is_atom(Error) ->
|
|
|
|
%oauth2_wrq:redirected_error_response(
|
|
|
|
% ReqData, RedirectURI, Error, State, Context)
|
|
|
|
{302, [{<<"Location">>,
|
|
|
|
<<RedirectURI/binary,
|
|
|
|
"?error=", (atom_to_binary(Error, utf8))/binary,
|
|
|
|
"&state=", State/binary>>
|
|
|
|
}],
|
|
|
|
ejabberd_web:make_xhtml([?XC(<<"h1">>, <<"302 Found">>)])}
|
|
|
|
end;
|
2016-07-23 00:17:12 +02:00
|
|
|
process(_Handlers,
|
|
|
|
#request{method = 'POST', q = Q, lang = _Lang,
|
2019-09-27 19:36:35 +02:00
|
|
|
auth = HTTPAuth,
|
2016-07-23 00:17:12 +02:00
|
|
|
path = [_, <<"token">>]}) ->
|
2019-09-27 19:36:35 +02:00
|
|
|
Access =
|
|
|
|
case ejabberd_option:oauth_client_id_check() of
|
|
|
|
allow ->
|
|
|
|
case proplists:get_value(<<"grant_type">>, Q, <<"">>) of
|
|
|
|
<<"password">> ->
|
|
|
|
password;
|
|
|
|
_ ->
|
|
|
|
unsupported_grant_type
|
|
|
|
end;
|
|
|
|
deny ->
|
|
|
|
deny;
|
|
|
|
db ->
|
|
|
|
{ClientID, Secret} =
|
|
|
|
case HTTPAuth of
|
|
|
|
{ClientID1, Secret1} ->
|
|
|
|
{ClientID1, Secret1};
|
|
|
|
_ ->
|
|
|
|
ClientID1 = proplists:get_value(
|
|
|
|
<<"client_id">>, Q, <<"">>),
|
|
|
|
Secret1 = proplists:get_value(
|
|
|
|
<<"client_secret">>, Q, <<"">>),
|
|
|
|
{ClientID1, Secret1}
|
2016-07-23 00:17:12 +02:00
|
|
|
end,
|
2019-09-27 19:36:35 +02:00
|
|
|
DBMod = get_db_backend(),
|
|
|
|
case DBMod:lookup_client(ClientID) of
|
2019-10-03 05:18:07 +02:00
|
|
|
{ok, #oauth_client{grant_type = password} = Client} ->
|
|
|
|
case get_client_secret(Client) of
|
|
|
|
Secret ->
|
|
|
|
case proplists:get_value(<<"grant_type">>, Q, <<"">>) of
|
|
|
|
<<"password">> when
|
|
|
|
Client#oauth_client.grant_type == password ->
|
|
|
|
password;
|
|
|
|
_ ->
|
|
|
|
unsupported_grant_type
|
|
|
|
end;
|
2019-09-27 19:36:35 +02:00
|
|
|
_ ->
|
2019-10-03 05:18:07 +02:00
|
|
|
deny
|
2019-09-27 19:36:35 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
deny
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case Access of
|
|
|
|
password ->
|
|
|
|
SScope = proplists:get_value(<<"scope">>, Q, <<"">>),
|
|
|
|
StringJID = proplists:get_value(<<"username">>, Q, <<"">>),
|
|
|
|
#jid{user = Username, server = Server} = jid:decode(StringJID),
|
|
|
|
Password = proplists:get_value(<<"password">>, Q, <<"">>),
|
|
|
|
Scope = str:tokens(SScope, <<" ">>),
|
|
|
|
TTL = proplists:get_value(<<"ttl">>, Q, <<"">>),
|
|
|
|
ExpiresIn = case TTL of
|
|
|
|
<<>> -> undefined;
|
|
|
|
_ -> binary_to_integer(TTL)
|
|
|
|
end,
|
|
|
|
case oauth2:authorize_password({Username, Server},
|
|
|
|
Scope,
|
2019-10-03 05:18:07 +02:00
|
|
|
#oauth_ctx{password = Password}) of
|
2019-09-27 19:36:35 +02:00
|
|
|
{ok, {_AppContext, Authorization}} ->
|
|
|
|
{ok, {_AppContext2, Response}} =
|
|
|
|
oauth2:issue_token(Authorization, [{expiry_time, ExpiresIn} || ExpiresIn /= undefined ]),
|
|
|
|
{ok, AccessToken} = oauth2_response:access_token(Response),
|
|
|
|
{ok, Type} = oauth2_response:token_type(Response),
|
|
|
|
%%Ugly: workardound to return the correct expirity time, given than oauth2 lib doesn't really have
|
|
|
|
%%per-case expirity time.
|
|
|
|
Expires = case ExpiresIn of
|
|
|
|
undefined ->
|
|
|
|
{ok, Ex} = oauth2_response:expires_in(Response),
|
|
|
|
Ex;
|
|
|
|
_ ->
|
|
|
|
ExpiresIn
|
|
|
|
end,
|
|
|
|
{ok, VerifiedScope} = oauth2_response:scope(Response),
|
|
|
|
json_response(200, {[
|
|
|
|
{<<"access_token">>, AccessToken},
|
|
|
|
{<<"token_type">>, Type},
|
|
|
|
{<<"scope">>, str:join(VerifiedScope, <<" ">>)},
|
|
|
|
{<<"expires_in">>, Expires}]});
|
|
|
|
{error, Error} when is_atom(Error) ->
|
|
|
|
json_error(400, <<"invalid_grant">>, Error)
|
|
|
|
end;
|
|
|
|
unsupported_grant_type ->
|
|
|
|
json_error(400, <<"unsupported_grant_type">>,
|
|
|
|
unsupported_grant_type);
|
|
|
|
deny ->
|
|
|
|
ejabberd_web:error(not_allowed)
|
|
|
|
end;
|
2016-07-23 00:17:12 +02:00
|
|
|
|
2015-09-25 14:53:25 +02:00
|
|
|
process(_Handlers, _Request) ->
|
|
|
|
ejabberd_web:error(not_found).
|
|
|
|
|
2016-07-20 15:55:45 +02:00
|
|
|
-spec get_db_backend() -> module().
|
|
|
|
|
|
|
|
get_db_backend() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
DBType = ejabberd_option:oauth_db_type(),
|
2019-06-15 14:14:45 +02:00
|
|
|
list_to_existing_atom("ejabberd_oauth_" ++ atom_to_list(DBType)).
|
2016-07-20 15:55:45 +02:00
|
|
|
|
2019-10-03 05:18:07 +02:00
|
|
|
get_client_secret(#oauth_client{grant_type = password, options = Options}) ->
|
|
|
|
proplists:get_value(secret, Options, false).
|
|
|
|
|
|
|
|
get_redirect_uri(#oauth_client{grant_type = implicit, options = Options}) ->
|
|
|
|
proplists:get_value(redirect_uri, Options, false).
|
2015-09-25 14:53:25 +02:00
|
|
|
|
2016-07-23 18:57:57 +02:00
|
|
|
%% Headers as per RFC 6749
|
2016-07-23 00:17:12 +02:00
|
|
|
json_response(Code, Body) ->
|
|
|
|
{Code, [{<<"Content-Type">>, <<"application/json;charset=UTF-8">>},
|
2016-07-23 18:57:57 +02:00
|
|
|
{<<"Cache-Control">>, <<"no-store">>},
|
|
|
|
{<<"Pragma">>, <<"no-cache">>}],
|
2016-07-23 00:17:12 +02:00
|
|
|
jiffy:encode(Body)}.
|
2015-09-25 14:53:25 +02:00
|
|
|
|
2016-07-24 14:10:12 +02:00
|
|
|
%% OAauth error are defined in:
|
|
|
|
%% https://tools.ietf.org/html/draft-ietf-oauth-v2-25#section-5.2
|
|
|
|
json_error(Code, Error, Reason) ->
|
|
|
|
Desc = json_error_desc(Reason),
|
|
|
|
Body = {[{<<"error">>, Error},
|
|
|
|
{<<"error_description">>, Desc}]},
|
|
|
|
json_response(Code, Body).
|
2015-09-25 14:53:25 +02:00
|
|
|
|
2016-07-24 14:10:12 +02:00
|
|
|
json_error_desc(access_denied) -> <<"Access denied">>;
|
|
|
|
json_error_desc(unsupported_grant_type) -> <<"Unsupported grant type">>;
|
|
|
|
json_error_desc(invalid_scope) -> <<"Invalid scope">>.
|
2015-09-25 14:53:25 +02:00
|
|
|
|
|
|
|
web_head() ->
|
|
|
|
[?XA(<<"meta">>, [{<<"http-equiv">>, <<"X-UA-Compatible">>},
|
|
|
|
{<<"content">>, <<"IE=edge">>}]),
|
|
|
|
?XA(<<"meta">>, [{<<"name">>, <<"viewport">>},
|
|
|
|
{<<"content">>,
|
|
|
|
<<"width=device-width, initial-scale=1">>}]),
|
|
|
|
?XC(<<"title">>, <<"Authorization request">>),
|
|
|
|
?XC(<<"style">>, css())
|
|
|
|
].
|
|
|
|
|
|
|
|
css() ->
|
2017-10-09 22:35:42 +02:00
|
|
|
case misc:read_css("oauth.css") of
|
|
|
|
{ok, Data} -> Data;
|
|
|
|
{error, _} -> <<>>
|
|
|
|
end.
|
2015-09-25 14:53:25 +02:00
|
|
|
|
|
|
|
logo() ->
|
2017-10-09 22:35:42 +02:00
|
|
|
case misc:read_img("oauth-logo.png") of
|
|
|
|
{ok, Img} ->
|
|
|
|
B64Img = base64:encode(Img),
|
|
|
|
<<"data:image/png;base64,", B64Img/binary>>;
|
|
|
|
{error, _} ->
|
|
|
|
<<>>
|
|
|
|
end.
|