Gate registration and guest access per-virtual host

This commit is contained in:
Neil Alexander 2022-11-18 11:33:36 +00:00
parent a8e7ffc7ab
commit b55f00fa98
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
3 changed files with 36 additions and 3 deletions

View file

@ -650,7 +650,13 @@ func handleGuestRegistration(
cfg *config.ClientAPI,
userAPI userapi.ClientUserAPI,
) util.JSONResponse {
if cfg.RegistrationDisabled || cfg.GuestsDisabled {
registrationEnabled := !cfg.RegistrationDisabled
guestsEnabled := !cfg.GuestsDisabled
if r.ServerName != cfg.Matrix.ServerName {
registrationEnabled, guestsEnabled = cfg.Matrix.VirtualHost(r.ServerName).RegistrationAllowed()
}
if !registrationEnabled || !guestsEnabled {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("Guest registration is disabled"),
@ -660,6 +666,7 @@ func handleGuestRegistration(
var res userapi.PerformAccountCreationResponse
err := userAPI.PerformAccountCreation(req.Context(), &userapi.PerformAccountCreationRequest{
AccountType: userapi.AccountTypeGuest,
ServerName: r.ServerName,
}, &res)
if err != nil {
return util.JSONResponse{
@ -736,7 +743,11 @@ func handleRegistrationFlow(
)
}
if cfg.RegistrationDisabled && r.Auth.Type != authtypes.LoginTypeSharedSecret {
registrationEnabled := !cfg.RegistrationDisabled
if r.ServerName != cfg.Matrix.ServerName {
registrationEnabled, _ = cfg.Matrix.VirtualHost(r.ServerName).RegistrationAllowed()
}
if !registrationEnabled && r.Auth.Type != authtypes.LoginTypeSharedSecret {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("Registration is disabled"),

View file

@ -235,7 +235,7 @@ func loadConfig(
if v.KeyValidityPeriod == 0 {
v.KeyValidityPeriod = c.Global.KeyValidityPeriod
}
if v.PrivateKeyPath == "" {
if v.PrivateKeyPath == "" || v.PrivateKey == nil || v.KeyID == "" {
v.KeyID = c.Global.KeyID
v.PrivateKey = c.Global.PrivateKey
continue

View file

@ -151,6 +151,15 @@ func (c *Global) SplitLocalID(sigil byte, id string) (string, gomatrixserverlib.
return u, s, nil
}
func (c *Global) VirtualHost(serverName gomatrixserverlib.ServerName) *VirtualHost {
for _, v := range c.VirtualHosts {
if v.ServerName == serverName {
return v
}
}
return nil
}
func (c *Global) SigningIdentityFor(serverName gomatrixserverlib.ServerName) (*gomatrixserverlib.SigningIdentity, error) {
for _, id := range c.SigningIdentities() {
if id.ServerName == serverName {
@ -202,6 +211,9 @@ type VirtualHost struct {
// Is registration enabled on this virtual host?
AllowRegistration bool `json:"allow_registration"`
// Is guest registration enabled on this virtual host?
AllowGuests bool `json:"allow_guests"`
}
func (v *VirtualHost) Verify(configErrs *ConfigErrors) {
@ -216,6 +228,16 @@ func (v *VirtualHost) SigningIdentity() *gomatrixserverlib.SigningIdentity {
}
}
// RegistrationAllowed returns two bools, the first states whether registration
// is allowed for this virtual host and the second states whether guests are
// allowed for this virtual host.
func (v *VirtualHost) RegistrationAllowed() (bool, bool) {
if v == nil {
return false, false
}
return v.AllowRegistration, v.AllowGuests
}
type OldVerifyKeys struct {
// Path to the private key.
PrivateKeyPath Path `yaml:"private_key"`