Register processes a /register request. http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#post-matrix-client-unstable-register
( req *http.Request, userAPI userapi.ClientUserAPI, cfg *config.ClientAPI, )
| 534 | // Register processes a /register request. |
| 535 | // http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#post-matrix-client-unstable-register |
| 536 | func Register( |
| 537 | req *http.Request, |
| 538 | userAPI userapi.ClientUserAPI, |
| 539 | cfg *config.ClientAPI, |
| 540 | ) util.JSONResponse { |
| 541 | defer req.Body.Close() // nolint: errcheck |
| 542 | reqBody, err := io.ReadAll(req.Body) |
| 543 | if err != nil { |
| 544 | return util.JSONResponse{ |
| 545 | Code: http.StatusBadRequest, |
| 546 | JSON: jsonerror.NotJSON("Unable to read request body"), |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | var r registerRequest |
| 551 | sessionID := gjson.GetBytes(reqBody, "auth.session").String() |
| 552 | if sessionID == "" { |
| 553 | // Generate a new, random session ID |
| 554 | sessionID = util.RandomString(sessionIDLength) |
| 555 | } else if data, ok := sessions.getParams(sessionID); ok { |
| 556 | // Use the parameters from the session as our defaults. |
| 557 | // Some of these might end up being overwritten if the |
| 558 | // values are specified again in the request body. |
| 559 | r.Username = data.Username |
| 560 | r.Password = data.Password |
| 561 | r.DeviceID = data.DeviceID |
| 562 | r.InitialDisplayName = data.InitialDisplayName |
| 563 | r.InhibitLogin = data.InhibitLogin |
| 564 | // Check if the user already registered using this session, if so, return that result |
| 565 | if response, ok := sessions.getCompletedRegistration(sessionID); ok { |
| 566 | return util.JSONResponse{ |
| 567 | Code: http.StatusOK, |
| 568 | JSON: response, |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | } |
| 573 | if resErr := httputil.UnmarshalJSON(reqBody, &r); resErr != nil { |
| 574 | return *resErr |
| 575 | } |
| 576 | if req.URL.Query().Get("kind") == "guest" { |
| 577 | return handleGuestRegistration(req, r, cfg, userAPI) |
| 578 | } |
| 579 | |
| 580 | // Don't allow numeric usernames less than MAX_INT64. |
| 581 | if _, err := strconv.ParseInt(r.Username, 10, 64); err == nil { |
| 582 | return util.JSONResponse{ |
| 583 | Code: http.StatusBadRequest, |
| 584 | JSON: jsonerror.InvalidUsername("Numeric user IDs are reserved"), |
| 585 | } |
| 586 | } |
| 587 | // Auto generate a numeric username if r.Username is empty |
| 588 | if r.Username == "" { |
| 589 | res := &userapi.QueryNumericLocalpartResponse{} |
| 590 | if err := userAPI.QueryNumericLocalpart(req.Context(), res); err != nil { |
| 591 | util.GetLogger(req.Context()).WithError(err).Error("userAPI.QueryNumericLocalpart failed") |
| 592 | return jsonerror.InternalServerError() |
| 593 | } |
no test coverage detected