Skip to content
Snippets Groups Projects
specification.rst 54.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Kegan Dougal's avatar
    Kegan Dougal committed
    
    After the login is completed, the client's fully-qualified user ID and a new access 
    token MUST be returned::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "user_id": "@user:matrix.org",
        "access_token": "abcdef0123456789"
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    The ``user_id`` key is particularly useful if the home server wishes to support 
    localpart entry of usernames (e.g. "user" rather than "@user:matrix.org"), as the
    client may not be able to determine its ``user_id`` in this case.
    
    If a login has multiple requests, the home server may wish to create a session. If
    a home server responds with a 'session' key to a request, clients MUST submit it in 
    subsequent requests until the login is completed::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "session": "<session id>"
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    This specification defines the following login types:
    
     - ``m.login.password``
     - ``m.login.oauth2``
     - ``m.login.email.code``
     - ``m.login.email.url``
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    Password-based
    --------------
    
    Kegan Dougal's avatar
    Kegan Dougal committed
      ``m.login.password``
    
    Kegan Dougal's avatar
    Kegan Dougal committed
      Login is supported via a username and password.
    
    To respond to this type, reply with::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "type": "m.login.password",
        "user": "<user_id or user localpart>",
        "password": "<password>"
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    The home server MUST respond with either new credentials, the next stage of the login
    process, or a standard error response.
    
    Kegan Dougal's avatar
    Kegan Dougal committed
      ``m.login.oauth2``
    
    Kegan Dougal's avatar
    Kegan Dougal committed
      Login is supported via OAuth2 URLs. This login consists of multiple requests.
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    To respond to this type, reply with::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "user": "<user_id or user localpart>"
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    The server MUST respond with::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "uri": <Authorization Request URI OR service selection URI>
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    The home server acts as a 'confidential' client for the purposes of OAuth2.
    If the uri is a ``sevice selection URI``, it MUST point to a webpage which prompts the 
    user to choose which service to authorize with. On selection of a service, this
    MUST link through to an ``Authorization Request URI``. If there is only 1 service which the
    
    home server accepts when logging in, this indirection can be skipped and the
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    "uri" key can be the ``Authorization Request URI``. 
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    The client then visits the ``Authorization Request URI``, which then shows the OAuth2 
    Allow/Deny prompt. Hitting 'Allow' returns the ``redirect URI`` with the auth code. 
    Home servers can choose any path for the ``redirect URI``. The client should visit 
    the ``redirect URI``, which will then finish the OAuth2 login process, granting the 
    
    home server an access token for the chosen service. When the home server gets 
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    this access token, it verifies that the cilent has authorised with the 3rd party, and 
    can now complete the login. The OAuth2 ``redirect URI`` (with auth code) MUST respond 
    with either new credentials, the next stage of the login process, or a standard error 
    response.
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    For example, if a home server accepts OAuth2 from Google, it would return the 
    Authorization Request URI for Google::
    
    
      {
        "uri": "https://accounts.google.com/o/oauth2/auth?response_type=code&
        client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=photos"
      }
    
    The client then visits this URI and authorizes the home server. The client then
    visits the REDIRECT_URI with the auth code= query parameter which returns::
    
      {
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "user_id": "@user:matrix.org",
    
        "access_token": "0123456789abcdef"
      }
    
    Email-based (code)
    ------------------
    
    Kegan Dougal's avatar
    Kegan Dougal committed
      ``m.login.email.code``
    
    Kegan Dougal's avatar
    Kegan Dougal committed
      Login is supported by typing in a code which is sent in an email. This login 
      consists of multiple requests.
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    To respond to this type, reply with::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "user": "<user_id or user localpart>",
        "email": "<email address>"
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    After validating the email address, the home server MUST send an email containing
    an authentication code and return::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "type": "m.login.email.code",
        "session": "<session id>"
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    The second request in this login stage involves sending this authentication code::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "session": "<session id>",
        "code": "<code in email sent>"
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    The home server MUST respond to this with either new credentials, the next stage of 
    the login process, or a standard error response.
    
    Kegan Dougal's avatar
    Kegan Dougal committed
      ``m.login.email.url``
    
    Kegan Dougal's avatar
    Kegan Dougal committed
      Login is supported by clicking on a URL in an email. This login consists of 
      multiple requests.
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    To respond to this type, reply with::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "user": "<user_id or user localpart>",
        "email": "<email address>"
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    After validating the email address, the home server MUST send an email containing
    an authentication URL and return::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "type": "m.login.email.url",
        "session": "<session id>"
    
      }
    
    The email contains a URL which must be clicked. After it has been clicked, the
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    client should perform another request::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "session": "<session id>"
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    The home server MUST respond to this with either new credentials, the next stage of 
    the login process, or a standard error response. 
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    A common client implementation will be to periodically poll until the link is clicked.
    If the link has not been visited yet, a standard error response with an errcode of 
    ``M_LOGIN_EMAIL_URL_NOT_YET`` should be returned.
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    N-Factor Authentication
    -----------------------
    Multiple login stages can be combined to create N-factor authentication during login.
    
    This can be achieved by responding with the ``next`` login type on completion of a 
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    previous login stage::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "next": "<next login type>"
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    If a home server implements N-factor authentication, it MUST respond with all 
    
    ``stages`` when initially queried for their login requirements::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
        "type": "<1st login type>",
        "stages": [ <1st login type>, <2nd login type>, ... , <Nth login type> ]
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    This can be represented conceptually as::
    
       _______________________
      |    Login Stage 1      |
      | type: "<login type1>" |
      |  ___________________  |
      | |_Request_1_________| | <-- Returns "session" key which is used throughout.
      |  ___________________  |     
      | |_Request_2_________| | <-- Returns a "next" value of "login type2"
      |_______________________|
                |
                |
       _________V_____________
      |    Login Stage 2      |
      | type: "<login type2>" |
      |  ___________________  |
      | |_Request_1_________| |
      |  ___________________  |
      | |_Request_2_________| |
      |  ___________________  |
      | |_Request_3_________| | <-- Returns a "next" value of "login type3"
      |_______________________|
                |
                |
       _________V_____________
      |    Login Stage 3      |
      | type: "<login type3>" |
      |  ___________________  |
      | |_Request_1_________| | <-- Returns user credentials
      |_______________________|
    
    Kegan Dougal's avatar
    Kegan Dougal committed
    Clients cannot be expected to be able to know how to process every single
    login type. If a client determines it does not know how to handle a given
    login type, it should request a login fallback page::
    
    Kegan Dougal's avatar
    Kegan Dougal committed
      GET matrix/client/api/v1/login/fallback
    
    
    This MUST return an HTML page which can perform the entire login process.
    
    Identity
    ========
    
    .. NOTE::
      This section is a work in progress.
    
    .. TODO Dave
      - 3PIDs and identity server, functions
    
    
    Federation
    ==========
    
    Federation is the term used to describe how to communicate between Matrix home 
    servers. Federation is a mechanism by which two home servers can exchange
    Matrix event messages, both as a real-time push of current events, and as a
    historic fetching mechanism to synchronise past history for clients to view. It
    uses HTTP connections between each pair of servers involved as the underlying
    transport. Messages are exchanged between servers in real-time by active pushing
    from each server's HTTP client into the server of the other. Queries to fetch
    historic data for the purpose of back-filling scrollback buffers and the like
    can also be performed.
    
    There are three main kinds of communication that occur between home servers:
    
    
       These are single request/response interactions between a given pair of
    
       servers, initiated by one side sending an HTTP GET request to obtain some
    
       information, and responded by the other. They are not persisted and contain
       no long-term significant history. They simply request a snapshot state at the
       instant the query is made.
    
    
       These are notifications of events that are pushed from one home server to
       another. They are not persisted and contain no long-term significant history,
       nor does the receiving home server have to reply to them.
    
    
       These are notifications of events that are broadcast from one home server to
       any others that are interested in the same "context" (namely, a Room ID).
       They are persisted to long-term storage and form the record of history for
       that context.
    
    
    EDUs and PDUs are further wrapped in an envelope called a Transaction, which is 
    transferred from the origin to the destination home server using an HTTP PUT request.
    
    .. WARNING::
      This section may be misleading or inaccurate.
    
    
    The transfer of EDUs and PDUs between home servers is performed by an exchange
    
    of Transaction messages, which are encoded as JSON objects, passed over an 
    HTTP PUT request. A Transaction is meaningful only to the pair of home servers that 
    exchanged it; they are not globally-meaningful.
    
    Each transaction has:
     - An opaque transaction ID.
     - A timestamp (UNIX epoch time in milliseconds) generated by its origin server.
     - An origin and destination server name.
     - A list of "previous IDs".
     - A list of PDUs and EDUs - the actual message payload that the Transaction carries.
    
     {
      "transaction_id":"916d630ea616342b42e98a3be0b74113",
    
      "ts":1404835423000,
      "origin":"red",
      "destination":"blue",
      "prev_ids":["e1da392e61898be4d2009b9fecce5325"],
      "pdus":[...],
    
    The ``prev_ids`` field contains a list of previous transaction IDs that
    the ``origin`` server has sent to this ``destination``. Its purpose is to act as a
    
    sequence checking mechanism - the destination server can check whether it has
    successfully received that Transaction, or ask for a retransmission if not.
    
    
    The ``pdus`` field of a transaction is a list, containing zero or more PDUs.[*]
    Each PDU is itself a JSON object containing a number of keys, the exact details of
    which will vary depending on the type of PDU. Similarly, the ``edus`` field is
    
    another list containing the EDUs. This key may be entirely absent if there are
    no EDUs to transfer.
    
    (* Normally the PDU list will be non-empty, but the server should cope with
    receiving an "empty" transaction, as this is useful for informing peers of other
    transaction IDs they should be aware of. This effectively acts as a push
    mechanism to encourage peers to continue to replicate content.)
    
    
    .. WARNING::
      This section may be misleading or inaccurate.
    
    
    All PDUs have:
     - An ID
     - A context
     - A declaration of their type
     - A list of other PDU IDs that have been seen recently on that context (regardless of which origin
       sent them)
    
    
    [[TODO(paul): Update this structure so that 'pdu_id' is a two-element
    [origin,ref] pair like the prev_pdus are]]
    
    
      "context":"#example.green",
      "origin":"green",
      "ts":1404838188000,
      "pdu_type":"m.text",
      "prev_pdus":[["blue","99d16afbc857975916f1d73e49e52b65"]],
      "content":...
    
    In contrast to Transactions, it is important to note that the ``prev_pdus``
    
    field of a PDU refers to PDUs that any origin server has sent, rather than
    
    previous IDs that this ``origin`` has sent. This list may refer to other PDUs sent
    
    by the same origin as the current one, or other origins.
    
    Because of the distributed nature of participants in a Matrix conversation, it
    is impossible to establish a globally-consistent total ordering on the events.
    However, by annotating each outbound PDU at its origin with IDs of other PDUs it
    has received, a partial ordering can be constructed allowing causallity
    relationships to be preserved. A client can then display these messages to the
    end-user in some order consistent with their content and ensure that no message
    that is semantically in reply of an earlier one is ever displayed before it.
    
    PDUs fall into two main categories: those that deliver Events, and those that
    synchronise State. For PDUs that relate to State synchronisation, additional
    keys exist to support this:
    
    
     {...,
      "is_state":true,
      "state_key":TODO
      "power_level":TODO
      "prev_state_id":TODO
      "prev_state_origin":TODO}
    
    [[TODO(paul): At this point we should probably have a long description of how
    State management works, with descriptions of clobbering rules, power levels, etc
    etc... But some of that detail is rather up-in-the-air, on the whiteboard, and
    so on. This part needs refining. And writing in its own document as the details
    relate to the server/system as a whole, not specifically to server-server
    federation.]]
    
    EDUs, by comparison to PDUs, do not have an ID, a context, or a list of
    "previous" IDs. The only mandatory fields for these are the type, origin and
    destination home server names, and the actual nested content.
    
    
     {"edu_type":"m.presence",
      "origin":"blue",
      "destination":"orange",
      "content":...}
    
    Backfilling
    -----------
    
    .. NOTE::
      This section is a work in progress.
    
    .. TODO
      - What it is, when is it used, how is it done
    
    .. NOTE::
      This section is a work in progress.
    
    .. TODO
      - Why it is needed
    
    Rate limiting
    -------------
    Home servers SHOULD implement rate limiting to reduce the risk of being overloaded. If a
    request is refused due to rate limiting, it should return a standard error response of
    the form::
    
      {
        "errcode": "M_LIMIT_EXCEEDED",
        "error": "string",
        "retry_after_ms": integer (optional)
      }
    
    The ``retry_after_ms`` key SHOULD be included to tell the client how long they have to wait
    in milliseconds before they can try again.
    
    
    .. TODO
      - crypto (s-s auth)
      - E2E
      - Lawful intercept + Key Escrow
      TODO Mark
    
    .. NOTE::
      This section is a work in progress.
    
    .. NOTE::
      This section is a work in progress.
    
    .. TODO
      - path to upload
      - format for thumbnail paths, mention what it is protecting against.
      - content size limit and associated M_ERROR.
    
    
    Address book repository
    =======================
    
    .. NOTE::
      This section is a work in progress.
    
    .. TODO
      - format: POST(?) wodges of json, some possible processing, then return wodges of json on GET.
      - processing may remove dupes, merge contacts, pepper with extra info (e.g. matrix-ability of
        contacts), etc.
      - Standard json format for contacts? Piggy back off vcards?
    
    .. NOTE::
      This section is a work in progress.
    
    .. TODO
      - domain specific words/acronyms with definitions
    
    
    User ID:
      An opaque ID which identifies an end-user, which consists of some opaque 
      localpart combined with the domain name of their home server. 
    
    
    .. Links through the external API docs are below
    .. =============================================
    
    
    .. |createRoom| replace:: ``/createRoom``
    .. _createRoom: /-rooms/create_room
    
    .. |initialSync| replace:: ``/initialSync``
    .. _initialSync: /-events/initial_sync
    
    
    .. |/rooms/<room_id>/initialSync| replace:: ``/rooms/<room_id>/initialSync``
    .. _/rooms/<room_id>/initialSync: /-rooms/get_room_sync_data
    
    .. |/rooms/<room_id>/messages| replace:: ``/rooms/<room_id>/messages``
    .. _/rooms/<room_id>/messages: /-rooms/get_messages
    
    .. |/rooms/<room_id>/members| replace:: ``/rooms/<room_id>/members``
    .. _/rooms/<room_id>/members: /-rooms/get_members
    
    .. |/rooms/<room_id>/state| replace:: ``/rooms/<room_id>/state``
    .. _/rooms/<room_id>/state: /-rooms/get_state_events
    
    .. |/rooms/<room_id>/send/<event_type>| replace:: ``/rooms/<room_id>/send/<event_type>``
    .. _/rooms/<room_id>/send/<event_type>: /-rooms/send_non_state_event
    
    .. |/rooms/<room_id>/state/<event_type>/<state_key>| replace:: ``/rooms/<room_id>/state/<event_type>/<state_key>``
    .. _/rooms/<room_id>/state/<event_type>/<state_key>: /-rooms/send_state_event
    
    .. |/rooms/<room_id>/invite| replace:: ``/rooms/<room_id>/invite``
    .. _/rooms/<room_id>/invite: /-rooms/invite
    
    .. |/rooms/<room_id>/join| replace:: ``/rooms/<room_id>/join``
    .. _/rooms/<room_id>/join: /-rooms/join_room
    
    .. |/rooms/<room_id>/leave| replace:: ``/rooms/<room_id>/leave``
    .. _/rooms/<room_id>/leave: /-rooms/leave
    
    .. |/rooms/<room_id>/ban| replace:: ``/rooms/<room_id>/ban``
    .. _/rooms/<room_id>/ban: /-rooms/ban
    
    .. |/join/<room_alias_or_id>| replace:: ``/join/<room_alias_or_id>``
    .. _/join/<room_alias_or_id>: /-rooms/join
    
    
    .. _`Event Stream`: /-events/get_event_stream