Token-based reconnection
Introduction
Automatic reconnection after spurious disconnection is a must-have feature in modern IM applications. One way of providing this feature is storing the user login information on the disk. Here you need to balance two values - security and convenience for the end-user. To put it simply: storing passwords in plaintext is inherently insecure while protecting the XMPP password with a master-password damages the user experience. With a token-based authentication mechanism, the user has to provide login information only once, for the initial connection to the XMPP server, and can later rely on the application's automatic use of tokens for subsequent reconnections.
Reconnecting to the XMPP server, usually means that the client has to go through the same long process of SASL challenge-response exchange which may cause noticeable lags, especially while using SCRAM-based mechanisms. Providing a token to the XMPP server is secure and doesn't require multiple challenge-response roundtrips, therefore might significantly speed up reconnection times.
Requirements
This extension requires the client application to authenticate to the XMPP server using a regular XMPP authentication mechanism like SCRAM-SHA-1 at least once.
After that, the following authentications may be done using X-OAUTH SASL mechanism with a token obtained from the server.
To enable the feature, modules mod_auth_token
and mod_keystore
have to be enabled on the server. For more details regarding the configuration see mod_auth_token documentation and mod_keystore.
Token types
Token Type | Description |
---|---|
Access token | These are short lived tokens whose grants aren't tracked by the server (i.e. there's no need to store anything in a database). Access tokens can be used as a payload for the X-OAUTH authentication mechanism and grant access to the system. Access tokens can't be revoked. An access token is valid only until its expiry date is reached. |
Refresh token | These are longer lived tokens which are tracked by the server, and therefore require persistent storage. Refresh tokens can be used as a payload for the X-OAUTH authentication mechanism and grant access to the system, as well as result in a new set of tokens being returned upon successful authentication. Refresh tokens can be revoked. A refresh token is valid until it has expired, unless it has been revoked. On revocation, it immediately becomes invalid. As the server stores information about granted tokens, it can also persistently mark them as revoked. |
While only two token types have been described above, implementations might use other token types for specific purposes. For example, a particular token type could limit the access privileges of a user logged into the system or denote an affiliation with a Multi User Chat room. None of such capability grants are a subject of this specification though.
Use cases
Obtaining a token
After authenticating with some other mechanism like SCRAM-SHA-1, a client may request a token from the server by sending the following iq get to its own bare JID:
Client requests tokens
1 2 3 |
|
Server responds with a tokens
1 2 3 4 5 6 |
|
Authentication with an access token
Client authenticates with an access token
1 2 3 |
|
1 |
|
Authentication with a refresh token
In this situation server will respond with a new refresh token which SHOULD be used in future authentication.
Client authenticates with a refresh token
1 2 3 |
|
Server responds with a success and a new refresh token
1 2 3 |
|
Token format
All tokens are exchanged as Base64 encoded binary data.
Serialization format of the token before encoding with Base64 is dependent on its type.
Common parts in every token are BARE_JID
and EXPIRES_AT
. EXPIRES_AT
is a timestamp saying when a given token will expire. \0
stands for the ASCII null character (i.e. byte 0).
Text in single quotes ('example') is literal.
ALL_CAPS
denote parameters.
Access token format
1 2 |
|
Example (please note the line break was added only for readability):
1 2 |
|
Refresh token format
1 2 |
|
Example (please note the line break was added only for readability):
1 2 |
|