From b7ca7550c7f41af010136ac62109b800723da24d Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 17 Jul 2018 16:34:58 +0100 Subject: [PATCH] Change InhibitLogin to a WeakBoolean --- .../dendrite/clientapi/routing/register.go | 11 +++++---- .../matrix-org/dendrite/common/types.go | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go index 957a3dfdf..20f7fd60f 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go @@ -40,6 +40,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/userutil" + "github.com/matrix-org/dendrite/common" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" "github.com/prometheus/client_golang/prometheus" @@ -117,7 +118,7 @@ type registerRequest struct { InitialDisplayName *string `json:"initial_device_display_name"` // Prevent this user from logging in - InhibitLogin int `json:"inhibit_login"` + InhibitLogin common.WeakBoolean `json:"inhibit_login"` // Application Services place Type in the root of their registration // request, whereas clients place it in the authDict struct. @@ -646,10 +647,10 @@ func LegacyRegister( return util.MessageResponse(http.StatusForbidden, "HMAC incorrect") } - return completeRegistration(req.Context(), accountDB, deviceDB, r.Username, r.Password, "", 0, nil) + return completeRegistration(req.Context(), accountDB, deviceDB, r.Username, r.Password, "", false, nil) case authtypes.LoginTypeDummy: // there is nothing to do - return completeRegistration(req.Context(), accountDB, deviceDB, r.Username, r.Password, "", 0, nil) + return completeRegistration(req.Context(), accountDB, deviceDB, r.Username, r.Password, "", false, nil) default: return util.JSONResponse{ Code: http.StatusNotImplemented, @@ -692,7 +693,7 @@ func completeRegistration( accountDB *accounts.Database, deviceDB *devices.Database, username, password, appserviceID string, - inhibitLogin int, + inhibitLogin common.WeakBoolean, displayName *string, ) util.JSONResponse { if username == "" { @@ -724,7 +725,7 @@ func completeRegistration( // Check whether inhibit_login option is set. If so, don't create an access // token or a device for this user - if inhibitLogin != 0 { + if inhibitLogin { return util.JSONResponse{ Code: http.StatusOK, JSON: registerResponse{ diff --git a/src/github.com/matrix-org/dendrite/common/types.go b/src/github.com/matrix-org/dendrite/common/types.go index d8c5c5a7e..e539774e2 100644 --- a/src/github.com/matrix-org/dendrite/common/types.go +++ b/src/github.com/matrix-org/dendrite/common/types.go @@ -14,6 +14,10 @@ package common +import ( + "strconv" +) + // AccountData represents account data sent from the client API server to the // sync API server type AccountData struct { @@ -36,3 +40,22 @@ type AvatarURL struct { type DisplayName struct { DisplayName string `json:"displayname"` } + +// WeakBoolean is a type that will Unmarshal to true or false even if the encoded +// representation is "true"/1 or "false"/0, as well as whatever other forms are +// recognized by strconv.ParseBool +type WeakBoolean bool + +// UnmarshalJSON is overridden here to allow strings vaguely representing a true +// or false boolean to be set as their closest counterpart +func (b *WeakBoolean) UnmarshalJSON(data []byte) error { + result, err := strconv.ParseBool(string(data)) + if err != nil { + return err + } + + // Set boolean value based on string input + *b = WeakBoolean(result) + + return nil +}