mas: added username_available endpoint

This commit is contained in:
Roman Isaev 2024-12-22 00:23:25 +00:00
parent ba542fedcd
commit 2c47959600
No known key found for this signature in database
GPG key ID: 7BE2B6A6C89AEC7F
4 changed files with 60 additions and 2 deletions

View file

@ -496,6 +496,27 @@ func AdminDownloadState(req *http.Request, device *api.Device, rsAPI roomserverA
} }
} }
func AdminCheckUsernameAvailable(
req *http.Request,
userAPI userapi.ClientUserAPI,
cfg *config.ClientAPI,
) util.JSONResponse {
username := req.URL.Query().Get("username")
if username == "" {
return util.MessageResponse(http.StatusBadRequest, "Query parameter 'username' is missing or empty")
}
rq := userapi.QueryAccountAvailabilityRequest{Localpart: username, ServerName: cfg.Matrix.ServerName}
rs := userapi.QueryAccountAvailabilityResponse{}
if err := userAPI.QueryAccountAvailability(req.Context(), &rq, &rs); err != nil {
return util.ErrorResponse(err)
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: map[string]bool{"available": rs.Available},
}
}
// GetEventReports returns reported events for a given user/room. // GetEventReports returns reported events for a given user/room.
func GetEventReports( func GetEventReports(
req *http.Request, req *http.Request,

View file

@ -334,7 +334,12 @@ func Setup(
return util.JSONResponse{Code: http.StatusOK, JSON: map[string]string{ return util.JSONResponse{Code: http.StatusOK, JSON: map[string]string{
"issuer": m.Issuer, "issuer": m.Issuer,
}} }}
})) })).Methods(http.MethodGet)
synapseAdminRouter.Handle("/admin/v1/username_available",
httputil.MakeServiceAdminAPI("admin_username_available", m.AdminToken, func(r *http.Request) util.JSONResponse {
return AdminCheckUsernameAvailable(r, userAPI, cfg)
})).Methods(http.MethodGet)
} }
if mscCfg.Enabled("msc2753") { if mscCfg.Enabled("msc2753") {

View file

@ -136,6 +136,38 @@ func MakeAdminAPI(
}) })
} }
// MakeServiceAdminAPI is a wrapper around MakeAuthAPI which enforces that the request can only be
// completed by a trusted service e.g. Matrix Auth Service.
func MakeServiceAdminAPI(
metricsName, serviceToken string,
f func(*http.Request) util.JSONResponse,
) http.Handler {
h := func(req *http.Request) util.JSONResponse {
logger := util.GetLogger(req.Context())
token, err := auth.ExtractAccessToken(req)
if err != nil {
logger.Debugf("ExtractAccessToken %s -> HTTP %d", req.RemoteAddr, http.StatusUnauthorized)
return util.JSONResponse{
Code: http.StatusUnauthorized,
JSON: spec.MissingToken(err.Error()),
}
}
if token != serviceToken {
logger.Debugf("Invalid service token '%s'", token)
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: spec.UnknownToken(token),
}
}
// add the service addr to the logger
logger = logger.WithField("service_useragent", req.UserAgent())
req = req.WithContext(util.ContextWithLogger(req.Context(), logger))
return f(req)
}
return MakeExternalAPI(metricsName, h)
}
// MakeExternalAPI turns a util.JSONRequestHandler function into an http.Handler. // MakeExternalAPI turns a util.JSONRequestHandler function into an http.Handler.
// This is used for APIs that are called from the internet. // This is used for APIs that are called from the internet.
func MakeExternalAPI(metricsName string, f func(*http.Request) util.JSONResponse) http.Handler { func MakeExternalAPI(metricsName string, f func(*http.Request) util.JSONResponse) http.Handler {