synapse.api.auth module — Synapse 1.0 documentation

# synapse.api.auth module

class `synapse.api.auth.` `Auth`(hs)

  Bases: `object`

  FIXME: This class contains a mix of functions for authenticating users of our client-server API and authenticating events added to room graphs.

  `can_federate`(event, auth_events)
  `check`(room_version, event, auth_events, do_sig_check=True)

    Checks if this event is correctly authed.

    Parameters: | room_version (str) – version of the room event – the event being checked. (dict (auth_events) – event-key -> event): the existing room state.
    Returns: | True if the auth checks pass.

  `check_auth_blocking`(user_id=None, threepid=None)

    Checks if the user should be rejected for some external reason, such as monthly active user limiting or global disable flag

    Parameters: | user_id (str|None) – If present, checks for presence against existing cohort (MAU) – threepid (dict|None) – If present, checks for presence against configured threepid. Used in cases where the user is trying register (reserved) – a MAU blocked server, normally they would be rejected but their (with) – is on the reserved list. user_id and (threepid) – should never be set at the same time. (threepid) –

  `check_can_change_room_list`(room_id, user)

    Check if the user is allowed to edit the room’s entry in the published room list.

    Parameters: | room_id (str) – user (UserID) –

  `check_from_context`(room_version, event, context, do_sig_check=True)
  `check_host_in_room`(room_id, host)
  `check_in_room_or_world_readable`(room_id, user_id)

    Checks that the user is or was in the room or the room is world readable. If it isn’t then an exception is raised.

    Returns: | Resolves to the current membership of the user in the room and the membership event ID of the user. If the user is not in the room and never has been, then (Membership.JOIN, None) is returned.
    Return type: | Deferred[tuple[str, str|None]]

  `check_joined_room`(room_id, user_id, current_state=None)

    Check if the user is currently joined in the room :param room_id: The room to check. :type room_id: str :param user_id: The user to check. :type user_id: str :param current_state: Optional map of the current state of the room.

    > If provided then that map is used to check whether they are a member of the room. Otherwise the current membership is loaded from the database.

    Raises: | AuthError if the user is not in the room. –
    Returns: | A deferred membership event for the user if the user is in the room.

  `check_redaction`(room_version, event, auth_events)

    Check whether the event sender is allowed to redact the target event.

    Returns: | True if the the sender is allowed to redact the target event if the target event was created by them. False if the sender is allowed to redact the target event with no further checks.
    Raises: | AuthError if the event sender is definitely not allowed to redact – the target event. –

  `check_user_was_in_room`(room_id, user_id)

    Check if the user was in the room at some point. :param room_id: The room to check. :type room_id: str :param user_id: The user to check. :type user_id: str

    Raises: | AuthError if the user was never in the room. –
    Returns: | A deferred membership event for the user if the user was in the room. This will be the join event if they are currently joined to the room. This will be the leave event if they have left the room.

  `compute_auth_events`(event, current_state_ids, for_verification=False)
  static `get_access_token_from_request`(request, token_not_found_http_status=401)

    Extracts the access_token from the request.

    Parameters: | request – The http request. token_not_found_http_status (int) – The HTTP status code to set in the AuthError if the token isn’t found. This is used in some of the legacy APIs to change the status code to 403 from the default of 401 since some of the old clients depended on auth errors returning 403.
    Returns: | The access_token
    Return type: | unicode
    Raises: | AuthError – If there isn’t an access_token in the request.

  `get_appservice_by_req`(request)
  `get_public_keys`(invite_event)
  `get_user_by_access_token`(token, rights='access')

    Validate access token and get user_id from it

    Parameters: | token (str) – The access token to get the user by. rights (str) – The operation being performed; the access token must allow this.
    Returns: | dict that includes: user (UserID) is_guest (bool) token_id (int|None): access token id. May be None if guest device_id (str|None): device corresponding to access token
    Return type: | Deferred[dict]
    Raises: | AuthError if no user by that token exists or the token is invalid. –

  `get_user_by_req`(request, allow_guest=False, rights='access')

    Get a registered user’s ID.

    Parameters: | - An HTTP request with an access_token query parameter. (request) –
    Returns: | resolves to a `synapse.types.Requester` object
    Return type: | defer.Deferred
    Raises: | AuthError if no user by that token exists or the token is invalid. –

  `get_user_id_from_macaroon`(macaroon)

    Retrieve the user_id given by the caveats on the macaroon.

    Does not validate the macaroon.

    Parameters: | macaroon (pymacaroons.Macaroon) – The macaroon to validate
    Returns: | (str) user id
    Raises: | AuthError if there is no user_id caveat in the macaroon –

  static `has_access_token`(request)

    Checks if the request has an access_token.

    Returns: | False if no access_token was given, True otherwise.
    Return type: | bool

  `is_server_admin`(user)

    Check if the given user is a local server admin.

    Parameters: | user (str) – mxid of user to check
    Returns: | True if the user is an admin
    Return type: | bool

  `validate_macaroon`(macaroon, type_string, verify_expiry, user_id)

    validate that a Macaroon is understood by and was signed by this server.

    Parameters: | macaroon (pymacaroons.Macaroon) – The macaroon to validate type_string (str) – The kind of token required (e.g. “access”, “delete_pusher”) verify_expiry (bool) – Whether to verify whether the macaroon has expired. user_id (str) – The user_id required