OAuth Support - Ejabberd Docs
OAuth Support - Ejabberd Docs
OAuth Support - Ejabberd Docs
Introduction
ejabberd includes a full support OAuth 2.0 deep inside the ejabberd stack.
an ideal project to develop XMPP applications with Web in mind, as it exposes ejabberd features as ReST or
XML-RPC HTTP based API endpoints. OAuth makes ejabberd the ideal XMPP server to integrate in a larger
Web / HTTP ecosystem.
a more secure tool that can leverage the use of oAuth token to authenticate, hiding your real password from
the client itself. As your password is never shared with client directly with our X-OAUTH2 authentication
mechanism, user have less risks of having their primary password leaked.
a tool that can be used at the core of larger platforms as oauth token can be used by users and admins to
delegate rights to subcomponents / subservices.
a tool that is friendly to other online services as users can delegate rights to others SaaS platform they are
using. This will be possible to let services access your message archive, show your offline message count or
with future commands send message to users and chatrooms on your behalf. This is done in a granular way,
with a scope limited to a specific function. And the delegation rights for a specific app / third party can always
be revoked at any time as this is usually the case with OAuth services.
Configuration
Authentication method
An X-OAUTH2 SASL mechanism is available as default for authentication in ejabberd.
If ejabberd_oauth HTTP request handlers is not enabled, there is no way to generate token from outside
ejabberd. However, you can still explicitly disabled X-OAUTH2 with the disable_sasl_mechanisms option in
ejabberd.yml file, either at global or at virtual host level:
disable_sasl_mechanisms: ["X-OAUTH2"]
ejabberd listeners
To enable OAuth support in ejabberd, you need to edit your ejabberd.yml file to add the following snippets.
If you want to support commands using the XML-RPC protocol, you can add ejabberd_xmlrpc as a specific
listener on a separate port.
Here is a example of the listen section in ejabberd configuration file, focusing on HTTP handlers:
listen:
## To handle ejabberd commands using XML-RPC
-
port: 4560
module: ejabberd_xmlrpc
access_commands: {}
-
port: 5280
module: ejabberd_http
request_handlers:
"/websocket": ejabberd_http_ws
"/log": mod_log_http
# OAuth support:
"/oauth": ejabberd_oauth
# ReST API:
"/api": mod_http_api
web_admin: true
http_bind: true
captcha: true
Module configuration
Make sure you have enabled the modules supporting the OAuth commands you want to use in ejabberd.yml
modules section.
Please, refer to the List of commands available with OAuth support for reference.
commands_admin_access : This option will reference an ejabberd access rule that will define the user that will be
able to use commands that are defined as admin only.
commands : This option is used to define the list of commands we want to enable through a remote mechanism
(ReST or XML-RPC). This is a list of add_commands and remove_commands options. You can provide list of either
commands or complete categories of commands. The add_commands and remove_commands directives are
executed in order and you can have several of them to control the precise list of commands you want to
expose through OAuth. Possible categories of commands are:
open : no auth required for that command. Typically, for generic service information related commands.
admin : Only an admin user can use the method.
user : User can use the method to access server level information (like browse MUC room list as user) or
their own data. Admin can still use it to access any user data.
restricted : Such admin command is not exposed over internet and is only available through local secure
tool like ejabberdctl command-line tool.
oauth_expire : Time during which the token is valid, in seconds. After that amount of time, the token expires
and the delegated credential cannot be used and is removed from the database.
oauth_access : By default creating OAuth tokens is not allowed. To define which users can create OAuth
tokens, you can refer to an ejabberd access rule in the oauth_access option. Use all to allow everyone to
create tokens.
commands_admin_access: configure
commands:
- add_commands:
- user
oauth_expire: 3600
oauth_access: all
In the previous example, tokens expire after an hour. All commands in category user are exposed. All users can
create tokens, and admin access is granted to users that can pass the configure access rule defined in the
config file.
http://example.net:5280/oauth/authorization_token?response_type=token&client_id=Client1&redirect_uri=htt
p://client.uri&scope=get_roster+sasl_auth
Note:: To use get_roster scope, you need to have mod_admin_extra enabled. Otherwise, the command is
unknown and you will get an invalid_scope error.
Directing the user to this URL will present an authentication form summarizing what is the app requiring the
token and the scope / rights that are going to be granted.
The user can then put his login and password to confirm that he accepts granting delegating rights and confirms
the token generation. If the provided credentials are valid, the browser or webview will redirect the user to the
redirect_uri, to actually let ejabberd pass the token to the app that requested it. It can be either a Web app or `a
mobile / desktop application.
http://client.uri/?access_token=RHIT8DoudzOctdzBhYL9bYvXz28xQ4Oj&token_type=bearer&expires_in=3600&scope=
user_get_roster+sasl_auth&state=
access_token : This is the actual token that the client application can use for OAuth authentication.
token_type : ejabberd supports bearer token type.
expires_in : This is the validity duration of the token, in seconds. When the token expires, a new authorization
token will need to be generated an approved by the user.
scope : Confirms the granted scope to the requesting application. Several scopes can be passed, separated by
'+'.
state : If a state parameter was passed by requesting application in authorization_token URL, it will be
passed back to the application as a parameter of the redirect_uri to help with the client workflow.
Available default scopes
sasl_auth : This scope is use to generate a token that can login over XMPP using SASL X-OAUTH2
mechanism.
user_get_roster (mod_oauth_test): This scope is used to allow retrieving user roster.
Authentication with X-OAUTH2 is done by modifying the SASL auth element as follow:
<auth mechanism='X-OAUTH2'
xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
base64("\0" + user_name + "\0" + oauth_token)
</auth>
The content in the auth element should be the base64 encoding of a string containing a null byte, followed by
the user name, another null byte and the string representation of the user’s OAuth token. This is similar to how
to authenticate with a password using the PLAIN mechanism, except the token is added instead of the user’s
password.
The response is standard for SASL XMPP authentication. For example, on success, server will reply with:
<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
Passing credentials
To pass your bearer token using ReST API, you need to pass your token as Bearer token in Authorization HTTP
header:
Using ReST API, authorization is using a bearer token, meaning you do not need to pass the user and host
parameters.
For XML-RPC, credentials must be passed as XML-RPC parameters, including token but also user and host
parameters. This is for legacy reason, but will likely change in a future version, making user and host implicit,
thanks to bearer token.
Acting as an admin
With both HTTP remote call mechanisms, you can either act as a user or act as an admin (See previous
reference about access rules).
To act as an admin from a ReST API call, the HTTP request must contain the following header:
X-Admin: true
To act as an admin from an XML-RPC query, the XML-RPC query must contain:
{admin,true}
The HTTP endpoint does not take any parameter, so we can just do an HTTP post with empty JSON structure
list (see -d option):
XML-RPC examples
With a command like get_roster , you can get your own roster, or act as an admin to get any user roster.
Here is an (Erlang) XML-RPC example on how to get your own roster:
To get roster of other user using admin authorization, this erlang XML-RPC code can be used:
import xmlrpclib
server_url = 'http://127.0.0.1:4560'
server = xmlrpclib.ServerProxy(server_url)
Admin only
You can enable all those commands individually or enable them all at once by adding all commands in admin
policy.
ejabberd core:
ejabberd core: