XEP-xxxx: Matrix Client-Server API over XMPP

Abstract:This specification defines an XMPP transport for the Matrix Client-Server API
Author:Hubert Chathi
Copyright:© 1999 – 2017 XMPP Standards Foundation. SEE LEGAL NOTICES.
Status:ProtoXEP
Type:Humorous
Version:0.0.2
Last Updated:2017-04-01

WARNING: This document has not yet been accepted for consideration or approved in any official manner by the XMPP Standards Foundation, and this document is not yet an XMPP Extension Protocol (XEP). If this document is accepted as a XEP by the XMPP Council, it will be published at <http://xmpp.org/extensions/> and announced on the <[email protected]> mailing list.


Table of Contents


1. Introduction
2. Business Rules
    2.1. Matrix Identity
    2.2. Enter the Matrix
    2.3. Matrix Transformations
3. Implementation Notes
4. Security Considerations
    4.1. Server Security
    4.2. Client Security
5. IANA Considerations
6. XMPP Registrar Considerations

Appendices
    A: Document Information
    B: Author Information
    C: Legal Notices
    D: Relation to XMPP
    E: Discussion Venue
    F: Requirements Conformance
    G: Notes
    H: Revision History


1. Introduction

A common complaint about the Matrix protocol is its use of HTTP as its baseline transport. As everyone knows, HTTP is a terrible protocol to use as a transport for real-time communications. To remedy this, this specification defines a transport for the Matrix Client-Server API using a proven real-time communications protocol, namely XMPP.

The Matrix specification is large; it is said to include a kitchen sink, and no doubt includes other fixtures as well, given its ability to create plumbed rooms. Further complications in writing a full specification arise from the fact that, unfortunately, no one can be told what the Matrix is; you have to see it for yourself. Thus rather than attempting to present a complete API, this specification presents a general way to transform an HTTP-based API call as specified in Matrix Client-Server API [1] to an XMPP-based API call.

2. Business Rules

2.1 Matrix Identity

Since Matrix user IDs (MXIDs) are in a different form from JIDs, they must be mapped to a valid JID that can be used where the XMPP stream expects a valid JID. This can be done simply by mapping the MXID @localpart:domain to the JID precislocalpart@_matrix.domain, where precislocalpart is localpart prepared according to the UsernameCaseMapped profile defined in RFC 7613 [2] § 3.2. In most cases, this identity map will leave the localpart unchanged. The _matrix subdomain is prepended to the domain in order to avoid collisions in the case where a domain serves both Matrix and XMPP users.

2.2 Enter the Matrix

To begin the session, the Matrix client opens an XML stream with the Matrix server. The XML stream follows the normal lifecycle of an XMPP stream as specified in XMPP Core [3]. The authentication step is used in place of the /login Matrix HTTP endpoint.

The server MUST treat the closing of the stream as if the /logout Matrix HTTP endpoint was called.

2.3 Matrix Transformations

Once an XML stream is established and ready for exchanging XML stanzas, the client can begin sending requests to the server.

An HTTP Matrix request can easily be transformed to an XMPP request using HTTP Over XMPP Transport (XEP-0332) [4]. Since the request is intended to be processed directly by the server, the "to" attribute is omitted in the <iq> request stanza. In order to distinguish the request from a normal XEP-0332 request, the Host header is set to the special value "[matrix]", which will never occur in a real HTTP Host header. Since the XML stream is authenticated, there is no need to send the access token in the request as is required in the HTTP-based API.

Example 1. Resolving a room alias to a room ID

<iq type='set' id='1'>
  <req xmlns='urn:xmpp:http' method='GET'
       resource='/_matrix/client/r0/directory/room/%23rabbithole%3Awonderland.lit'
       version='1.1'>
    <headers xmlns='http://jabber.org/protocol/shim'>
      <header name='Host'>[matrix]</header>
    </headers>
  </req>
</iq>

<iq type='result' id='1'>
  <resp xmlns='urn:xmpp:http' version='1.1' statusCode='200'
        statusMessage='OK'>
    <headers xmlns='http://jabber.org/protocol/shim'>
      <header name='Date'>Sat, 01 Apr 2017 00:00:00 GMT</header>
      <header name='Server'>Synapse/0.19.3</header>
      <header name='Content-Length'>...</header>
      <header name='Content-Type'>application/json</header>
    </headers>
    <data>
      <text>{
  "room_id": "!abnjk1jdasj98:wonderland.lit",
  "servers": [
    "wonderland.lit",
    "matrix.org"
  ]
}</text>
    </data>
  </resp>
</iq>

Example 2. Sending a message

<iq type='set' id='2'>
  <req xmlns='urn:xmpp:http' method='PUT'
       resource='/_matrix/client/r0/rooms/%21636q39766251%3Amatrix.org/send/m.room.message/35'
       version='1.1'>
    <headers xmlns='http://jabber.org/protocol/shim'>
      <header name='Host'>[matrix]</header>
      <header name='Content-Type'>application/json</header>
      <header name='Content-Length'>...</header>
    </headers>
    <data>
      <text>{
  "msgtype": "m.text",
  "body": "There is no spoon."
}</text>
    </data>
  </req>
</iq>

