]> granicus.if.org Git - ejabberd/commitdiff
Add ejabberd_oauth:verify_resowner_scope callback back
authorPaweł Chmielowski <pchmielowski@process-one.net>
Fri, 6 Sep 2019 14:46:03 +0000 (16:46 +0200)
committerPaweł Chmielowski <pchmielowski@process-one.net>
Fri, 6 Sep 2019 14:47:22 +0000 (16:47 +0200)
This fixes issue #2982

src/ejabberd_oauth.erl

index 31826fa538b6fed53c13ab96d12c1b9469d1ef93..d7f440751cf470686dbf657693f7c628fce0919c 100644 (file)
          check_token/2,
          scope_in_scope_list/2,
          process/2,
-        config_reloaded/0]).
+         config_reloaded/0,
+         verify_resowner_scope/3]).
 
 -export([get_commands_spec/0,
-        oauth_issue_token/3, oauth_list_tokens/0, oauth_revoke_token/1]).
+         oauth_issue_token/3, oauth_list_tokens/0, oauth_revoke_token/1]).
 
 -include("xmpp.hrl").
 -include("logger.hrl").
@@ -211,6 +212,21 @@ authenticate_user({User, Server}, Ctx) ->
 
 authenticate_client(Client, Ctx) -> {ok, {Ctx, {client, Client}}}.
 
+-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}.
+
 %% This is callback for oauth tokens generated through the command line.  Only open and admin commands are
 %% made available.
 %verify_client_scope({client, ejabberd_ctl}, Scope, Ctx) ->