<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE xep SYSTEM 'xep.dtd' [
  <!ENTITY % ents SYSTEM 'xep.ent'>
%ents;
]>
<?xml-stylesheet type='text/xsl' href='xep.xsl'?>
<xep>
<header>
  <title>Token-based reconnection</title>
  <abstract>This specification defines a token-based session authentication mechanism.</abstract>
  <legal>
    <copyright>This XMPP Extension Protocol is copyright (c) 1999 - 2016 by the XMPP Standards Foundation (XSF).</copyright>
    <permissions>Permission is hereby granted, free of charge, to any person obtaining a copy of this specification (the &quot;Specification&quot;), to make use of the Specification without restriction, including without limitation the rights to implement the Specification in a software program, deploy the Specification in a network service, and copy, modify, merge, publish, translate, distribute, sublicense, or sell copies of the Specification, and to permit persons to whom the Specification is furnished to do so, subject to the condition that the foregoing copyright notice and this permission notice shall be included in all copies or substantial portions of the Specification. Unless separate permission is granted, modified works that are redistributed shall not contain misleading information regarding the authors, title, number, or publisher of the Specification, and shall not claim endorsement of the modified works by the authors, any organization or project to which the authors belong, or the XMPP Standards Foundation.</permissions>
    <warranty>## NOTE WELL: This Specification is provided on an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. In no event shall the XMPP Standards Foundation or the authors of this Specification be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the Specification or the implementation, deployment, or other use of the Specification. ##</warranty>
    <liability>In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall the XMPP Standards Foundation or any author of this Specification be liable for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising out of the use or inability to use the Specification (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if the XMPP Standards Foundation or such author has been advised of the possibility of such damages.</liability>
    <conformance>This XMPP Extension Protocol has been contributed in full conformance with the XSF's Intellectual Property Rights Policy (a copy of which may be found at &lt;<link url='http://xmpp.org/extensions/ipr-policy.shtml'>http://xmpp.org/extensions/ipr-policy.shtml</link>&gt; or obtained by writing to XSF, P.O. Box 1641, Denver, CO 80201 USA).</conformance>
  </legal>
  <number>xxxx</number>
  <status>ProtoXEP</status>
  <type>Standards Track</type>
  <sig>Standards</sig>
  <approver>Council</approver>
  <dependencies>
    <spec>XMPP Core</spec>
    <spec>XEP-0001</spec>
  </dependencies>
  <supersedes/>
  <supersededby/>
  <shortname>NOT_YET_ASSIGNED</shortname>
  <author>
    <firstname>Michał</firstname>
    <surname>Piotrowski</surname>
    <email>michal.piotrowski@erlang-solutions.com</email>
  </author>
  <author>
    <firstname>Radosław</firstname>
    <surname>Szymczyszyn</surname>
    <email>radoslaw.szymczyszyn@erlang-solutions.com</email>
  </author>
  <revision>
    <version>0.0.2</version>
    <date>2016-02-16</date>
    <initials>mp</initials>
    <remark><p>Removed mention of OAuth from Abstract; removed mention of DIGEST-MD5.</p></remark>
  </revision>
  <revision>
    <version>0.0.1</version>
    <date>2016-02-04</date>
    <initials>mp</initials>
    <remark><p>First draft.</p></remark>
  </revision>
</header>
<section1 topic='Introduction' anchor='intro'>
  <p>Automatic reconnection after spurious disconnection is a must-have feature in modern IM applications. One way of providing this feature is storing user login information on disk for reuse. However, plaintext storage of passwords is inherently insecure, while protecting the XMPP password with a master-password is inconvenient for the end-user. With a token-based authentication mechanism, the user only has to provide login information once, for the initial connection to the XMPP server, and can later rely on the application's automatic use of tokens for subsequent reconnections.</p>
  <p>Moreover, while reconnecting to the XMPP server, the client usually has to go through the same long process of SASL challenge-response exchange which may lead to a noticably long reconnection time, especially while using SCRAM-based mechanisms. Providing a token to the XMPP server is both secure and doesn't require multiple challenge-response roundtrips, therefore might significantly speed up reconnection times.</p>
</section1>
<section1 topic='Requirements' anchor='reqs'>
  <p>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.</p>
</section1>
<section1 topic='Glossary' anchor='glossary'>
  <section2 topic="Token types" anchor="token_types">
    <table caption="Token types">
      <tr><th>Token Type</th><th>Description</th></tr>
      <tr><td>Access token</td><td>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.</td></tr>
      <tr><td>Refresh token</td><td>These are longer lived tokens which are tracked by the server,
  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.</td></tr>
    </table>
    <p>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 subject of this specification, though.</p>
  </section2>
  <section2 topic="Token format" anchor="token_formats">
    <p><strong>This section is informal.</strong> The token formats described below are used in one of the experimental implementations, but are not proposed as standard. If such a standard turns out to be required, then <link url="http://jwt.io/">JSON Web Tokens</link> might be a viable format to adopt.</p>
    <p>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 <tt>BARE_JID</tt> and <tt>EXPIRES_AT</tt>. <tt>EXPIRES_AT</tt> is a timestamp saying when given token will expire. <tt>\0</tt> stands for the ASCII null character (i.e. byte 0). Text in single quotes (<tt>'example'</tt>) is literal. <tt>ALL_CAPS</tt> denote parameters.</p>
    <section3 topic="Access token format">
      <code>
BASE64_encode
        ('access', \0, BARE_JID, \0, EXPIRES_AT, \0, DATA)
      </code>
      <p>Example, please note the line break was added only for readability:</p>
      <code>
'access' \0 Q8@wonderland.com \0 64875466454
    \0 0acd0a66d06934791d046060cf9f1ad3c2abb3274cc7e7d7b2bc7e2ac4453ed774b6c6813b40ebec2bbc3774d59d4087
      </code>
    </section3>
    <section3 topic="Refresh token format">
      <code>
BASE64_encode
        ('refresh', \0, BARE_JID, \0, EXPIRES_AT, \0, SEQUENCE_NO, \0, DATA)
      </code>
      <p>Example, please note the line break was added only for readability:</p>
      <code>
'refresh' \0 qp@wonderland.com \0 64875466457 \0 6
    \0 8f57cb019cd6dc6e7779be165b9558611baf71ee4a40d03e77b78b069f482f96c9d23b1ac1ef69f64c1a1db3d36a96ad
      </code>
    </section3>
  </section2>
</section1>
<section1 topic='Use Cases' anchor='usecases'>
  <section2 topic="Obtaining a token" anchor="obtaining_token">
    <p>After authentication with some other mechanism like SCRAM-SHA-1, a client may request a token from the server by sending following iq get to its own bare JID:</p>
    <example caption="Client requests tokens"><![CDATA[
<iq type='get' to='alice@wonderland.com' id='123'>
    <query xmlns='erlang-solutions.com:xmpp:token-auth:0'/>
</iq>
]]></example>
    <example caption="Servers responds with tokens"><![CDATA[
<iq from="alice@wonderland.com" type="result" to="alice@wonderland.com/resource" id="123">
  <items xmlns="erlang-solutions.com:xmpp:token-auth:0">
    <access_token>YWNjZXNzAGFsaWNlQHdvbmRlcmxhbmQuY29tL01pY2hhbC1QaW90cm93c2tpcy1NYWNCb29rLVBybwA2MzYyMTg4Mzc2NAA4M2QwNzNiZjBkOGJlYzVjZmNkODgyY2ZlMzkyZWM5NGIzZjA4ODNlNDI4ZjQzYjc5MGYxOWViM2I2ZWJlNDc0ODc3MDkxZTIyN2RhOGMwYTk2ZTc5ODBhNjM5NjE1Zjk=</access_token>
    <refresh_token>cmVmcmVzaABhbGljZUB3b25kZXJsYW5kLmNvbS9NaWNoYWwtUGlvdHJvd3NraXMtTWFjQm9vay1Qcm8ANjM2MjMwMDYxODQAMQAwZGQxOGJjODhkMGQ0N2MzNTBkYzAwYjcxZjMyZDVmOWIwOTljMmI1ODU5MmNhN2QxZGFmNWFkNGM0NDQ2ZGU2MWYxYzdhNTJjNDUyMGI5YmIxNGIxNTMwMTE4YTM1NTc=</refresh_token>
  </items>
</iq>
      ]]></example>
  </section2>
  <section2 topic="Authentication with an access token" anchor="access_token_auth">
    <example caption="Client authenticates with access token"><![CDATA[
<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="X-OAUTH">
YWNjZXNzAGFsaWNlQHdvbmRlcmxhbmQuY29tL01pY2hhbC1QaW90cm93c2tpcy1NYWNCb29rLVBybwA2MzYyMTg4Mzc2NAA4M2QwNzNiZjBkOGJlYzVjZmNkODgyY2ZlMzkyZWM5NGIzZjA4ODNlNDI4ZjQzYjc5MGYxOWViM2I2ZWJlNDc0ODc3MDkxZTIyN2RhOGMwYTk2ZTc5ODBhNjM5NjE1Zjk=
</auth>
]]></example>
  <example caption="Server responds with success"><![CDATA[
<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
    ]]></example>
  </section2>
  <section2 topic="Authentication with a refresh token" anchor="refresh_token_auth">
    <p>In this situation server will respond with a new refresh token which SHOULD be used in future authentication.</p>
    <example caption="Client authenticates with refresh token"><![CDATA[
<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="X-OAUTH">
cmVmcmVzaABhbGljZUB3b25kZXJsYW5kLmNvbS9NaWNoYWwtUGlvdHJvd3NraXMtTWFjQm9vay1Qcm8ANjM2MjMwMDYxODQAMQAwZGQxOGJjODhkMGQ0N2MzNTBkYzAwYjcxZjMyZDVmOWIwOTljMmI1ODU5MmNhN2QxZGFmNWFkNGM0NDQ2ZGU2MWYxYzdhNTJjNDUyMGI5YmIxNGIxNTMwMTE4YTM1NTc=
</auth>
    ]]></example>
    <example caption="Server responds with success and new refresh token"><![CDATA[
<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl">
cmVmcmVzaABhbGljZUB3b25kZXJsYW5kLmNvbS9NaWNoYWwtUGlvdHJvd3NraXMtTWFjQm9vay1Qcm8ANjM2MjMwMDYxODQAMgAwZGQxOGJjODhkMGQ0N2MzNTBkYzAwYjcxZjMyZDVmOWIwOTljMmI1ODU5MmNhN2QxZGFmNWFkNGM0NDQ2ZGU2MWYxYzdhNTJjNDUyMGI5YmIxNGIxNTMwMTE4YTM1NTc=
</success>    ]]></example>
  </section2>
</section1>
<section1 topic='Business Rules' anchor='rules'>
  <p>A classical authentication is required in order to be able to use a token-based authentication later.</p>
</section1>
<section1 topic='Implementation Notes' anchor='impl'>
  <p>The DATA field in token is relevant only to the XMPP server supporting  this extension.</p>
</section1>
<section1 topic='Security Considerations' anchor='security'>
  <p>The tokens MUST be exhanged over a secured/encrypted C2S stream (via TLS, for example).</p>
</section1>
<section1 topic='IANA Considerations' anchor='iana'>
  <p>This document requires no interaction with &IANA;.</p>
</section1>
<section1 topic='XMPP Registrar Considerations' anchor='registrar'>
  <p>This document requires no interaction with the &REGISTRAR;.</p>
</section1>
<section1 topic='XML Schema' anchor='schema'>
  <p>REQUIRED for protocol specifications.</p>
</section1>
</xep>