<iq type='result' id='2'>
  <resp xmlns='urn:xmpp:http' version='1.1' statusCode='200'
        statusMessage='OK'>
    <headers xmlns='http://jabber.org/protocol/shim'>
      <header name='Date'>Sat, 01 Apr 2017 00:00:00 GMT</header>
      <header name='Server'>Synapse/0.19.3</header>
      <header name='Content-Length'>...</header>
      <header name='Content-Type'>application/json</header>
    </headers>
    <data>
      <text>{
    "event_id": "YUwRidLecu"
}</text>
    </data>
  </resp>
</iq>

This is an obvious improvement over the baseline Matrix Client-Server API.

3. Implementation Notes

Additional efficiency MAY be achieved by combining this specification with Binary XMPP (XEP-0239) [5], resulting in a protocol that is clearly superior to the HTTP-based Matrix Client-Server API.

If a normal XMPP session is not possible due to network or application limitations, this specification MAY be used in conjunction with XMPP Over BOSH (XEP-0206) [6] to get around any such limitations.

4. Security Considerations

4.1 Server Security

Enhanced security for Matrix may be achieved through the combined use of agents and sentinels. Server administrators SHOULD ensure that these security measures work with XMPP-based clients.

4.2 Client Security

Unexpected changes in Matrix can result in stanzas being re-sent. When clients detect such instances of déjà vu, they should find an exit and disconnect gracefully as soon as possible in order to avoid unexpected termination.

5. IANA Considerations

None.

6. XMPP Registrar Considerations

None.


Appendices


Appendix A: Document Information

Series: XEP
Number: xxxx
Publisher: XMPP Standards Foundation
Status: ProtoXEP
Type: Humorous
Version: 0.0.2
Last Updated: 2017-04-01
Approving Body: XMPP Council
Dependencies: XMPP Core, XEP-0332, RFC 7613, Matrix Client-Server API
Supersedes: None
Superseded By: None
Short Name: N/A
This document in other formats: XML  PDF


Appendix B: Author Information

Hubert Chathi

Email: [email protected]
JabberID: [email protected]


Appendix C: Legal Notices

Copyright

This XMPP Extension Protocol is copyright © 1999 – 2017 by the XMPP Standards Foundation (XSF).

Permissions

Permission is hereby granted, free of charge, to any person obtaining a copy of this specification (the "Specification"), 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.

Disclaimer of Warranty

## NOTE WELL: This Specification is provided on an "AS IS" 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. ##

Limitation of 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 from, out of, or in connection with the Specification or the implementation, deployment, or other use of 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.

IPR Conformance

This XMPP Extension Protocol has been contributed in full conformance with the XSF's Intellectual Property Rights Policy (a copy of which can be found at <https://xmpp.org/about/xsf/ipr-policy> or obtained by writing to XMPP Standards Foundation, P.O. Box 787, Parker, CO 80134 USA).

Appendix D: Relation to XMPP

The Extensible Messaging and Presence Protocol (XMPP) is defined in the XMPP Core (RFC 6120) and XMPP IM (RFC 6121) specifications contributed by the XMPP Standards Foundation to the Internet Standards Process, which is managed by the Internet Engineering Task Force in accordance with RFC 2026. Any protocol defined in this document has been developed outside the Internet Standards Process and is to be understood as an extension to XMPP rather than as an evolution, development, or modification of XMPP itself.


Appendix E: Discussion Venue

The primary venue for discussion of XMPP Extension Protocols is the <[email protected]> discussion list.

Discussion on other xmpp.org discussion lists might also be appropriate; see <http://xmpp.org/about/discuss.shtml> for a complete list.

Given that this XMPP Extension Protocol normatively references IETF technologies, discussion on the <[email protected]> list might also be appropriate.

Errata can be sent to <[email protected]>.


Appendix F: Requirements Conformance

The following requirements keywords as used in this document are to be interpreted as described in RFC 2119: "MUST", "SHALL", "REQUIRED"; "MUST NOT", "SHALL NOT"; "SHOULD", "RECOMMENDED"; "SHOULD NOT", "NOT RECOMMENDED"; "MAY", "OPTIONAL".


Appendix G: Notes

1. Matrix Client-Server API <https://matrix.org/speculator/spec/HEAD/client_server/unstable.html>.

2. RFC 7613: Preparation, Enforcement, and Comparison of Internationalized Strings Representing Usernames and Passwords<http://tools.ietf.org/html/rfc7613>.

3. RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core <http://tools.ietf.org/html/rfc6120>.

4. XEP-0332: HTTP Over XMPP Transport <https://xmpp.org/extensions/xep-0332.html>.

5. XEP-0239: Binary XMPP <https://xmpp.org/extensions/xep-0239.html>.

6. XEP-0206: XMPP Over BOSH <https://xmpp.org/extensions/xep-0206.html>.


Appendix H: Revision History

Note: Older versions of this specification might be available at http://xmpp.org/extensions/attic/

Version 0.0.2 (2017-04-01)

The first version was designed to be a perfect spec. Where none suffered, where everyone would be happy. It was a disaster. No client would accept the spec. Some believed we lacked the programming language to describe the perfect spec.

(hyc)

Version 0.0.1 (2017-04-01)

Initial release.

(hyc)

END