mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 10:33:11 -06:00
Get and Delete APIs
This commit is contained in:
parent
86d2aa41c1
commit
31f3125c26
|
|
@ -198,6 +198,46 @@ func getReturnValueExpiryTime(expiryTime int64) interface{} {
|
||||||
return expiryTime
|
return expiryTime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AdminGetRegistrationToken(req *http.Request, cfg *config.ClientAPI, userAPI userapi.ClientUserAPI) util.JSONResponse {
|
||||||
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
||||||
|
if err != nil {
|
||||||
|
return util.ErrorResponse(err)
|
||||||
|
}
|
||||||
|
tokenText := vars["token"]
|
||||||
|
token, err := userAPI.PerformAdminGetRegistrationToken(req.Context(), tokenText)
|
||||||
|
if err != nil {
|
||||||
|
return util.MatrixErrorResponse(
|
||||||
|
http.StatusNotFound,
|
||||||
|
string(spec.ErrorUnknown),
|
||||||
|
fmt.Sprintf("token: %s not found", tokenText),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: 200,
|
||||||
|
JSON: token,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminDeleteRegistrationToken(req *http.Request, cfg *config.ClientAPI, userAPI userapi.ClientUserAPI) util.JSONResponse {
|
||||||
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
||||||
|
if err != nil {
|
||||||
|
return util.ErrorResponse(err)
|
||||||
|
}
|
||||||
|
tokenText := vars["token"]
|
||||||
|
err = userAPI.PerformAdminDeleteRegistrationToken(req.Context(), tokenText)
|
||||||
|
if err != nil {
|
||||||
|
return util.MatrixErrorResponse(
|
||||||
|
http.StatusNotFound,
|
||||||
|
string(spec.ErrorUnknown),
|
||||||
|
fmt.Sprintf("token: %s not found", tokenText),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: 200,
|
||||||
|
JSON: map[string]interface{}{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func AdminEvacuateRoom(req *http.Request, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
|
func AdminEvacuateRoom(req *http.Request, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
|
||||||
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -169,11 +169,29 @@ func Setup(
|
||||||
).Methods(http.MethodPost, http.MethodOptions)
|
).Methods(http.MethodPost, http.MethodOptions)
|
||||||
|
|
||||||
dendriteAdminRouter.Handle("/admin/registrationTokens",
|
dendriteAdminRouter.Handle("/admin/registrationTokens",
|
||||||
httputil.MakeAdminAPI("admin_registration_tokens", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
|
httputil.MakeAdminAPI("admin_list_registration_tokens", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
|
||||||
return AdminListRegistrationTokens(req, cfg, userAPI)
|
return AdminListRegistrationTokens(req, cfg, userAPI)
|
||||||
}),
|
}),
|
||||||
).Methods(http.MethodGet, http.MethodOptions)
|
).Methods(http.MethodGet, http.MethodOptions)
|
||||||
|
|
||||||
|
dendriteAdminRouter.Handle("/admin/registrationTokens/{token}",
|
||||||
|
httputil.MakeAdminAPI("admin_get_registration_token", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
|
||||||
|
if req.Method == http.MethodGet {
|
||||||
|
return AdminGetRegistrationToken(req, cfg, userAPI)
|
||||||
|
} else if req.Method == http.MethodPut {
|
||||||
|
|
||||||
|
} else if req.Method == http.MethodDelete {
|
||||||
|
return AdminDeleteRegistrationToken(req, cfg, userAPI)
|
||||||
|
}
|
||||||
|
return util.MatrixErrorResponse(
|
||||||
|
404,
|
||||||
|
string(spec.ErrorNotFound),
|
||||||
|
"unknown method",
|
||||||
|
)
|
||||||
|
|
||||||
|
}),
|
||||||
|
).Methods(http.MethodGet, http.MethodPut, http.MethodDelete, http.MethodOptions)
|
||||||
|
|
||||||
dendriteAdminRouter.Handle("/admin/evacuateRoom/{roomID}",
|
dendriteAdminRouter.Handle("/admin/evacuateRoom/{roomID}",
|
||||||
httputil.MakeAdminAPI("admin_evacuate_room", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
|
httputil.MakeAdminAPI("admin_evacuate_room", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
|
||||||
return AdminEvacuateRoom(req, rsAPI)
|
return AdminEvacuateRoom(req, rsAPI)
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,8 @@ type ClientUserAPI interface {
|
||||||
QueryAccountAvailability(ctx context.Context, req *QueryAccountAvailabilityRequest, res *QueryAccountAvailabilityResponse) error
|
QueryAccountAvailability(ctx context.Context, req *QueryAccountAvailabilityRequest, res *QueryAccountAvailabilityResponse) error
|
||||||
PerformAdminCreateRegistrationToken(ctx context.Context, registrationToken *clientapi.RegistrationToken) (bool, error)
|
PerformAdminCreateRegistrationToken(ctx context.Context, registrationToken *clientapi.RegistrationToken) (bool, error)
|
||||||
PerformAdminListRegistrationTokens(ctx context.Context, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error)
|
PerformAdminListRegistrationTokens(ctx context.Context, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error)
|
||||||
|
PerformAdminGetRegistrationToken(ctx context.Context, tokenString string) (*clientapi.RegistrationToken, error)
|
||||||
|
PerformAdminDeleteRegistrationToken(ctx context.Context, tokenString string) error
|
||||||
PerformAccountCreation(ctx context.Context, req *PerformAccountCreationRequest, res *PerformAccountCreationResponse) error
|
PerformAccountCreation(ctx context.Context, req *PerformAccountCreationRequest, res *PerformAccountCreationResponse) error
|
||||||
PerformDeviceCreation(ctx context.Context, req *PerformDeviceCreationRequest, res *PerformDeviceCreationResponse) error
|
PerformDeviceCreation(ctx context.Context, req *PerformDeviceCreationRequest, res *PerformDeviceCreationResponse) error
|
||||||
PerformDeviceUpdate(ctx context.Context, req *PerformDeviceUpdateRequest, res *PerformDeviceUpdateResponse) error
|
PerformDeviceUpdate(ctx context.Context, req *PerformDeviceUpdateRequest, res *PerformDeviceUpdateResponse) error
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,18 @@ func (a *UserInternalAPI) PerformAdminListRegistrationTokens(ctx context.Context
|
||||||
return tokens, nil
|
return tokens, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *UserInternalAPI) PerformAdminGetRegistrationToken(ctx context.Context, tokenString string) (*clientapi.RegistrationToken, error) {
|
||||||
|
token, err := a.DB.GetRegistrationToken(ctx, tokenString)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return token, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *UserInternalAPI) PerformAdminDeleteRegistrationToken(ctx context.Context, tokenString string) error {
|
||||||
|
return a.DB.DeleteRegistrationToken(ctx, tokenString)
|
||||||
|
}
|
||||||
|
|
||||||
func (a *UserInternalAPI) InputAccountData(ctx context.Context, req *api.InputAccountDataRequest, res *api.InputAccountDataResponse) error {
|
func (a *UserInternalAPI) InputAccountData(ctx context.Context, req *api.InputAccountDataRequest, res *api.InputAccountDataResponse) error {
|
||||||
local, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
local, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,8 @@ type RegistrationTokens interface {
|
||||||
RegistrationTokenExists(ctx context.Context, token string) (bool, error)
|
RegistrationTokenExists(ctx context.Context, token string) (bool, error)
|
||||||
InsertRegistrationToken(ctx context.Context, registrationToken *clientapi.RegistrationToken) (bool, error)
|
InsertRegistrationToken(ctx context.Context, registrationToken *clientapi.RegistrationToken) (bool, error)
|
||||||
ListRegistrationTokens(ctx context.Context, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error)
|
ListRegistrationTokens(ctx context.Context, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error)
|
||||||
|
GetRegistrationToken(ctx context.Context, tokenString string) (*clientapi.RegistrationToken, error)
|
||||||
|
DeleteRegistrationToken(ctx context.Context, tokenString string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Profile interface {
|
type Profile interface {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package postgres
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/api"
|
"github.com/matrix-org/dendrite/clientapi/api"
|
||||||
|
|
@ -38,12 +39,20 @@ const listInvalidTokensSQL = "" +
|
||||||
"SELECT * FROM userapi_registration_tokens WHERE" +
|
"SELECT * FROM userapi_registration_tokens WHERE" +
|
||||||
"(uses_allowed <= pending + completed OR expiry_time <= $1)"
|
"(uses_allowed <= pending + completed OR expiry_time <= $1)"
|
||||||
|
|
||||||
|
const getTokenSQL = "" +
|
||||||
|
"SELECT pending, completed, uses_allowed, expiry_time FROM userapi_registration_tokens WHERE token = $1"
|
||||||
|
|
||||||
|
const deleteTokenSQL = "" +
|
||||||
|
"DELETE FROM userapi_registration_tokens WHERE token = $1"
|
||||||
|
|
||||||
type registrationTokenStatements struct {
|
type registrationTokenStatements struct {
|
||||||
selectTokenStatement *sql.Stmt
|
selectTokenStatement *sql.Stmt
|
||||||
insertTokenStatement *sql.Stmt
|
insertTokenStatement *sql.Stmt
|
||||||
listAllTokensStatement *sql.Stmt
|
listAllTokensStatement *sql.Stmt
|
||||||
listValidTokensStatement *sql.Stmt
|
listValidTokensStatement *sql.Stmt
|
||||||
listInvalidTokenStatement *sql.Stmt
|
listInvalidTokenStatement *sql.Stmt
|
||||||
|
getTokenStatement *sql.Stmt
|
||||||
|
deleteTokenStatement *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPostgresRegistrationTokensTable(db *sql.DB) (tables.RegistrationTokensTable, error) {
|
func NewPostgresRegistrationTokensTable(db *sql.DB) (tables.RegistrationTokensTable, error) {
|
||||||
|
|
@ -58,6 +67,8 @@ func NewPostgresRegistrationTokensTable(db *sql.DB) (tables.RegistrationTokensTa
|
||||||
{&s.listAllTokensStatement, listAllTokensSQL},
|
{&s.listAllTokensStatement, listAllTokensSQL},
|
||||||
{&s.listValidTokensStatement, listValidTokensSQL},
|
{&s.listValidTokensStatement, listValidTokensSQL},
|
||||||
{&s.listInvalidTokenStatement, listInvalidTokensSQL},
|
{&s.listInvalidTokenStatement, listInvalidTokensSQL},
|
||||||
|
{&s.getTokenStatement, getTokenSQL},
|
||||||
|
{&s.deleteTokenStatement, deleteTokenSQL},
|
||||||
}.Prepare(db)
|
}.Prepare(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,12 +140,18 @@ func (s *registrationTokenStatements) ListRegistrationTokens(ctx context.Context
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tokens, err
|
return tokens, err
|
||||||
}
|
}
|
||||||
|
tokenString := tokenString.String
|
||||||
|
pending := pending.Int32
|
||||||
|
completed := completed.Int32
|
||||||
|
usesAllowed := getReturnValueForInt32(usesAllowed)
|
||||||
|
expiryTime := getReturnValueForInt64(expiryTime)
|
||||||
|
|
||||||
tokenMap := api.RegistrationToken{
|
tokenMap := api.RegistrationToken{
|
||||||
Token: &tokenString.String,
|
Token: &tokenString,
|
||||||
Pending: &pending.Int32,
|
Pending: &pending,
|
||||||
Completed: &pending.Int32,
|
Completed: &completed,
|
||||||
UsesAllowed: getReturnValueForInt32(usesAllowed),
|
UsesAllowed: usesAllowed,
|
||||||
ExpiryTime: getReturnValueForInt64(expiryTime),
|
ExpiryTime: expiryTime,
|
||||||
}
|
}
|
||||||
tokens = append(tokens, tokenMap)
|
tokens = append(tokens, tokenMap)
|
||||||
}
|
}
|
||||||
|
|
@ -156,3 +173,37 @@ func getReturnValueForInt64(value sql.NullInt64) *int64 {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *registrationTokenStatements) GetRegistrationToken(ctx context.Context, tx *sql.Tx, tokenString string) (*api.RegistrationToken, error) {
|
||||||
|
stmt := s.getTokenStatement
|
||||||
|
var pending, completed, usesAllowed sql.NullInt32
|
||||||
|
var expiryTime sql.NullInt64
|
||||||
|
err := stmt.QueryRowContext(ctx, tokenString).Scan(&pending, &completed, &usesAllowed, &expiryTime)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
token := api.RegistrationToken{
|
||||||
|
Token: &tokenString,
|
||||||
|
Pending: &pending.Int32,
|
||||||
|
Completed: &completed.Int32,
|
||||||
|
UsesAllowed: getReturnValueForInt32(usesAllowed),
|
||||||
|
ExpiryTime: getReturnValueForInt64(expiryTime),
|
||||||
|
}
|
||||||
|
return &token, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *registrationTokenStatements) DeleteRegistrationToken(ctx context.Context, tx *sql.Tx, tokenString string) error {
|
||||||
|
stmt := s.deleteTokenStatement
|
||||||
|
res, err := stmt.ExecContext(ctx, tokenString)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
count, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if count == 0 {
|
||||||
|
return fmt.Errorf("token: %s does not exists", tokenString)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -96,6 +96,14 @@ func (d *Database) ListRegistrationTokens(ctx context.Context, returnAll bool, v
|
||||||
return d.RegistrationTokens.ListRegistrationTokens(ctx, nil, returnAll, valid)
|
return d.RegistrationTokens.ListRegistrationTokens(ctx, nil, returnAll, valid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Database) GetRegistrationToken(ctx context.Context, tokenString string) (*clientapi.RegistrationToken, error) {
|
||||||
|
return d.RegistrationTokens.GetRegistrationToken(ctx, nil, tokenString)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Database) DeleteRegistrationToken(ctx context.Context, tokenString string) error {
|
||||||
|
return d.RegistrationTokens.DeleteRegistrationToken(ctx, nil, tokenString)
|
||||||
|
}
|
||||||
|
|
||||||
// GetAccountByPassword returns the account associated with the given localpart and password.
|
// GetAccountByPassword returns the account associated with the given localpart and password.
|
||||||
// Returns sql.ErrNoRows if no account exists which matches the given localpart.
|
// Returns sql.ErrNoRows if no account exists which matches the given localpart.
|
||||||
func (d *Database) GetAccountByPassword(
|
func (d *Database) GetAccountByPassword(
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,8 @@ type RegistrationTokensTable interface {
|
||||||
RegistrationTokenExists(ctx context.Context, txn *sql.Tx, token string) (bool, error)
|
RegistrationTokenExists(ctx context.Context, txn *sql.Tx, token string) (bool, error)
|
||||||
InsertRegistrationToken(ctx context.Context, txn *sql.Tx, registrationToken *clientapi.RegistrationToken) (bool, error)
|
InsertRegistrationToken(ctx context.Context, txn *sql.Tx, registrationToken *clientapi.RegistrationToken) (bool, error)
|
||||||
ListRegistrationTokens(ctx context.Context, txn *sql.Tx, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error)
|
ListRegistrationTokens(ctx context.Context, txn *sql.Tx, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error)
|
||||||
|
GetRegistrationToken(ctx context.Context, txn *sql.Tx, tokenString string) (*clientapi.RegistrationToken, error)
|
||||||
|
DeleteRegistrationToken(ctx context.Context, txn *sql.Tx, tokenString string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccountDataTable interface {
|
type AccountDataTable interface {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue