SetLocalAlias implements PUT /directory/room/{roomAlias}
( req *http.Request, device *userapi.Device, alias string, cfg *config.ClientAPI, rsAPI roomserverAPI.ClientRoomserverAPI, )
| 113 | |
| 114 | // SetLocalAlias implements PUT /directory/room/{roomAlias} |
| 115 | func SetLocalAlias( |
| 116 | req *http.Request, |
| 117 | device *userapi.Device, |
| 118 | alias string, |
| 119 | cfg *config.ClientAPI, |
| 120 | rsAPI roomserverAPI.ClientRoomserverAPI, |
| 121 | ) util.JSONResponse { |
| 122 | _, domain, err := gomatrixserverlib.SplitID('#', alias) |
| 123 | if err != nil { |
| 124 | return util.JSONResponse{ |
| 125 | Code: http.StatusBadRequest, |
| 126 | JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"), |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if domain != cfg.Matrix.ServerName { |
| 131 | return util.JSONResponse{ |
| 132 | Code: http.StatusForbidden, |
| 133 | JSON: jsonerror.Forbidden("Alias must be on local homeserver"), |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Check that the alias does not fall within an exclusive namespace of an |
| 138 | // application service |
| 139 | // TODO: This code should eventually be refactored with: |
| 140 | // 1. The new method for checking for things matching an AS's namespace |
| 141 | // 2. Using an overall Regex object for all AS's just like we did for usernames |
| 142 | reqUserID, _, err := gomatrixserverlib.SplitID('@', device.UserID) |
| 143 | if err != nil { |
| 144 | return util.JSONResponse{ |
| 145 | Code: http.StatusBadRequest, |
| 146 | JSON: jsonerror.BadJSON("User ID must be in the form '@localpart:domain'"), |
| 147 | } |
| 148 | } |
| 149 | for _, appservice := range cfg.Derived.ApplicationServices { |
| 150 | // Don't prevent AS from creating aliases in its own namespace |
| 151 | // Note that Dendrite uses SenderLocalpart as UserID for AS users |
| 152 | if reqUserID != appservice.SenderLocalpart { |
| 153 | if aliasNamespaces, ok := appservice.NamespaceMap["aliases"]; ok { |
| 154 | for _, namespace := range aliasNamespaces { |
| 155 | if namespace.Exclusive && namespace.RegexpObject.MatchString(alias) { |
| 156 | return util.JSONResponse{ |
| 157 | Code: http.StatusBadRequest, |
| 158 | JSON: jsonerror.ASExclusive("Alias is reserved by an application service"), |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | var r struct { |
| 167 | RoomID string `json:"room_id"` |
| 168 | } |
| 169 | if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil { |
| 170 | return *resErr |
| 171 | } |
| 172 |
no test coverage detected