checkRestrictedJoin finds out whether or not we can assist in processing a restricted room join. If the room version does not support restricted joins then this function returns with no side effects. This returns three values: - an optional JSON response body (i.e. M_UNABLE_TO_AUTHORISE_JOIN) which
( httpReq *http.Request, rsAPI api.FederationRoomserverAPI, roomVersion gomatrixserverlib.RoomVersion, roomID, userID string, )
| 444 | // like if the room version isn't known or a problem happened talking to |
| 445 | // the roomserver |
| 446 | func checkRestrictedJoin( |
| 447 | httpReq *http.Request, |
| 448 | rsAPI api.FederationRoomserverAPI, |
| 449 | roomVersion gomatrixserverlib.RoomVersion, |
| 450 | roomID, userID string, |
| 451 | ) (*util.JSONResponse, string, error) { |
| 452 | if allowRestricted, err := roomVersion.MayAllowRestrictedJoinsInEventAuth(); err != nil { |
| 453 | return nil, "", err |
| 454 | } else if !allowRestricted { |
| 455 | return nil, "", nil |
| 456 | } |
| 457 | req := &api.QueryRestrictedJoinAllowedRequest{ |
| 458 | RoomID: roomID, |
| 459 | UserID: userID, |
| 460 | } |
| 461 | res := &api.QueryRestrictedJoinAllowedResponse{} |
| 462 | if err := rsAPI.QueryRestrictedJoinAllowed(httpReq.Context(), req, res); err != nil { |
| 463 | return nil, "", err |
| 464 | } |
| 465 | |
| 466 | switch { |
| 467 | case !res.Restricted: |
| 468 | // The join rules for the room don't restrict membership. |
| 469 | return nil, "", nil |
| 470 | |
| 471 | case !res.Resident: |
| 472 | // The join rules restrict membership but our server isn't currently |
| 473 | // joined to all of the allowed rooms, so we can't actually decide |
| 474 | // whether or not to allow the user to join. This error code should |
| 475 | // tell the joining server to try joining via another resident server |
| 476 | // instead. |
| 477 | return &util.JSONResponse{ |
| 478 | Code: http.StatusBadRequest, |
| 479 | JSON: jsonerror.UnableToAuthoriseJoin("This server cannot authorise the join."), |
| 480 | }, "", nil |
| 481 | |
| 482 | case !res.Allowed: |
| 483 | // The join rules restrict membership, our server is in the relevant |
| 484 | // rooms and the user wasn't joined to join any of the allowed rooms |
| 485 | // and therefore can't join this room. |
| 486 | return &util.JSONResponse{ |
| 487 | Code: http.StatusForbidden, |
| 488 | JSON: jsonerror.Forbidden("You are not joined to any matching rooms."), |
| 489 | }, "", nil |
| 490 | |
| 491 | default: |
| 492 | // The join rules restrict membership, our server is in the relevant |
| 493 | // rooms and the user was allowed to join because they belong to one |
| 494 | // of the allowed rooms. We now need to pick one of our own local users |
| 495 | // from within the room to use as the authorising user ID, so that it |
| 496 | // can be referred to from within the membership content. |
| 497 | return nil, res.AuthorisedVia, nil |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | type eventsByDepth []*gomatrixserverlib.HeaderedEvent |
| 502 |
no test coverage detected