mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-23 06:43:09 -06:00
User API support for password changes
This commit is contained in:
parent
2570418f42
commit
af94d74362
|
|
@ -26,6 +26,7 @@ import (
|
||||||
type UserInternalAPI interface {
|
type UserInternalAPI interface {
|
||||||
InputAccountData(ctx context.Context, req *InputAccountDataRequest, res *InputAccountDataResponse) error
|
InputAccountData(ctx context.Context, req *InputAccountDataRequest, res *InputAccountDataResponse) error
|
||||||
PerformAccountCreation(ctx context.Context, req *PerformAccountCreationRequest, res *PerformAccountCreationResponse) error
|
PerformAccountCreation(ctx context.Context, req *PerformAccountCreationRequest, res *PerformAccountCreationResponse) error
|
||||||
|
PerformPasswordUpdate(ctx context.Context, req *PerformPasswordUpdateRequest, res *PerformPasswordUpdateResponse) error
|
||||||
PerformDeviceCreation(ctx context.Context, req *PerformDeviceCreationRequest, res *PerformDeviceCreationResponse) error
|
PerformDeviceCreation(ctx context.Context, req *PerformDeviceCreationRequest, res *PerformDeviceCreationResponse) error
|
||||||
PerformDeviceDeletion(ctx context.Context, req *PerformDeviceDeletionRequest, res *PerformDeviceDeletionResponse) error
|
PerformDeviceDeletion(ctx context.Context, req *PerformDeviceDeletionRequest, res *PerformDeviceDeletionResponse) error
|
||||||
PerformDeviceUpdate(ctx context.Context, req *PerformDeviceUpdateRequest, res *PerformDeviceUpdateResponse) error
|
PerformDeviceUpdate(ctx context.Context, req *PerformDeviceUpdateRequest, res *PerformDeviceUpdateResponse) error
|
||||||
|
|
@ -63,6 +64,10 @@ type PerformDeviceDeletionRequest struct {
|
||||||
UserID string
|
UserID string
|
||||||
// The devices to delete. An empty slice means delete all devices.
|
// The devices to delete. An empty slice means delete all devices.
|
||||||
DeviceIDs []string
|
DeviceIDs []string
|
||||||
|
// The requesting device ID to exclude from deletion. This is needed
|
||||||
|
// so that a password change doesn't cause that client to be logged
|
||||||
|
// out. Only specify when DeviceIDs is empty.
|
||||||
|
ExceptDeviceID string
|
||||||
}
|
}
|
||||||
|
|
||||||
type PerformDeviceDeletionResponse struct {
|
type PerformDeviceDeletionResponse struct {
|
||||||
|
|
@ -165,6 +170,18 @@ type PerformAccountCreationResponse struct {
|
||||||
Account *Account
|
Account *Account
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PerformAccountCreationRequest is the request for PerformAccountCreation
|
||||||
|
type PerformPasswordUpdateRequest struct {
|
||||||
|
Localpart string // Required: The localpart for this account.
|
||||||
|
Password string // Required: The new password to set.
|
||||||
|
}
|
||||||
|
|
||||||
|
// PerformAccountCreationResponse is the response for PerformAccountCreation
|
||||||
|
type PerformPasswordUpdateResponse struct {
|
||||||
|
PasswordUpdated bool
|
||||||
|
Account *Account
|
||||||
|
}
|
||||||
|
|
||||||
// PerformDeviceCreationRequest is the request for PerformDeviceCreation
|
// PerformDeviceCreationRequest is the request for PerformDeviceCreation
|
||||||
type PerformDeviceCreationRequest struct {
|
type PerformDeviceCreationRequest struct {
|
||||||
Localpart string
|
Localpart string
|
||||||
|
|
|
||||||
|
|
@ -98,6 +98,15 @@ func (a *UserInternalAPI) PerformAccountCreation(ctx context.Context, req *api.P
|
||||||
res.Account = acc
|
res.Account = acc
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *UserInternalAPI) PerformPasswordUpdate(ctx context.Context, req *api.PerformPasswordUpdateRequest, res *api.PerformPasswordUpdateResponse) error {
|
||||||
|
if err := a.AccountDB.SetPassword(ctx, req.Localpart, req.Password); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
res.PasswordUpdated = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (a *UserInternalAPI) PerformDeviceCreation(ctx context.Context, req *api.PerformDeviceCreationRequest, res *api.PerformDeviceCreationResponse) error {
|
func (a *UserInternalAPI) PerformDeviceCreation(ctx context.Context, req *api.PerformDeviceCreationRequest, res *api.PerformDeviceCreationResponse) error {
|
||||||
util.GetLogger(ctx).WithFields(logrus.Fields{
|
util.GetLogger(ctx).WithFields(logrus.Fields{
|
||||||
"localpart": req.Localpart,
|
"localpart": req.Localpart,
|
||||||
|
|
@ -128,8 +137,10 @@ func (a *UserInternalAPI) PerformDeviceDeletion(ctx context.Context, req *api.Pe
|
||||||
var devices []api.Device
|
var devices []api.Device
|
||||||
devices, err = a.DeviceDB.RemoveAllDevices(ctx, local)
|
devices, err = a.DeviceDB.RemoveAllDevices(ctx, local)
|
||||||
for _, d := range devices {
|
for _, d := range devices {
|
||||||
|
if d.ID != req.ExceptDeviceID {
|
||||||
deletedDeviceIDs = append(deletedDeviceIDs, d.ID)
|
deletedDeviceIDs = append(deletedDeviceIDs, d.ID)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
err = a.DeviceDB.RemoveDevices(ctx, local, req.DeviceIDs)
|
err = a.DeviceDB.RemoveDevices(ctx, local, req.DeviceIDs)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ const (
|
||||||
|
|
||||||
PerformDeviceCreationPath = "/userapi/performDeviceCreation"
|
PerformDeviceCreationPath = "/userapi/performDeviceCreation"
|
||||||
PerformAccountCreationPath = "/userapi/performAccountCreation"
|
PerformAccountCreationPath = "/userapi/performAccountCreation"
|
||||||
|
PerformPasswordUpdatePath = "/userapi/performPasswordUpdate"
|
||||||
PerformDeviceDeletionPath = "/userapi/performDeviceDeletion"
|
PerformDeviceDeletionPath = "/userapi/performDeviceDeletion"
|
||||||
PerformDeviceUpdatePath = "/userapi/performDeviceUpdate"
|
PerformDeviceUpdatePath = "/userapi/performDeviceUpdate"
|
||||||
|
|
||||||
|
|
@ -81,6 +82,18 @@ func (h *httpUserInternalAPI) PerformAccountCreation(
|
||||||
return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *httpUserInternalAPI) PerformPasswordUpdate(
|
||||||
|
ctx context.Context,
|
||||||
|
request *api.PerformPasswordUpdateRequest,
|
||||||
|
response *api.PerformPasswordUpdateResponse,
|
||||||
|
) error {
|
||||||
|
span, ctx := opentracing.StartSpanFromContext(ctx, "PerformPasswordUpdate")
|
||||||
|
defer span.Finish()
|
||||||
|
|
||||||
|
apiURL := h.apiURL + PerformPasswordUpdatePath
|
||||||
|
return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
||||||
|
}
|
||||||
|
|
||||||
func (h *httpUserInternalAPI) PerformDeviceCreation(
|
func (h *httpUserInternalAPI) PerformDeviceCreation(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
request *api.PerformDeviceCreationRequest,
|
request *api.PerformDeviceCreationRequest,
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,19 @@ func AddRoutes(internalAPIMux *mux.Router, s api.UserInternalAPI) {
|
||||||
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
internalAPIMux.Handle(PerformAccountCreationPath,
|
||||||
|
httputil.MakeInternalAPI("performPasswordUpdate", func(req *http.Request) util.JSONResponse {
|
||||||
|
request := api.PerformPasswordUpdateRequest{}
|
||||||
|
response := api.PerformPasswordUpdateResponse{}
|
||||||
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||||
|
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
||||||
|
}
|
||||||
|
if err := s.PerformPasswordUpdate(req.Context(), &request, &response); err != nil {
|
||||||
|
return util.ErrorResponse(err)
|
||||||
|
}
|
||||||
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
||||||
|
}),
|
||||||
|
)
|
||||||
internalAPIMux.Handle(PerformDeviceCreationPath,
|
internalAPIMux.Handle(PerformDeviceCreationPath,
|
||||||
httputil.MakeInternalAPI("performDeviceCreation", func(req *http.Request) util.JSONResponse {
|
httputil.MakeInternalAPI("performDeviceCreation", func(req *http.Request) util.JSONResponse {
|
||||||
request := api.PerformDeviceCreationRequest{}
|
request := api.PerformDeviceCreationRequest{}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ type Database interface {
|
||||||
internal.PartitionStorer
|
internal.PartitionStorer
|
||||||
GetAccountByPassword(ctx context.Context, localpart, plaintextPassword string) (*api.Account, error)
|
GetAccountByPassword(ctx context.Context, localpart, plaintextPassword string) (*api.Account, error)
|
||||||
GetProfileByLocalpart(ctx context.Context, localpart string) (*authtypes.Profile, error)
|
GetProfileByLocalpart(ctx context.Context, localpart string) (*authtypes.Profile, error)
|
||||||
|
SetPassword(ctx context.Context, localpart string, plaintextPassword string) error
|
||||||
SetAvatarURL(ctx context.Context, localpart string, avatarURL string) error
|
SetAvatarURL(ctx context.Context, localpart string, avatarURL string) error
|
||||||
SetDisplayName(ctx context.Context, localpart string, displayName string) error
|
SetDisplayName(ctx context.Context, localpart string, displayName string) error
|
||||||
// CreateAccount makes a new account with the given login name and password, and creates an empty profile
|
// CreateAccount makes a new account with the given login name and password, and creates an empty profile
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,9 @@ CREATE SEQUENCE IF NOT EXISTS numeric_username_seq START 1;
|
||||||
const insertAccountSQL = "" +
|
const insertAccountSQL = "" +
|
||||||
"INSERT INTO account_accounts(localpart, created_ts, password_hash, appservice_id) VALUES ($1, $2, $3, $4)"
|
"INSERT INTO account_accounts(localpart, created_ts, password_hash, appservice_id) VALUES ($1, $2, $3, $4)"
|
||||||
|
|
||||||
|
const updatePasswordSQL = "" +
|
||||||
|
"UPDATE account_accounts SET password_hash = $1 WHERE localpart = $2"
|
||||||
|
|
||||||
const selectAccountByLocalpartSQL = "" +
|
const selectAccountByLocalpartSQL = "" +
|
||||||
"SELECT localpart, appservice_id FROM account_accounts WHERE localpart = $1"
|
"SELECT localpart, appservice_id FROM account_accounts WHERE localpart = $1"
|
||||||
|
|
||||||
|
|
@ -56,10 +59,9 @@ const selectPasswordHashSQL = "" +
|
||||||
const selectNewNumericLocalpartSQL = "" +
|
const selectNewNumericLocalpartSQL = "" +
|
||||||
"SELECT nextval('numeric_username_seq')"
|
"SELECT nextval('numeric_username_seq')"
|
||||||
|
|
||||||
// TODO: Update password
|
|
||||||
|
|
||||||
type accountsStatements struct {
|
type accountsStatements struct {
|
||||||
insertAccountStmt *sql.Stmt
|
insertAccountStmt *sql.Stmt
|
||||||
|
updatePasswordStmt *sql.Stmt
|
||||||
selectAccountByLocalpartStmt *sql.Stmt
|
selectAccountByLocalpartStmt *sql.Stmt
|
||||||
selectPasswordHashStmt *sql.Stmt
|
selectPasswordHashStmt *sql.Stmt
|
||||||
selectNewNumericLocalpartStmt *sql.Stmt
|
selectNewNumericLocalpartStmt *sql.Stmt
|
||||||
|
|
@ -74,6 +76,9 @@ func (s *accountsStatements) prepare(db *sql.DB, server gomatrixserverlib.Server
|
||||||
if s.insertAccountStmt, err = db.Prepare(insertAccountSQL); err != nil {
|
if s.insertAccountStmt, err = db.Prepare(insertAccountSQL); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if s.updatePasswordStmt, err = db.Prepare(updatePasswordSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
if s.selectAccountByLocalpartStmt, err = db.Prepare(selectAccountByLocalpartSQL); err != nil {
|
if s.selectAccountByLocalpartStmt, err = db.Prepare(selectAccountByLocalpartSQL); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -114,6 +119,13 @@ func (s *accountsStatements) insertAccount(
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *accountsStatements) updatePassword(
|
||||||
|
ctx context.Context, localpart, passwordHash string,
|
||||||
|
) (err error) {
|
||||||
|
_, err = s.updatePasswordStmt.ExecContext(ctx, passwordHash, localpart)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (s *accountsStatements) selectPasswordHash(
|
func (s *accountsStatements) selectPasswordHash(
|
||||||
ctx context.Context, localpart string,
|
ctx context.Context, localpart string,
|
||||||
) (hash string, err error) {
|
) (hash string, err error) {
|
||||||
|
|
|
||||||
|
|
@ -112,6 +112,17 @@ func (d *Database) SetDisplayName(
|
||||||
return d.profiles.setDisplayName(ctx, localpart, displayName)
|
return d.profiles.setDisplayName(ctx, localpart, displayName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetPassword sets the account password to the given hash.
|
||||||
|
func (d *Database) SetPassword(
|
||||||
|
ctx context.Context, localpart, plaintextPassword string,
|
||||||
|
) error {
|
||||||
|
hash, err := hashPassword(plaintextPassword)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return d.accounts.updatePassword(ctx, localpart, hash)
|
||||||
|
}
|
||||||
|
|
||||||
// CreateGuestAccount makes a new guest account and creates an empty profile
|
// CreateGuestAccount makes a new guest account and creates an empty profile
|
||||||
// for this account.
|
// for this account.
|
||||||
func (d *Database) CreateGuestAccount(ctx context.Context) (acc *api.Account, err error) {
|
func (d *Database) CreateGuestAccount(ctx context.Context) (acc *api.Account, err error) {
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,9 @@ CREATE TABLE IF NOT EXISTS account_accounts (
|
||||||
const insertAccountSQL = "" +
|
const insertAccountSQL = "" +
|
||||||
"INSERT INTO account_accounts(localpart, created_ts, password_hash, appservice_id) VALUES ($1, $2, $3, $4)"
|
"INSERT INTO account_accounts(localpart, created_ts, password_hash, appservice_id) VALUES ($1, $2, $3, $4)"
|
||||||
|
|
||||||
|
const updatePasswordSQL = "" +
|
||||||
|
"UPDATE account_accounts SET password_hash = $1 WHERE localpart = $2"
|
||||||
|
|
||||||
const selectAccountByLocalpartSQL = "" +
|
const selectAccountByLocalpartSQL = "" +
|
||||||
"SELECT localpart, appservice_id FROM account_accounts WHERE localpart = $1"
|
"SELECT localpart, appservice_id FROM account_accounts WHERE localpart = $1"
|
||||||
|
|
||||||
|
|
@ -54,11 +57,10 @@ const selectPasswordHashSQL = "" +
|
||||||
const selectNewNumericLocalpartSQL = "" +
|
const selectNewNumericLocalpartSQL = "" +
|
||||||
"SELECT COUNT(localpart) FROM account_accounts"
|
"SELECT COUNT(localpart) FROM account_accounts"
|
||||||
|
|
||||||
// TODO: Update password
|
|
||||||
|
|
||||||
type accountsStatements struct {
|
type accountsStatements struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
insertAccountStmt *sql.Stmt
|
insertAccountStmt *sql.Stmt
|
||||||
|
updatePasswordStmt *sql.Stmt
|
||||||
selectAccountByLocalpartStmt *sql.Stmt
|
selectAccountByLocalpartStmt *sql.Stmt
|
||||||
selectPasswordHashStmt *sql.Stmt
|
selectPasswordHashStmt *sql.Stmt
|
||||||
selectNewNumericLocalpartStmt *sql.Stmt
|
selectNewNumericLocalpartStmt *sql.Stmt
|
||||||
|
|
@ -75,6 +77,9 @@ func (s *accountsStatements) prepare(db *sql.DB, server gomatrixserverlib.Server
|
||||||
if s.insertAccountStmt, err = db.Prepare(insertAccountSQL); err != nil {
|
if s.insertAccountStmt, err = db.Prepare(insertAccountSQL); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if s.updatePasswordStmt, err = db.Prepare(updatePasswordSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
if s.selectAccountByLocalpartStmt, err = db.Prepare(selectAccountByLocalpartSQL); err != nil {
|
if s.selectAccountByLocalpartStmt, err = db.Prepare(selectAccountByLocalpartSQL); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -115,6 +120,13 @@ func (s *accountsStatements) insertAccount(
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *accountsStatements) updatePassword(
|
||||||
|
ctx context.Context, localpart, passwordHash string,
|
||||||
|
) (err error) {
|
||||||
|
_, err = s.updatePasswordStmt.ExecContext(ctx, passwordHash, localpart)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (s *accountsStatements) selectPasswordHash(
|
func (s *accountsStatements) selectPasswordHash(
|
||||||
ctx context.Context, localpart string,
|
ctx context.Context, localpart string,
|
||||||
) (hash string, err error) {
|
) (hash string, err error) {
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
|
@ -126,6 +127,20 @@ func (d *Database) SetDisplayName(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetPassword sets the account password to the given hash.
|
||||||
|
func (d *Database) SetPassword(
|
||||||
|
ctx context.Context, localpart, plaintextPassword string,
|
||||||
|
) error {
|
||||||
|
hash, err := hashPassword(plaintextPassword)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println("PASSWORD:", localpart, plaintextPassword, hash)
|
||||||
|
err = d.accounts.updatePassword(ctx, localpart, hash)
|
||||||
|
fmt.Println("ERROR:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// CreateGuestAccount makes a new guest account and creates an empty profile
|
// CreateGuestAccount makes a new guest account and creates an empty profile
|
||||||
// for this account.
|
// for this account.
|
||||||
func (d *Database) CreateGuestAccount(ctx context.Context) (acc *api.Account, err error) {
|
func (d *Database) CreateGuestAccount(ctx context.Context) (acc *api.Account, err error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue