Return newly created error if user already exists (#1002)

Signed-off-by: Till Faelligen <tfaelligen@gmail.com>
This commit is contained in:
Till Faelligen 2020-06-01 13:55:30 +02:00
parent 97c64bdb6d
commit c7bd7aff58
4 changed files with 17 additions and 10 deletions

View file

@ -16,6 +16,7 @@ package appservice
import ( import (
"context" "context"
"errors"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@ -29,6 +30,7 @@ import (
"github.com/matrix-org/dendrite/appservice/workers" "github.com/matrix-org/dendrite/appservice/workers"
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
"github.com/matrix-org/dendrite/internal"
"github.com/matrix-org/dendrite/internal/basecomponent" "github.com/matrix-org/dendrite/internal/basecomponent"
"github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/dendrite/internal/config"
"github.com/matrix-org/dendrite/internal/transactions" "github.com/matrix-org/dendrite/internal/transactions"
@ -117,12 +119,12 @@ func generateAppServiceAccount(
ctx := context.Background() ctx := context.Background()
// Create an account for the application service // Create an account for the application service
acc, err := accountsDB.CreateAccount(ctx, as.SenderLocalpart, "", as.ID) _, err := accountsDB.CreateAccount(ctx, as.SenderLocalpart, "", as.ID)
if err != nil { if err != nil {
if errors.Is(err, internal.ErrSQLUserExists) { // This account already exists
return nil
}
return err return err
} else if acc == nil {
// This account already exists
return nil
} }
// Create a dummy device with a dummy token for the application service // Create a dummy device with a dummy token for the application service

View file

@ -164,7 +164,7 @@ func (d *Database) createAccount(
} }
if err := d.profiles.insertProfile(ctx, txn, localpart); err != nil { if err := d.profiles.insertProfile(ctx, txn, localpart); err != nil {
if internal.IsUniqueConstraintViolationErr(err) { if internal.IsUniqueConstraintViolationErr(err) {
return nil, nil return nil, internal.ErrSQLUserExists
} }
return nil, err return nil, err
} }

View file

@ -830,15 +830,16 @@ func completeRegistration(
acc, err := accountDB.CreateAccount(ctx, username, password, appserviceID) acc, err := accountDB.CreateAccount(ctx, username, password, appserviceID)
if err != nil { if err != nil {
if errors.Is(err, internal.ErrSQLUserExists) { // user already exists
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.UserInUse("Desired user ID is already taken."),
}
}
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusInternalServerError, Code: http.StatusInternalServerError,
JSON: jsonerror.Unknown("failed to create account: " + err.Error()), JSON: jsonerror.Unknown("failed to create account: " + err.Error()),
} }
} else if acc == nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.UserInUse("Desired user ID is already taken."),
}
} }
// Increment prometheus counter for created users // Increment prometheus counter for created users

View file

@ -16,11 +16,15 @@ package internal
import ( import (
"database/sql" "database/sql"
"errors"
"fmt" "fmt"
"runtime" "runtime"
"time" "time"
) )
// ErrUserExists is returned if a username already exists in the database.
var ErrSQLUserExists = errors.New("Username already exists")
// A Transaction is something that can be committed or rolledback. // A Transaction is something that can be committed or rolledback.
type Transaction interface { type Transaction interface {
// Commit the transaction // Commit the transaction