{ok,
[{username, User}, {authzid, AuthzId},
{auth_module, ejabberd_oauth}]};
- false ->
+ _ ->
{error, <<"not-authorized">>, User}
end;
_ -> {error, <<"bad-protocol">>}
case ejabberd_oauth:check_token(User, Server, ScopeList, Token) of
true ->
{ok, User, Server};
- false ->
+ _ ->
throw({error, invalid_account_data})
end;
check_auth(_Command, {User, Server, Password, _}) when is_binary(Password) ->
expire = Expire}] ->
{MegaSecs, Secs, _} = os:timestamp(),
TS = 1000000 * MegaSecs + Secs,
- TokenScopeSet = oauth2_priv_set:new(TokenScope),
- lists:any(fun(Scope) ->
- oauth2_priv_set:is_member(Scope, TokenScopeSet) end,
- ScopeList) andalso Expire > TS;
+ 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;
_ ->
- false
+ {false, not_found}
end.
check_token(ScopeList, Token) ->
expire = Expire}] ->
{MegaSecs, Secs, _} = os:timestamp(),
TS = 1000000 * MegaSecs + Secs,
- TokenScopeSet = oauth2_priv_set:new(TokenScope),
- case lists:any(fun(Scope) ->
- oauth2_priv_set:is_member(Scope, TokenScopeSet) end,
- ScopeList) andalso Expire > TS of
- true -> {ok, user, US};
- false -> false
+ 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}
+ end;
+ true ->
+ {false, expired}
end;
_ ->
- false
+ {false, not_found}
end.
case oauth_check_token(ScopeList, Token) of
{ok, user, {User, Server}} ->
{ok, {User, Server, {oauth, Token}, Admin}};
- false ->
- false
+ {false, Reason} ->
+ {false, Reason}
end;
_ ->
false
end,
case Auth of
{ok, A} -> {allowed, Call, A};
+ {false, no_matching_scope} -> outofscope_response();
_ -> unauthorized_response()
end;
check_permissions2(_Request, Call, open, _Scope) ->
Commands when is_list(Commands) ->
case lists:member(Call, Commands) of
true -> {allowed, Call, admin};
- _ -> unauthorized_response()
+ _ -> outofscope_response()
end;
_E ->
{allowed, Call, noauth}
format_result(404, {_Name, _}) ->
"not_found".
+
format_error_result(conflict, Code, Msg) ->
{409, Code, iolist_to_binary(Msg)};
format_error_result(_ErrorAtom, Code, Msg) ->
unauthorized_response() ->
json_error(401, 10, <<"Oauth Token is invalid or expired.">>).
+outofscope_response() ->
+ json_error(401, 11, <<"Token does not grant usage to command required scope.">>).
+
badrequest_response() ->
badrequest_response(<<"400 Bad Request">>).
badrequest_response(Body) ->