From 4e826f4167b1da7d3c6e3c0751cc1201bb9284d2 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Sat, 29 May 2021 18:51:43 +0100 Subject: [PATCH] Matrix: Store user on sign-up --- api.go | 39 ++++++++++++++++++++++++++++++++++++++- lang/form/en-us.json | 1 + models.go | 2 ++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/api.go b/api.go index 66b6f0f..1ebd7db 100644 --- a/api.go +++ b/api.go @@ -354,6 +354,34 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc } } } + var matrixUser MatrixUser + matrixVerified := false + if matrixEnabled { + if req.MatrixPIN == "" { + if app.config.Section("matrix").Key("required").MustBool(false) { + f = func(gc *gin.Context) { + app.debug.Printf("%s: New user failed: Matrix verification not completed", req.Code) + respond(401, "errorMatrixVerification") + } + success = false + return + } + } else { + user, ok := app.matrix.tokens[req.MatrixPIN] + if !ok || !user.Verified { + matrixVerified = false + f = func(gc *gin.Context) { + app.debug.Printf("%s: New user failed: Matrix PIN was invalid", req.Code) + respond(401, "errorInvalidPIN", gc) + } + success = false + return + } + matrixVerified = user.Verified + matrixUser = *user.User + + } + } telegramTokenIndex := -1 if telegramEnabled { if req.TelegramPIN == "" { @@ -536,7 +564,16 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc app.telegram.verifiedTokens = app.telegram.verifiedTokens[:len(app.telegram.verifiedTokens)-1] } } - + if matrixVerified { + delete(app.matrix.tokens, req.MatrixPIN) + if app.storage.matrix == nil { + app.storage.matrix = map[string]MatrixUser{} + } + app.storage.matrix[user.ID] = matrixUser + if err := app.storage.storeMatrixUsers; err != nil { + app.err.Printf("Failed to store Matrix users: %v", err) + } + } if (emailEnabled && app.config.Section("welcome_email").Key("enabled").MustBool(false) && req.Email != "") || telegramTokenIndex != -1 || discordVerified { name := app.getAddressOrName(user.ID) app.debug.Printf("%s: Sending welcome message to %s", req.Username, name) diff --git a/lang/form/en-us.json b/lang/form/en-us.json index 995da7f..7b85cfc 100644 --- a/lang/form/en-us.json +++ b/lang/form/en-us.json @@ -27,6 +27,7 @@ "errorInvalidCode": "Invalid invite code.", "errorTelegramVerification": "Telegram verification required.", "errorDiscordVerification": "Discord verification required.", + "errorMatrixVerification": "Matrix verification required.", "errorInvalidPIN": "PIN is invalid.", "errorUnknown": "Unknown error.", "verified": "Account verified." diff --git a/models.go b/models.go index a5b2ae6..950ccaa 100644 --- a/models.go +++ b/models.go @@ -19,6 +19,8 @@ type newUserDTO struct { TelegramContact bool `json:"telegram_contact"` // Whether or not to use telegram for notifications/pwrs DiscordPIN string `json:"discord_pin" example:"A1-B2-3C"` // Discord verification PIN (if used) DiscordContact bool `json:"discord_contact"` // Whether or not to use discord for notifications/pwrs + MatrixPIN string `json:"matrix_pin" example:"A1-B2-3C"` // Matrix verification PIN (if used) + MatrixContact bool `json:"matrix_contact"` // Whether or not to use matrix for notifications/pwrs } type newUserResponse struct {